‪TYPO3CMS  9.5
TableGarbageCollectionAdditionalFieldProvider.php
Go to the documentation of this file.
1 <?php
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 
21 
27 {
31  protected ‪$defaultNumberOfDays = [];
32 
41  public function ‪getAdditionalFields(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
42  {
44  $additionalFields = [];
45  $additionalFields['task_tableGarbageCollection_allTables'] = $this->‪getAllTablesAdditionalField($taskInfo, $task, $schedulerModule);
46  $additionalFields['task_tableGarbageCollection_table'] = $this->‪getTableAdditionalField($taskInfo, $task, $schedulerModule);
47  $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->‪getNumberOfDaysAdditionalField($taskInfo, $task, $schedulerModule);
48  return $additionalFields;
49  }
50 
54  protected function ‪initDefaultNumberOfDays()
55  {
56  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
57  foreach ($tableConfiguration as $tableName => $configuration) {
58  if (isset($configuration['expirePeriod'])) {
59  $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
60  }
61  }
62  }
63 
72  protected function ‪getAllTablesAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
73  {
74  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
75 
76  if ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
77  $checked = $task->allTables === true ? 'checked="checked" ' : '';
78  } else {
79  $checked = '';
80  }
81 
82  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
83  $fieldId = 'task_tableGarbageCollection_allTables';
84  $fieldHtml = '<div class="checkbox"><label><input type="checkbox" ' . $checked . ' name="' . $fieldName . '" ' . 'id="' . $fieldId . '"></label></div>';
85  $fieldConfiguration = [
86  'code' => $fieldHtml,
87  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
88  'cshKey' => '_MOD_system_txschedulerM1',
89  'cshLabel' => $fieldId
90  ];
91 
92  return $fieldConfiguration;
93  }
94 
103  protected function ‪getTableAdditionalField(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
104  {
105  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
106  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
107  $options = [];
108  // Add an empty option on top if an existing task is configured
109  // with a table that can not be found in configuration anymore
110  if (!array_key_exists($task->table, $tableConfiguration) && $currentSchedulerModuleAction->equals(‪Action::EDIT)) {
111  $options[] = '<option value="" selected="selected"></option>';
112  }
113  foreach ($tableConfiguration as $tableName => $configuration) {
114  if ($currentSchedulerModuleAction->equals(‪Action::ADD) && empty($options)) {
115  // Select first table by default if adding a new task
116  $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
117  } elseif ($task->table === $tableName) {
118  // Select currently selected table
119  $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
120  } else {
121  $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
122  }
123  }
124  $disabled = $task->allTables === true ? ' disabled="disabled"' : '';
125  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
126  $fieldId = 'task_tableGarbageCollection_table';
127  $fieldHtml = [];
128  // Add table drop down html
129  $fieldHtml[] = '<select class="form-control" name="' . $fieldName . '" ' . $disabled . ' id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
130  // Add js array for default 'number of days' values
131  $fieldHtml[] = '<script type="text/javascript">/*<![CDATA[*/<!--';
132  $fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($this->defaultNumberOfDays) . ';';
133  $fieldHtml[] = '// -->/*]]>*/</script>';
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  if ($task->allTables === true) {
169  $disabled = ' disabled="disabled"';
170  }
171  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
172  $fieldId = 'task_tableGarbageCollection_numberOfDays';
173  $fieldHtml = '<input class="form-control" type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . (int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] . '" ' . 'size="4">';
174  $fieldConfiguration = [
175  'code' => $fieldHtml,
176  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.numberOfDays',
177  'cshKey' => '_MOD_system_txschedulerM1',
178  'cshLabel' => $fieldId
179  ];
180  return $fieldConfiguration;
181  }
182 
190  public function ‪validateAdditionalFields(array &$submittedData, ‪SchedulerModuleController $schedulerModule)
191  {
192  $validData = $this->‪validateAllTablesAdditionalField($submittedData);
193  $validData &= $this->‪validateTableAdditionalField($submittedData);
194  $validData &= $this->‪validateNumberOfDaysAdditionalField($submittedData);
195  return $validData;
196  }
197 
204  public function ‪validateAllTablesAdditionalField(array &$submittedData)
205  {
206  $validData = false;
207  if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
208  $validData = true;
209  } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
210  $validData = true;
211  }
212  return $validData;
213  }
214 
221  public function ‪validateTableAdditionalField(array &$submittedData)
222  {
223  $validData = false;
224  $tableConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
225  if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
226  $validData = true;
227  } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
228  $validData = true;
229  }
230  return $validData;
231  }
232 
239  public function ‪validateNumberOfDaysAdditionalField(array &$submittedData)
240  {
241  $validData = false;
242  if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
243  $validData = true;
244  } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
245  $validData = true;
246  } else {
247  // Issue error message
248  $this->‪addMessage(‪$GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), ‪FlashMessage::ERROR);
249  }
250  return $validData;
251  }
252 
259  public function ‪saveAdditionalFields(array $submittedData, ‪AbstractTask $task)
260  {
261  $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on';
262  $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
263  $task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
264  }
265 }
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getTableAdditionalField
‪array getTableAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:102
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\ADD
‪const ADD
Definition: Action.php:26
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\initDefaultNumberOfDays
‪initDefaultNumberOfDays()
Definition: TableGarbageCollectionAdditionalFieldProvider.php:53
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:2
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider
Definition: AbstractAdditionalFieldProvider.php:26
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider\addMessage
‪addMessage(string $message, int $severity=FlashMessage::OK)
Definition: AbstractAdditionalFieldProvider.php:33
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\$defaultNumberOfDays
‪array $defaultNumberOfDays
Definition: TableGarbageCollectionAdditionalFieldProvider.php:30
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateAllTablesAdditionalField
‪bool validateAllTablesAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:203
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:32
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
Definition: SchedulerModuleController.php:53
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getAllTablesAdditionalField
‪array getAllTablesAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:71
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\EDIT
‪const EDIT
Definition: Action.php:28
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController\getCurrentAction
‪Action getCurrentAction()
Definition: SchedulerModuleController.php:198
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\getAdditionalFields
‪array getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:40
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateNumberOfDaysAdditionalField
‪bool validateNumberOfDaysAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:238
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\saveAdditionalFields
‪saveAdditionalFields(array $submittedData, AbstractTask $task)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:258
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action
Definition: Action.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider
Definition: TableGarbageCollectionAdditionalFieldProvider.php:27
‪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:29
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateTableAdditionalField
‪bool validateTableAdditionalField(array &$submittedData)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:220
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionAdditionalFieldProvider\validateAdditionalFields
‪bool validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
Definition: TableGarbageCollectionAdditionalFieldProvider.php:189