[TASK] Fixed links to issues in Changelog
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / SleepTaskAdditionalFieldProvider.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Scheduler\Example;
17
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
20 use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
21 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
22 use TYPO3\CMS\Scheduler\Task\AbstractTask;
23 use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
24
25 /**
26 * Additional fields provider class for usage with the Scheduler's sleep task
27 * @internal This class is an example is not considered part of the Public TYPO3 API.
28 */
29 class SleepTaskAdditionalFieldProvider extends AbstractAdditionalFieldProvider
30 {
31 /**
32 * This method is used to define new fields for adding or editing a task
33 * In this case, it adds a sleep time field
34 *
35 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
36 * @param SleepTask|null $task When editing, reference to the current task. NULL when adding.
37 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
38 * @return array Array containing all the information pertaining to the additional fields
39 */
40 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
41 {
42 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
43
44 // Initialize extra field value
45 if (empty($taskInfo['sleepTime'])) {
46 if ($currentSchedulerModuleAction->equals(Action::ADD)) {
47 // In case of new task and if field is empty, set default sleep time
48 $taskInfo['sleepTime'] = 30;
49 } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
50 // In case of edit, set to internal value if no data was submitted already
51 $taskInfo['sleepTime'] = $task->sleepTime;
52 } else {
53 // Otherwise set an empty value, as it will not be used anyway
54 $taskInfo['sleepTime'] = '';
55 }
56 }
57 // Write the code for the field
58 $fieldID = 'task_sleepTime';
59 $fieldCode = '<input type="text" class="form-control" name="tx_scheduler[sleepTime]" id="' . $fieldID . '" value="' . $taskInfo['sleepTime'] . '" size="10">';
60 $additionalFields = [];
61 $additionalFields[$fieldID] = [
62 'code' => $fieldCode,
63 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.sleepTime',
64 'cshKey' => '_MOD_system_txschedulerM1',
65 'cshLabel' => $fieldID
66 ];
67 return $additionalFields;
68 }
69
70 /**
71 * This method checks any additional data that is relevant to the specific task
72 * If the task class is not relevant, the method is expected to return TRUE
73 *
74 * @param array $submittedData Reference to the array containing the data submitted by the user
75 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
76 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
77 */
78 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
79 {
80 $submittedData['sleepTime'] = (int)$submittedData['sleepTime'];
81 if ($submittedData['sleepTime'] < 0) {
82 $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidSleepTime'), FlashMessage::ERROR);
83 $result = false;
84 } else {
85 $result = true;
86 }
87 return $result;
88 }
89
90 /**
91 * This method is used to save any additional input into the current task object
92 * if the task class matches
93 *
94 * @param array $submittedData Array containing the data submitted by the user
95 * @param SleepTask $task Reference to the current task object
96 */
97 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
98 {
99 $task->sleepTime = $submittedData['sleepTime'];
100 }
101
102 /**
103 * @return LanguageService|null
104 */
105 protected function getLanguageService(): ?LanguageService
106 {
107 return $GLOBALS['LANG'] ?? null;
108 }
109 }