f987d8685f39909c9845917dbcd3976bdb6522dc
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / ViewHelpers / Be / FormEngineBasedCheckboxGroupViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Lang\ViewHelpers\Be;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2012 Kai Vogel <kai.vogel@speedprogs.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25
26 /**
27 * Checkbox group view helper
28 *
29 *
30 * Usage:
31 *
32 * {namespace myext=ENET\MyExt\ViewHelpers}
33 * <myext:be.formEngineBasedCheckboxGroup name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="{paymentOptions}" />
34 *
35 * or:
36 *
37 * <myext:be.formEngineBasedCheckboxGroup name="paymentOptions" options="{options}" optionValueField="id" optionLabelField="firstName" />
38 *
39 *
40 * @author Kai Vogel <kai.vogel@speedprogs.de>
41 * @package lang
42 * @subpackage ViewHelpers
43 */
44 class FormEngineBasedCheckboxGroupViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper {
45
46 /**
47 * @var string
48 */
49 protected $tagName = 'div';
50
51 /**
52 * @var TYPO3\CMS\Extbase\Object\ObjectManager
53 */
54 protected $objectManager;
55
56 /**
57 * Injects the object manager
58 *
59 * @param TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
60 * @return void
61 */
62 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
63 $this->objectManager = $objectManager;
64 }
65
66
67 /**
68 * Arguments initialization
69 *
70 * @return void
71 */
72 public function initializeArguments() {
73 parent::initializeArguments();
74 $this->registerArgument('optionIcon', 'string', 'If specified, will show the given icon in front of each option label.');
75 }
76
77 /**
78 * Render the field
79 *
80 * @return string Rendered field
81 */
82 public function render() {
83 if ($items === NULL) {
84 $items = $this->renderChildren();
85 }
86
87 $formName = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
88 $fieldName = $this->getName();
89 $content = $this->renderCheckboxGroupField($formName, $fieldName);
90 $this->tag->setContent($content);
91
92 return $this->tag->render();
93 }
94
95
96 /**
97 * Returns the HTML content for a checkbox group field
98 *
99 * @param string $formName Name of the form
100 * @param string $fieldName Name of the field
101 * @return string HTML content
102 */
103 protected function renderCheckboxGroupField($formName, $fieldName) {
104 if (empty($this->arguments['options'])) {
105 return '';
106 }
107
108 $options = $this->getOptions();
109 if (empty($options)) {
110 $options = array('' => '');
111 }
112
113 $icon = (!empty($this->arguments['optionIcon']) ? $this->arguments['optionIcon'] : '');
114 $items = array();
115 $checkedItems = array();
116
117 foreach($options as $value => $label) {
118 $items[] = array($label, $value, $icon);
119 if ($this->isChecked($value)) {
120 $checkedItems[] = $value;
121 }
122 // Register the field name for each option for the token generation
123 $this->registerFieldNameForFormTokenGeneration($fieldName . '[]');
124 }
125
126 $setup = array(
127 'itemFormElValue' => implode(',', $checkedItems),
128 'itemFormElName' => $this->getName(),
129 'fieldChangeFunc' => array(),
130 );
131
132 // Render field with default form engine
133 $formEngine = $this->objectManager->create('TYPO3\CMS\Backend\Form\FormEngine');
134 $formEngine->formName = $formName;
135 $content = $formEngine->getSingleField_typeSelect_checkbox('', '', array(), $setup, array(), $items, '%s');
136 unset($formEngine);
137
138 return $content;
139 }
140
141 /**
142 * Check if an option is checked
143 *
144 * @param string $value Value to check
145 * @return boolean TRUE if the value is checked
146 */
147 protected function isChecked($value) {
148 $selectedValue = $this->getSelectedValue();
149 return (is_array($selectedValue) && in_array($value, $selectedValue));
150 }
151
152 }
153 ?>