ce1a5fd889e0455efb89f2837d586a08522a9298
[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 = '';
35 if ($this->formEngine->renderReadonly || $config['readOnly']) {
36 $disabled = ' disabled="disabled"';
37 }
38 // Traversing the array of items
39 $selectedItems = $this->formEngine->initItemArray($additionalInformation['fieldConf']);
40 if ($config['itemsProcFunc']) {
41 $selectedItems = $this->formEngine->procItems(
42 $selectedItems,
43 $additionalInformation['fieldTSConfig']['itemsProcFunc.'],
44 $config,
45 $table,
46 $row,
47 $field
48 );
49 }
50
51 $selectedItemsCount = count($selectedItems);
52 if ($selectedItemsCount === 0) {
53 $selectedItems[] = array('', '');
54 $selectedItemsCount = 1;
55 }
56
57 $formElementValue = (int)$additionalInformation['itemFormElValue'];
58 $cols = (int)$config['cols'];
59 if ($cols > 1) {
60 $item .= '<table border="0" cellspacing="0" cellpadding="0" class="typo3-TCEforms-checkboxArray">';
61 for ($c = 0; $c < $selectedItemsCount; $c++) {
62 $selectedItem = $selectedItems[$c];
63 if (!($c % $cols)) {
64 $item .= '<tr>';
65 }
66 $checkboxParameters = $this->checkBoxParams(
67 $additionalInformation['itemFormElName'],
68 $formElementValue,
69 $c,
70 $selectedItemsCount,
71 implode('', $additionalInformation['fieldChangeFunc'])
72 );
73 $checkboxName = $additionalInformation['itemFormElName'] . '_' . $c;
74 $checkboxId = $additionalInformation['itemFormElID'] . '_' . $c;
75 $item .= '<td nowrap="nowrap"><input type="checkbox" ' . $this->formEngine->insertDefStyle('check')
76 . ' value="1" name="' . $checkboxName . '" ' . $checkboxParameters . $disabled . ' id="' . $checkboxId . '" />'
77 . '<label for="' . $checkboxId . '">' . htmlspecialchars($selectedItem[0]) . '</label>&nbsp;'
78 . '</td>';
79 if ($c % $cols + 1 == $cols) {
80 $item .= '</tr>';
81 }
82 }
83 if ($c % $cols) {
84 $rest = $cols - $c % $cols;
85 for ($c = 0; $c < $rest; $c++) {
86 $item .= '<td></td>';
87 }
88 if ($c > 0) {
89 $item .= '</tr>';
90 }
91 }
92 $item .= '</table>';
93 } else {
94 for ($c = 0; $c < $selectedItemsCount; $c++) {
95 $selectedItem = $selectedItems[$c];
96 $checkboxParameters = $this->checkBoxParams(
97 $additionalInformation['itemFormElName'],
98 $formElementValue,
99 $c,
100 $selectedItemsCount,
101 implode('', $additionalInformation['fieldChangeFunc'])
102 );
103 $checkboxName = $additionalInformation['itemFormElName'] . '_' . $c;
104 $checkboxId = $additionalInformation['itemFormElID'] . '_' . $c;
105 $item .= ($c > 0 ? '<br />' : '') . '<input type="checkbox" ' . $this->formEngine->insertDefStyle('check')
106 . ' value="1" name="' . $checkboxName . '"' . $checkboxParameters . $additionalInformation['onFocus'] . $disabled
107 . ' id="' . $checkboxId . '" /> '
108 . '<label for="' . $checkboxId . '">' . htmlspecialchars($selectedItem[0]) . '</label>';
109 }
110 }
111 if (!$disabled) {
112 $item .= '<input type="hidden" name="' . $additionalInformation['itemFormElName'] . '" value="' . htmlspecialchars($formElementValue) . '" />';
113 }
114 return $item;
115 }
116
117 /**
118 * Creates checkbox parameters
119 *
120 * @param string $itemName Form element name
121 * @param int $formElementValue The value of the checkbox (representing checkboxes with the bits)
122 * @param int $checkbox Checkbox # (0-9?)
123 * @param int $checkboxesCount Total number of checkboxes in the array.
124 * @param string $additionalJavaScript Additional JavaScript for the onclick handler.
125 * @return string The onclick attribute + possibly the checked-option set.
126 */
127 protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '') {
128 $elementName = $this->formEngine->elName($itemName);
129 $checkboxPow = pow(2, $checkbox);
130 $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
131 . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
132 return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
133 }
134 }