[TASK] Use ServerRequestInterface in SchedulerModuleController
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Task / SystemStatusUpdateTaskNotificationEmailField.php
1 <?php
2 namespace TYPO3\CMS\Reports\Task;
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\Localization\LanguageService;
18 use TYPO3\CMS\Core\Messaging\FlashMessage;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 field to set the notification email address(es) for system health
27 * issue notifications.
28 */
29 class SystemStatusUpdateTaskNotificationEmailField extends AbstractAdditionalFieldProvider
30 {
31 /**
32 * Additional fields
33 *
34 * @var array
35 */
36 protected $fields = ['notificationEmail', 'notificationAll'];
37
38 /**
39 * Field prefix.
40 *
41 * @var string
42 */
43 protected $fieldPrefix = 'SystemStatusUpdate';
44
45 /**
46 * Gets additional fields to render in the form to add/edit a task
47 *
48 * @param array $taskInfo Values of the fields from the add/edit task form
49 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
50 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
51 * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
52 */
53 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
54 {
55 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
56
57 if ($currentSchedulerModuleAction->equals(Action::EDIT)) {
58 $taskInfo[$this->fieldPrefix . 'NotificationEmail'] = $task->getNotificationEmail();
59 $taskInfo[$this->fieldPrefix . 'NotificationAll'] = $task->getNotificationAll();
60 }
61 // build html for additional email field
62 $fieldName = $this->getFullFieldName('notificationEmail');
63 $fieldId = 'task_' . $fieldName;
64 $fieldHtml = '<textarea class="form-control" ' . 'rows="5" cols="50" name="tx_scheduler[' . $fieldName . ']" ' . 'id="' . $fieldId . '" ' . '>' . htmlspecialchars($taskInfo[$fieldName]) . '</textarea>';
65
66 $additionalFields = [];
67 $additionalFields[$fieldId] = [
68 'code' => $fieldHtml,
69 'label' => 'LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationEmails',
70 'cshKey' => '',
71 'cshLabel' => $fieldId
72 ];
73
74 // build html for additional mail all checkbox field
75 $fieldName = $this->getFullFieldName('notificationAll');
76 $fieldId = 'task_' . $fieldName;
77 $fieldHtml = '<input type="checkbox" name="tx_scheduler[' . $fieldName . ']" id="' . $fieldId . '" value="1"' . ($taskInfo[$fieldName] ? ' checked="checked"' : '') . '>';
78
79 $additionalFields[$fieldId] = [
80 'code' => $fieldHtml,
81 'label' => 'LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationAll',
82 'cshKey' => '',
83 'cshLabel' => $fieldId
84 ];
85
86 return $additionalFields;
87 }
88
89 /**
90 * Validates the additional fields' values
91 *
92 * @param array $submittedData An array containing the data submitted by the add/edit task form
93 * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
94 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
95 */
96 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
97 {
98 $validInput = true;
99 $notificationEmails = GeneralUtility::trimExplode(LF, $submittedData[$this->fieldPrefix . 'NotificationEmail'], true);
100 foreach ($notificationEmails as $notificationEmail) {
101 if (!GeneralUtility::validEmail($notificationEmail)) {
102 $validInput = false;
103 break;
104 }
105 }
106 if (!$validInput || empty($submittedData[$this->fieldPrefix . 'NotificationEmail'])) {
107 $this->addMessage($this->getLanguageService()->sL('LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationEmails_invalid'), FlashMessage::ERROR);
108 $validInput = false;
109 }
110 return $validInput;
111 }
112
113 /**
114 * Takes care of saving the additional fields' values in the task's object
115 *
116 * @param array $submittedData An array containing the data submitted by the add/edit task form
117 * @param AbstractTask $task Reference to the scheduler backend module
118 * @throws \InvalidArgumentException
119 */
120 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
121 {
122 if (!$task instanceof SystemStatusUpdateTask) {
123 throw new \InvalidArgumentException('Expected a task of type ' . SystemStatusUpdateTask::class . ', but got ' . get_class($task), 1295012802);
124 }
125 $task->setNotificationEmail($submittedData[$this->fieldPrefix . 'NotificationEmail']);
126 $task->setNotificationAll(!empty($submittedData[$this->fieldPrefix . 'NotificationAll']));
127 }
128
129 /**
130 * Constructs the full field name which can be used in HTML markup.
131 *
132 * @param string $fieldName A raw field name
133 * @return string Field name ready to use in HTML markup
134 */
135 protected function getFullFieldName($fieldName)
136 {
137 return $this->fieldPrefix . ucfirst($fieldName);
138 }
139
140 /**
141 * @return LanguageService
142 */
143 protected function getLanguageService()
144 {
145 return $GLOBALS['LANG'];
146 }
147 }