TableGarbageCollectionAdditionalFieldProvider.php 12.9 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Scheduler\Task;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
18
19
20
21
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;

22
23
/**
 * Additional BE fields for sys log table garbage collection task.
24
 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
25
 */
26
class TableGarbageCollectionAdditionalFieldProvider extends AbstractAdditionalFieldProvider
27
28
29
30
{
    /**
     * @var array Default number of days by table
     */
31
    protected $defaultNumberOfDays = [];
32

33
34
35
36
    /**
     * Add additional fields
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
37
     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
38
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
39
40
     * @return array Array containing all the information pertaining to the additional fields
     */
41
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
42
43
    {
        $this->initDefaultNumberOfDays();
44
        $additionalFields = [];
45
46
47
        $additionalFields['task_tableGarbageCollection_allTables'] = $this->getAllTablesAdditionalField($taskInfo, $task, $schedulerModule);
        $additionalFields['task_tableGarbageCollection_table'] = $this->getTableAdditionalField($taskInfo, $task, $schedulerModule);
        $additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $schedulerModule);
48
49
        return $additionalFields;
    }
50

51
52
53
54
55
    /**
     * Initialize the default number of days for all configured tables
     */
    protected function initDefaultNumberOfDays()
    {
56
        $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
57
58
59
60
61
62
        foreach ($tableConfiguration as $tableName => $configuration) {
            if (isset($configuration['expirePeriod'])) {
                $this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
            }
        }
    }
63

64
65
66
67
    /**
     * Add a select field of available tables.
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
68
     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
69
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
70
71
     * @return array Array containing all the information pertaining to the additional fields
     */
72
    protected function getAllTablesAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
73
    {
74
75
76
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();

        if ($currentSchedulerModuleAction->equals(Action::EDIT)) {
77
78
79
80
            $checked = $task->allTables === true ? 'checked="checked" ' : '';
        } else {
            $checked = '';
        }
81

82
83
84
        $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
        $fieldId = 'task_tableGarbageCollection_allTables';
        $fieldHtml = '<div class="checkbox"><label><input type="checkbox" ' . $checked . ' name="' . $fieldName . '" ' . 'id="' . $fieldId . '"></label></div>';
85
        $fieldConfiguration = [
86
87
88
89
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.allTables',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
90
        ];
91

92
93
        return $fieldConfiguration;
    }
94

95
96
97
98
    /**
     * Add a select field of available tables.
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
99
     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
100
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
101
102
     * @return array Array containing all the information pertaining to the additional fields
     */
103
    protected function getTableAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
104
    {
105
106
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
        $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
107
        $options = [];
108
109
        // Add an empty option on top if an existing task is configured
        // with a table that can not be found in configuration anymore
110
        if (!array_key_exists($task->table, $tableConfiguration) && $currentSchedulerModuleAction->equals(Action::EDIT)) {
111
112
113
            $options[] = '<option value="" selected="selected"></option>';
        }
        foreach ($tableConfiguration as $tableName => $configuration) {
114
            if ($currentSchedulerModuleAction->equals(Action::ADD) && empty($options)) {
115
116
117
118
119
120
121
122
123
124
125
126
                // Select first table by default if adding a new task
                $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
            } elseif ($task->table === $tableName) {
                // Select currently selected table
                $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
            } else {
                $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
            }
        }
        $disabled = $task->allTables === true ? ' disabled="disabled"' : '';
        $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
        $fieldId = 'task_tableGarbageCollection_table';
127
        $fieldHtml = [];
128
129
130
131
132
133
        // Add table drop down html
        $fieldHtml[] = '<select class="form-control" name="' . $fieldName . '" ' . $disabled . ' id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
        // Add js array for default 'number of days' values
        $fieldHtml[] = '<script type="text/javascript">/*<![CDATA[*/<!--';
        $fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($this->defaultNumberOfDays) . ';';
        $fieldHtml[] = '// -->/*]]>*/</script>';
134
        $fieldConfiguration = [
135
136
137
138
            'code' => implode(LF, $fieldHtml),
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.table',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
139
        ];
140
141
        return $fieldConfiguration;
    }
142

143
144
145
146
    /**
     * Add an input field to get the number of days.
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
147
     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
148
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
149
150
     * @return array Array containing all the information pertaining to the additional fields
     */
151
    protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
152
    {
153
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
154
155
156
        // Initialize selected fields
        $disabled = '';
        if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
157
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
158
159
                // In case of new task, set to 180 days
                $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
160
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
161
162
163
164
165
166
167
168
169
170
171
172
173
                // In case of editing the task, set to currently selected value
                $taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
                if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
                    $disabled = ' disabled="disabled"';
                }
            }
        }
        if ($task->allTables === true) {
            $disabled = ' disabled="disabled"';
        }
        $fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
        $fieldId = 'task_tableGarbageCollection_numberOfDays';
        $fieldHtml = '<input class="form-control" type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . (int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] . '" ' . 'size="4">';
174
        $fieldConfiguration = [
175
176
177
178
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.numberOfDays',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
179
        ];
180
181
        return $fieldConfiguration;
    }
182

183
184
185
186
    /**
     * Validate additional fields
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
187
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
188
189
     * @return bool True if validation was ok (or selected class is not relevant), false otherwise
     */
190
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
191
    {
192
193
194
        $validData = $this->validateAllTablesAdditionalField($submittedData);
        $validData &= $this->validateTableAdditionalField($submittedData);
        $validData &= $this->validateNumberOfDaysAdditionalField($submittedData);
195
196
        return $validData;
    }
197

198
199
200
201
202
203
    /**
     * Checks if all table field is correct
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
     * @return bool True if data is valid
     */
204
    public function validateAllTablesAdditionalField(array &$submittedData)
205
206
207
208
209
210
211
212
213
    {
        $validData = false;
        if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
            $validData = true;
        } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
            $validData = true;
        }
        return $validData;
    }
214

215
216
217
218
219
220
    /**
     * Checks given table for existence in configuration array
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
     * @return bool True if table exists in configuration, false otherwise
     */
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
226
227
228
229
230
231
        if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
            $validData = true;
        } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
            $validData = true;
        }
        return $validData;
    }
232

233
234
235
236
237
238
    /**
     * Checks if given number of days is a positive integer
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
     * @return bool True if validation was ok (or selected class is not relevant), false otherwise
     */
239
    public function validateNumberOfDaysAdditionalField(array &$submittedData)
240
241
242
243
244
245
246
247
    {
        $validData = false;
        if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
            $validData = true;
        } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
            $validData = true;
        } else {
            // Issue error message
248
            $this->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), FlashMessage::ERROR);
249
250
251
        }
        return $validData;
    }
252

253
254
255
256
257
258
    /**
     * Save additional field in task
     *
     * @param array $submittedData Contains data submitted by the user
     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
     */
259
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
260
261
262
263
264
    {
        $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on';
        $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
        $task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
    }
265
}