[TASK] Avoid ObjectManager in ext:form FormRuntime
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / TestTaskAdditionalFieldProvider.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 test task
27 * @internal This class is an example is not considered part of the Public TYPO3 API.
28 */
29 class TestTaskAdditionalFieldProvider 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 an email field
34 *
35 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
36 * @param TestTask|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['email'])) {
46 if ($currentSchedulerModuleAction->equals(Action::ADD)) {
47 // In case of new task and if field is empty, set default email address
48 $taskInfo['email'] = $GLOBALS['BE_USER']->user['email'];
49 } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
50 // In case of edit, and editing a test task, set to internal value if not data was submitted already
51 $taskInfo['email'] = $task->email;
52 } else {
53 // Otherwise set an empty value, as it will not be used anyway
54 $taskInfo['email'] = '';
55 }
56 }
57 // Write the code for the field
58 $fieldID = 'task_email';
59 $fieldCode = '<input type="text" class="form-control" name="tx_scheduler[email]" id="' . $fieldID . '" value="' . htmlspecialchars($taskInfo['email']) . '" size="30">';
60 $additionalFields = [];
61 $additionalFields[$fieldID] = [
62 'code' => $fieldCode,
63 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.email',
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['email'] = trim($submittedData['email']);
81 if (empty($submittedData['email'])) {
82 $this->addMessage(
83 $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.noEmail'),
84 FlashMessage::ERROR
85 );
86 $result = false;
87 } else {
88 $result = true;
89 }
90 return $result;
91 }
92
93 /**
94 * This method is used to save any additional input into the current task object
95 * if the task class matches
96 *
97 * @param array $submittedData Array containing the data submitted by the user
98 * @param TestTask $task Reference to the current task object
99 */
100 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
101 {
102 $task->email = $submittedData['email'];
103 }
104
105 /**
106 * @return LanguageService|null
107 */
108 protected function getLanguageService(): ?LanguageService
109 {
110 return $GLOBALS['LANG'] ?? null;
111 }
112 }