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