[TASK] Use ServerRequestInterface in SchedulerModuleController
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Task / CleanerFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Task;
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
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
19 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
20 use TYPO3\CMS\Scheduler\Task\AbstractTask;
21 use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
22
23 /**
24 * A task that should be run regularly that deletes
25 * datasets flagged as "deleted" from the DB.
26 */
27 class CleanerFieldProvider extends AbstractAdditionalFieldProvider
28 {
29 /**
30 * Gets additional fields to render in the form to add/edit a task
31 *
32 * @param array $taskInfo Values of the fields from the add/edit task form
33 * @param \TYPO3\CMS\Recycler\Task\CleanerTask $task The task object being edited. NULL when adding a task!
34 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
35 * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
36 */
37 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
38 {
39 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
40
41 if ($currentSchedulerModuleAction->equals(Action::EDIT)) {
42 $taskInfo['RecyclerCleanerTCA'] = $task->getTcaTables();
43 $taskInfo['RecyclerCleanerPeriod'] = $task->getPeriod();
44 }
45
46 $additionalFields['period'] = [
47 'code' => '<input type="text" class="form-control" name="tx_scheduler[RecyclerCleanerPeriod]" value="' . $taskInfo['RecyclerCleanerPeriod'] . '">',
48 'label' => 'LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskPeriod',
49 'cshKey' => '',
50 'cshLabel' => 'task_recyclerCleaner_selectedPeriod'
51 ];
52
53 $additionalFields['tca'] = [
54 'code' => $this->getTcaSelectHtml($taskInfo['RecyclerCleanerTCA']),
55 'label' => 'LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskTCA',
56 'cshKey' => '',
57 'cshLabel' => 'task_recyclerCleaner_selectedTables'
58 ];
59
60 return $additionalFields;
61 }
62
63 /**
64 * Gets the select-box from the TCA-fields
65 *
66 * @param array $selectedTables
67 * @return string
68 */
69 protected function getTcaSelectHtml($selectedTables = [])
70 {
71 if (!is_array($selectedTables)) {
72 $selectedTables = [];
73 }
74 $tcaSelectHtml = '<select name="tx_scheduler[RecyclerCleanerTCA][]" multiple="multiple" class="form-control" size="10">';
75
76 $options = [];
77 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
78 if (!empty($tableConf['ctrl']['delete'])) {
79 $selected = in_array($table, $selectedTables, true) ? ' selected="selected"' : '';
80 $tableTitle = $this->getLanguageService()->sL($tableConf['ctrl']['title']);
81 $options[$tableTitle . ' ' . $table] = '<option' . $selected . ' value="' . $table . '">' . htmlspecialchars($tableTitle . ' (' . $table . ')') . '</option>';
82 }
83 }
84 ksort($options);
85
86 $tcaSelectHtml .= implode('', $options);
87 $tcaSelectHtml .= '</select>';
88
89 return $tcaSelectHtml;
90 }
91
92 /**
93 * Validates the additional fields' values
94 *
95 * @param array $submittedData An array containing the data submitted by the add/edit task form
96 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
97 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
98 */
99 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
100 {
101 $validPeriod = $this->validateAdditionalFieldPeriod($submittedData['RecyclerCleanerPeriod']);
102 $validTca = $this->validateAdditionalFieldTca($submittedData['RecyclerCleanerTCA']);
103
104 return $validPeriod && $validTca;
105 }
106
107 /**
108 * Validates the selected Tables
109 *
110 * @param array $tca The given TCA-tables as array
111 * @return bool TRUE if validation was ok, FALSE otherwise
112 */
113 protected function validateAdditionalFieldTca($tca)
114 {
115 return $this->checkTcaIsNotEmpty($tca) && $this->checkTcaIsValid($tca);
116 }
117
118 /**
119 * Checks if the array is empty
120 *
121 * @param array $tca The given TCA-tables as array
122 * @return bool TRUE if validation was ok, FALSE otherwise
123 */
124 protected function checkTcaIsNotEmpty($tca)
125 {
126 if (is_array($tca) && !empty($tca)) {
127 $validTca = true;
128 } else {
129 $this->addMessage(
130 $this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskErrorTCAempty'),
131 FlashMessage::ERROR
132 );
133 $validTca = false;
134 }
135
136 return $validTca;
137 }
138
139 /**
140 * Checks if the given tables are in the TCA
141 *
142 * @param array $tca The given TCA-tables as array
143 * @return bool TRUE if validation was ok, FALSE otherwise
144 */
145 protected function checkTcaIsValid(array $tca)
146 {
147 $checkTca = false;
148 foreach ($tca as $tcaTable) {
149 if (!isset($GLOBALS['TCA'][$tcaTable])) {
150 $checkTca = false;
151 $this->addMessage(
152 sprintf($this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskErrorTCANotSet'), $tcaTable),
153 FlashMessage::ERROR
154 );
155 break;
156 }
157 $checkTca = true;
158 }
159
160 return $checkTca;
161 }
162
163 /**
164 * Validates the input of period
165 *
166 * @param int $period The given period as integer
167 * @return bool TRUE if validation was ok, FALSE otherwise
168 */
169 protected function validateAdditionalFieldPeriod($period)
170 {
171 if (!empty($period) && filter_var($period, FILTER_VALIDATE_INT) !== false) {
172 $validPeriod = true;
173 } else {
174 $this->addMessage(
175 $this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskErrorPeriod'),
176 FlashMessage::ERROR
177 );
178 $validPeriod = false;
179 }
180
181 return $validPeriod;
182 }
183
184 /**
185 * Takes care of saving the additional fields' values in the task's object
186 *
187 * @param array $submittedData An array containing the data submitted by the add/edit task form
188 * @param AbstractTask $task Reference to the scheduler backend module
189 * @throws \InvalidArgumentException
190 */
191 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
192 {
193 if (!$task instanceof CleanerTask) {
194 throw new \InvalidArgumentException(
195 'Expected a task of type ' . CleanerTask::class . ', but got ' . get_class($task),
196 1329219449
197 );
198 }
199
200 $task->setTcaTables($submittedData['RecyclerCleanerTCA']);
201 $task->setPeriod($submittedData['RecyclerCleanerPeriod']);
202 }
203
204 /**
205 * @return \TYPO3\CMS\Core\Localization\LanguageService
206 */
207 protected function getLanguageService()
208 {
209 return $GLOBALS['LANG'];
210 }
211 }