[BUGFIX] Fix broken scheduler tasks
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Task / CleanerFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Recycler\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 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
19 use TYPO3\CMS\Scheduler\Task\AbstractTask;
20
21 /**
22 * A task that should be run regularly that deletes
23 * datasets flagged as "deleted" from the DB.
24 *
25 * @author Philipp Bergsmann <p.bergsmann@opendo.at>
26 */
27 class CleanerFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
28
29 /**
30 * Gets additional fields to render in the form to add/edit a task
31 *
32 * @param array $taskInfo Values of the fields from the add/edit task form
33 * @param \TYPO3\CMS\Recycler\Task\CleanerTask $task The task object being edited. NULL when adding a task!
34 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
35 * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
36 */
37 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule) {
38 if ($schedulerModule->CMD === 'edit') {
39 $taskInfo['RecyclerCleanerTCA'] = $task->getTcaTables();
40 $taskInfo['RecyclerCleanerPeriod'] = $task->getPeriod();
41 }
42
43 $additionalFields['period'] = array(
44 'code' => '<input type="text" class="form-control" name="tx_scheduler[RecyclerCleanerPeriod]" value="' . $taskInfo['RecyclerCleanerPeriod'] . '">',
45 'label' => 'LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskPeriod',
46 'cshKey' => '',
47 'cshLabel' => 'task_recyclerCleaner_selectedPeriod'
48 );
49
50 $additionalFields['tca'] = array(
51 'code' => $this->getTcaSelectHtml($taskInfo['RecyclerCleanerTCA']),
52 'label' => 'LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskTCA',
53 'cshKey' => '',
54 'cshLabel' => 'task_recyclerCleaner_selectedTables'
55 );
56
57 return $additionalFields;
58 }
59
60 /**
61 * Gets the select-box from the TCA-fields
62 *
63 * @param array $selectedTables
64 * @return string
65 */
66 protected function getTcaSelectHtml($selectedTables = array()) {
67 if (!is_array($selectedTables)) {
68 $selectedTables = array();
69 }
70 $tcaSelectHtml = '<select name="tx_scheduler[RecyclerCleanerTCA][]" multiple="multiple" class="form-control" size="10">';
71
72 $options = array();
73 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
74 if (!$tableConf['ctrl']['adminOnly'] && !empty($tableConf['ctrl']['delete'])) {
75 $selected = in_array($table, $selectedTables, TRUE) ? ' selected="selected"' : '';
76 $tableTitle = $this->getLanguageService()->sL($tableConf['ctrl']['title']);
77 $options[$tableTitle] = '<option' . $selected . ' value="' . $table . '">' . htmlspecialchars($tableTitle . ' (' . $table . ')') . '</option>';
78 }
79 }
80 ksort($options);
81
82 $tcaSelectHtml .= implode('', $options);
83 $tcaSelectHtml .= '</select>';
84
85 return $tcaSelectHtml;
86 }
87
88 /**
89 * Validates the additional fields' values
90 *
91 * @param array $submittedData An array containing the data submitted by the add/edit task form
92 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
93 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
94 */
95 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule) {
96 $validPeriod = $this->validateAdditionalFieldPeriod($submittedData['RecyclerCleanerPeriod'], $schedulerModule);
97 $validTca = $this->validateAdditionalFieldTca($submittedData['RecyclerCleanerTCA'], $schedulerModule);
98
99 return $validPeriod && $validTca;
100 }
101
102 /**
103 * Validates the selected Tables
104 *
105 * @param array $tca The given TCA-tables as array
106 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
107 * @return bool TRUE if validation was ok, FALSE otherwise
108 */
109 protected function validateAdditionalFieldTca($tca, SchedulerModuleController $schedulerModule) {
110 return $this->checkTcaIsNotEmpty($tca, $schedulerModule) && $this->checkTcaIsValid($tca, $schedulerModule);
111 }
112
113 /**
114 * Checks if the array is empty
115 *
116 * @param array $tca The given TCA-tables as array
117 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
118 * @return bool TRUE if validation was ok, FALSE otherwise
119 */
120 protected function checkTcaIsNotEmpty($tca, SchedulerModuleController $schedulerModule) {
121 if (is_array($tca) && !empty($tca)) {
122 $validTca = TRUE;
123 } else {
124 $schedulerModule->addMessage(
125 $this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskErrorTCAempty', TRUE),
126 FlashMessage::ERROR
127 );
128 $validTca = FALSE;
129 }
130
131 return $validTca;
132 }
133
134 /**
135 * Checks if the given tables are in the TCA
136 *
137 * @param array $tca The given TCA-tables as array
138 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
139 * @return bool TRUE if validation was ok, FALSE otherwise
140 */
141 protected function checkTcaIsValid(array $tca, SchedulerModuleController $schedulerModule) {
142 $checkTca = FALSE;
143 foreach ($tca as $tcaTable) {
144 if (!isset($GLOBALS['TCA'][$tcaTable])) {
145 $checkTca = FALSE;
146 $schedulerModule->addMessage(
147 sprintf($this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskErrorTCANotSet', TRUE), $tcaTable),
148 FlashMessage::ERROR
149 );
150 break;
151 } else {
152 $checkTca = TRUE;
153 }
154 }
155
156 return $checkTca;
157 }
158
159 /**
160 * Validates the input of period
161 *
162 * @param int $period The given period as integer
163 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
164 * @return bool TRUE if validation was ok, FALSE otherwise
165 */
166 protected function validateAdditionalFieldPeriod($period, SchedulerModuleController $schedulerModule) {
167 if (!empty($period) && filter_var($period, FILTER_VALIDATE_INT) !== FALSE) {
168 $validPeriod = TRUE;
169 } else {
170 $schedulerModule->addMessage(
171 $this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskErrorPeriod', TRUE),
172 FlashMessage::ERROR
173 );
174 $validPeriod = FALSE;
175 }
176
177 return $validPeriod;
178 }
179
180 /**
181 * Takes care of saving the additional fields' values in the task's object
182 *
183 * @param array $submittedData An array containing the data submitted by the add/edit task form
184 * @param AbstractTask $task Reference to the scheduler backend module
185 * @return void
186 * @throws \InvalidArgumentException
187 */
188 public function saveAdditionalFields(array $submittedData, AbstractTask $task) {
189 if (!$task instanceof CleanerTask) {
190 throw new \InvalidArgumentException(
191 'Expected a task of type \TYPO3\CMS\Recycler\Task\CleanerTask, but got ' . get_class($task),
192 1329219449
193 );
194 }
195
196 $task->setTcaTables($submittedData['RecyclerCleanerTCA']);
197 $task->setPeriod($submittedData['RecyclerCleanerPeriod']);
198 }
199
200 /**
201 * @return \TYPO3\CMS\Lang\LanguageService
202 */
203 protected function getLanguageService() {
204 return $GLOBALS['LANG'];
205 }
206
207 }