[TASK] Add "Abstract" prefix to abstract task class
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / CachingFrameworkGarbageCollectionAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2011 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Additional BE fields for caching framework garbage collection task.
28 * Creates a multi selectbox with all available cache backends to select from.
29 *
30 * @author Christian Kuhn <lolli@schwarzbu.ch>
31 * @package TYPO3
32 * @subpackage scheduler
33 */
34 class CachingFrameworkGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
35
36 /**
37 * Add a multi select box with all available cache backends.
38 *
39 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
40 * @param object $task When editing, reference to the current task object. Null when adding.
41 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
42 * @return array Array containing all the information pertaining to the additional fields
43 */
44 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
45 // Initialize selected fields
46 if (empty($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
47 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = array();
48 if ($parentObject->CMD == 'add') {
49 // In case of new task, set to dbBackend if it's available
50 if (in_array('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', $this->getRegisteredBackends())) {
51 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'][] = 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend';
52 }
53 } elseif ($parentObject->CMD == 'edit') {
54 // In case of editing the task, set to currently selected value
55 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = $task->selectedBackends;
56 }
57 }
58 $fieldName = 'tx_scheduler[scheduler_cachingFrameworkGarbageCollection_selectedBackends][]';
59 $fieldId = 'task_cachingFrameworkGarbageCollection_selectedBackends';
60 $fieldOptions = $this->getCacheBackendOptions($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends']);
61 $fieldHtml = '<select name="' . $fieldName . '" id="' . $fieldId . '" class="wide" size="10" multiple="multiple">' . $fieldOptions . '</select>';
62 $additionalFields[$fieldId] = array(
63 'code' => $fieldHtml,
64 'label' => 'LLL:EXT:scheduler/mod1/locallang.xml:label.cachingFrameworkGarbageCollection.selectBackends',
65 'cshKey' => '_MOD_tools_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 \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
76 * @return boolean TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
77 */
78 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
79 $validData = TRUE;
80 $availableBackends = $this->getRegisteredBackends();
81 if (is_array($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
82 $invalidBackends = array_diff($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'], $availableBackends);
83 if (!empty($invalidBackends)) {
84 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:msg.selectionOfNonExistingCacheBackends'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
85 $validData = FALSE;
86 }
87 } else {
88 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:msg.noCacheBackendSelected'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
89 $validData = FALSE;
90 }
91 return $validData;
92 }
93
94 /**
95 * Save selected backends in task object
96 *
97 * @param array $submittedData Contains data submitted by the user
98 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
99 * @return void
100 */
101 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
102 $task->selectedBackends = $submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'];
103 }
104
105 /**
106 * Build select options of available backends and set currently selected backends
107 *
108 * @param array $selectedBackends Selected backends
109 * @return string HTML of selectbox options
110 */
111 protected function getCacheBackendOptions(array $selectedBackends) {
112 $options = array();
113 $availableBackends = $this->getRegisteredBackends();
114 foreach ($availableBackends as $backendName) {
115 if (in_array($backendName, $selectedBackends)) {
116 $selected = ' selected="selected"';
117 } else {
118 $selected = '';
119 }
120 $options[] = '<option value="' . $backendName . '"' . $selected . '>' . $backendName . '</option>';
121 }
122 return implode('', $options);
123 }
124
125 /**
126 * Get all registered caching framework backends
127 *
128 * @return array Registered backends
129 */
130 protected function getRegisteredBackends() {
131 $backends = array();
132 $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
133 if (is_array($cacheConfigurations)) {
134 foreach ($cacheConfigurations as $cacheConfiguration) {
135 $backend = $cacheConfiguration['backend'];
136 if (!in_array($backend, $backends)) {
137 $backends[] = $backend;
138 }
139 }
140 }
141 return $backends;
142 }
143
144 }
145
146
147 ?>