‪TYPO3CMS  9.5
ValidatorTaskAdditionalFieldProvider.php
Go to the documentation of this file.
1 <?php
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 
25 
31 {
37  protected ‪$languageFile = 'LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf';
38 
48  public function ‪getAdditionalFields(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule)
49  {
50  $additionalFields = [];
51  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
52 
53  if (empty($taskInfo['configuration'])) {
54  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
55  $taskInfo['configuration'] = $taskInfo['linkvalidator']['configuration'];
56  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
57  $taskInfo['configuration'] = $task->getConfiguration();
58  } else {
59  $taskInfo['configuration'] = $task->getConfiguration();
60  }
61  }
62 
63  if (empty($taskInfo['depth'])) {
64  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
65  $taskInfo['depth'] = $taskInfo['linkvalidator']['depth'];
66  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
67  $taskInfo['depth'] = $task->getDepth();
68  } else {
69  $taskInfo['depth'] = $task->getDepth();
70  }
71  }
72 
73  if (empty($taskInfo['page'])) {
74  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
75  $taskInfo['page'] = $taskInfo['linkvalidator']['page'];
76  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
77  $taskInfo['page'] = $task->getPage();
78  } else {
79  $taskInfo['page'] = $task->getPage();
80  }
81  }
82  if (empty($taskInfo['email'])) {
83  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
84  $taskInfo['email'] = $taskInfo['linkvalidator']['email'];
85  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
86  $taskInfo['email'] = $task->getEmail();
87  } else {
88  $taskInfo['email'] = $task->getEmail();
89  }
90  }
91  if (empty($taskInfo['emailOnBrokenLinkOnly'])) {
92  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
93  $taskInfo['emailOnBrokenLinkOnly'] = $taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] ? $taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] : 1;
94  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
95  $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
96  } else {
97  $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
98  }
99  }
100  if (empty($taskInfo['emailTemplateFile'])) {
101  if ($currentSchedulerModuleAction->equals(‪Action::ADD)) {
102  $taskInfo['emailTemplateFile'] = $taskInfo['linkvalidator']['emailTemplateFile'] ? $taskInfo['linkvalidator']['emailTemplateFile'] : 'EXT:linkvalidator/Resources/Private/Templates/mailtemplate.html';
103  } elseif ($currentSchedulerModuleAction->equals(‪Action::EDIT)) {
104  $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
105  } else {
106  $taskInfo['emailTemplateFile'] = $task->getEmailTemplateFile();
107  }
108  }
109  $fieldId = 'task_page';
110  $fieldCode = '<input type="number" min="0" class="form-control" name="tx_scheduler[linkvalidator][page]" id="'
111  . $fieldId
112  . '" value="'
113  . htmlspecialchars($taskInfo['page'])
114  . '">';
115  $lang = $this->‪getLanguageService();
116  $label = $lang->sL($this->languageFile . ':tasks.validate.page');
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  'cshTable' => 'linkvalidator',
126  'cshLabel' => $fieldId,
127  'label' => $label
128  ];
129  // input for depth
130  $fieldId = 'task_depth';
131  $fieldValueArray = [
132  '0' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
133  '1' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
134  '2' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
135  '3' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
136  '4' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
137  '999' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi')
138  ];
139  $fieldCode = '<select class="form-control" name="tx_scheduler[linkvalidator][depth]" id="' . $fieldId . '">';
140  foreach ($fieldValueArray as $depth => $label) {
141  $fieldCode .= "\t" . '<option value="' . htmlspecialchars($depth) . '"'
142  . (($depth == $taskInfo['depth']) ? ' selected="selected"' : '') . '>'
143  . $label
144  . '</option>';
145  }
146  $fieldCode .= '</select>';
147  $label = $lang->sL($this->languageFile . ':tasks.validate.depth');
148  $additionalFields[$fieldId] = [
149  'code' => $fieldCode,
150  'cshKey' => 'linkvalidator',
151  'cshLabel' => $fieldId,
152  'label' => $label
153  ];
154  $fieldId = 'task_configuration';
155  $fieldCode = '<textarea class="form-control" name="tx_scheduler[linkvalidator][configuration]" id="'
156  . $fieldId
157  . '" >'
158  . htmlspecialchars($taskInfo['configuration'])
159  . '</textarea>';
160  $label = $lang->sL($this->languageFile . ':tasks.validate.conf');
161  $additionalFields[$fieldId] = [
162  'code' => $fieldCode,
163  'cshKey' => 'linkvalidator',
164  'cshLabel' => $fieldId,
165  'label' => $label
166  ];
167  $fieldId = 'task_email';
168  $fieldCode = '<textarea class="form-control" rows="5" cols="50" name="tx_scheduler[linkvalidator][email]" id="'
169  . $fieldId
170  . '">'
171  . htmlspecialchars($taskInfo['email'])
172  . '</textarea>';
173  $label = $lang->sL($this->languageFile . ':tasks.validate.email');
174  $additionalFields[$fieldId] = [
175  'code' => $fieldCode,
176  'cshKey' => 'linkvalidator',
177  'cshLabel' => $fieldId,
178  'label' => $label
179  ];
180  $fieldId = 'task_emailOnBrokenLinkOnly';
181  $fieldCode = '<div class="checkbox"><label>'
182  . '<input type="checkbox" name="tx_scheduler[linkvalidator][emailOnBrokenLinkOnly]" id="' . $fieldId . '" '
183  . (htmlspecialchars($taskInfo['emailOnBrokenLinkOnly']) ? 'checked="checked"' : '')
184  . '></label></div>';
185  $label = $lang->sL($this->languageFile . ':tasks.validate.emailOnBrokenLinkOnly');
186  $additionalFields[$fieldId] = [
187  'code' => $fieldCode,
188  'cshKey' => 'linkvalidator',
189  'cshLabel' => $fieldId,
190  'label' => $label
191  ];
192  $fieldId = 'task_emailTemplateFile';
193  $fieldCode = '<input class="form-control" type="text" name="tx_scheduler[linkvalidator][emailTemplateFile]" '
194  . 'id="'
195  . $fieldId
196  . '" value="'
197  . htmlspecialchars($taskInfo['emailTemplateFile'])
198  . '">';
199  $label = $lang->sL($this->languageFile . ':tasks.validate.emailTemplateFile');
200  $additionalFields[$fieldId] = [
201  'code' => $fieldCode,
202  'cshKey' => 'linkvalidator',
203  'cshLabel' => $fieldId,
204  'label' => $label
205  ];
206  return $additionalFields;
207  }
208 
216  protected function ‪getSelectedState(array $configurationArray, $currentValue)
217  {
218  $selected = '';
219  if (in_array($currentValue, $configurationArray, true)) {
220  $selected = 'selected="selected" ';
221  }
222  return $selected;
223  }
224 
233  public function ‪validateAdditionalFields(array &$submittedData, ‪SchedulerModuleController $schedulerModule)
234  {
235  $isValid = true;
236  // @todo add validation to validate the $submittedData['configuration']
237  // @todo which is normally a comma separated string
238  $lang = $this->‪getLanguageService();
239  if (!empty($submittedData['linkvalidator']['email'])) {
240  if (strpos($submittedData['linkvalidator']['email'], ',') !== false) {
241  $emailList = GeneralUtility::trimExplode(',', $submittedData['linkvalidator']['email']);
242  } else {
243  $emailList = GeneralUtility::trimExplode(LF, $submittedData['linkvalidator']['email']);
244  }
245  foreach ($emailList as $emailAdd) {
246  if (!GeneralUtility::validEmail($emailAdd)) {
247  $isValid = false;
248  $this->‪addMessage(
249  $lang->sL($this->languageFile . ':tasks.validate.invalidEmail'),
251  );
252  }
253  }
254  }
255 
256  $row = ‪BackendUtility::getRecord('pages', (int)$submittedData['linkvalidator']['page'], '*', '', false);
257  if (empty($row)) {
258  $isValid = false;
259  $this->‪addMessage(
260  $lang->sL($this->languageFile . ':tasks.validate.invalidPage'),
262  );
263  }
264  if ($submittedData['linkvalidator']['depth'] < 0) {
265  $isValid = false;
266  $this->‪addMessage(
267  $lang->sL($this->languageFile . ':tasks.validate.invalidDepth'),
269  );
270  }
271  return $isValid;
272  }
273 
281  public function ‪saveAdditionalFields(array $submittedData, ‪AbstractTask $task)
282  {
284  $task->setDepth($submittedData['linkvalidator']['depth']);
285  $task->setPage($submittedData['linkvalidator']['page']);
286  $task->setEmail($submittedData['linkvalidator']['email']);
287  if ($submittedData['linkvalidator']['emailOnBrokenLinkOnly']) {
288  $task->setEmailOnBrokenLinkOnly(1);
289  } else {
290  $task->setEmailOnBrokenLinkOnly(0);
291  }
292  $task->setConfiguration($submittedData['linkvalidator']['configuration']);
293  $task->setEmailTemplateFile($submittedData['linkvalidator']['emailTemplateFile']);
294  }
295 
302  private function ‪getPageTitle($pageId)
303  {
304  $page = ‪BackendUtility::getRecord('pages', $pageId, 'title', '', false);
305  if ($page === null) {
306  return '';
307  }
308  return $page['title'];
309  }
310 
314  protected function ‪getLanguageService()
315  {
316  return ‪$GLOBALS['LANG'];
317  }
318 }
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\ADD
‪const ADD
Definition: Action.php:26
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider
Definition: AbstractAdditionalFieldProvider.php:26
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getSelectedState
‪string getSelectedState(array $configurationArray, $currentValue)
Definition: ValidatorTaskAdditionalFieldProvider.php:215
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider\addMessage
‪addMessage(string $message, int $severity=FlashMessage::OK)
Definition: AbstractAdditionalFieldProvider.php:33
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider
Definition: ValidatorTaskAdditionalFieldProvider.php:31
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getAdditionalFields
‪array getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: ValidatorTaskAdditionalFieldProvider.php:47
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:32
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
Definition: SchedulerModuleController.php:53
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action\EDIT
‪const EDIT
Definition: Action.php:28
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController\getCurrentAction
‪Action getCurrentAction()
Definition: SchedulerModuleController.php:198
‪TYPO3\CMS\Linkvalidator\Task
Definition: ValidatorTask.php:2
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:130
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getPageTitle
‪string getPageTitle($pageId)
Definition: ValidatorTaskAdditionalFieldProvider.php:301
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getLanguageService
‪LanguageService getLanguageService()
Definition: ValidatorTaskAdditionalFieldProvider.php:313
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Scheduler\Task\Enumeration\Action
Definition: Action.php:24
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\validateAdditionalFields
‪bool validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
Definition: ValidatorTaskAdditionalFieldProvider.php:232
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\saveAdditionalFields
‪saveAdditionalFields(array $submittedData, AbstractTask $task)
Definition: ValidatorTaskAdditionalFieldProvider.php:280
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\$languageFile
‪string $languageFile
Definition: ValidatorTaskAdditionalFieldProvider.php:36
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:29