[CLEANUP] Improve the @param/@return/@var PHPDoc
[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 * @author Christian Kuhn <lolli@schwarzbu.ch>
21 */
22 class TableGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
23
24 /**
25 * @var array Default number of days by table
26 */
27 protected $defaultNumberOfDays = array();
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 \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject 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, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
38 $this->initDefaultNumberOfDays();
39 $additionalFields = array();
40 $additionalFields['task_tableGarbageCollection_allTables'] = $this->getAllTablesAdditionalField($taskInfo, $task, $parentObject);
41 $additionalFields['task_tableGarbageCollection_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
42 $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
43 return $additionalFields;
44 }
45
46 /**
47 * Initialize the default number of days for all configured tables
48 *
49 * @return void
50 */
51 protected function initDefaultNumberOfDays() {
52 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
53 foreach ($tableConfiguration as $tableName => $configuration) {
54 if (isset($configuration['expirePeriod'])) {
55 $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
56 }
57 }
58 }
59
60 /**
61 * Add a select field of available tables.
62 *
63 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
64 * @param AbstractTask|NULL $task When editing, reference to the current task. NULL when adding.
65 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
66 * @return array Array containing all the information pertaining to the additional fields
67 */
68 protected function getAllTablesAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
69 if ($parentObject->CMD === 'edit') {
70 $checked = $task->allTables === TRUE ? 'checked="checked" ' : '';
71 } else {
72 $checked = '';
73 }
74 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
75 $fieldId = 'task_tableGarbageCollection_allTables';
76 $fieldHtml = '<input type="checkbox" ' . $checked . ' name="' . $fieldName . '" ' . 'id="' . $fieldId . '" />';
77 $fieldConfiguration = array(
78 'code' => $fieldHtml,
79 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
80 'cshKey' => '_MOD_system_txschedulerM1',
81 'cshLabel' => $fieldId
82 );
83 return $fieldConfiguration;
84 }
85
86 /**
87 * Add a select field of available tables.
88 *
89 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
90 * @param AbstractTask|NULL $task When editing, reference to the current task. NULL when adding.
91 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
92 * @return array Array containing all the information pertaining to the additional fields
93 */
94 protected function getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
95 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
96 $options = array();
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' && count($options) === 0) {
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 = array();
117 // Add table drop down html
118 $fieldHtml[] = '<select ' . '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 = array(
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 a 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 // Initialize selected fields
142 $disabled = '';
143 if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
144 if ($parentObject->CMD === 'add') {
145 // In case of new task, set to 180 days
146 $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
147 } elseif ($parentObject->CMD === 'edit') {
148 // In case of editing the task, set to currently selected value
149 $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
150 if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
151 $disabled = ' disabled="disabled"';
152 }
153 }
154 }
155 if ($task->allTables === TRUE) {
156 $disabled = ' disabled="disabled"';
157 }
158 $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
159 $fieldId = 'task_tableGarbageCollection_numberOfDays';
160 $fieldHtml = '<input type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . (int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] . '" ' . 'size="4" />';
161 $fieldConfiguration = array(
162 'code' => $fieldHtml,
163 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.numberOfDays',
164 'cshKey' => '_MOD_system_txschedulerM1',
165 'cshLabel' => $fieldId
166 );
167 return $fieldConfiguration;
168 }
169
170 /**
171 * Validate additional fields
172 *
173 * @param array $submittedData Reference to the array containing the data submitted by the user
174 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
175 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
176 */
177 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
178 $validData = $this->validateAllTablesAdditionalField($submittedData, $parentObject);
179 $validData &= $this->validateTableAdditionalField($submittedData, $parentObject);
180 $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
181 return $validData;
182 }
183
184 /**
185 * Checks if all table field is correct
186 *
187 * @param array $submittedData Reference to the array containing the data submitted by the user
188 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
189 * @return bool True if data is valid
190 */
191 public function validateAllTablesAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
192 $validData = FALSE;
193 if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
194 $validData = TRUE;
195 } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
196 $validData = TRUE;
197 }
198 return $validData;
199 }
200
201 /**
202 * Checks given table for existence in configuration array
203 *
204 * @param array $submittedData Reference to the array containing the data submitted by the user
205 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
206 * @return bool True if table exists in configuration, false otherwise
207 */
208 public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
209 $validData = FALSE;
210 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
211 if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
212 $validData = TRUE;
213 } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
214 $validData = TRUE;
215 }
216 return $validData;
217 }
218
219 /**
220 * Checks if given number of days is a positive integer
221 *
222 * @param array $submittedData Reference to the array containing the data submitted by the user
223 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
224 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
225 */
226 public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
227 $validData = FALSE;
228 if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
229 $validData = TRUE;
230 } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
231 $validData = TRUE;
232 } else {
233 // Issue error message
234 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
235 }
236 return $validData;
237 }
238
239 /**
240 * Save additional field in task
241 *
242 * @param array $submittedData Contains data submitted by the user
243 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
244 * @return void
245 */
246 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
247 $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on';
248 $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
249 $task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
250 }
251
252 }