[BUGFIX] Do not try to fetch additionalFields for invalid tasks
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / Task / ValidatorTaskAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Linkvalidator\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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Messaging\FlashMessage;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Lang\LanguageService;
21 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
22 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
23 use TYPO3\CMS\Scheduler\Task\AbstractTask;
24
25 /**
26 * This class provides Scheduler Additional Field plugin implementation
27 */
28 class ValidatorTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
29 {
30 /**
31 * Default language file of the extension linkvalidator
32 *
33 * @var string
34 */
35 protected $languageFile = 'LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf';
36
37 /**
38 * Render additional information fields within the scheduler backend.
39 *
40 * @param array $taskInfo Array information of task to return
41 * @param AbstractTask|null $task The task object being edited. Null when adding a task!
42 * @param SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
43 * @return array Additional fields
44 * @see AdditionalFieldProviderInterface->getAdditionalFields($taskInfo, $task, $schedulerModule)
45 */
46 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
47 {
48 $additionalFields = [];
49 if (empty($taskInfo['configuration'])) {
50 if ($schedulerModule->CMD === 'add') {
51 $taskInfo['configuration'] = $taskInfo['linkvalidator']['configuration'];
52 } elseif ($schedulerModule->CMD === 'edit') {
53 $taskInfo['configuration'] = $task->getConfiguration();
54 } else {
55 $taskInfo['configuration'] = $task->getConfiguration();
56 }
57 }
58 if (empty($taskInfo['depth'])) {
59 if ($schedulerModule->CMD === 'add') {
60 $taskInfo['depth'] = $taskInfo['linkvalidator']['depth'];
61 } elseif ($schedulerModule->CMD === 'edit') {
62 $taskInfo['depth'] = $task->getDepth();
63 } else {
64 $taskInfo['depth'] = $task->getDepth();
65 }
66 }
67 if (empty($taskInfo['page'])) {
68 if ($schedulerModule->CMD === 'add') {
69 $taskInfo['page'] = $taskInfo['linkvalidator']['page'];
70 } elseif ($schedulerModule->CMD === 'edit') {
71 $taskInfo['page'] = $task->getPage();
72 } else {
73 $taskInfo['page'] = $task->getPage();
74 }
75 }
76 if (empty($taskInfo['email'])) {
77 if ($schedulerModule->CMD === 'add') {
78 $taskInfo['email'] = $taskInfo['linkvalidator']['email'];
79 } elseif ($schedulerModule->CMD === 'edit') {
80 $taskInfo['email'] = $task->getEmail();
81 } else {
82 $taskInfo['email'] = $task->getEmail();
83 }
84 }
85 if (empty($taskInfo['emailOnBrokenLinkOnly'])) {
86 if ($schedulerModule->CMD === 'add') {
87 $taskInfo['emailOnBrokenLinkOnly'] = $taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] ? $taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] : 1;
88 } elseif ($schedulerModule->CMD === 'edit') {
89 $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
90 } else {
91 $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
92 }
93 }
94 if (empty($taskInfo['emailTemplateFile'])) {
95 if ($schedulerModule->CMD === 'add') {
96 $taskInfo['emailTemplateFile'] = $taskInfo['linkvalidator']['emailTemplateFile'] ? $taskInfo['linkvalidator']['emailTemplateFile'] : 'EXT:linkvalidator/Resources/Private/Templates/mailtemplate.html';
97 } elseif ($schedulerModule->CMD === 'edit') {
98 $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
99 } else {
100 $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
101 }
102 }
103 $fieldId = 'task_page';
104 $fieldCode = '<input type="number" min="0" class="form-control" name="tx_scheduler[linkvalidator][page]" id="'
105 . $fieldId
106 . '" value="'
107 . htmlspecialchars($taskInfo['page'])
108 . '">';
109 $lang = $this->getLanguageService();
110 $label = $lang->sL($this->languageFile . ':tasks.validate.page');
111 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
112 $pageTitle = '';
113 if (!empty($taskInfo['page'])) {
114 $pageTitle = $this->getPageTitle((int)$taskInfo['page']);
115 }
116 $additionalFields[$fieldId] = [
117 'browser' => 'page',
118 'pageTitle' => $pageTitle,
119 'code' => $fieldCode,
120 'label' => $label
121 ];
122 // input for depth
123 $fieldId = 'task_depth';
124 $fieldValueArray = [
125 '0' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
126 '1' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
127 '2' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
128 '3' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
129 '4' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
130 '999' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi')
131 ];
132 $fieldCode = '<select class="form-control" name="tx_scheduler[linkvalidator][depth]" id="' . $fieldId . '">';
133 foreach ($fieldValueArray as $depth => $label) {
134 $fieldCode .= "\t" . '<option value="' . htmlspecialchars($depth) . '"'
135 . (($depth == $taskInfo['depth']) ? ' selected="selected"' : '') . '>'
136 . $label
137 . '</option>';
138 }
139 $fieldCode .= '</select>';
140 $label = $lang->sL($this->languageFile . ':tasks.validate.depth');
141 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
142 $additionalFields[$fieldId] = [
143 'code' => $fieldCode,
144 'label' => $label
145 ];
146 $fieldId = 'task_configuration';
147 $fieldCode = '<textarea class="form-control" name="tx_scheduler[linkvalidator][configuration]" id="'
148 . $fieldId
149 . '" >'
150 . htmlspecialchars($taskInfo['configuration'])
151 . '</textarea>';
152 $label = $lang->sL($this->languageFile . ':tasks.validate.conf');
153 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
154 $additionalFields[$fieldId] = [
155 'code' => $fieldCode,
156 'label' => $label
157 ];
158 $fieldId = 'task_email';
159 $fieldCode = '<textarea class="form-control" rows="5" cols="50" name="tx_scheduler[linkvalidator][email]" id="'
160 . $fieldId
161 . '">'
162 . htmlspecialchars($taskInfo['email'])
163 . '</textarea>';
164 $label = $lang->sL($this->languageFile . ':tasks.validate.email');
165 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
166 $additionalFields[$fieldId] = [
167 'code' => $fieldCode,
168 'label' => $label
169 ];
170 $fieldId = 'task_emailOnBrokenLinkOnly';
171 $fieldCode = '<div class="checkbox"><label>'
172 . '<input type="checkbox" name="tx_scheduler[linkvalidator][emailOnBrokenLinkOnly]" id="' . $fieldId . '" '
173 . (htmlspecialchars($taskInfo['emailOnBrokenLinkOnly']) ? 'checked="checked"' : '')
174 . '></label></div>';
175 $label = $lang->sL($this->languageFile . ':tasks.validate.emailOnBrokenLinkOnly');
176 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
177 $additionalFields[$fieldId] = [
178 'code' => $fieldCode,
179 'label' => $label
180 ];
181 $fieldId = 'task_emailTemplateFile';
182 $fieldCode = '<input class="form-control" type="text" name="tx_scheduler[linkvalidator][emailTemplateFile]" '
183 . 'id="'
184 . $fieldId
185 . '" value="'
186 . htmlspecialchars($taskInfo['emailTemplateFile'])
187 . '">';
188 $label = $lang->sL($this->languageFile . ':tasks.validate.emailTemplateFile');
189 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
190 $additionalFields[$fieldId] = [
191 'code' => $fieldCode,
192 'label' => $label
193 ];
194 return $additionalFields;
195 }
196
197 /**
198 * Mark current value as selected by returning the "selected" attribute
199 *
200 * @param array $configurationArray Array of configuration
201 * @param string $currentValue Value of selector object
202 * @return string Html fragment for a selected option or empty
203 */
204 protected function getSelectedState(array $configurationArray, $currentValue)
205 {
206 $selected = '';
207 if (in_array($currentValue, $configurationArray, true)) {
208 $selected = 'selected="selected" ';
209 }
210 return $selected;
211 }
212
213 /**
214 * This method checks any additional data that is relevant to the specific task.
215 * If the task class is not relevant, the method is expected to return TRUE.
216 *
217 * @param array $submittedData Reference to the array containing the data submitted by the user
218 * @param SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
219 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
220 */
221 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
222 {
223 $isValid = true;
224 // @todo add validation to validate the $submittedData['configuration']
225 // @todo which is normally a comma separated string
226 $lang = $this->getLanguageService();
227 if (!empty($submittedData['linkvalidator']['email'])) {
228 if (strpos($submittedData['linkvalidator']['email'], ',') !== false) {
229 $emailList = GeneralUtility::trimExplode(',', $submittedData['linkvalidator']['email']);
230 } else {
231 $emailList = GeneralUtility::trimExplode(LF, $submittedData['linkvalidator']['email']);
232 }
233 foreach ($emailList as $emailAdd) {
234 if (!GeneralUtility::validEmail($emailAdd)) {
235 $isValid = false;
236 $schedulerModule->addMessage(
237 $lang->sL($this->languageFile . ':tasks.validate.invalidEmail'),
238 FlashMessage::ERROR
239 );
240 }
241 }
242 }
243
244 $row = BackendUtility::getRecord('pages', (int)$submittedData['linkvalidator']['page'], '*', '', false);
245 if (empty($row)) {
246 $isValid = false;
247 $schedulerModule->addMessage(
248 $lang->sL($this->languageFile . ':tasks.validate.invalidPage'),
249 FlashMessage::ERROR
250 );
251 }
252 if ($submittedData['linkvalidator']['depth'] < 0) {
253 $isValid = false;
254 $schedulerModule->addMessage(
255 $lang->sL($this->languageFile . ':tasks.validate.invalidDepth'),
256 FlashMessage::ERROR
257 );
258 }
259 return $isValid;
260 }
261
262 /**
263 * This method is used to save any additional input into the current task object
264 * if the task class matches.
265 *
266 * @param array $submittedData Array containing the data submitted by the user
267 * @param AbstractTask $task Reference to the current task object
268 */
269 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
270 {
271 /** @var $task ValidatorTask */
272 $task->setDepth($submittedData['linkvalidator']['depth']);
273 $task->setPage($submittedData['linkvalidator']['page']);
274 $task->setEmail($submittedData['linkvalidator']['email']);
275 if ($submittedData['linkvalidator']['emailOnBrokenLinkOnly']) {
276 $task->setEmailOnBrokenLinkOnly(1);
277 } else {
278 $task->setEmailOnBrokenLinkOnly(0);
279 }
280 $task->setConfiguration($submittedData['linkvalidator']['configuration']);
281 $task->setEmailTemplateFile($submittedData['linkvalidator']['emailTemplateFile']);
282 }
283
284 /**
285 * Get the title of the selected page
286 *
287 * @param int $pageId
288 * @return string Page title or empty string
289 */
290 private function getPageTitle($pageId)
291 {
292 $page = BackendUtility::getRecord('pages', $pageId, 'title', '', false);
293 if ($page === null) {
294 return '';
295 }
296 return $page['title'];
297 }
298
299 /**
300 * @return LanguageService
301 */
302 protected function getLanguageService()
303 {
304 return $GLOBALS['LANG'];
305 }
306 }