[TASK] Use single quotes instead of double quotes
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / CheckboxElement.php
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Generation of TCEform elements of the type "check"
21 */
22 class CheckboxElement extends AbstractFormElement
23 {
24 /**
25 * This will render a checkbox or an array of checkboxes
26 *
27 * @return array As defined in initializeResultArray() of AbstractNode
28 */
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[] = array('', '');
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 = array();
50 if ($colWidth == 6) {
51 $colClass = 'col-sm-6';
52 $colClear = array(
53 2 => 'visible-sm-block visible-md-block visible-lg-block',
54 );
55 } elseif ($colWidth === 4) {
56 $colClass = 'col-sm-4';
57 $colClear = array(
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 = array(
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 = array(
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
109 /**
110 * This functions builds the HTML output for the checkbox
111 *
112 * @param string $label Label of this item
113 * @param int $itemCounter Number of this element in the list of all elements
114 * @param int $formElementValue Value of this element
115 * @param int $numberOfItems Full number of items
116 * @param array $additionalInformation Information with additional configuration options.
117 * @param bool $disabled TRUE if form element is disabled
118 * @return string Single element HTML
119 */
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 ' . $additionalInformation['onFocus'] . '
140 ' . (!$disabled ?: ' disabled="disabled"') . '
141 id="' . $checkboxId . '" />
142 ' . ($label ? htmlspecialchars($label) : '&nbsp;') . '
143 </label>
144 </div>';
145 }
146
147 /**
148 * Creates checkbox parameters
149 *
150 * @param string $itemName Form element name
151 * @param int $formElementValue The value of the checkbox (representing checkboxes with the bits)
152 * @param int $checkbox Checkbox # (0-9?)
153 * @param int $checkboxesCount Total number of checkboxes in the array.
154 * @param string $additionalJavaScript Additional JavaScript for the onclick handler.
155 * @return string The onclick attribute + possibly the checked-option set.
156 */
157 protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '')
158 {
159 $elementName = 'document.editform[' . Generalutility::quoteJSvalue($itemName) . ']';
160 $checkboxPow = pow(2, $checkbox);
161 $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
162 . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
163 return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
164 }
165 }