5890275c0e06bf79db4be817b5f58b20e2b717ba
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputColorPickerElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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 use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\MathUtility;
22 use TYPO3\CMS\Core\Utility\StringUtility;
23
24 /**
25 * Class InputColorPickerElement
26 */
27 class InputColorPickerElement extends AbstractFormElement
28 {
29
30 /**
31 * This will render a single-line input form field, possibly with various control/validation features
32 *
33 * @return array As defined in initializeResultArray() of AbstractNode
34 */
35 public function render()
36 {
37 $table = $this->data['tableName'];
38 $fieldName = $this->data['fieldName'];
39 $row = $this->data['databaseRow'];
40 $parameterArray = $this->data['parameterArray'];
41 $resultArray = $this->initializeResultArray();
42
43 $config = $parameterArray['fieldConf']['config'];
44 $specConf = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
45 $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
46 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
47 $classes = array();
48 $attributes = array();
49
50 // readonly
51 if ($config['readOnly']) {
52 $itemFormElValue = $parameterArray['itemFormElValue'];
53 $options = $this->data;
54 $options['parameterArray'] = array(
55 'fieldConf' => array(
56 'config' => $config,
57 ),
58 'itemFormElValue' => $itemFormElValue,
59 );
60 $options['renderType'] = 'none';
61 return $this->nodeFactory->create($options)->render();
62 }
63
64 // @todo: The whole eval handling is a mess and needs refactoring
65 foreach ($evalList as $func) {
66 switch ($func) {
67 case 'required':
68 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(array('required' => true));
69 break;
70 default:
71 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
72 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
73 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
74 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
75 if (class_exists($func)) {
76 $evalObj = GeneralUtility::makeInstance($func);
77 if (method_exists($evalObj, 'deevaluateFieldValue')) {
78 $_params = array(
79 'value' => $parameterArray['itemFormElValue']
80 );
81 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
82 }
83 }
84 }
85 }
86 }
87
88 $paramsList = array(
89 'field' => $parameterArray['itemFormElName'],
90 'evalList' => implode(',', $evalList),
91 'is_in' => trim($config['is_in']),
92 );
93
94 // set classes
95 $classes[] = 'form-control';
96 $classes[] = 'hasDefaultValue';
97 $classes[] = 't3js-clearable';
98 $classes[] = 't3js-color-picker';
99 $classes[] = 'formengine-colorpickerelement';
100 $attributes['class'] = implode(' ', $classes);
101
102 // Load needed js library
103 $resultArray['requireJsModules'][] = array(
104 'TYPO3/CMS/Backend/ColorPicker' => 'function(ColorPicker){ColorPicker.initialize()}'
105 );
106
107 // calculate attributes
108 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString($config);
109 $attributes['data-formengine-input-params'] = json_encode($paramsList);
110 $attributes['data-formengine-input-name'] = $parameterArray['itemFormElName'];
111 $attributes['id'] = StringUtility::getUniqueId('formengine-input-');
112 $attributes['value'] = '';
113 if (isset($config['max']) && (int)$config['max'] > 0) {
114 $attributes['maxlength'] = (int)$config['max'];
115 }
116
117 // This is the EDITABLE form field.
118 if (!empty($config['placeholder'])) {
119 $attributes['placeholder'] = trim($config['placeholder']);
120 }
121
122 if (isset($config['autocomplete'])) {
123 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'off' : 'on';
124 }
125
126 // Build the attribute string
127 $attributeString = '';
128 foreach ($attributes as $attributeName => $attributeValue) {
129 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
130 }
131
132 $html = '<input type="text" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" ' . $attributeString . ' />';
133
134 // This is the ACTUAL form field - values from the EDITABLE field must be transferred to this field which is the one that is written to the database.
135 $html .= '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
136
137 // Going through all custom evaluations configured for this field
138 // @todo: Similar to above code!
139 foreach ($evalList as $evalData) {
140 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
141 if (class_exists($evalData)) {
142 $evalObj = GeneralUtility::makeInstance($evalData);
143 if (method_exists($evalObj, 'returnFieldJS')) {
144 $resultArray['extJSCODE'] .= LF . 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '}';
145 }
146 }
147 }
148 }
149
150 // Wrap a wizard around the item?
151 $html = $this->renderWizards(
152 array($html),
153 $config['wizards'],
154 $table,
155 $row,
156 $fieldName,
157 $parameterArray,
158 $parameterArray['itemFormElName'],
159 $specConf
160 );
161
162 // Add a wrapper to remain maximum width
163 $width = (int)$this->formMaxWidth($size);
164 $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
165 $resultArray['html'] = $html;
166 return $resultArray;
167 }
168 }