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