[TASK] Refactor code to match new structure
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / Task / ValidatorTaskAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Linkvalidator\Task;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 - 2013 Michael Miousse (michael.miousse@infoglobe.ca)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * This class provides Scheduler Additional Field plugin implementation
29 *
30 * @author Dimitri K├Ânig <dk@cabag.ch>
31 * @author Michael Miousse <michael.miousse@infoglobe.ca>
32 */
33 class ValidatorTaskAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
34
35 /**
36 * Render additional information fields within the scheduler backend.
37 *
38 * @param array $taskInfo Array information of task to return
39 * @param ValidatorTask $task Task object
40 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
41 * @return array Additional fields
42 * @see \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface->getAdditionalFields($taskInfo, $task, $schedulerModule)
43 */
44 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
45 $additionalFields = array();
46 if (empty($taskInfo['configuration'])) {
47 if ($schedulerModule->CMD == 'add') {
48 $taskInfo['configuration'] = '';
49 } elseif ($schedulerModule->CMD == 'edit') {
50 $taskInfo['configuration'] = $task->getConfiguration();
51 } else {
52 $taskInfo['configuration'] = $task->getConfiguration();
53 }
54 }
55 if (empty($taskInfo['depth'])) {
56 if ($schedulerModule->CMD == 'add') {
57 $taskInfo['depth'] = array();
58 } elseif ($schedulerModule->CMD == 'edit') {
59 $taskInfo['depth'] = $task->getDepth();
60 } else {
61 $taskInfo['depth'] = $task->getDepth();
62 }
63 }
64 if (empty($taskInfo['page'])) {
65 if ($schedulerModule->CMD == 'add') {
66 $taskInfo['page'] = '';
67 } elseif ($schedulerModule->CMD == 'edit') {
68 $taskInfo['page'] = $task->getPage();
69 } else {
70 $taskInfo['page'] = $task->getPage();
71 }
72 }
73 if (empty($taskInfo['email'])) {
74 if ($schedulerModule->CMD == 'add') {
75 $taskInfo['email'] = '';
76 } elseif ($schedulerModule->CMD == 'edit') {
77 $taskInfo['email'] = $task->getEmail();
78 } else {
79 $taskInfo['email'] = $task->getEmail();
80 }
81 }
82 if (empty($taskInfo['emailOnBrokenLinkOnly'])) {
83 if ($schedulerModule->CMD == 'add') {
84 $taskInfo['emailOnBrokenLinkOnly'] = 1;
85 } elseif ($schedulerModule->CMD == 'edit') {
86 $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
87 } else {
88 $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
89 }
90 }
91 if (empty($taskInfo['emailTemplateFile'])) {
92 if ($schedulerModule->CMD == 'add') {
93 $taskInfo['emailTemplateFile'] = 'EXT:linkvalidator/Resources/Private/Templates/mailtemplate.html';
94 } elseif ($schedulerModule->CMD == 'edit') {
95 $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
96 } else {
97 $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
98 }
99 }
100 $fieldId = 'task_page';
101 $fieldCode = '<input type="text" name="tx_scheduler[linkvalidator][page]" id="' . $fieldId . '" value="' . htmlspecialchars($taskInfo['page']) . '"/>';
102 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.page');
103 $label = \TYPO3\CMS\Backend\Utility\BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
104 $additionalFields[$fieldId] = array(
105 'code' => $fieldCode,
106 'label' => $label
107 );
108 // input for depth
109 $fieldId = 'task_depth';
110 $fieldValueArray = array(
111 '0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_0'),
112 '1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_1'),
113 '2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_2'),
114 '3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_3'),
115 '4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_4'),
116 '999' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi')
117 );
118 $fieldCode = '<select name="tx_scheduler[linkvalidator][depth]" id="' . $fieldId . '">';
119 foreach ($fieldValueArray as $depth => $label) {
120 $fieldCode .= "\t" . '<option value="' . htmlspecialchars($depth) . '"' .
121 (($depth == $taskInfo['depth']) ? ' selected="selected"' : '') .
122 '>' . $label . '</option>';
123 }
124 $fieldCode .= '</select>';
125 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.depth');
126 $label = \TYPO3\CMS\Backend\Utility\BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
127 $additionalFields[$fieldId] = array(
128 'code' => $fieldCode,
129 'label' => $label
130 );
131 $fieldId = 'task_configuration';
132 $fieldCode = '<textarea name="tx_scheduler[linkvalidator][configuration]" id="' . $fieldId . '" >' .
133 htmlspecialchars($taskInfo['configuration']) . '</textarea>';
134 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.conf');
135 $label = \TYPO3\CMS\Backend\Utility\BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
136 $additionalFields[$fieldId] = array(
137 'code' => $fieldCode,
138 'label' => $label
139 );
140 $fieldId = 'task_email';
141 $fieldCode = '<input type="text" name="tx_scheduler[linkvalidator][email]" id="' . $fieldId . '" value="' .
142 htmlspecialchars($taskInfo['email']) . '" />';
143 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.email');
144 $label = \TYPO3\CMS\Backend\Utility\BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
145 $additionalFields[$fieldId] = array(
146 'code' => $fieldCode,
147 'label' => $label
148 );
149 $fieldId = 'task_emailOnBrokenLinkOnly';
150 $fieldCode = '<input type="checkbox" name="tx_scheduler[linkvalidator][emailOnBrokenLinkOnly]" id="' . $fieldId . '" ' .
151 (htmlspecialchars($taskInfo['emailOnBrokenLinkOnly']) ? 'checked="checked"' : '') . ' />';
152 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.emailOnBrokenLinkOnly');
153 $label = \TYPO3\CMS\Backend\Utility\BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
154 $additionalFields[$fieldId] = array(
155 'code' => $fieldCode,
156 'label' => $label
157 );
158 $fieldId = 'task_emailTemplateFile';
159 $fieldCode = '<input type="text" name="tx_scheduler[linkvalidator][emailTemplateFile]" id="' . $fieldId .
160 '" value="' . htmlspecialchars($taskInfo['emailTemplateFile']) . '" />';
161 $label = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.emailTemplateFile');
162 $label = \TYPO3\CMS\Backend\Utility\BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
163 $additionalFields[$fieldId] = array(
164 'code' => $fieldCode,
165 'label' => $label
166 );
167 return $additionalFields;
168 }
169
170 /**
171 * Mark current value as selected by returning the "selected" attribute
172 *
173 * @param array $configurationArray Array of configuration
174 * @param string $currentValue Value of selector object
175 * @return string Html fragment for a selected option or empty
176 */
177 protected function getSelectedState(array $configurationArray, $currentValue) {
178 $selected = '';
179 if (in_array($currentValue, $configurationArray, TRUE)) {
180 $selected = 'selected="selected" ';
181 }
182 return $selected;
183 }
184
185 /**
186 * This method checks any additional data that is relevant to the specific task.
187 * If the task class is not relevant, the method is expected to return TRUE.
188 *
189 * @param array $submittedData Reference to the array containing the data submitted by the user
190 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
191 * @return boolean TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
192 */
193 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
194 $isValid = TRUE;
195 //TODO add validation to validate the $submittedData['configuration'] which is normally a comma separated string
196 if (!empty($submittedData['linkvalidator']['email'])) {
197 $emailList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $submittedData['linkvalidator']['email']);
198 foreach ($emailList as $emailAdd) {
199 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($emailAdd)) {
200 $isValid = FALSE;
201 $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidEmail'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
202 }
203 }
204 }
205 if ($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid = ' . intval($submittedData['linkvalidator']['page']))) {
206 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) == 0 && $submittedData['linkvalidator']['page'] > 0) {
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 $GLOBALS['TYPO3_DB']->sql_free_result($res);
214 } else {
215 $isValid = FALSE;
216 $schedulerModule->addMessage(
217 $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidPage'),
218 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
219 );
220 }
221 if ($submittedData['linkvalidator']['depth'] < 0) {
222 $isValid = FALSE;
223 $schedulerModule->addMessage(
224 $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.validate.invalidDepth'),
225 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
226 );
227 }
228 return $isValid;
229 }
230
231 /**
232 * This method is used to save any additional input into the current task object
233 * if the task class matches.
234 *
235 * @param array $submittedData Array containing the data submitted by the user
236 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
237 * @return void
238 */
239 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
240 /** @var $task ValidatorTask */
241 $task->setDepth($submittedData['linkvalidator']['depth']);
242 $task->setPage($submittedData['linkvalidator']['page']);
243 $task->setEmail($submittedData['linkvalidator']['email']);
244 if ($submittedData['linkvalidator']['emailOnBrokenLinkOnly']) {
245 $task->setEmailOnBrokenLinkOnly(1);
246 } else {
247 $task->setEmailOnBrokenLinkOnly(0);
248 }
249 $task->setConfiguration($submittedData['linkvalidator']['configuration']);
250 $task->setEmailTemplateFile($submittedData['linkvalidator']['emailTemplateFile']);
251 }
252
253 }
254 ?>