fd90bd899a0022d7694ebec83063c3d23b13c787
[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 * @author Christian Kuhn <lolli@schwarzbu.ch>
22 */
23 class CachingFrameworkGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
24
25 /**
26 * Add a multi select box with all available cache backends.
27 *
28 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
29 * @param object $task When editing, reference to the current task object. Null when adding.
30 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
31 * @return array Array containing all the information pertaining to the additional fields
32 */
33 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
34 // Initialize selected fields
35 if (empty($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
36 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = array();
37 if ($parentObject->CMD === 'add') {
38 // In case of new task, set to dbBackend if it's available
39 if (in_array(\TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class, $this->getRegisteredBackends())) {
40 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'][] = \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class;
41 }
42 } elseif ($parentObject->CMD === 'edit') {
43 // In case of editing the task, set to currently selected value
44 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = $task->selectedBackends;
45 }
46 }
47 $fieldName = 'tx_scheduler[scheduler_cachingFrameworkGarbageCollection_selectedBackends][]';
48 $fieldId = 'task_cachingFrameworkGarbageCollection_selectedBackends';
49 $fieldOptions = $this->getCacheBackendOptions($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends']);
50 $fieldHtml = '<select name="' . $fieldName . '" id="' . $fieldId . '" class="wide" size="10" multiple="multiple">' . $fieldOptions . '</select>';
51 $additionalFields[$fieldId] = array(
52 'code' => $fieldHtml,
53 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.cachingFrameworkGarbageCollection.selectBackends',
54 'cshKey' => '_MOD_system_txschedulerM1',
55 'cshLabel' => $fieldId
56 );
57 return $additionalFields;
58 }
59
60 /**
61 * Checks that all selected backends exist in available backend list
62 *
63 * @param array $submittedData Reference to the array containing the data submitted by the user
64 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
65 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
66 */
67 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
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 * @return void
89 */
90 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
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 $options = array();
102 $availableBackends = $this->getRegisteredBackends();
103 foreach ($availableBackends as $backendName) {
104 if (in_array($backendName, $selectedBackends)) {
105 $selected = ' selected="selected"';
106 } else {
107 $selected = '';
108 }
109 $options[] = '<option value="' . $backendName . '"' . $selected . '>' . $backendName . '</option>';
110 }
111 return implode('', $options);
112 }
113
114 /**
115 * Get all registered caching framework backends
116 *
117 * @return array Registered backends
118 */
119 protected function getRegisteredBackends() {
120 $backends = array();
121 $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
122 if (is_array($cacheConfigurations)) {
123 foreach ($cacheConfigurations as $cacheConfiguration) {
124 $backend = $cacheConfiguration['backend'];
125 if (!in_array($backend, $backends)) {
126 $backends[] = $backend;
127 }
128 }
129 }
130 return $backends;
131 }
132
133 }