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