‪TYPO3CMS  11.5
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 
24 
30 {
34  protected ‪$defaultNumberOfDays = [];
35 
44  public function ‪getAdditionalFields(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
45  {
47  $additionalFields = [];
48  $additionalFields['task_tableGarbageCollection_allTables'] = $this->‪getAllTablesAdditionalField($taskInfo, $task, $schedulerModule);
49  $additionalFields['task_tableGarbageCollection_table'] = $this->‪getTableAdditionalField($taskInfo, $task, $schedulerModule);
50  $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->‪getNumberOfDaysAdditionalField($taskInfo, $task, $schedulerModule);
51  return $additionalFields;
52  }
53 
57  protected function ‪initDefaultNumberOfDays()
58  {
59  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
60  foreach ($tableConfiguration as $tableName => $configuration) {
61  if (isset($configuration['expirePeriod'])) {
62  $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
63  }
64  }
65  }
66 
75  protected function ‪getAllTablesAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
76  {
77  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
78 
79  if ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
80  $checked = $task->allTables === true ? 'checked="checked" ' : '';
81  } else {
82  $checked = '';
83  }
84 
85  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
86  $fieldId = 'task_tableGarbageCollection_allTables';
87  $fieldHtml = '<div class="form-check"><input class="form-check-input" type="checkbox" ' . $checked . ' name="' . $fieldName . '" id="' . $fieldId . '"></div>';
88  $fieldConfiguration = [
89  'code' => $fieldHtml,
90  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
91  'cshKey' => '_MOD_system_txschedulerM1',
92  'cshLabel' => $fieldId,
93  ];
94 
95  return $fieldConfiguration;
96  }
97 
106  protected function ‪getTableAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
107  {
108  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
109  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
110  $options = [];
111  // Add an empty option on top if an existing task is configured
112  // with a table that can not be found in configuration anymore
113  if ($task && !array_key_exists($task->table, $tableConfiguration) && $currentSchedulerModuleAction->equals(‪Action::EDIT)) {
114  $options[] = '<option value="" selected="selected"></option>';
115  }
116  foreach ($tableConfiguration as $tableName => $configuration) {
117  if ($currentSchedulerModuleAction->equals(‪Action::ADD) && empty($options)) {
118  // Select first table by default if adding a new task
119  $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
120  } elseif ($task && $task->table === $tableName) {
121  // Select currently selected table
122  $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
123  } else {
124  $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
125  }
126  }
127  $disabled = ($task && $task->allTables === true) ? ' disabled="disabled"' : '';
128  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
129  $fieldId = 'task_tableGarbageCollection_table';
130  $fieldHtml = [];
131  // Add table drop down html
132  $fieldHtml[] = '<select class="form-select" name="' . $fieldName . '"' . $disabled . ' id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
133  // Add js array for default 'number of days' values
134  $fieldConfiguration = [
135  'code' => implode(LF, $fieldHtml),
136  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.table',
137  'cshKey' => '_MOD_system_txschedulerM1',
138  'cshLabel' => $fieldId,
139  ];
140  return $fieldConfiguration;
141  }
142 
151  protected function ‪getNumberOfDaysAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
152  {
153  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
154  // Initialize selected fields
155  $disabled = '';
156  if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
157  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
158  // In case of new task, set to 180 days
159  $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
160  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
161  // In case of editing the task, set to currently selected value
162  $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
163  if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
164  $disabled = ' disabled="disabled"';
165  }
166  }
167  }
168  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
169  $fieldId = 'task_tableGarbageCollection_numberOfDays';
170  $attrs = [
171  'class' => 'form-control',
172  'type' => 'text',
173  'name' => $fieldName,
174  'id' => $fieldId,
175  'size' => '4',
176  'value' => (string)(int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'],
177  'data-default-number-of-days' => GeneralUtility::jsonEncodeForHtmlAttribute($this->defaultNumberOfDays, false),
178  ];
179  if ($task && $task->allTables === true) {
180  $attrs['disabled'] = 'disabled';
181  }
182  $fieldHtml = '<input ' . GeneralUtility::implodeAttributes($attrs, true) . '>';
183  $fieldConfiguration = [
184  'code' => $fieldHtml,
185  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.numberOfDays',
186  'cshKey' => '_MOD_system_txschedulerM1',
187  'cshLabel' => $fieldId,
188  ];
189  return $fieldConfiguration;
190  }
191 
199  public function ‪validateAdditionalFields(array &$submittedData, ‪SchedulerModuleController $schedulerModule)
200  {
201  return $this->‪validateAllTablesAdditionalField($submittedData)
202  && $this->‪validateTableAdditionalField($submittedData)
203  && $this->‪validateNumberOfDaysAdditionalField($submittedData);
204  }
205 
212  public function ‪validateAllTablesAdditionalField(array &$submittedData)
213  {
214  $validData = false;
215  if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
216  $validData = true;
217  } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
218  $validData = true;
219  }
220  return $validData;
221  }
222 
229  public function ‪validateTableAdditionalField(array &$submittedData)
230  {
231  $validData = false;
232  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
233  if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
234  $validData = true;
235  } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
236  $validData = true;
237  }
238  return $validData;
239  }
240 
247  public function ‪validateNumberOfDaysAdditionalField(array &$submittedData)
248  {
249  $validData = false;
250  if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
251  $validData = true;
252  } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
253  $validData = true;
254  } else {
255  // Issue error message
256  $this->‪addMessage($this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), ‪FlashMessage::ERROR);
257  }
258  return $validData;
259  }
260 
267  public function ‪saveAdditionalFields(array $submittedData, ‪AbstractTask $task)
268  {
269  $task->allTables = ($submittedData['scheduler_tableGarbageCollection_allTables'] ?? '') === 'on';
270  $task->table = $submittedData['scheduler_tableGarbageCollection_table'] ?? '';
271  $task->numberOfDays = (int)($submittedData['scheduler_tableGarbageCollection_numberOfDays'] ?? 0);
272  }
273 
277  protected function ‪getLanguageService(): ?‪LanguageService
278  {
279  return ‪$GLOBALS['LANG'] ?? null;
280  }
281 }
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getTableAdditionalField
‪array getTableAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:105
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\ADD
‪const ADD
Definition: Action.php:28
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\initDefaultNumberOfDays
‪initDefaultNumberOfDays()
Definition: TableGarbageCollectionAdditionalFieldProvider.php:56
‪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:33
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateAllTablesAdditionalField
‪bool validateAllTablesAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:211
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:35
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getLanguageService
‪LanguageService null getLanguageService()
Definition: TableGarbageCollectionAdditionalFieldProvider.php:276
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
Definition: SchedulerModuleController.php:58
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getAllTablesAdditionalField
‪array getAllTablesAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:74
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\EDIT
‪const EDIT
Definition: Action.php:30
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController\getCurrentAction
‪Action getCurrentAction()
Definition: SchedulerModuleController.php:192
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getAdditionalFields
‪array getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:43
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateNumberOfDaysAdditionalField
‪bool validateNumberOfDaysAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:246
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\saveAdditionalFields
‪saveAdditionalFields(array $submittedData, AbstractTask $task)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:266
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action
Definition: Action.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider
Definition: TableGarbageCollectionAdditionalFieldProvider.php:30
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getNumberOfDaysAdditionalField
‪array getNumberOfDaysAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:150
‪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:228
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateAdditionalFields
‪bool validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:198