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