TYPO3 CMS  TYPO3_8-7
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 {
25  protected $defaultNumberOfDays = [];
26 
35  public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
36  {
37  $this->initDefaultNumberOfDays();
38  $additionalFields = [];
39  $additionalFields['task_tableGarbageCollection_allTables'] = $this->getAllTablesAdditionalField($taskInfo, $task, $parentObject);
40  $additionalFields['task_tableGarbageCollection_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
41  $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
42  return $additionalFields;
43  }
44 
48  protected function initDefaultNumberOfDays()
49  {
50  $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
51  foreach ($tableConfiguration as $tableName => $configuration) {
52  if (isset($configuration['expirePeriod'])) {
53  $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
54  }
55  }
56  }
57 
66  protected function getAllTablesAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
67  {
68  if ($parentObject->CMD === 'edit') {
69  $checked = $task->allTables === true ? 'checked="checked" ' : '';
70  } else {
71  $checked = '';
72  }
73  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
74  $fieldId = 'task_tableGarbageCollection_allTables';
75  $fieldHtml = '<div class="checkbox"><label><input type="checkbox" ' . $checked . ' name="' . $fieldName . '" ' . 'id="' . $fieldId . '"></label></div>';
76  $fieldConfiguration = [
77  'code' => $fieldHtml,
78  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
79  'cshKey' => '_MOD_system_txschedulerM1',
80  'cshLabel' => $fieldId
81  ];
82  return $fieldConfiguration;
83  }
84 
93  protected function getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
94  {
95  $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
96  $options = [];
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' && empty($options)) {
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 = [];
117  // Add table drop down html
118  $fieldHtml[] = '<select class="form-control" 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 = [
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 
140  protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
141  {
142  // Initialize selected fields
143  $disabled = '';
144  if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
145  if ($parentObject->CMD === 'add') {
146  // In case of new task, set to 180 days
147  $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
148  } elseif ($parentObject->CMD === 'edit') {
149  // In case of editing the task, set to currently selected value
150  $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
151  if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
152  $disabled = ' disabled="disabled"';
153  }
154  }
155  }
156  if ($task->allTables === true) {
157  $disabled = ' disabled="disabled"';
158  }
159  $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
160  $fieldId = 'task_tableGarbageCollection_numberOfDays';
161  $fieldHtml = '<input class="form-control" type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . (int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] . '" ' . 'size="4">';
162  $fieldConfiguration = [
163  'code' => $fieldHtml,
164  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.numberOfDays',
165  'cshKey' => '_MOD_system_txschedulerM1',
166  'cshLabel' => $fieldId
167  ];
168  return $fieldConfiguration;
169  }
170 
178  public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
179  {
180  $validData = $this->validateAllTablesAdditionalField($submittedData, $parentObject);
181  $validData &= $this->validateTableAdditionalField($submittedData, $parentObject);
182  $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
183  return $validData;
184  }
185 
193  public function validateAllTablesAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
194  {
195  $validData = false;
196  if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
197  $validData = true;
198  } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
199  $validData = true;
200  }
201  return $validData;
202  }
203 
211  public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
212  {
213  $validData = false;
214  $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
215  if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
216  $validData = true;
217  } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
218  $validData = true;
219  }
220  return $validData;
221  }
222 
230  public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
231  {
232  $validData = false;
233  if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
234  $validData = true;
235  } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
236  $validData = true;
237  } else {
238  // Issue error message
239  $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
240  }
241  return $validData;
242  }
243 
250  public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
251  {
252  $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on';
253  $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
254  $task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
255  }
256 }
getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
validateAllTablesAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getAllTablesAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)