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