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