TYPO3 CMS  TYPO3_8-7
CheckboxElement.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
23 {
29  protected $defaultFieldWizard = [
30  'localizationStateSelector' => [
31  'renderType' => 'localizationStateSelector',
32  ],
33  'otherLanguageContent' => [
34  'renderType' => 'otherLanguageContent',
35  'after' => [
36  'localizationStateSelector'
37  ],
38  ],
39  'defaultLanguageDifferences' => [
40  'renderType' => 'defaultLanguageDifferences',
41  'after' => [
42  'otherLanguageContent',
43  ],
44  ],
45  ];
46 
52  public function render()
53  {
54  $resultArray = $this->initializeResultArray();
55 
56  $elementHtml = '';
57  $disabled = false;
58  if ($this->data['parameterArray']['fieldConf']['config']['readOnly']) {
59  $disabled = true;
60  }
61  // Traversing the array of items
62  $items = $this->data['parameterArray']['fieldConf']['config']['items'];
63 
64  $numberOfItems = count($items);
65  if ($numberOfItems === 0) {
66  $items[] = ['', ''];
67  $numberOfItems = 1;
68  }
69  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
70  $cols = (int)$this->data['parameterArray']['fieldConf']['config']['cols'];
71  if ($cols > 1) {
72  $colWidth = (int)floor(12 / $cols);
73  $colClass = 'col-md-12';
74  $colClear = [];
75  if ($colWidth == 6) {
76  $colClass = 'col-sm-6';
77  $colClear = [
78  2 => 'visible-sm-block visible-md-block visible-lg-block',
79  ];
80  } elseif ($colWidth === 4) {
81  $colClass = 'col-sm-4';
82  $colClear = [
83  3 => 'visible-sm-block visible-md-block visible-lg-block',
84  ];
85  } elseif ($colWidth === 3) {
86  $colClass = 'col-sm-6 col-md-3';
87  $colClear = [
88  2 => 'visible-sm-block',
89  4 => 'visible-md-block visible-lg-block',
90  ];
91  } elseif ($colWidth <= 2) {
92  $colClass = 'checkbox-column col-sm-6 col-md-3 col-lg-2';
93  $colClear = [
94  2 => 'visible-sm-block',
95  4 => 'visible-md-block',
96  6 => 'visible-lg-block'
97  ];
98  }
99  $elementHtml .= '<div class="checkbox-row row">';
100  $counter = 0;
101  // @todo: figure out in which cases checkbox items to not begin at 0 and why and when this would be useful
102  foreach ($items as $itemKey => $itemDefinition) {
103  $label = $itemDefinition[0];
104  $elementHtml .=
105  '<div class="checkbox-column ' . $colClass . '">'
106  . $this->renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
107  '</div>';
108  $counter = $counter + 1;
109  if ($counter < $numberOfItems && !empty($colClear)) {
110  foreach ($colClear as $rowBreakAfter => $clearClass) {
111  if ($counter % $rowBreakAfter === 0) {
112  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
113  }
114  }
115  }
116  }
117  $elementHtml .= '</div>';
118  } else {
119  $counter = 0;
120  foreach ($items as $itemKey => $itemDefinition) {
121  $label = $itemDefinition[0];
122  $elementHtml .= $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
123  $counter = $counter + 1;
124  }
125  }
126  if (!$disabled) {
127  $elementHtml .= '<input type="hidden" name="' . $this->data['parameterArray']['itemFormElName'] . '" value="' . htmlspecialchars($formElementValue) . '" />';
128  }
129 
130  $fieldInformationResult = $this->renderFieldInformation();
131  $fieldInformationHtml = $fieldInformationResult['html'];
132  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
133 
134  $fieldWizardResult = $this->renderFieldWizard();
135  $fieldWizardHtml = $fieldWizardResult['html'];
136  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
137 
138  $html = [];
139  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
140  if (!$disabled) {
141  $html[] = $fieldInformationHtml;
142  }
143  $html[] = '<div class="form-wizards-wrap">';
144  $html[] = '<div class="form-wizards-element">';
145  $html[] = $elementHtml;
146  $html[] = '</div>';
147  if (!$disabled) {
148  $html[] = '<div class="form-wizards-items-bottom">';
149  $html[] = $fieldWizardHtml;
150  $html[] = '</div>';
151  }
152  $html[] = '</div>';
153  $html[] = '</div>';
154 
155  $resultArray['html'] = implode(LF, $html);
156  return $resultArray;
157  }
158 
170  protected function renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
171  {
172  $config = $additionalInformation['fieldConf']['config'];
173  $inline = !empty($config['cols']) && $config['cols'] === 'inline';
174  $checkboxParameters = $this->checkBoxParams(
175  $additionalInformation['itemFormElName'],
176  $formElementValue,
177  $itemCounter,
178  $numberOfItems,
179  implode('', $additionalInformation['fieldChangeFunc'])
180  );
181  $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter;
182  return '
183  <div class="checkbox' . ($inline ? ' checkbox-inline' : '') . (!$disabled ? '' : ' disabled') . '">
184  <label>
185  <input type="checkbox"
186  value="1"
187  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
188  ' . $checkboxParameters . '
189  ' . (!$disabled ?: ' disabled="disabled"') . '
190  id="' . $checkboxId . '" />
191  ' . ($label ? htmlspecialchars($label) : '&nbsp;') . '
192  </label>
193  </div>';
194  }
195 
206  protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '')
207  {
208  $elementName = 'document.editform[' . GeneralUtility::quoteJSvalue($itemName) . ']';
209  $checkboxPow = pow(2, $checkbox);
210  $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
211  . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
212  return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
213  }
214 }
checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript='')
renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)