[TASK] Re-style record list search box
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / CachingFrameworkGarbageCollectionAdditionalFieldProvider.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Scheduler\Task;
17
18 use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
22 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
23 use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
24
25 /**
26 * Additional BE fields for caching framework garbage collection task.
27 * Creates a multi selectbox with all available cache backends to select from.
28 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
29 */
30 class CachingFrameworkGarbageCollectionAdditionalFieldProvider extends AbstractAdditionalFieldProvider
31 {
32 /**
33 * Add a multi select box with all available cache backends.
34 *
35 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
36 * @param CachingFrameworkGarbageCollectionTask|null $task When editing, reference to the current task. NULL when adding.
37 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
38 * @return array Array containing all the information pertaining to the additional fields
39 */
40 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
41 {
42 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
43
44 // Initialize selected fields
45 if (empty($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
46 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = [];
47 if ($currentSchedulerModuleAction->equals(Action::ADD)) {
48 // In case of new task, set to dbBackend if it's available
49 if (in_array(Typo3DatabaseBackend::class, $this->getRegisteredBackends())) {
50 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'][] = Typo3DatabaseBackend::class;
51 }
52 } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
53 // In case of editing the task, set to currently selected value
54 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = $task->selectedBackends;
55 }
56 }
57 $fieldName = 'tx_scheduler[scheduler_cachingFrameworkGarbageCollection_selectedBackends][]';
58 $fieldId = 'task_cachingFrameworkGarbageCollection_selectedBackends';
59 $fieldOptions = $this->getCacheBackendOptions($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends']);
60 $fieldHtml = '<select class="form-select" name="' . $fieldName . '" id="' . $fieldId . '" class="from-control" size="10" multiple="multiple">' . $fieldOptions . '</select>';
61 $additionalFields = [];
62 $additionalFields[$fieldId] = [
63 'code' => $fieldHtml,
64 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.cachingFrameworkGarbageCollection.selectBackends',
65 'cshKey' => '_MOD_system_txschedulerM1',
66 'cshLabel' => $fieldId
67 ];
68 return $additionalFields;
69 }
70
71 /**
72 * Checks that all selected backends exist in available backend list
73 *
74 * @param array $submittedData Reference to the array containing the data submitted by the user
75 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
76 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
77 */
78 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
79 {
80 $validData = true;
81 $availableBackends = $this->getRegisteredBackends();
82 if (is_array($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
83 $invalidBackends = array_diff($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'], $availableBackends);
84 if (!empty($invalidBackends)) {
85 $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.selectionOfNonExistingCacheBackends'), FlashMessage::ERROR);
86 $validData = false;
87 }
88 } else {
89 $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.noCacheBackendSelected'), FlashMessage::ERROR);
90 $validData = false;
91 }
92 return $validData;
93 }
94
95 /**
96 * Save selected backends in task object
97 *
98 * @param array $submittedData Contains data submitted by the user
99 * @param CachingFrameworkGarbageCollectionTask $task Reference to the current task object
100 */
101 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
102 {
103 $task->selectedBackends = $submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'];
104 }
105
106 /**
107 * Build select options of available backends and set currently selected backends
108 *
109 * @param array $selectedBackends Selected backends
110 * @return string HTML of selectbox options
111 */
112 protected function getCacheBackendOptions(array $selectedBackends)
113 {
114 $options = [];
115 $availableBackends = $this->getRegisteredBackends();
116 foreach ($availableBackends as $backendName) {
117 if (in_array($backendName, $selectedBackends)) {
118 $selected = ' selected="selected"';
119 } else {
120 $selected = '';
121 }
122 $options[] = '<option value="' . $backendName . '"' . $selected . '>' . $backendName . '</option>';
123 }
124 return implode('', $options);
125 }
126
127 /**
128 * Get all registered caching framework backends
129 *
130 * @return array Registered backends
131 */
132 protected function getRegisteredBackends()
133 {
134 $backends = [];
135 $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
136 if (is_array($cacheConfigurations)) {
137 foreach ($cacheConfigurations as $cacheConfiguration) {
138 $backend = $cacheConfiguration['backend'];
139 if (!in_array($backend, $backends)) {
140 $backends[] = $backend;
141 }
142 }
143 }
144 return $backends;
145 }
146
147 /**
148 * @return LanguageService|null
149 */
150 protected function getLanguageService(): ?LanguageService
151 {
152 return $GLOBALS['LANG'] ?? null;
153 }
154 }