[BUGFIX] Fix broken additional fields for garbarge collection task
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / IpAnonymizationAdditionalFieldProvider.php
1 <?php
2
3 namespace TYPO3\CMS\Scheduler\Task;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
20
21 /**
22 * Additional BE fields for ip address anonymization task.
23 */
24 class IpAnonymizationAdditionalFieldProvider implements AdditionalFieldProviderInterface
25 {
26 /**
27 * Add additional fields
28 *
29 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
30 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
31 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
32 * @return array Array containing all the information pertaining to the additional fields
33 */
34 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
35 {
36 $additionalFields = [];
37 $additionalFields['task_ipAnonymization_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
38 $additionalFields['task_ipAnonymization_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
39 $additionalFields['task_ipAnonymization_mask'] = $this->getMaskAdditionalField($taskInfo, $task, $parentObject);
40 return $additionalFields;
41 }
42
43 /**
44 * Add a select field of available tables.
45 *
46 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
47 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
48 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
49 * @return array Array containing all the information pertaining to the additional fields
50 */
51 protected function getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
52 {
53 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\IpAnonymizationTask::class]['options']['tables'];
54 $options = [];
55 // Add an empty option on top if an existing task is configured
56 // with a table that can not be found in configuration anymore
57 if ($parentObject->CMD === 'edit' && !array_key_exists($task->table, $tableConfiguration)) {
58 $options[] = '<option value="" selected="selected"></option>';
59 }
60 foreach ($tableConfiguration as $tableName => $configuration) {
61 if ($parentObject->CMD === 'add' && empty($options)) {
62 // Select first table by default if adding a new task
63 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
64 } elseif ($task->table === $tableName) {
65 // Select currently selected table
66 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
67 } else {
68 $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
69 }
70 }
71 $fieldName = 'tx_scheduler[scheduler_ipAnonymization_table]';
72 $fieldId = 'task_ipAnonymization_table';
73 $fieldHtml = [];
74 // Add table drop down html
75 $fieldHtml[] = '<select class="form-control" name="' . $fieldName . '" id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
76 $fieldConfiguration = [
77 'code' => implode(LF, $fieldHtml),
78 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.table',
79 'cshKey' => '_MOD_system_txschedulerM1',
80 'cshLabel' => $fieldId
81 ];
82 return $fieldConfiguration;
83 }
84
85 /**
86 * Add an input field to get the number of days.
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 getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
94 {
95 $fieldId = 'scheduler_ipAnonymization_numberOfDays';
96 if (empty($taskInfo[$fieldId])) {
97 $taskInfo[$fieldId] = $task->numberOfDays ?? 180;
98 }
99 $fieldName = 'tx_scheduler[' . $fieldId . ']';
100 $fieldHtml = '<input class="form-control" type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . 'value="' . (int)$taskInfo[$fieldId] . '" ' . 'size="4">';
101 $fieldConfiguration = [
102 'code' => $fieldHtml,
103 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.numberOfDays',
104 'cshKey' => '_MOD_system_txschedulerM1',
105 'cshLabel' => $fieldId
106 ];
107 return $fieldConfiguration;
108 }
109
110 /**
111 * Add an input field to get the mask.
112 *
113 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
114 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
115 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
116 * @return array Array containing all the information pertaining to the additional fields
117 */
118 protected function getMaskAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
119 {
120 $fieldId = 'scheduler_ipAnonymization_mask';
121 if (empty($taskInfo[$fieldId])) {
122 $taskInfo[$fieldId] = $task->mask ?? 2;
123 }
124 $fieldName = 'tx_scheduler[' . $fieldId . ']';
125
126 $fieldHtml = '';
127 foreach ([1, 2] as $mask) {
128 $selected = (int)$taskInfo[$fieldId] === $mask ? ' selected' : '';
129 $fieldHtml .= '<option value="' . $mask . '"' . $selected . '>'
130 . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.mask.' . $mask))
131 . '</option>';
132 }
133
134 $fieldHtml = '<select class="form-control" name="' . $fieldName . '" ' . 'id="' . $fieldId . '">' . $fieldHtml . '</select>';
135
136 $fieldConfiguration = [
137 'code' => $fieldHtml,
138 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.mask',
139 'cshKey' => '_MOD_system_txschedulerM1',
140 'cshLabel' => $fieldId
141 ];
142 return $fieldConfiguration;
143 }
144
145 /**
146 * Validate additional fields
147 *
148 * @param array $submittedData Reference to the array containing the data submitted by the user
149 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
150 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
151 */
152 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
153 {
154 $validData = $this->validateTableAdditionalField($submittedData, $parentObject);
155 $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
156 return $validData;
157 }
158
159 /**
160 * Checks given table for existence in configuration array
161 *
162 * @param array $submittedData Reference to the array containing the data submitted by the user
163 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
164 * @return bool True if table exists in configuration, false otherwise
165 */
166 public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
167 {
168 $validData = false;
169 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\IpAnonymizationTask::class]['options']['tables'];
170 if (!isset($submittedData['scheduler_ipAnonymization_table'])) {
171 $validData = true;
172 } elseif (array_key_exists($submittedData['scheduler_ipAnonymization_table'], $tableConfiguration)) {
173 $validData = true;
174 }
175 return $validData;
176 }
177
178 /**
179 * Checks if given number of days is a positive integer
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 bool True if validation was ok (or selected class is not relevant), false otherwise
184 */
185 public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
186 {
187 $validData = false;
188 if (!isset($submittedData['scheduler_ipAnonymization_numberOfDays'])) {
189 $validData = true;
190 } elseif ((int)$submittedData['scheduler_ipAnonymization_numberOfDays'] >= 0) {
191 $validData = true;
192 } else {
193 // Issue error message
194 $parentObject->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
195 }
196 return $validData;
197 }
198
199 /**
200 * Save additional field in task
201 *
202 * @param array $submittedData Contains data submitted by the user
203 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
204 */
205 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
206 {
207 $task->table = $submittedData['scheduler_ipAnonymization_table'];
208 $task->mask = $submittedData['scheduler_ipAnonymization_mask'];
209 $task->numberOfDays = (int)$submittedData['scheduler_ipAnonymization_numberOfDays'];
210 }
211
212 /**
213 * Returns an instance of LanguageService
214 *
215 * @return LanguageService
216 */
217 protected function getLanguageService(): LanguageService
218 {
219 return $GLOBALS['LANG'];
220 }
221 }