f1cc415dd51180605c46c362c08abec5f377c643
[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
19 /**
20 * This class provides Scheduler Additional Field plugin implementation
21 *
22 * @author Dimitri K├Ânig <dk@cabag.ch>
23 * @author Michael Miousse <michael.miousse@infoglobe.ca>
24 */
25 class ValidatorTaskAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
26
27 /**
28 * Render additional information fields within the scheduler backend.
29 *
30 * @param array $taskInfo Array information of task to return
31 * @param ValidatorTask $task Task object
32 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
33 * @return array Additional fields
34 * @see \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface->getAdditionalFields($taskInfo, $task, $schedulerModule)
35 */
36 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
37 $additionalFields = array();
38 if (empty($taskInfo['configuration'])) {
39 if ($schedulerModule->CMD == 'add') {
40 $taskInfo['configuration'] = '';
41 } elseif ($schedulerModule->CMD == 'edit') {
42 $taskInfo['configuration'] = $task->getConfiguration();
43 } else {
44 $taskInfo['configuration'] = $task->getConfiguration();
45 }
46 }
47 if (empty($taskInfo['depth'])) {
48 if ($schedulerModule->CMD == 'add') {
49 $taskInfo['depth'] = array();
50 } elseif ($schedulerModule->CMD == 'edit') {
51 $taskInfo['depth'] = $task->getDepth();
52 } else {
53 $taskInfo['depth'] = $task->getDepth();
54 }
55 }
56 if (empty($taskInfo['page'])) {
57 if ($schedulerModule->CMD == 'add') {
58 $taskInfo['page'] = '';
59 } elseif ($schedulerModule->CMD == 'edit') {
60 $taskInfo['page'] = $task->getPage();
61 } else {
62 $taskInfo['page'] = $task->getPage();
63 }
64 }
65 if (empty($taskInfo['email'])) {
66 if ($schedulerModule->CMD == 'add') {
67 $taskInfo['email'] = '';
68 } elseif ($schedulerModule->CMD == 'edit') {
69 $taskInfo['email'] = $task->getEmail();
70 } else {
71 $taskInfo['email'] = $task->getEmail();
72 }
73 }
74 if (empty($taskInfo['emailOnBrokenLinkOnly'])) {
75 if ($schedulerModule->CMD == 'add') {
76 $taskInfo['emailOnBrokenLinkOnly'] = 1;
77 } elseif ($schedulerModule->CMD == 'edit') {
78 $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
79 } else {
80 $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
81 }
82 }
83 if (empty($taskInfo['emailTemplateFile'])) {
84 if ($schedulerModule->CMD == 'add') {
85 $taskInfo['emailTemplateFile'] = 'EXT:linkvalidator/Resources/Private/Templates/mailtemplate.html';
86 } elseif ($schedulerModule->CMD == 'edit') {
87 $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
88 } else {
89 $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
90 }
91 }
92 $fieldId = 'task_page';
93 $fieldCode = '<input type="text" name="tx_scheduler[linkvalidator][page]" id="' . $fieldId . '" value="' . htmlspecialchars($taskInfo['page']) . '"/>';
94 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.page');
95 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
96 $additionalFields[$fieldId] = array(
97 'code' => $fieldCode,
98 'label' => $label
99 );
100 // input for depth
101 $fieldId = 'task_depth';
102 $fieldValueArray = array(
103 '0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_0'),
104 '1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_1'),
105 '2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_2'),
106 '3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_3'),
107 '4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_4'),
108 '999' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi')
109 );
110 $fieldCode = '<select name="tx_scheduler[linkvalidator][depth]" id="' . $fieldId . '">';
111 foreach ($fieldValueArray as $depth => $label) {
112 $fieldCode .= "\t" . '<option value="' . htmlspecialchars($depth) . '"' .
113 (($depth == $taskInfo['depth']) ? ' selected="selected"' : '') .
114 '>' . $label . '</option>';
115 }
116 $fieldCode .= '</select>';
117 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.depth');
118 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
119 $additionalFields[$fieldId] = array(
120 'code' => $fieldCode,
121 'label' => $label
122 );
123 $fieldId = 'task_configuration';
124 $fieldCode = '<textarea name="tx_scheduler[linkvalidator][configuration]" id="' . $fieldId . '" >' .
125 htmlspecialchars($taskInfo['configuration']) . '</textarea>';
126 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.conf');
127 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
128 $additionalFields[$fieldId] = array(
129 'code' => $fieldCode,
130 'label' => $label
131 );
132 $fieldId = 'task_email';
133 $fieldCode = '<input type="text" name="tx_scheduler[linkvalidator][email]" id="' . $fieldId . '" value="' .
134 htmlspecialchars($taskInfo['email']) . '" />';
135 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.email');
136 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
137 $additionalFields[$fieldId] = array(
138 'code' => $fieldCode,
139 'label' => $label
140 );
141 $fieldId = 'task_emailOnBrokenLinkOnly';
142 $fieldCode = '<input type="checkbox" name="tx_scheduler[linkvalidator][emailOnBrokenLinkOnly]" id="' . $fieldId . '" ' .
143 (htmlspecialchars($taskInfo['emailOnBrokenLinkOnly']) ? 'checked="checked"' : '') . ' />';
144 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.emailOnBrokenLinkOnly');
145 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
146 $additionalFields[$fieldId] = array(
147 'code' => $fieldCode,
148 'label' => $label
149 );
150 $fieldId = 'task_emailTemplateFile';
151 $fieldCode = '<input type="text" name="tx_scheduler[linkvalidator][emailTemplateFile]" id="' . $fieldId .
152 '" value="' . htmlspecialchars($taskInfo['emailTemplateFile']) . '" />';
153 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.emailTemplateFile');
154 $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
155 $additionalFields[$fieldId] = array(
156 'code' => $fieldCode,
157 'label' => $label
158 );
159 return $additionalFields;
160 }
161
162 /**
163 * Mark current value as selected by returning the "selected" attribute
164 *
165 * @param array $configurationArray Array of configuration
166 * @param string $currentValue Value of selector object
167 * @return string Html fragment for a selected option or empty
168 */
169 protected function getSelectedState(array $configurationArray, $currentValue) {
170 $selected = '';
171 if (in_array($currentValue, $configurationArray, TRUE)) {
172 $selected = 'selected="selected" ';
173 }
174 return $selected;
175 }
176
177 /**
178 * This method checks any additional data that is relevant to the specific task.
179 * If the task class is not relevant, the method is expected to return TRUE.
180 *
181 * @param array $submittedData Reference to the array containing the data submitted by the user
182 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
183 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
184 */
185 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
186 $isValid = TRUE;
187 //TODO add validation to validate the $submittedData['configuration'] which is normally a comma separated string
188 if (!empty($submittedData['linkvalidator']['email'])) {
189 $emailList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $submittedData['linkvalidator']['email']);
190 foreach ($emailList as $emailAdd) {
191 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($emailAdd)) {
192 $isValid = FALSE;
193 $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidEmail'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
194 }
195 }
196 }
197 if ($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid = ' . (int)$submittedData['linkvalidator']['page'])) {
198 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) == 0 && $submittedData['linkvalidator']['page'] > 0) {
199 $isValid = FALSE;
200 $schedulerModule->addMessage(
201 $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidPage'),
202 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
203 );
204 }
205 $GLOBALS['TYPO3_DB']->sql_free_result($res);
206 } else {
207 $isValid = FALSE;
208 $schedulerModule->addMessage(
209 $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidPage'),
210 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
211 );
212 }
213 if ($submittedData['linkvalidator']['depth'] < 0) {
214 $isValid = FALSE;
215 $schedulerModule->addMessage(
216 $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidDepth'),
217 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
218 );
219 }
220 return $isValid;
221 }
222
223 /**
224 * This method is used to save any additional input into the current task object
225 * if the task class matches.
226 *
227 * @param array $submittedData Array containing the data submitted by the user
228 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
229 * @return void
230 */
231 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
232 /** @var $task ValidatorTask */
233 $task->setDepth($submittedData['linkvalidator']['depth']);
234 $task->setPage($submittedData['linkvalidator']['page']);
235 $task->setEmail($submittedData['linkvalidator']['email']);
236 if ($submittedData['linkvalidator']['emailOnBrokenLinkOnly']) {
237 $task->setEmailOnBrokenLinkOnly(1);
238 } else {
239 $task->setEmailOnBrokenLinkOnly(0);
240 }
241 $task->setConfiguration($submittedData['linkvalidator']['configuration']);
242 $task->setEmailTemplateFile($submittedData['linkvalidator']['emailTemplateFile']);
243 }
244
245 }