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