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