e7825d22d20e4f7dc0494234fae93e8cd41d1473
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / CheckboxViewHelper.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * View Helper which creates a simple checkbox (<input type="checkbox">).
25 *
26 * = Examples =
27 *
28 * <code title="Example">
29 * <f:form.checkbox name="myCheckBox" value="someValue" />
30 * </code>
31 *
32 * Output:
33 * <input type="checkbox" name="myCheckBox" value="someValue" />
34 *
35 * <code title="Preselect">
36 * <f:form.checkbox name="myCheckBox" value="someValue" checked="{object.value} == 5" />
37 * </code>
38 *
39 * Output:
40 * <input type="checkbox" name="myCheckBox" value="someValue" checked="checked" />
41 * (depending on $object)
42 *
43 * <code title="Bind to object property">
44 * <f:form.checkbox property="interests" value="TYPO3" />
45 * </code>
46 *
47 * Output:
48 * <input type="checkbox" name="user[interests][]" value="TYPO3" checked="checked" />
49 * (depending on property "interests")
50 *
51 * @version $Id$
52 * @package Fluid
53 * @subpackage ViewHelpers\Form
54 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
55 * @api
56 * @scope prototype
57 */
58 class Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper extends Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper {
59
60 /**
61 * @var string
62 */
63 protected $tagName = 'input';
64
65 /**
66 * Initialize the arguments.
67 *
68 * @return void
69 * @author Bastian Waidelich <bastian@typo3.org>
70 * @api
71 */
72 public function initializeArguments() {
73 parent::initializeArguments();
74 $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
75 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
76 #$this->registerArgument('value', 'string', 'Value of input tag. Required for checkboxes', TRUE);
77 $this->registerUniversalTagAttributes();
78 }
79
80 /**
81 * Renders the checkbox.
82 *
83 * @param boolean $checked Specifies that the input element should be preselected
84 *
85 * @return string
86 * @author Bastian Waidelich <bastian@typo3.org>
87 * @api
88 */
89 public function render($checked = NULL) {
90 $this->tag->addAttribute('type', 'checkbox');
91
92 $nameAttribute = $this->getName();
93 $valueAttribute = $this->getValue();
94 if ($checked === NULL && $this->isObjectAccessorMode()) {
95 $propertyValue = $this->getPropertyValue();
96 if (is_bool($propertyValue)) {
97 $checked = $propertyValue === (boolean)$valueAttribute;
98 } elseif (is_array($propertyValue)) {
99 $checked = in_array($valueAttribute, $propertyValue);
100 $nameAttribute .= '[]';
101 } else {
102 throw new Tx_Fluid_Core_ViewHelper_Exception('Checkbox viewhelpers can only be bound to properties of type boolean or array. Property "' . $this->arguments['property'] . '" is of type "' . (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)) . '".' , 1248261038);
103 }
104 }
105
106 $this->registerFieldNameForFormTokenGeneration($nameAttribute);
107 $this->tag->addAttribute('name', $nameAttribute);
108 $this->tag->addAttribute('value', $valueAttribute);
109 if ($checked) {
110 $this->tag->addAttribute('checked', 'checked');
111 }
112
113 $this->setErrorClassAttribute();
114
115 $hiddenField = $this->renderHiddenFieldForEmptyValue();
116 return $hiddenField . $this->tag->render();
117 }
118
119 /**
120 * Renders a hidden field with the same name as the element, to make sure the
121 */
122 protected function renderHiddenFieldForEmptyValue() {
123 if ($this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper', 'checkboxFieldNames')) {
124 $checkboxFieldNames = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper', 'checkboxFieldNames');
125 } else {
126 $checkboxFieldNames = array();
127 }
128
129 $nameOfElement = $this->getName();
130 if (!in_array($nameOfElement, $checkboxFieldNames)) {
131 $checkboxFieldNames[] = $nameOfElement;
132 $this->viewHelperVariableContainer->addOrUpdate('Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper', 'checkboxFieldNames', $checkboxFieldNames);
133
134 $tagBuilder = t3lib_div::makeInstance('Tx_Fluid_Core_ViewHelper_TagBuilder', 'input');
135 $tagBuilder->addAttribute('type', 'hidden');
136 $tagBuilder->addAttribute('name', $nameOfElement);
137 $tagBuilder->addAttribute('value', '');
138 return $tagBuilder->render();
139 }
140 return '';
141 }
142 }
143
144 ?>