4081ce0f46e1a7e8754931bb20345306b8fc9372
[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 textfile 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 );
49
50 public $tagName = 'input';
51
52 /**
53 * Initialize arguments of this view helper
54 *
55 * @return void
56 */
57 public function initializeArguments() {
58 parent::initializeArguments();
59 $this->registerArgument('name', 'string', 'Name of input tag');
60 $this->registerArgument('value', 'mixed', 'Value of input tag');
61 $this->registerUniversalTagAttributes();
62 }
63
64 /**
65 * Render
66 *
67 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
68 * @return string the rendered tag
69 */
70 public function render(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
71 $input = '';
72 if (isset($this->viewHelperMapping[$configuration->getType()]) && method_exists($this, $this->viewHelperMapping[$configuration->getType()])) {
73 $input = $this->{$this->viewHelperMapping[$configuration->getType()]}($configuration);
74 } else {
75 $input = $this->{$this->viewHelperMapping['default']}($configuration);
76 }
77
78 return $input;
79 }
80
81 /**
82 * Render field of type color picker
83 *
84 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
85 * @return string
86 */
87 protected function renderColorPicker(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
88 $this->tag->setTagName('input');
89 $this->tag->addAttribute('type', 'text');
90 $this->tag->addAttribute('name', $this->getName($configuration));
91 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
92 $doc = $this->getDocInstance();
93 $pageRenderer = $doc->getPageRenderer();
94 $pageRenderer->addCssFile('sysext/extensionmanager/Resources/Public/Contrib/Farbtastic/farbtastic.css');
95 $pageRenderer->addJsFile('sysext/extensionmanager/Resources/Public/Contrib/Farbtastic/farbtastic.js');
96 $pageRenderer->addJsInlineCode('colorpicker', '
97 jQuery(document).ready(function() {
98 jQuery(".colorPicker").farbtastic("#em-' . $configuration->getName() . '");
99 });
100 ');
101 if ($configuration->getValue() !== NULL) {
102 $this->tag->addAttribute('value', $configuration->getValue());
103 }
104 return $this->tag->render() . '<div class="colorPicker"></div>';
105 }
106
107 /**
108 * Render field of type "offset"
109 *
110 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
111 * @return string
112 */
113 protected function renderOffsetField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
114 $this->tag->setTagName('input');
115 $this->tag->addAttribute('type', 'text');
116 $this->tag->addAttribute('name', $this->getName($configuration));
117 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
118 $this->tag->addAttribute('class', 'offset');
119 if ($configuration->getValue() !== NULL) {
120 $this->tag->addAttribute('value', $configuration->getValue());
121 }
122 return $this->tag->render();
123 }
124
125 /**
126 * Render field of type "wrap"
127 *
128 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
129 * @return string
130 */
131 protected function renderWrapField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
132 $this->tag->setTagName('input');
133 $this->tag->addAttribute('type', 'text');
134 $this->tag->addAttribute('name', $this->getName($configuration));
135 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
136 $this->tag->addAttribute('class', 'wrap');
137 if ($configuration->getValue() !== NULL) {
138 $this->tag->addAttribute('value', $configuration->getValue());
139 }
140 return $this->tag->render();
141 }
142
143 /**
144 * Render field of type "option"
145 *
146 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
147 * @return string
148 */
149 protected function renderOptionSelect(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
150 $this->tag->addAttribute('name', $this->getName($configuration));
151 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
152 $this->tag->setTagName('select');
153 $optionValueArray = $configuration->getGeneric();
154 $output = '';
155 foreach ($optionValueArray as $label => $value) {
156 $output .= '<option value="' . htmlspecialchars($value) . '"';
157 if ($configuration->getValue() == $value) {
158 $output .= ' selected="selected"';
159 }
160 $output .= '>' . $GLOBALS['LANG']->sL($label, TRUE) . '</option>';
161 }
162 $this->tag->setContent($output);
163 return $this->tag->render();
164 }
165
166 /**
167 * Render field of type "int+"
168 *
169 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
170 * @return string
171 */
172 protected function renderPositiveIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
173 $this->tag->setTagName('input');
174 $this->tag->addAttribute('type', 'number');
175 $this->tag->addAttribute('min', '0');
176 $this->tag->addAttribute('name', $this->getName($configuration));
177 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
178 if ($configuration->getValue() !== NULL) {
179 $this->tag->addAttribute('value', $configuration->getValue());
180 }
181 return $this->tag->render();
182 }
183
184 /**
185 * Render field of type "integer"
186 *
187 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
188 * @return string
189 */
190 protected function renderIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
191 $this->tag->setTagName('input');
192 $this->tag->addAttribute('type', 'number');
193 $this->tag->addAttribute('name', $this->getName($configuration));
194 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
195 if ($configuration->getValue() !== NULL) {
196 $this->tag->addAttribute('value', $configuration->getValue());
197 }
198 return $this->tag->render();
199 }
200
201 /**
202 * Render field of type "text"
203 *
204 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
205 * @return string
206 */
207 protected function renderTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
208 $this->tag->setTagName('input');
209 $this->tag->addAttribute('type', 'text');
210 $this->tag->addAttribute('name', $this->getName($configuration));
211 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
212 if ($configuration->getValue() !== NULL) {
213 $this->tag->addAttribute('value', $configuration->getValue());
214 }
215 return $this->tag->render();
216 }
217
218 /**
219 * Render field of type "small text"
220 *
221 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
222 * @return string
223 */
224 protected function renderSmallTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
225 $this->tag->addAttribute('class', 'small');
226 return $this->renderTextField($configuration);
227 }
228
229 /**
230 * Render field of type "checkbox"
231 *
232 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
233 * @return string
234 */
235 public function renderCheckbox(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
236 $this->tag->addAttribute('type', 'checkbox');
237 $this->tag->addAttribute('name', $this->getName($configuration));
238 $this->tag->addAttribute('value', 1);
239 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
240 if ($configuration->getValue() == 1) {
241 $this->tag->addAttribute('checked', 'checked');
242 }
243 $hiddenField = $this->renderHiddenFieldForEmptyValue($configuration);
244 return $hiddenField . $this->tag->render();
245 }
246
247 /**
248 * Render field of type "userFunc"
249 *
250 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
251 * @return string
252 */
253 protected function renderUserFunction(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
254 $userFunction = $configuration->getGeneric();
255 $userFunctionParams = array(
256 'fieldName' => $this->getName($configuration),
257 'fieldValue' => $configuration->getValue(),
258 'propertyName' => $configuration->getName()
259 );
260 return \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this, '');
261 }
262
263 /**
264 * Get Field Name
265 *
266 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
267 * @return string
268 */
269 protected function getName(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration) {
270 return 'tx_extensionmanager_tools_extensionmanagerextensionmanager[config][' . $configuration->getName() . '][value]';
271 }
272
273 /**
274 * Render a hidden field for empty values
275 *
276 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
277 * @return string
278 */
279 protected function renderHiddenFieldForEmptyValue($configuration) {
280 $hiddenFieldNames = array();
281 if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')) {
282 $hiddenFieldNames = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields');
283 }
284 $fieldName = $this->getName($configuration);
285 if (substr($fieldName, -2) === '[]') {
286 $fieldName = substr($fieldName, 0, -2);
287 }
288 if (!in_array($fieldName, $hiddenFieldNames)) {
289 $hiddenFieldNames[] = $fieldName;
290 $this->viewHelperVariableContainer->addOrUpdate('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields', $hiddenFieldNames);
291 return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="0" />';
292 }
293 return '';
294 }
295
296 /**
297 * Gets instance of template if exists or create a new one.
298 *
299 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate $doc
300 */
301 public function getDocInstance() {
302 if (!isset($GLOBALS['SOBE']->doc)) {
303 $GLOBALS['SOBE']->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
304 $GLOBALS['SOBE']->doc->backPath = $GLOBALS['BACK_PATH'];
305 }
306 return $GLOBALS['SOBE']->doc;
307 }
308
309 }
310
311
312 ?>