TableGarbageCollectionAdditionalFieldProvider.php 13.1 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * 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.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Scheduler\Task;

18
use TYPO3\CMS\Core\Localization\LanguageService;
19
20
21
22
23
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;

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

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

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

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

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

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

94
95
        return $fieldConfiguration;
    }
96

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

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

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

200
201
202
203
204
205
    /**
     * 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
     */
206
    public function validateAllTablesAdditionalField(array &$submittedData)
207
208
209
210
211
212
213
214
215
    {
        $validData = false;
        if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
            $validData = true;
        } elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
            $validData = true;
        }
        return $validData;
    }
216

217
218
219
220
221
222
    /**
     * 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
     */
223
    public function validateTableAdditionalField(array &$submittedData)
224
225
    {
        $validData = false;
226
        $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
227
228
229
230
231
232
233
        if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
            $validData = true;
        } elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
            $validData = true;
        }
        return $validData;
    }
234

235
236
237
238
239
240
    /**
     * 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
     */
241
    public function validateNumberOfDaysAdditionalField(array &$submittedData)
242
243
244
245
246
247
248
249
    {
        $validData = false;
        if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
            $validData = true;
        } elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
            $validData = true;
        } else {
            // Issue error message
250
            $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), FlashMessage::ERROR);
251
252
253
        }
        return $validData;
    }
254

255
256
257
258
    /**
     * Save additional field in task
     *
     * @param array $submittedData Contains data submitted by the user
259
     * @param TableGarbageCollectionTask $task Reference to the current task object
260
     */
261
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
262
263
264
265
266
    {
        $task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on';
        $task->table = $submittedData['scheduler_tableGarbageCollection_table'];
        $task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
    }
267
268
269
270
271
272
273
274

    /**
     * @return LanguageService|null
     */
    protected function getLanguageService(): ?LanguageService
    {
        return $GLOBALS['LANG'] ?? null;
    }
275
}