[FEATURE] Add table garbage collection scheduler task
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / tasks / class.tx_scheduler_tablegarbagecollection_additionalfieldprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Christian Kuhn <lolli@schwarzbu.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Additional BE fields for sys log table garbage collection task.
27 *
28 * @author Christian Kuhn <lolli@schwarzbu.ch>
29 * @package TYPO3
30 * @subpackage scheduler
31 */
32 class tx_scheduler_TableGarbageCollection_AdditionalFieldProvider implements tx_scheduler_AdditionalFieldProvider {
33
34 /**
35 * Add addional fields
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 tx_scheduler_Module $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, tx_scheduler_Module $parentObject) {
43 $additionalFields['task_tableGarbageCollection_allTables'] = $this->getAllTablesAdditionalField($taskInfo, $task, $parentObject);
44 $additionalFields['task_tableGarbageCollection_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
45 $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
46
47 return $additionalFields;
48 }
49
50 /**
51 * Add a select field of available tables.
52 *
53 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
54 * @param object $task When editing, reference to the current task object. Null when adding.
55 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
56 * @return array Array containing all the information pertaining to the additional fields
57 */
58 protected function getAllTablesAdditionalField(array &$taskInfo, $task, tx_scheduler_Module $parentObject) {
59 if ($parentObject->CMD === 'edit') {
60 $checked = $task->allTables === TRUE ? 'checked="checked" ' : '';
61 } else {
62 $checked = '';
63 }
64
65 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
66 $fieldId = 'task_tableGarbageCollection_allTables';
67 $fieldHtml = '<input type="checkbox" ' .
68 $checked .
69 'onChange="actOnChangeSchedulerTableGarbageCollectionAllTables(this)" ' .
70 'name="' . $fieldName . '" ' .
71 'id="' . $fieldId . '" />';
72
73 $fieldConfiguration = array(
74 'code' => $fieldHtml,
75 'label' => 'LLL:EXT:scheduler/mod1/locallang.xml:label.tableGarbageCollection.allTables',
76 'cshKey' => '_MOD_tools_txschedulerM1',
77 'cshLabel' => $fieldId,
78 );
79
80 return $fieldConfiguration;
81 }
82
83 /**
84 * Add a select field of available tables.
85 *
86 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
87 * @param object $task When editing, reference to the current task object. Null when adding.
88 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
89 * @return array Array containing all the information pertaining to the additional fields
90 */
91 protected function getTableAdditionalField(array &$taskInfo, $task, tx_scheduler_Module $parentObject) {
92 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_scheduler_TableGarbageCollection']['options']['tables'];
93
94 $options = array();
95
96 // Add an empty option on top if an existing task is configured
97 // with a table that can not be found in configuration anymore
98 if ($parentObject->CMD === 'edit' && !array_key_exists($task->table, $tableConfiguration)) {
99 $options[] = '<option value="" selected="selected"></option>';
100 }
101
102 $defaultNumberOfDays = array();
103 foreach ($tableConfiguration as $tableName => $configuration) {
104 if ($parentObject->CMD === 'add' && count($options) === 0) {
105 // Select first table by default if adding a new task
106 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
107 if (isset($configuration['expirePeriod'])) {
108 $defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
109 }
110 } elseif ($task->table === $tableName) {
111 // Select currently selected table
112 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
113 if (isset($configuration['expirePeriod'])) {
114 $defaultNumberOfDays[$tableName] = $task->numberOfDays;
115 }
116 } else {
117 $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
118 if (isset($configuration['expirePeriod'])) {
119 $defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
120 }
121 }
122 }
123
124 $disabled = $task->allTables === TRUE ? ' disabled="disabled"' : '';
125
126 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
127 $fieldId = 'task_tableGarbageCollection_table';
128
129 $fieldHtml = array();
130 // Add table drop down html
131 $fieldHtml[] = '<select ' .
132 'name="' . $fieldName . '" ' .
133 $disabled .
134 'onChange="actOnChangeSchedulerTableGarbageCollectionTable(this)"' .
135 'id="' . $fieldId . '">' .
136 implode(LF, $options) .
137 '</select>';
138 // Add js array for default 'number of days' values
139 $fieldHtml[] = '<script type="text/javascript">/*<![CDATA[*/<!--';
140 $fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($defaultNumberOfDays) . ';';
141 $fieldHtml[] = '// -->/*]]>*/</script>';
142
143 $fieldConfiguration = array(
144 'code' => implode(LF, $fieldHtml),
145 'label' => 'LLL:EXT:scheduler/mod1/locallang.xml:label.tableGarbageCollection.table',
146 'cshKey' => '_MOD_tools_txschedulerM1',
147 'cshLabel' => $fieldId,
148 );
149
150 return $fieldConfiguration;
151 }
152
153 /**
154 * Add a input field to get the number of days.
155 *
156 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
157 * @param object $task When editing, reference to the current task object. Null when adding.
158 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
159 * @return array Array containing all the information pertaining to the additional fields
160 */
161 protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, tx_scheduler_Module $parentObject) {
162 // Initialize selected fields
163 $disabled = '';
164 if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
165 if ($parentObject->CMD === 'add') {
166 // In case of new task, set to 180 days
167 $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
168 } elseif ($parentObject->CMD === 'edit') {
169 // In case of editing the task, set to currently selected value
170 $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
171 if ($task->numberOfDays === 0) {
172 $disabled = ' disabled="disabled"';
173 }
174 }
175 }
176
177 if ($task->allTables === TRUE) {
178 $disabled = ' disabled="disabled"';
179 }
180
181 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
182 $fieldId = 'task_tableGarbageCollection_numberOfDays';
183 $fieldHtml = '<input type="text" ' .
184 'name="' . $fieldName . '" ' .
185 'id="' . $fieldId . '" ' .
186 $disabled .
187 'value="' . intval($taskInfo['scheduler_tableGarbageCollection_numberOfDays']) . '" ' .
188 'size="4" />';
189
190 $fieldConfiguration = array(
191 'code' => $fieldHtml,
192 'label' => 'LLL:EXT:scheduler/mod1/locallang.xml:label.tableGarbageCollection.numberOfDays',
193 'cshKey' => '_MOD_tools_txschedulerM1',
194 'cshLabel' => $fieldId,
195 );
196
197 return $fieldConfiguration;
198 }
199
200 /**
201 * Validate additional fields
202 *
203 * @param array $submittedData Reference to the array containing the data submitted by the user
204 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
205 * @return boolean True if validation was ok (or selected class is not relevant), false otherwise
206 */
207 public function validateAdditionalFields(array &$submittedData, tx_scheduler_Module $parentObject) {
208 $validData = $this->validateAllTablesAdditionalField($submittedData, $parentObject);
209 $validData &= $this->validateTableAdditionalField($submittedData, $parentObject);
210 $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
211
212 return $validData;
213 }
214
215 /**
216 * Checks if all table field is correct
217 *
218 * @param array $submittedData Reference to the array containing the data submitted by the user
219 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
220 * @return boolean True if data is valid
221 */
222 public function validateAllTablesAdditionalField(array &$submittedData, tx_scheduler_Module $parentObject) {
223 $validData = FALSE;
224 if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
225 $validData = TRUE;
226 } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
227 $validData = TRUE;
228 }
229
230 return $validData;
231 }
232
233 /**
234 * Checks given table for existence in configuration array
235 *
236 * @param array $submittedData Reference to the array containing the data submitted by the user
237 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
238 * @return boolean True if table exists in configuration, false otherwise
239 */
240 public function validateTableAdditionalField(array &$submittedData, tx_scheduler_Module $parentObject) {
241 $validData = FALSE;
242 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_scheduler_TableGarbageCollection']['options']['tables'];
243 if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
244 $validData = TRUE;
245 } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
246 $validData = TRUE;
247 }
248
249 return $validData;
250 }
251
252 /**
253 * Checks if given number of days is a positive integer
254 *
255 * @param array $submittedData Reference to the array containing the data submitted by the user
256 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
257 * @return boolean True if validation was ok (or selected class is not relevant), false otherwise
258 */
259 public function validateNumberOfDaysAdditionalField(array &$submittedData, tx_scheduler_Module $parentObject) {
260 $validData = FALSE;
261 if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
262 $validData = TRUE;
263 } elseif (intval($submittedData['scheduler_tableGarbageCollection_numberOfDays']) >= 0) {
264 $validData = TRUE;
265 } else {
266 // Issue error message
267 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:msg.invalidNumberOfDays'), t3lib_FlashMessage::ERROR);
268 }
269
270 return $validData;
271 }
272
273 /**
274 * Save additional field in task
275 *
276 * @param array $submittedData Contains data submitted by the user
277 * @param tx_scheduler_Task $task Reference to the current task object
278 * @return void
279 */
280 public function saveAdditionalFields(array $submittedData, tx_scheduler_Task $task) {
281 $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on' ? TRUE : FALSE;
282 $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
283 $task->numberOfDays = intval($submittedData['scheduler_tableGarbageCollection_numberOfDays']);
284 }
285 }
286
287 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/tasks/class.tx_scheduler_tablegarbagecollection_additionalfieldprovider.php'])) {
288 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/tasks/class.tx_scheduler_tablegarbagecollection_additionalfieldprovider.php']);
289 }
290
291 ?>