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