2 namespace TYPO3\CMS\Scheduler\Task
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
18 * Additional BE fields for sys log table garbage collection task.
20 * @author Christian Kuhn <lolli@schwarzbu.ch>
22 class TableGarbageCollectionAdditionalFieldProvider
implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface
{
25 * @var array Default number of days by table
27 protected $defaultNumberOfDays = array();
30 * Add additional fields
32 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
33 * @param object $task When editing, reference to the current task object. 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
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;
47 * Initialize the default number of days for all configured tables
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'];
61 * Add a select field of available tables.
63 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
64 * @param object $task When editing, reference to the current task object. 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
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" ' : '';
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(
79 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
80 'cshKey' => '_MOD_system_txschedulerM1',
81 'cshLabel' => $fieldId
83 return $fieldConfiguration;
87 * Add a select field of available tables.
89 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
90 * @param object $task When editing, reference to the current task object. 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
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'];
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>';
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>';
110 $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
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
129 return $fieldConfiguration;
133 * Add a input field to get the number of days.
135 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
136 * @param object $task When editing, reference to the current task object. 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
140 protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
$parentObject) {
141 // Initialize selected fields
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"';
155 if ($task->allTables
=== TRUE) {
156 $disabled = ' disabled="disabled"';
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
167 return $fieldConfiguration;
171 * Validate additional fields
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
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);
185 * Checks if all table field is correct
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
191 public function validateAllTablesAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
$parentObject) {
193 if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
195 } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
202 * Checks given table for existence in configuration array
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
208 public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
$parentObject) {
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'])) {
213 } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
220 * Checks if given number of days is a positive integer
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
226 public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
$parentObject) {
228 if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
230 } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
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
);
240 * Save additional field in task
242 * @param array $submittedData Contains data submitted by the user
243 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
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'];