[TASK] Re-work/simplify copyright header in PHP files - Part 9
[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 * Additional BE fields for caching framework garbage collection task.
18 * Creates a multi selectbox with all available cache backends to select from.
19 *
20 * @author Christian Kuhn <lolli@schwarzbu.ch>
21 */
22 class CachingFrameworkGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
23
24 /**
25 * Add a multi select box with all available cache backends.
26 *
27 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
28 * @param object $task When editing, reference to the current task object. Null when adding.
29 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
30 * @return array Array containing all the information pertaining to the additional fields
31 */
32 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
33 // Initialize selected fields
34 if (empty($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
35 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = array();
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', $this->getRegisteredBackends())) {
39 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'][] = 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend';
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 name="' . $fieldName . '" id="' . $fieldId . '" class="wide" size="10" multiple="multiple">' . $fieldOptions . '</select>';
50 $additionalFields[$fieldId] = array(
51 'code' => $fieldHtml,
52 'label' => 'LLL:EXT:scheduler/mod1/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 boolean 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 $validData = TRUE;
68 $availableBackends = $this->getRegisteredBackends();
69 if (is_array($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
70 $invalidBackends = array_diff($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'], $availableBackends);
71 if (!empty($invalidBackends)) {
72 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:msg.selectionOfNonExistingCacheBackends'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
73 $validData = FALSE;
74 }
75 } else {
76 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:msg.noCacheBackendSelected'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
77 $validData = FALSE;
78 }
79 return $validData;
80 }
81
82 /**
83 * Save selected backends in task object
84 *
85 * @param array $submittedData Contains data submitted by the user
86 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
87 * @return void
88 */
89 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
90 $task->selectedBackends = $submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'];
91 }
92
93 /**
94 * Build select options of available backends and set currently selected backends
95 *
96 * @param array $selectedBackends Selected backends
97 * @return string HTML of selectbox options
98 */
99 protected function getCacheBackendOptions(array $selectedBackends) {
100 $options = array();
101 $availableBackends = $this->getRegisteredBackends();
102 foreach ($availableBackends as $backendName) {
103 if (in_array($backendName, $selectedBackends)) {
104 $selected = ' selected="selected"';
105 } else {
106 $selected = '';
107 }
108 $options[] = '<option value="' . $backendName . '"' . $selected . '>' . $backendName . '</option>';
109 }
110 return implode('', $options);
111 }
112
113 /**
114 * Get all registered caching framework backends
115 *
116 * @return array Registered backends
117 */
118 protected function getRegisteredBackends() {
119 $backends = array();
120 $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
121 if (is_array($cacheConfigurations)) {
122 foreach ($cacheConfigurations as $cacheConfiguration) {
123 $backend = $cacheConfiguration['backend'];
124 if (!in_array($backend, $backends)) {
125 $backends[] = $backend;
126 }
127 }
128 }
129 return $backends;
130 }
131
132 }