[FEATURE] Description-field for scheduler-jobs
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / TableGarbageCollectionAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-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 sys log table garbage collection task.
28 *
29 * @author Christian Kuhn <lolli@schwarzbu.ch>
30 */
31 class TableGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
32
33 /**
34 * @var array Default number of days by table
35 */
36 protected $defaultNumberOfDays = array();
37
38 /**
39 * Add additional fields
40 *
41 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
42 * @param object $task When editing, reference to the current task object. Null when adding.
43 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
44 * @return array Array containing all the information pertaining to the additional fields
45 */
46 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
47 $this->initDefaultNumberOfDays();
48 $additionalFields['task_tableGarbageCollection_allTables'] = $this->getAllTablesAdditionalField($taskInfo, $task, $parentObject);
49 $additionalFields['task_tableGarbageCollection_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
50 $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
51 return $additionalFields;
52 }
53
54 /**
55 * Initialize the default number of days for all configured tables
56 *
57 * @return void
58 */
59 protected function initDefaultNumberOfDays() {
60 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
61 foreach ($tableConfiguration as $tableName => $configuration) {
62 if (isset($configuration['expirePeriod'])) {
63 $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
64 }
65 }
66 }
67
68 /**
69 * Add a select field of available tables.
70 *
71 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
72 * @param object $task When editing, reference to the current task object. Null when adding.
73 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
74 * @return array Array containing all the information pertaining to the additional fields
75 */
76 protected function getAllTablesAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
77 if ($parentObject->CMD === 'edit') {
78 $checked = $task->allTables === TRUE ? 'checked="checked" ' : '';
79 } else {
80 $checked = '';
81 }
82 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
83 $fieldId = 'task_tableGarbageCollection_allTables';
84 $fieldHtml = '<input type="checkbox" ' . $checked . 'onChange="actOnChangeSchedulerTableGarbageCollectionAllTables(this)" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" />';
85 $fieldConfiguration = array(
86 'code' => $fieldHtml,
87 'label' => 'LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.allTables',
88 'cshKey' => '_MOD_system_txschedulerM1',
89 'cshLabel' => $fieldId
90 );
91 return $fieldConfiguration;
92 }
93
94 /**
95 * Add a select field of available tables.
96 *
97 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
98 * @param object $task When editing, reference to the current task object. Null when adding.
99 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
100 * @return array Array containing all the information pertaining to the additional fields
101 */
102 protected function getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
103 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
104 $options = array();
105 // Add an empty option on top if an existing task is configured
106 // with a table that can not be found in configuration anymore
107 if ($parentObject->CMD === 'edit' && !array_key_exists($task->table, $tableConfiguration)) {
108 $options[] = '<option value="" selected="selected"></option>';
109 }
110 foreach ($tableConfiguration as $tableName => $configuration) {
111 if ($parentObject->CMD === 'add' && count($options) === 0) {
112 // Select first table by default if adding a new task
113 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
114 } elseif ($task->table === $tableName) {
115 // Select currently selected table
116 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
117 } else {
118 $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
119 }
120 }
121 $disabled = $task->allTables === TRUE ? ' disabled="disabled"' : '';
122 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
123 $fieldId = 'task_tableGarbageCollection_table';
124 $fieldHtml = array();
125 // Add table drop down html
126 $fieldHtml[] = '<select ' . 'name="' . $fieldName . '" ' . $disabled . 'onChange="actOnChangeSchedulerTableGarbageCollectionTable(this)"' . 'id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
127 // Add js array for default 'number of days' values
128 $fieldHtml[] = '<script type="text/javascript">/*<![CDATA[*/<!--';
129 $fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($this->defaultNumberOfDays) . ';';
130 $fieldHtml[] = '// -->/*]]>*/</script>';
131 $fieldConfiguration = array(
132 'code' => implode(LF, $fieldHtml),
133 'label' => 'LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.table',
134 'cshKey' => '_MOD_system_txschedulerM1',
135 'cshLabel' => $fieldId
136 );
137 return $fieldConfiguration;
138 }
139
140 /**
141 * Add a input field to get the number of days.
142 *
143 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
144 * @param object $task When editing, reference to the current task object. Null when adding.
145 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
146 * @return array Array containing all the information pertaining to the additional fields
147 */
148 protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
149 // Initialize selected fields
150 $disabled = '';
151 if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
152 if ($parentObject->CMD === 'add') {
153 // In case of new task, set to 180 days
154 $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
155 } elseif ($parentObject->CMD === 'edit') {
156 // In case of editing the task, set to currently selected value
157 $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
158 if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
159 $disabled = ' disabled="disabled"';
160 }
161 }
162 }
163 if ($task->allTables === TRUE) {
164 $disabled = ' disabled="disabled"';
165 }
166 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
167 $fieldId = 'task_tableGarbageCollection_numberOfDays';
168 $fieldHtml = '<input type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . intval($taskInfo['scheduler_tableGarbageCollection_numberOfDays']) . '" ' . 'size="4" />';
169 $fieldConfiguration = array(
170 'code' => $fieldHtml,
171 'label' => 'LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.numberOfDays',
172 'cshKey' => '_MOD_system_txschedulerM1',
173 'cshLabel' => $fieldId
174 );
175 return $fieldConfiguration;
176 }
177
178 /**
179 * Validate additional fields
180 *
181 * @param array $submittedData Reference to the array containing the data submitted by the user
182 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
183 * @return boolean True if validation was ok (or selected class is not relevant), false otherwise
184 */
185 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
186 $validData = $this->validateAllTablesAdditionalField($submittedData, $parentObject);
187 $validData &= $this->validateTableAdditionalField($submittedData, $parentObject);
188 $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
189 return $validData;
190 }
191
192 /**
193 * Checks if all table field is correct
194 *
195 * @param array $submittedData Reference to the array containing the data submitted by the user
196 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
197 * @return boolean True if data is valid
198 */
199 public function validateAllTablesAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
200 $validData = FALSE;
201 if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
202 $validData = TRUE;
203 } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
204 $validData = TRUE;
205 }
206 return $validData;
207 }
208
209 /**
210 * Checks given table for existence in configuration array
211 *
212 * @param array $submittedData Reference to the array containing the data submitted by the user
213 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
214 * @return boolean True if table exists in configuration, false otherwise
215 */
216 public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
217 $validData = FALSE;
218 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
219 if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
220 $validData = TRUE;
221 } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
222 $validData = TRUE;
223 }
224 return $validData;
225 }
226
227 /**
228 * Checks if given number of days is a positive integer
229 *
230 * @param array $submittedData Reference to the array containing the data submitted by the user
231 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
232 * @return boolean True if validation was ok (or selected class is not relevant), false otherwise
233 */
234 public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
235 $validData = FALSE;
236 if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
237 $validData = TRUE;
238 } elseif (intval($submittedData['scheduler_tableGarbageCollection_numberOfDays']) >= 0) {
239 $validData = TRUE;
240 } else {
241 // Issue error message
242 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:msg.invalidNumberOfDays'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
243 }
244 return $validData;
245 }
246
247 /**
248 * Save additional field in task
249 *
250 * @param array $submittedData Contains data submitted by the user
251 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
252 * @return void
253 */
254 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
255 $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on' ? TRUE : FALSE;
256 $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
257 $task->numberOfDays = intval($submittedData['scheduler_tableGarbageCollection_numberOfDays']);
258 }
259
260 }