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