[TASK] Merge submodule linkvalidator into core
[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-2013 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 */
32 class CachingFrameworkGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
33
34 /**
35 * Add a multi select box with all available cache backends.
36 *
37 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
38 * @param object $task When editing, reference to the current task object. Null when adding.
39 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
40 * @return array Array containing all the information pertaining to the additional fields
41 */
42 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
43 // Initialize selected fields
44 if (empty($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
45 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = array();
46 if ($parentObject->CMD == 'add') {
47 // In case of new task, set to dbBackend if it's available
48 if (in_array('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', $this->getRegisteredBackends())) {
49 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'][] = 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend';
50 }
51 } elseif ($parentObject->CMD == 'edit') {
52 // In case of editing the task, set to currently selected value
53 $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = $task->selectedBackends;
54 }
55 }
56 $fieldName = 'tx_scheduler[scheduler_cachingFrameworkGarbageCollection_selectedBackends][]';
57 $fieldId = 'task_cachingFrameworkGarbageCollection_selectedBackends';
58 $fieldOptions = $this->getCacheBackendOptions($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends']);
59 $fieldHtml = '<select name="' . $fieldName . '" id="' . $fieldId . '" class="wide" size="10" multiple="multiple">' . $fieldOptions . '</select>';
60 $additionalFields[$fieldId] = array(
61 'code' => $fieldHtml,
62 'label' => 'LLL:EXT:scheduler/mod1/locallang.xml:label.cachingFrameworkGarbageCollection.selectBackends',
63 'cshKey' => '_MOD_tools_txschedulerM1',
64 'cshLabel' => $fieldId
65 );
66 return $additionalFields;
67 }
68
69 /**
70 * Checks that all selected backends exist in available backend list
71 *
72 * @param array $submittedData Reference to the array containing the data submitted by the user
73 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
74 * @return boolean TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
75 */
76 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
77 $validData = TRUE;
78 $availableBackends = $this->getRegisteredBackends();
79 if (is_array($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
80 $invalidBackends = array_diff($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'], $availableBackends);
81 if (!empty($invalidBackends)) {
82 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:msg.selectionOfNonExistingCacheBackends'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
83 $validData = FALSE;
84 }
85 } else {
86 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:msg.noCacheBackendSelected'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
87 $validData = FALSE;
88 }
89 return $validData;
90 }
91
92 /**
93 * Save selected backends in task object
94 *
95 * @param array $submittedData Contains data submitted by the user
96 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
97 * @return void
98 */
99 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
100 $task->selectedBackends = $submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'];
101 }
102
103 /**
104 * Build select options of available backends and set currently selected backends
105 *
106 * @param array $selectedBackends Selected backends
107 * @return string HTML of selectbox options
108 */
109 protected function getCacheBackendOptions(array $selectedBackends) {
110 $options = array();
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 $backends = array();
130 $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
131 if (is_array($cacheConfigurations)) {
132 foreach ($cacheConfigurations as $cacheConfiguration) {
133 $backend = $cacheConfiguration['backend'];
134 if (!in_array($backend, $backends)) {
135 $backends[] = $backend;
136 }
137 }
138 }
139 return $backends;
140 }
141
142 }
143
144
145 ?>