649fb972cee544e9c5975c4c58c0621f7bd49f48
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / ViewHelpers / Form / TypoScriptConstantsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\ViewHelpers\Form;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog, <typo3@susannemoog.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * View Helper for rendering Extension Manager Configuration Form
31 *
32 * @author Susanne Moog <typo3@susannemoog.de>
33 */
34 class TypoScriptConstantsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
35
36 public $viewHelperMapping = array(
37 'int' => 'renderIntegerField',
38 'int+' => 'renderPositiveIntegerField',
39 'integer' => 'renderIntegerField',
40 'color' => 'renderColorPicker',
41 'wrap' => 'renderWrapField',
42 'offset' => 'renderOffsetField',
43 'options' => 'renderOptionSelect',
44 'boolean' => 'renderCheckbox',
45 'user' => 'renderUserFunction',
46 'small' => 'renderSmallTextField',
47 'string' => 'renderTextField',
48 'input' => 'renderTextField', // only for backwards compatibility, many extensions depend on that
49 'default' => 'renderTextField' // only for backwards compatibility, many extensions depend on that
50 );
51
52 public $tagName = 'input';
53
54 /**
55 * Initialize arguments of this view helper
56 *
57 * @return void
58 */
59 public function initializeArguments() {
60 parent::initializeArguments();
61 $this->registerArgument('name', 'string', 'Name of input tag');
62 $this->registerArgument('value', 'mixed', 'Value of input tag');
63 $this->registerUniversalTagAttributes();
64 }
65
66 /**
67 * Render
68 *
69 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
70 * @return string the rendered tag
71 */
72 public function render(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
73 $input = '';
74 if (isset($this->viewHelperMapping[$configuration->getType()]) && method_exists($this, $this->viewHelperMapping[$configuration->getType()])) {
75 $input = $this->{$this->viewHelperMapping[$configuration->getType()]}($configuration);
76 } else {
77 $input = $this->{$this->viewHelperMapping['default']}($configuration);
78 }
79
80 return $input;
81 }
82
83 /**
84 * Render field of type color picker
85 *
86 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
87 * @return string
88 */
89 protected function renderColorPicker(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
90 $this->tag->setTagName('input');
91 $this->tag->addAttribute('type', 'text');
92 $this->tag->addAttribute('name', $this->getName($configuration));
93 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
94 $doc = $this->getDocInstance();
95 $pageRenderer = $doc->getPageRenderer();
96 $pageRenderer->addCssFile('sysext/extensionmanager/Resources/Public/Contrib/Farbtastic/farbtastic.css');
97 $pageRenderer->addJsFile('sysext/extensionmanager/Resources/Public/Contrib/Farbtastic/farbtastic.js');
98 $pageRenderer->addJsInlineCode('colorpicker', '
99 jQuery(document).ready(function() {
100 jQuery(".colorPicker").farbtastic("#em-' . $configuration->getName() . '");
101 });
102 ');
103 if ($configuration->getValue() !== NULL) {
104 $this->tag->addAttribute('value', $configuration->getValue());
105 }
106 return $this->tag->render() . '<div class="colorPicker"></div>';
107 }
108
109 /**
110 * Render field of type "offset"
111 *
112 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
113 * @return string
114 */
115 protected function renderOffsetField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
116 $this->tag->setTagName('input');
117 $this->tag->addAttribute('type', 'text');
118 $this->tag->addAttribute('name', $this->getName($configuration));
119 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
120 $this->tag->addAttribute('class', 'offset');
121 if ($configuration->getValue() !== NULL) {
122 $this->tag->addAttribute('value', $configuration->getValue());
123 }
124 return $this->tag->render();
125 }
126
127 /**
128 * Render field of type "wrap"
129 *
130 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
131 * @return string
132 */
133 protected function renderWrapField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
134 $this->tag->setTagName('input');
135 $this->tag->addAttribute('type', 'text');
136 $this->tag->addAttribute('name', $this->getName($configuration));
137 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
138 $this->tag->addAttribute('class', 'wrap');
139 if ($configuration->getValue() !== NULL) {
140 $this->tag->addAttribute('value', $configuration->getValue());
141 }
142 return $this->tag->render();
143 }
144
145 /**
146 * Render field of type "option"
147 *
148 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
149 * @return string
150 */
151 protected function renderOptionSelect(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
152 $this->tag->addAttribute('name', $this->getName($configuration));
153 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
154 $this->tag->setTagName('select');
155 $optionValueArray = $configuration->getGeneric();
156 $output = '';
157 foreach ($optionValueArray as $label => $value) {
158 $output .= '<option value="' . htmlspecialchars($value) . '"';
159 if ($configuration->getValue() == $value) {
160 $output .= ' selected="selected"';
161 }
162 $output .= '>' . $GLOBALS['LANG']->sL($label, TRUE) . '</option>';
163 }
164 $this->tag->setContent($output);
165 return $this->tag->render();
166 }
167
168 /**
169 * Render field of type "int+"
170 *
171 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
172 * @return string
173 */
174 protected function renderPositiveIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
175 $this->tag->setTagName('input');
176 $this->tag->addAttribute('type', 'number');
177 $this->tag->addAttribute('min', '0');
178 $this->tag->addAttribute('name', $this->getName($configuration));
179 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
180 if ($configuration->getValue() !== NULL) {
181 $this->tag->addAttribute('value', $configuration->getValue());
182 }
183 return $this->tag->render();
184 }
185
186 /**
187 * Render field of type "integer"
188 *
189 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
190 * @return string
191 */
192 protected function renderIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
193 $this->tag->setTagName('input');
194 $this->tag->addAttribute('type', 'number');
195 $this->tag->addAttribute('name', $this->getName($configuration));
196 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
197 if ($configuration->getValue() !== NULL) {
198 $this->tag->addAttribute('value', $configuration->getValue());
199 }
200 return $this->tag->render();
201 }
202
203 /**
204 * Render field of type "text"
205 *
206 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
207 * @return string
208 */
209 protected function renderTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
210 $this->tag->setTagName('input');
211 $this->tag->addAttribute('type', 'text');
212 $this->tag->addAttribute('name', $this->getName($configuration));
213 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
214 if ($configuration->getValue() !== NULL) {
215 $this->tag->addAttribute('value', $configuration->getValue());
216 }
217 return $this->tag->render();
218 }
219
220 /**
221 * Render field of type "small text"
222 *
223 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
224 * @return string
225 */
226 protected function renderSmallTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
227 $this->tag->addAttribute('class', 'small');
228 return $this->renderTextField($configuration);
229 }
230
231 /**
232 * Render field of type "checkbox"
233 *
234 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
235 * @return string
236 */
237 public function renderCheckbox(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
238 $this->tag->addAttribute('type', 'checkbox');
239 $this->tag->addAttribute('name', $this->getName($configuration));
240 $this->tag->addAttribute('value', 1);
241 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
242 if ($configuration->getValue() == 1) {
243 $this->tag->addAttribute('checked', 'checked');
244 }
245 $hiddenField = $this->renderHiddenFieldForEmptyValue($configuration);
246 return $hiddenField . $this->tag->render();
247 }
248
249 /**
250 * Render field of type "userFunc"
251 *
252 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
253 * @return string
254 */
255 protected function renderUserFunction(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
256 $userFunction = $configuration->getGeneric();
257 $userFunctionParams = array(
258 'fieldName' => $this->getName($configuration),
259 'fieldValue' => $configuration->getValue(),
260 'propertyName' => $configuration->getName()
261 );
262 return \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this, '');
263 }
264
265 /**
266 * Get Field Name
267 *
268 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
269 * @return string
270 */
271 protected function getName(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
272 return 'tx_extensionmanager_tools_extensionmanagerextensionmanager[config][' . $configuration->getName() . '][value]';
273 }
274
275 /**
276 * Render a hidden field for empty values
277 *
278 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
279 * @return string
280 */
281 protected function renderHiddenFieldForEmptyValue($configuration) {
282 $hiddenFieldNames = array();
283 if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')) {
284 $hiddenFieldNames = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields');
285 }
286 $fieldName = $this->getName($configuration);
287 if (substr($fieldName, -2) === '[]') {
288 $fieldName = substr($fieldName, 0, -2);
289 }
290 if (!in_array($fieldName, $hiddenFieldNames)) {
291 $hiddenFieldNames[] = $fieldName;
292 $this->viewHelperVariableContainer->addOrUpdate('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields', $hiddenFieldNames);
293 return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="0" />';
294 }
295 return '';
296 }
297
298 /**
299 * Gets instance of template if exists or create a new one.
300 *
301 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate $doc
302 */
303 public function getDocInstance() {
304 if (!isset($GLOBALS['SOBE']->doc)) {
305 $GLOBALS['SOBE']->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
306 $GLOBALS['SOBE']->doc->backPath = $GLOBALS['BACK_PATH'];
307 }
308 return $GLOBALS['SOBE']->doc;
309 }
310
311 }