ab4f007738cf7a4200fcb664783c24626cc793eb
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Wizard / ValueSliderWizard.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Wizard;
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\Core\Utility\GeneralUtility;
18
19 /**
20 * Wizard for rendering an AJAX selector for records
21 */
22 class ValueSliderWizard
23 {
24 /**
25 * Renders the slider value wizard
26 *
27 * @param array $params
28 * @return string
29 */
30 public function renderWizard($params)
31 {
32 $field = $params['field'];
33 if (is_array($params['row'][$field])) {
34 $value = $params['row'][$field][0];
35 } else {
36 $value = $params['row'][$field];
37 }
38 // If Slider is used in a flexform
39 if (!empty($params['flexFormPath'])) {
40 $flexFormTools = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools::class);
41 $flexFormValue = $flexFormTools->getArrayValueByPath($params['flexFormPath'], GeneralUtility::xml2array($value));
42 if ($flexFormValue !== null) {
43 $value = $flexFormValue;
44 }
45 }
46 $itemName = $params['itemName'];
47 // Set default values (which correspond to those of the JS component)
48 $min = 0;
49 $max = 10000;
50 // Use the range property, if defined, to set min and max values
51 if (isset($params['fieldConfig']['range'])) {
52 $min = isset($params['fieldConfig']['range']['lower']) ? (int)$params['fieldConfig']['range']['lower'] : 0;
53 $max = isset($params['fieldConfig']['range']['upper']) ? (int)$params['fieldConfig']['range']['upper'] : 10000;
54 }
55 $elementType = $params['fieldConfig']['type'];
56 $step = $params['wConf']['step'] ?: 1;
57 $width = (int)$params['wConf']['width'] ?: 400;
58 $type = 'null';
59 if (isset($params['fieldConfig']['eval'])) {
60 $eval = GeneralUtility::trimExplode(',', $params['fieldConfig']['eval'], true);
61 if (in_array('int', $eval, true)) {
62 $type = 'int';
63 $value = (int)$value;
64 } elseif (in_array('double2', $eval, true)) {
65 $type = 'double';
66 $value = (double)$value;
67 }
68 }
69 if (isset($params['fieldConfig']['items'])) {
70 $type = 'array';
71 $index = 0;
72 $itemAmount = count($params['fieldConfig']['items']);
73 for (; $index < $itemAmount; ++$index) {
74 $item = $params['fieldConfig']['items'][$index];
75 if ((string)$item[1] === $value) {
76 break;
77 }
78 }
79 $min = 0;
80 $max = $itemAmount -1;
81 $step = 1;
82 $value = $index;
83 }
84 $callbackParams = [ $params['table'], $params['row']['uid'], $params['field'], $params['itemName'] ];
85 $id = 'slider-' . $params['md5ID'];
86 $content =
87 '<div'
88 . ' id="' . $id . '"'
89 . ' data-slider-id="' . $id . '"'
90 . ' data-slider-min="' . $min . '"'
91 . ' data-slider-max="' . $max . '"'
92 . ' data-slider-step="' . htmlspecialchars($step) . '"'
93 . ' data-slider-value="' . htmlspecialchars($value) . '"'
94 . ' data-slider-value-type="' . htmlspecialchars($type) . '"'
95 . ' data-slider-item-name="' . htmlspecialchars($itemName) . '"'
96 . ' data-slider-element-type="' . htmlspecialchars($elementType) . '"'
97 . ' data-slider-callback-params="' . htmlspecialchars(json_encode($callbackParams)) . '"'
98 . ' style="width: ' . $width . 'px;"'
99 . '></div>';
100
101 return $content;
102 }
103 }