[TASK] Fix CGL violations against ClassDocComment
[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 * Usage:
30 *
31 * {namespace myext=ENET\MyExt\ViewHelpers}
32 * <myext:be.formEngineBasedCheckboxGroup name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="{paymentOptions}" />
33 *
34 * or:
35 *
36 * <myext:be.formEngineBasedCheckboxGroup name="paymentOptions" options="{options}" optionValueField="id" optionLabelField="firstName" />
37 *
38 * @author Kai Vogel <kai.vogel@speedprogs.de>
39 * @package lang
40 * @subpackage ViewHelpers
41 */
42 class FormEngineBasedCheckboxGroupViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper {
43
44 /**
45 * @var string
46 */
47 protected $tagName = 'div';
48
49 /**
50 * @var TYPO3\CMS\Extbase\Object\ObjectManager
51 */
52 protected $objectManager;
53
54 /**
55 * Injects the object manager
56 *
57 * @param TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
58 * @return void
59 */
60 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
61 $this->objectManager = $objectManager;
62 }
63
64
65 /**
66 * Arguments initialization
67 *
68 * @return void
69 */
70 public function initializeArguments() {
71 parent::initializeArguments();
72 $this->registerArgument('optionIcon', 'string', 'If specified, will show the given icon in front of each option label.');
73 }
74
75 /**
76 * Render the field
77 *
78 * @return string Rendered field
79 */
80 public function render() {
81 if ($items === NULL) {
82 $items = $this->renderChildren();
83 }
84
85 $formName = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
86 $fieldName = $this->getName();
87 $content = $this->renderCheckboxGroupField($formName, $fieldName);
88 $this->tag->setContent($content);
89
90 return $this->tag->render();
91 }
92
93
94 /**
95 * Returns the HTML content for a checkbox group field
96 *
97 * @param string $formName Name of the form
98 * @param string $fieldName Name of the field
99 * @return string HTML content
100 */
101 protected function renderCheckboxGroupField($formName, $fieldName) {
102 if (empty($this->arguments['options'])) {
103 return '';
104 }
105
106 $options = $this->getOptions();
107 if (empty($options)) {
108 $options = array('' => '');
109 }
110
111 $icon = (!empty($this->arguments['optionIcon']) ? $this->arguments['optionIcon'] : '');
112 $items = array();
113 $checkedItems = array();
114
115 foreach($options as $value => $label) {
116 $items[] = array($label, $value, $icon);
117 if ($this->isChecked($value)) {
118 $checkedItems[] = $value;
119 }
120 // Register the field name for each option for the token generation
121 $this->registerFieldNameForFormTokenGeneration($fieldName . '[]');
122 }
123
124 $setup = array(
125 'itemFormElValue' => implode(',', $checkedItems),
126 'itemFormElName' => $this->getName(),
127 'fieldChangeFunc' => array(),
128 );
129
130 // Render field with default form engine
131 $formEngine = $this->objectManager->create('TYPO3\CMS\Backend\Form\FormEngine');
132 $formEngine->formName = $formName;
133 $content = $formEngine->getSingleField_typeSelect_checkbox('', '', array(), $setup, array(), $items, '%s');
134 unset($formEngine);
135
136 return $content;
137 }
138
139 /**
140 * Check if an option is checked
141 *
142 * @param string $value Value to check
143 * @return boolean TRUE if the value is checked
144 */
145 protected function isChecked($value) {
146 $selectedValue = $this->getSelectedValue();
147 return (is_array($selectedValue) && in_array($value, $selectedValue));
148 }
149
150 }
151 ?>