TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CheckboxElement.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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  public function render()
30  {
31  $html = '';
32  $disabled = false;
33  if ($this->data['parameterArray']['fieldConf']['config']['readOnly']) {
34  $disabled = true;
35  }
36  // Traversing the array of items
37  $items = $this->data['parameterArray']['fieldConf']['config']['items'];
38 
39  $numberOfItems = count($items);
40  if ($numberOfItems === 0) {
41  $items[] = ['', ''];
42  $numberOfItems = 1;
43  }
44  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
45  $cols = (int)$this->data['parameterArray']['fieldConf']['config']['cols'];
46  if ($cols > 1) {
47  $colWidth = (int)floor(12 / $cols);
48  $colClass = 'col-md-12';
49  $colClear = [];
50  if ($colWidth == 6) {
51  $colClass = 'col-sm-6';
52  $colClear = [
53  2 => 'visible-sm-block visible-md-block visible-lg-block',
54  ];
55  } elseif ($colWidth === 4) {
56  $colClass = 'col-sm-4';
57  $colClear = [
58  3 => 'visible-sm-block visible-md-block visible-lg-block',
59  ];
60  } elseif ($colWidth === 3) {
61  $colClass = 'col-sm-6 col-md-3';
62  $colClear = [
63  2 => 'visible-sm-block',
64  4 => 'visible-md-block visible-lg-block',
65  ];
66  } elseif ($colWidth <= 2) {
67  $colClass = 'checkbox-column col-sm-6 col-md-3 col-lg-2';
68  $colClear = [
69  2 => 'visible-sm-block',
70  4 => 'visible-md-block',
71  6 => 'visible-lg-block'
72  ];
73  }
74  $html .= '<div class="checkbox-row row">';
75  $counter = 0;
76  // @todo: figure out in which cases checkbox items to not begin at 0 and why and when this would be useful
77  foreach ($items as $itemKey => $itemDefinition) {
78  $label = $itemDefinition[0];
79  $html .=
80  '<div class="checkbox-column ' . $colClass . '">'
81  . $this->renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
82  '</div>';
83  $counter = $counter + 1;
84  if ($counter < $numberOfItems && !empty($colClear)) {
85  foreach ($colClear as $rowBreakAfter => $clearClass) {
86  if ($counter % $rowBreakAfter === 0) {
87  $html .= '<div class="clearfix ' . $clearClass . '"></div>';
88  }
89  }
90  }
91  }
92  $html .= '</div>';
93  } else {
94  $counter = 0;
95  foreach ($items as $itemKey => $itemDefinition) {
96  $label = $itemDefinition[0];
97  $html .= $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
98  $counter = $counter + 1;
99  }
100  }
101  if (!$disabled) {
102  $html .= '<input type="hidden" name="' . $this->data['parameterArray']['itemFormElName'] . '" value="' . htmlspecialchars($formElementValue) . '" />';
103  }
104  $resultArray = $this->initializeResultArray();
105  $resultArray['html'] = $html;
106  return $resultArray;
107  }
108 
120  protected function renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
121  {
122  $config = $additionalInformation['fieldConf']['config'];
123  $inline = !empty($config['cols']) && $config['cols'] === 'inline';
124  $checkboxParameters = $this->checkBoxParams(
125  $additionalInformation['itemFormElName'],
126  $formElementValue,
127  $itemCounter,
128  $numberOfItems,
129  implode('', $additionalInformation['fieldChangeFunc'])
130  );
131  $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter;
132  return '
133  <div class="checkbox' . ($inline ? ' checkbox-inline' : '') . (!$disabled ? '' : ' disabled') . '">
134  <label>
135  <input type="checkbox"
136  value="1"
137  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
138  ' . $checkboxParameters . '
139  ' . (!$disabled ?: ' disabled="disabled"') . '
140  id="' . $checkboxId . '" />
141  ' . ($label ? htmlspecialchars($label) : '&nbsp;') . '
142  </label>
143  </div>';
144  }
145 
156  protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '')
157  {
158  $elementName = 'document.editform[' . GeneralUtility::quoteJSvalue($itemName) . ']';
159  $checkboxPow = pow(2, $checkbox);
160  $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
161  . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
162  return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
163  }
164 }
checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript= '')
renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)