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