‪TYPO3CMS  10.4
TableGarbageCollectionAdditionalFieldProvider.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
29 {
33  protected ‪$defaultNumberOfDays = [];
34 
43  public function ‪getAdditionalFields(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
44  {
46  $additionalFields = [];
47  $additionalFields['task_tableGarbageCollection_allTables'] = $this->‪getAllTablesAdditionalField($taskInfo, $task, $schedulerModule);
48  $additionalFields['task_tableGarbageCollection_table'] = $this->‪getTableAdditionalField($taskInfo, $task, $schedulerModule);
49  $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->‪getNumberOfDaysAdditionalField($taskInfo, $task, $schedulerModule);
50  return $additionalFields;
51  }
52 
56  protected function ‪initDefaultNumberOfDays()
57  {
58  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
59  foreach ($tableConfiguration as $tableName => $configuration) {
60  if (isset($configuration['expirePeriod'])) {
61  $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
62  }
63  }
64  }
65 
74  protected function ‪getAllTablesAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
75  {
76  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
77 
78  if ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
79  $checked = $task->allTables === true ? 'checked="checked" ' : '';
80  } else {
81  $checked = '';
82  }
83 
84  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
85  $fieldId = 'task_tableGarbageCollection_allTables';
86  $fieldHtml = '<div class="checkbox"><label><input type="checkbox" ' . $checked . ' name="' . $fieldName . '" id="' . $fieldId . '"></label></div>';
87  $fieldConfiguration = [
88  'code' => $fieldHtml,
89  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
90  'cshKey' => '_MOD_system_txschedulerM1',
91  'cshLabel' => $fieldId
92  ];
93 
94  return $fieldConfiguration;
95  }
96 
105  protected function ‪getTableAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
106  {
107  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
108  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
109  $options = [];
110  // Add an empty option on top if an existing task is configured
111  // with a table that can not be found in configuration anymore
112  if (!array_key_exists($task->table, $tableConfiguration) && $currentSchedulerModuleAction->equals(‪Action::EDIT)) {
113  $options[] = '<option value="" selected="selected"></option>';
114  }
115  foreach ($tableConfiguration as $tableName => $configuration) {
116  if ($currentSchedulerModuleAction->equals(‪Action::ADD) && empty($options)) {
117  // Select first table by default if adding a new task
118  $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
119  } elseif ($task->table === $tableName) {
120  // Select currently selected table
121  $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
122  } else {
123  $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
124  }
125  }
126  $disabled = $task->allTables === true ? ' disabled="disabled"' : '';
127  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
128  $fieldId = 'task_tableGarbageCollection_table';
129  $fieldHtml = [];
130  // Add table drop down html
131  $fieldHtml[] = '<select class="form-control" name="' . $fieldName . '"' . $disabled . ' id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
132  // Add js array for default 'number of days' values
133  $fieldHtml[] = '<script>/*<![CDATA[*/<!--';
134  $fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($this->defaultNumberOfDays) . ';';
135  $fieldHtml[] = '// -->/*]]>*/</script>';
136  $fieldConfiguration = [
137  'code' => implode(LF, $fieldHtml),
138  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.table',
139  'cshKey' => '_MOD_system_txschedulerM1',
140  'cshLabel' => $fieldId
141  ];
142  return $fieldConfiguration;
143  }
144 
153  protected function ‪getNumberOfDaysAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
154  {
155  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
156  // Initialize selected fields
157  $disabled = '';
158  if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
159  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
160  // In case of new task, set to 180 days
161  $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
162  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
163  // In case of editing the task, set to currently selected value
164  $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
165  if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
166  $disabled = ' disabled="disabled"';
167  }
168  }
169  }
170  if ($task->allTables === true) {
171  $disabled = ' disabled="disabled"';
172  }
173  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
174  $fieldId = 'task_tableGarbageCollection_numberOfDays';
175  $fieldHtml = '<input class="form-control" type="text" name="' . $fieldName . '" id="' . $fieldId . '"' . $disabled . ' value="' . (int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] . '" size="4">';
176  $fieldConfiguration = [
177  'code' => $fieldHtml,
178  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.numberOfDays',
179  'cshKey' => '_MOD_system_txschedulerM1',
180  'cshLabel' => $fieldId
181  ];
182  return $fieldConfiguration;
183  }
184 
192  public function ‪validateAdditionalFields(array &$submittedData, ‪SchedulerModuleController $schedulerModule)
193  {
194  $validData = $this->‪validateAllTablesAdditionalField($submittedData);
195  $validData &= $this->‪validateTableAdditionalField($submittedData);
196  $validData &= $this->‪validateNumberOfDaysAdditionalField($submittedData);
197  return $validData;
198  }
199 
206  public function ‪validateAllTablesAdditionalField(array &$submittedData)
207  {
208  $validData = false;
209  if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
210  $validData = true;
211  } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
212  $validData = true;
213  }
214  return $validData;
215  }
216 
223  public function ‪validateTableAdditionalField(array &$submittedData)
224  {
225  $validData = false;
226  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
227  if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
228  $validData = true;
229  } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
230  $validData = true;
231  }
232  return $validData;
233  }
234 
241  public function ‪validateNumberOfDaysAdditionalField(array &$submittedData)
242  {
243  $validData = false;
244  if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
245  $validData = true;
246  } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
247  $validData = true;
248  } else {
249  // Issue error message
250  $this->‪addMessage($this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), ‪FlashMessage::ERROR);
251  }
252  return $validData;
253  }
254 
261  public function ‪saveAdditionalFields(array $submittedData, ‪AbstractTask $task)
262  {
263  $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on';
264  $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
265  $task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
266  }
267 
271  protected function ‪getLanguageService(): ?‪LanguageService
272  {
273  return ‪$GLOBALS['LANG'] ?? null;
274  }
275 }
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getTableAdditionalField
‪array getTableAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:104
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\ADD
‪const ADD
Definition: Action.php:28
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\initDefaultNumberOfDays
‪initDefaultNumberOfDays()
Definition: TableGarbageCollectionAdditionalFieldProvider.php:55
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:16
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider
Definition: AbstractAdditionalFieldProvider.php:28
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider\addMessage
‪addMessage(string $message, int $severity=FlashMessage::OK)
Definition: AbstractAdditionalFieldProvider.php:35
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\$defaultNumberOfDays
‪array $defaultNumberOfDays
Definition: TableGarbageCollectionAdditionalFieldProvider.php:32
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateAllTablesAdditionalField
‪bool validateAllTablesAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:205
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:35
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getLanguageService
‪LanguageService null getLanguageService()
Definition: TableGarbageCollectionAdditionalFieldProvider.php:270
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
Definition: SchedulerModuleController.php:56
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getAllTablesAdditionalField
‪array getAllTablesAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:73
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\EDIT
‪const EDIT
Definition: Action.php:30
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController\getCurrentAction
‪Action getCurrentAction()
Definition: SchedulerModuleController.php:181
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getAdditionalFields
‪array getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:42
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateNumberOfDaysAdditionalField
‪bool validateNumberOfDaysAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:240
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\saveAdditionalFields
‪saveAdditionalFields(array $submittedData, AbstractTask $task)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:260
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action
Definition: Action.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider
Definition: TableGarbageCollectionAdditionalFieldProvider.php:29
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getNumberOfDaysAdditionalField
‪array getNumberOfDaysAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:152
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateTableAdditionalField
‪bool validateTableAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:222
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateAdditionalFields
‪bool validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:191