IpAnonymizationAdditionalFieldProvider.php 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

/*
 * This file is part of the TYPO3 CMS project.
 *
 * 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.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

16
17
namespace TYPO3\CMS\Scheduler\Task;

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

/**
 * Additional BE fields for ip address anonymization task.
26
 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
27
 */
28
class IpAnonymizationAdditionalFieldProvider extends AbstractAdditionalFieldProvider
29
30
31
32
33
{
    /**
     * Add additional fields
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
34
     * @param IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
35
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
36
37
     * @return array Array containing all the information pertaining to the additional fields
     */
38
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
39
40
    {
        $additionalFields = [];
41
42
43
        $additionalFields['task_ipAnonymization_table'] = $this->getTableAdditionalField($taskInfo, $task, $schedulerModule);
        $additionalFields['task_ipAnonymization_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $schedulerModule);
        $additionalFields['task_ipAnonymization_mask'] = $this->getMaskAdditionalField($taskInfo, $task, $schedulerModule);
44
45
46
47
48
49
50
        return $additionalFields;
    }

    /**
     * Add a select field of available tables.
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
51
     * @param IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
52
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
53
54
     * @return array Array containing all the information pertaining to the additional fields
     */
55
    protected function getTableAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
56
    {
57
58
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
        $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][IpAnonymizationTask::class]['options']['tables'];
59
60
61
        $options = [];
        // Add an empty option on top if an existing task is configured
        // with a table that can not be found in configuration anymore
62
        if (!array_key_exists($task->table, $tableConfiguration) && $currentSchedulerModuleAction->equals(Action::EDIT)) {
63
64
65
            $options[] = '<option value="" selected="selected"></option>';
        }
        foreach ($tableConfiguration as $tableName => $configuration) {
66
            if ($currentSchedulerModuleAction->equals(Action::ADD) && empty($options)) {
67
68
69
70
71
72
73
74
75
76
77
78
79
                // 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>';
            }
        }
        $fieldName = 'tx_scheduler[scheduler_ipAnonymization_table]';
        $fieldId = 'task_ipAnonymization_table';
        $fieldHtml = [];
        // Add table drop down html
80
        $fieldHtml[] = '<select class="form-select" name="' . $fieldName . '" id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
81
82
83
84
85
86
87
88
89
90
91
92
93
        $fieldConfiguration = [
            'code' => implode(LF, $fieldHtml),
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.table',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
        ];
        return $fieldConfiguration;
    }

    /**
     * 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
94
     * @param IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
95
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
96
97
     * @return array Array containing all the information pertaining to the additional fields
     */
98
    protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
99
100
101
    {
        $fieldId = 'scheduler_ipAnonymization_numberOfDays';
        if (empty($taskInfo[$fieldId])) {
102
            $taskInfo[$fieldId] = $task->numberOfDays ?? 180;
103
104
        }
        $fieldName = 'tx_scheduler[' . $fieldId . ']';
105
        $fieldHtml = '<input class="form-control" type="text" name="' . $fieldName . '" id="' . $fieldId . '" value="' . (int)$taskInfo[$fieldId] . '" size="4">';
106
107
108
109
110
111
112
113
114
115
116
117
118
        $fieldConfiguration = [
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.numberOfDays',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
        ];
        return $fieldConfiguration;
    }

    /**
     * Add an input field to get the mask.
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
119
     * @param IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
120
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
121
122
     * @return array Array containing all the information pertaining to the additional fields
     */
123
    protected function getMaskAdditionalField(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
124
125
126
    {
        $fieldId = 'scheduler_ipAnonymization_mask';
        if (empty($taskInfo[$fieldId])) {
127
            $taskInfo[$fieldId] = $task->mask ?? 2;
128
129
130
131
132
133
134
135
136
137
138
        }
        $fieldName = 'tx_scheduler[' . $fieldId . ']';

        $fieldHtml = '';
        foreach ([1, 2] as $mask) {
            $selected = (int)$taskInfo[$fieldId] === $mask ? ' selected' : '';
            $fieldHtml .= '<option value="' . $mask . '"' . $selected . '>'
                . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.mask.' . $mask))
                . '</option>';
        }

139
        $fieldHtml = '<select class="form-select" name="' . $fieldName . '" id="' . $fieldId . '">' . $fieldHtml . '</select>';
140
141
142
143
144
145
146
147
148
149
150
151
152
153

        $fieldConfiguration = [
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.mask',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
        ];
        return $fieldConfiguration;
    }

    /**
     * Validate additional fields
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
154
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
155
156
     * @return bool True if validation was ok (or selected class is not relevant), false otherwise
     */
157
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
158
    {
159
160
        $validData = $this->validateTableAdditionalField($submittedData, $schedulerModule);
        $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $schedulerModule);
161
162
163
164
165
166
167
        return $validData;
    }

    /**
     * Checks given table for existence in configuration array
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
168
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
169
170
     * @return bool True if table exists in configuration, false otherwise
     */
171
    public function validateTableAdditionalField(array &$submittedData, SchedulerModuleController $schedulerModule)
172
173
    {
        $validData = false;
174
        $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][IpAnonymizationTask::class]['options']['tables'];
175
176
177
178
179
180
181
182
183
184
185
186
        if (!isset($submittedData['scheduler_ipAnonymization_table'])) {
            $validData = true;
        } elseif (array_key_exists($submittedData['scheduler_ipAnonymization_table'], $tableConfiguration)) {
            $validData = true;
        }
        return $validData;
    }

    /**
     * Checks if given number of days is a positive integer
     *
     * @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 validateNumberOfDaysAdditionalField(array &$submittedData, SchedulerModuleController $schedulerModule)
191
192
193
194
195
196
197
198
    {
        $validData = false;
        if (!isset($submittedData['scheduler_ipAnonymization_numberOfDays'])) {
            $validData = true;
        } elseif ((int)$submittedData['scheduler_ipAnonymization_numberOfDays'] >= 0) {
            $validData = true;
        } else {
            // Issue error message
199
            $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), FlashMessage::ERROR);
200
201
202
203
204
205
206
207
        }
        return $validData;
    }

    /**
     * Save additional field in task
     *
     * @param array $submittedData Contains data submitted by the user
208
     * @param IpAnonymizationTask $task Reference to the current task object
209
     */
210
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
211
212
    {
        $task->table = $submittedData['scheduler_ipAnonymization_table'];
213
        $task->mask = (int)$submittedData['scheduler_ipAnonymization_mask'];
214
215
216
217
218
219
220
221
222
223
224
225
226
        $task->numberOfDays = (int)$submittedData['scheduler_ipAnonymization_numberOfDays'];
    }

    /**
     * Returns an instance of LanguageService
     *
     * @return LanguageService
     */
    protected function getLanguageService(): LanguageService
    {
        return $GLOBALS['LANG'];
    }
}