[TASK] Use ServerRequestInterface in SchedulerModuleController
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / TestTaskAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Example;
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
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
19 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
20 use TYPO3\CMS\Scheduler\Task\AbstractTask;
21 use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
22
23 /**
24 * Additional fields provider class for usage with the Scheduler's test task
25 */
26 class TestTaskAdditionalFieldProvider extends AbstractAdditionalFieldProvider
27 {
28 /**
29 * This method is used to define new fields for adding or editing a task
30 * In this case, it adds an email field
31 *
32 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
33 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
34 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
35 * @return array Array containing all the information pertaining to the additional fields
36 */
37 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
38 {
39 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
40
41 // Initialize extra field value
42 if (empty($taskInfo['email'])) {
43 if ($currentSchedulerModuleAction->equals(Action::ADD)) {
44 // In case of new task and if field is empty, set default email address
45 $taskInfo['email'] = $GLOBALS['BE_USER']->user['email'];
46 } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
47 // In case of edit, and editing a test task, set to internal value if not data was submitted already
48 $taskInfo['email'] = $task->email;
49 } else {
50 // Otherwise set an empty value, as it will not be used anyway
51 $taskInfo['email'] = '';
52 }
53 }
54 // Write the code for the field
55 $fieldID = 'task_email';
56 $fieldCode = '<input type="text" class="form-control" name="tx_scheduler[email]" id="' . $fieldID . '" value="' . htmlspecialchars($taskInfo['email']) . '" size="30">';
57 $additionalFields = [];
58 $additionalFields[$fieldID] = [
59 'code' => $fieldCode,
60 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.email',
61 'cshKey' => '_MOD_system_txschedulerM1',
62 'cshLabel' => $fieldID
63 ];
64 return $additionalFields;
65 }
66
67 /**
68 * This method checks any additional data that is relevant to the specific task
69 * If the task class is not relevant, the method is expected to return TRUE
70 *
71 * @param array $submittedData Reference to the array containing the data submitted by the user
72 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
73 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
74 */
75 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
76 {
77 $submittedData['email'] = trim($submittedData['email']);
78 if (empty($submittedData['email'])) {
79 $this->addMessage(
80 $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.noEmail'),
81 FlashMessage::ERROR
82 );
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 \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
96 */
97 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
98 {
99 $task->email = $submittedData['email'];
100 }
101 }