[FEATURE] Override check/radio labels with TSConfig
[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 /**
18 * Generation of TCEform elements of the type "check"
19 */
20 class CheckboxElement extends AbstractFormElement {
21
22 /**
23 * This will render a checkbox or an array of checkboxes
24 *
25 * @param string $table The table name of the record
26 * @param string $field The field name which this element is supposed to edit
27 * @param array $row The record data array where the value(s) for the field can be found
28 * @param array $additionalInformation An array with additional configuration options.
29 * @return string The HTML code for the TCEform field
30 */
31 public function render($table, $field, $row, &$additionalInformation) {
32 $config = $additionalInformation['fieldConf']['config'];
33 $item = '';
34 $disabled = FALSE;
35 if ($this->isRenderReadonly() || $config['readOnly']) {
36 $disabled = TRUE;
37 }
38 // Traversing the array of items
39 $items = $this->formEngine->initItemArray($additionalInformation['fieldConf']);
40 if ($config['itemsProcFunc']) {
41 $items = $this->formEngine->procItems(
42 $items,
43 $additionalInformation['fieldTSConfig']['itemsProcFunc.'],
44 $config,
45 $table,
46 $row,
47 $field
48 );
49 }
50
51 $numberOfItems = count($items);
52 if ($numberOfItems === 0) {
53 $items[] = array('', '');
54 $numberOfItems = 1;
55 }
56 $formElementValue = (int)$additionalInformation['itemFormElValue'];
57 $cols = (int)$config['cols'];
58 if ($cols > 1) {
59 $colWidth = floor(12 / $cols);
60 $colLeftover = 12 - $colWidth * $cols;
61 for ($counter = 0; $counter < $numberOfItems; $counter++) {
62 // use "default" for typical single checkboxes
63 $tsConfigKey = ($numberOfItems === 1 ? 'default' : $items[$counter][1]);
64 // useful for e.g. pages.l18n_cfg, where there is no value set
65 if ($tsConfigKey === '') {
66 $tsConfigKey = $counter;
67 }
68 if (isset($additionalInformation['fieldTSConfig']['altLabels.'][$tsConfigKey])) {
69 $label = $this->getLanguageService()->sL($additionalInformation['fieldTSConfig']['altLabels.'][$tsConfigKey]);
70 } else {
71 $label = $items[$counter][0];
72 }
73 if (!($counter % $cols)) {
74 $item .= '<div class="row">';
75 }
76 $item .= '<div class="col-md-' . $colWidth . '">'
77 . $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
78 . '</div>';
79 if ($counter % $cols + 1 == $cols) {
80 $item .= ($colLeftover > 0 ? '<div class="col-md-' . $colLeftover . '"></div>' : '') . '</div>';
81 }
82 }
83 if ($counter % $cols) {
84 $rest = $cols - $counter % $cols;
85 for ($counter = 0; $counter < $rest; $counter++) {
86 $item .= '<div class="col-md-' . $colWidth . '></div>';
87 }
88 if ($counter > 0) {
89 $item .= ($colLeftover > 0 ? '<div class="col-md-' . $colLeftover . '"></div>' : '') . '</div>';
90 }
91 }
92 } else {
93 for ($counter = 0; $counter < $numberOfItems; $counter++) {
94 // use "default" for typical single checkboxes
95 $tsConfigKey = ($numberOfItems === 1 ? 'default' : $items[$counter][1]);
96 // useful for e.g. pages.l18n_cfg, where there is no value set
97 if ($tsConfigKey === '') {
98 $tsConfigKey = $counter;
99 }
100 if (isset($additionalInformation['fieldTSConfig']['altLabels.'][$tsConfigKey])) {
101 $label = $this->getLanguageService()->sL($additionalInformation['fieldTSConfig']['altLabels.'][$tsConfigKey]);
102 } else {
103 $label = $items[$counter][0];
104 }
105 $item .= $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $additionalInformation, $disabled);
106 }
107 }
108 if (!$disabled) {
109 $item .= '<input type="hidden" name="' . $additionalInformation['itemFormElName'] . '" value="' . htmlspecialchars($formElementValue) . '" />';
110 }
111 return $item;
112 }
113
114 /**
115 * This functions builds the HTML output for the checkbox
116 *
117 * @param string $label Label of this item
118 * @param integer $itemCounter Number of this element in the list of all elements
119 * @param integer $formElementValue Value of this element
120 * @param integer $numberOfItems Full number of items
121 * @param array $additionalInformation Information with additional configuration options.
122 * @param boolean $disabled TRUE if form element is disabled
123 * @return string Single element HTML
124 */
125 protected function renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled) {
126 $checkboxParameters = $this->checkBoxParams(
127 $additionalInformation['itemFormElName'],
128 $formElementValue,
129 $itemCounter,
130 $numberOfItems,
131 implode('', $additionalInformation['fieldChangeFunc'])
132 );
133 $checkboxName = $additionalInformation['itemFormElName'] . '_' . $itemCounter;
134 $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter;
135 return '<div class="checkbox' . (!$disabled ?: ' disabled') . '">'
136 . '<label for="' . $checkboxId . '">'
137 . '<input '
138 . 'type="checkbox" '
139 . 'value="1" '
140 . 'class="' . $this->cssClassTypeElementPrefix . 'check" '
141 . 'name="' . $checkboxName . '" '
142 . $checkboxParameters . ' '
143 . $additionalInformation['onFocus'] . ' '
144 . (!$disabled ?: ' disabled="disabled"')
145 . ' id="' . $checkboxId . '" '
146 . ' />'
147 . htmlspecialchars($label)
148 . '</label>'
149 . '</div>';
150 }
151
152 /**
153 * Creates checkbox parameters
154 *
155 * @param string $itemName Form element name
156 * @param int $formElementValue The value of the checkbox (representing checkboxes with the bits)
157 * @param int $checkbox Checkbox # (0-9?)
158 * @param int $checkboxesCount Total number of checkboxes in the array.
159 * @param string $additionalJavaScript Additional JavaScript for the onclick handler.
160 * @return string The onclick attribute + possibly the checked-option set.
161 */
162 protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '') {
163 $elementName = $this->formEngine->elName($itemName);
164 $checkboxPow = pow(2, $checkbox);
165 $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
166 . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
167 return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
168 }
169
170 }