‪TYPO3CMS  ‪main
ValidatorTaskAdditionalFieldProvider.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
28 
34 {
40  protected ‪$languageFile = 'LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf';
41 
51  public function ‪getAdditionalFields(array &$taskInfo, $task, ‪SchedulerModuleController $schedulerModule): array
52  {
53  $additionalFields = [];
54  $currentSchedulerModuleAction = $schedulerModule->‪getCurrentAction();
55  $lang = $this->‪getLanguageService();
56 
57  if (empty($taskInfo['configuration'])) {
58  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
59  $taskInfo['configuration'] = $taskInfo['linkvalidator']['configuration'] ?? '';
60  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
61  $taskInfo['configuration'] = $task->getConfiguration();
62  } else {
63  $taskInfo['configuration'] = $task->getConfiguration();
64  }
65  }
66  if (empty($taskInfo['depth'])) {
67  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
68  $taskInfo['depth'] = $taskInfo['linkvalidator']['depth'] ?? 0;
69  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
70  $taskInfo['depth'] = $task->getDepth();
71  } else {
72  $taskInfo['depth'] = $task->getDepth();
73  }
74  }
75  if (empty($taskInfo['page'])) {
76  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
77  $taskInfo['page'] = $taskInfo['linkvalidator']['page'] ?? 0;
78  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
79  $taskInfo['page'] = $task->getPage();
80  } else {
81  $taskInfo['page'] = $task->getPage();
82  }
83  }
84  if (empty($taskInfo['languages'])) {
85  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
86  $taskInfo['languages'] = $taskInfo['linkvalidator']['languages'] ?? '';
87  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
88  $taskInfo['languages'] = $task->getLanguages();
89  } else {
90  $taskInfo['languages'] = $task->getLanguages();
91  }
92  }
93  if (empty($taskInfo['email'])) {
94  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
95  $taskInfo['email'] = $taskInfo['linkvalidator']['email'] ?? '';
96  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
97  $taskInfo['email'] = $task->getEmail();
98  } else {
99  $taskInfo['email'] = $task->getEmail();
100  }
101  }
102  if (empty($taskInfo['emailOnBrokenLinkOnly'])) {
103  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
104  $taskInfo['emailOnBrokenLinkOnly'] = ($taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] ?? false) ? (bool)$taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] : true;
105  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
106  $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
107  } else {
108  $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
109  }
110  }
111  if (empty($taskInfo['emailTemplateName'])) {
112  if ($currentSchedulerModuleAction === SchedulerManagementAction::ADD) {
113  $taskInfo['emailTemplateName'] = ($taskInfo['linkvalidator']['emailTemplateName'] ?? false) ? $taskInfo['linkvalidator']['emailTemplateName'] : '';
114  } elseif ($currentSchedulerModuleAction === SchedulerManagementAction::EDIT) {
115  $taskInfo['emailTemplateName'] = $task->getEmailTemplateName();
116  } else {
117  $taskInfo['emailTemplateName'] = $task->getEmailTemplateName();
118  }
119  }
120  $fieldId = 'task_page';
121  $pageUid = '';
122  $pageTitle = '';
123  if (!empty($taskInfo['page'])) {
124  $pageUid = (int)$taskInfo['page'];
125  $pageTitle = $this->‪getPageTitle((int)$taskInfo['page']);
126  }
127  $fieldCode = '<input type="number" min="0" class="form-control form-control-clearable t3js-clearable" name="tx_scheduler[linkvalidator][page]" id="'
128  . $fieldId
129  . '" value="'
130  . htmlspecialchars((string)$taskInfo['page'])
131  . '">';
132  $label = $lang->sL($this->languageFile . ':tasks.validate.page');
133  $additionalFields[$fieldId] = [
134  'browser' => 'page',
135  'pageTitle' => $pageTitle,
136  'pageUid' => $pageUid,
137  'code' => $fieldCode,
138  'cshTable' => 'linkvalidator',
139  'cshLabel' => $fieldId,
140  'label' => $label,
141  ];
142  // input for depth
143  $fieldId = 'task_depth';
144  $fieldValueArray = [
145  '0' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
146  '1' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
147  '2' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
148  '3' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
149  '4' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
150  '999' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
151  ];
153  $fieldCode = '<select class="form-select" name="tx_scheduler[linkvalidator][depth]" id="' . $fieldId . '">';
154  foreach ($fieldValueArray as $depth => $label) {
155  $fieldCode .= "\t" . '<option value="' . htmlspecialchars((string)$depth) . '"'
156  . (($depth === $taskInfo['depth']) ? ' selected="selected"' : '') . '>'
157  . $label
158  . '</option>';
159  }
160  $fieldCode .= '</select>';
161  $label = $lang->sL($this->languageFile . ':tasks.validate.depth');
162  $additionalFields[$fieldId] = [
163  'code' => $fieldCode,
164  'cshKey' => 'linkvalidator',
165  'cshLabel' => $fieldId,
166  'label' => $label,
167  'type' => 'select',
168  ];
169  $fieldId = 'task_languages';
170  $fieldCode = '<input class="form-control form-control-clearable t3js-clearable" type="text" name="tx_scheduler[linkvalidator][languages]"'
171  . ' id="' . $fieldId
172  . '" value="' . htmlspecialchars((string)$taskInfo['languages'])
173  . '">';
174  $label = $lang->sL($this->languageFile . ':tasks.validate.languages');
175  $additionalFields[$fieldId] = [
176  'code' => $fieldCode,
177  'cshKey' => 'linkvalidator',
178  'cshLabel' => $fieldId,
179  'label' => $label,
180  'type' => 'input',
181  ];
182  $fieldId = 'task_configuration';
183  $fieldCode = '<textarea class="form-control" rows="5" name="tx_scheduler[linkvalidator][configuration]" id="'
184  . $fieldId . '">'
185  . htmlspecialchars((string)$taskInfo['configuration'])
186  . '</textarea>';
187  $label = $lang->sL($this->languageFile . ':tasks.validate.conf');
188  $additionalFields[$fieldId] = [
189  'code' => $fieldCode,
190  'cshKey' => 'linkvalidator',
191  'cshLabel' => $fieldId,
192  'label' => $label,
193  'type' => 'textarea',
194  ];
195  $fieldId = 'task_email';
196  $fieldCode = '<textarea class="form-control" rows="3" cols="50" name="tx_scheduler[linkvalidator][email]" id="'
197  . $fieldId . '">'
198  . htmlspecialchars((string)$taskInfo['email'])
199  . '</textarea>';
200  $label = $lang->sL($this->languageFile . ':tasks.validate.email');
201  $additionalFields[$fieldId] = [
202  'code' => $fieldCode,
203  'cshKey' => 'linkvalidator',
204  'cshLabel' => $fieldId,
205  'label' => $label,
206  'type' => 'textarea',
207  ];
208  $fieldId = 'task_emailOnBrokenLinkOnly';
209  $fieldCode = '<input type="checkbox" class="form-check-input" name="tx_scheduler[linkvalidator][emailOnBrokenLinkOnly]" '
210  . 'id="' . $fieldId . '" ' . ((bool)$taskInfo['emailOnBrokenLinkOnly'] ? 'checked="checked"' : '') . '>';
211  $label = $lang->sL($this->languageFile . ':tasks.validate.emailOnBrokenLinkOnly');
212  $additionalFields[$fieldId] = [
213  'code' => $fieldCode,
214  'cshKey' => 'linkvalidator',
215  'cshLabel' => $fieldId,
216  'label' => $label,
217  'type' => 'checkToggle',
218  ];
219  $fieldId = 'task_emailTemplateName';
220  $fieldCode = '<input class="form-control form-control-clearable t3js-clearable" type="text" name="tx_scheduler[linkvalidator][emailTemplateName]" '
221  . 'id="' . $fieldId
222  . '" value="' . htmlspecialchars((string)$taskInfo['emailTemplateName'])
223  . '">';
224  $label = $lang->sL($this->languageFile . ':tasks.validate.emailTemplateName');
225  $additionalFields[$fieldId] = [
226  'code' => $fieldCode,
227  'cshKey' => 'linkvalidator',
228  'cshLabel' => $fieldId,
229  'label' => $label,
230  'type' => 'input',
231  ];
232  return $additionalFields;
233  }
234 
243  public function ‪validateAdditionalFields(array &$submittedData, ‪SchedulerModuleController $schedulerModule): bool
244  {
245  $isValid = true;
246  $lang = $this->‪getLanguageService();
247  $email = (string)($submittedData['linkvalidator']['email'] ?? '');
248  if ($email !== '') {
249  $emailList = ‪GeneralUtility::trimExplode((str_contains($email, ',')) ? ',' : LF, $email);
250  foreach ($emailList as $emailAdd) {
251  if (!GeneralUtility::validEmail($emailAdd)) {
252  $isValid = false;
253  $this->‪addMessage(
254  $lang->sL($this->languageFile . ':tasks.validate.invalidEmail'),
255  ContextualFeedbackSeverity::ERROR
256  );
257  }
258  }
259  }
260 
261  $row = BackendUtility::getRecord('pages', (int)$submittedData['linkvalidator']['page'], '*', '', false);
262  if ($row === null) {
263  $isValid = false;
264  $this->‪addMessage(
265  $lang->sL($this->languageFile . ':tasks.validate.invalidPage'),
266  ContextualFeedbackSeverity::ERROR
267  );
268  }
269  if ((int)$submittedData['linkvalidator']['depth'] < 0) {
270  $isValid = false;
271  $this->‪addMessage(
272  $lang->sL($this->languageFile . ':tasks.validate.invalidDepth'),
273  ContextualFeedbackSeverity::ERROR
274  );
275  }
276  return $isValid;
277  }
278 
286  public function ‪saveAdditionalFields(array $submittedData, ‪AbstractTask $task): void
287  {
289  $task
290  ->setDepth((int)$submittedData['linkvalidator']['depth'])
291  ->setPage((int)$submittedData['linkvalidator']['page'])
292  ->setLanguages($submittedData['linkvalidator']['languages'])
293  ->setEmail($submittedData['linkvalidator']['email'])
294  ->setEmailOnBrokenLinkOnly((bool)($submittedData['linkvalidator']['emailOnBrokenLinkOnly'] ?? false))
295  ->setConfiguration($submittedData['linkvalidator']['configuration'])
296  ->setEmailTemplateName($submittedData['linkvalidator']['emailTemplateName']);
297  }
298 
304  private function ‪getPageTitle(int $pageId): string
305  {
306  return (string)(BackendUtility::getRecord('pages', $pageId, 'title', '', false)['title'] ?? '');
307  }
308 
309  protected function ‪getLanguageService(): ‪LanguageService
310  {
311  return ‪$GLOBALS['LANG'];
312  }
313 }
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getLanguageService
‪getLanguageService()
Definition: ValidatorTaskAdditionalFieldProvider.php:308
‪TYPO3\CMS\Scheduler\SchedulerManagementAction
‪SchedulerManagementAction
Definition: SchedulerManagementAction.php:25
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getPageTitle
‪string getPageTitle(int $pageId)
Definition: ValidatorTaskAdditionalFieldProvider.php:303
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider
Definition: AbstractAdditionalFieldProvider.php:29
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider
Definition: ValidatorTaskAdditionalFieldProvider.php:34
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\getAdditionalFields
‪array getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: ValidatorTaskAdditionalFieldProvider.php:50
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController\getCurrentAction
‪getCurrentAction()
Definition: SchedulerModuleController.php:197
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:33
‪TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider\addMessage
‪addMessage(string $message, ContextualFeedbackSeverity $severity=ContextualFeedbackSeverity::OK)
Definition: AbstractAdditionalFieldProvider.php:36
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
Definition: SchedulerModuleController.php:61
‪TYPO3\CMS\Linkvalidator\Task
Definition: ValidatorTask.php:18
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\validateAdditionalFields
‪bool validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
Definition: ValidatorTaskAdditionalFieldProvider.php:242
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\saveAdditionalFields
‪saveAdditionalFields(array $submittedData, AbstractTask $task)
Definition: ValidatorTaskAdditionalFieldProvider.php:285
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Linkvalidator\Task\ValidatorTaskAdditionalFieldProvider\$languageFile
‪string $languageFile
Definition: ValidatorTaskAdditionalFieldProvider.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822