[FEATURE] Add option to "system status updates" report to send all tests
[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 use TYPO3\CMS\Core\Messaging\FlashMessage;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Lang\LanguageService;
19 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
20 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
21 use TYPO3\CMS\Scheduler\Task\AbstractTask;
22
23 /**
24 * Additional field to set the notification email address(es) for system health
25 * issue notifications.
26 */
27 class SystemStatusUpdateTaskNotificationEmailField implements AdditionalFieldProviderInterface
28 {
29 /**
30 * Additional fields
31 *
32 * @var array
33 */
34 protected $fields = ['notificationEmail', 'notificationAll'];
35
36 /**
37 * Field prefix.
38 *
39 * @var string
40 */
41 protected $fieldPrefix = 'SystemStatusUpdate';
42
43 /**
44 * Gets additional fields to render in the form to add/edit a task
45 *
46 * @param array $taskInfo Values of the fields from the add/edit task form
47 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task The task object being edited. Null when adding a task!
48 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
49 * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
50 */
51 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
52 {
53 if ($schedulerModule->CMD == 'edit') {
54 $taskInfo[$this->fieldPrefix . 'NotificationEmail'] = $task->getNotificationEmail();
55 $taskInfo[$this->fieldPrefix . 'NotificationAll'] = $task->getNotificationAll();
56 }
57 // build html for additional email field
58 $fieldName = $this->getFullFieldName('notificationEmail');
59 $fieldId = 'task_' . $fieldName;
60 $fieldHtml = '<textarea class="form-control" ' . 'rows="5" cols="50" name="tx_scheduler[' . $fieldName . ']" ' . 'id="' . $fieldId . '" ' . '>' . htmlspecialchars($taskInfo[$fieldName]) . '</textarea>';
61
62 $additionalFields = [];
63 $additionalFields[$fieldId] = [
64 'code' => $fieldHtml,
65 'label' => 'LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationEmails',
66 'cshKey' => '',
67 'cshLabel' => $fieldId
68 ];
69
70 // build html for additional mail all checkbox field
71 $fieldName = $this->getFullFieldName('notificationAll');
72 $fieldId = 'task_' . $fieldName;
73 $fieldHtml = '<input type="checkbox" name="tx_scheduler[' . $fieldName . ']" id="' . $fieldId . '" value="1"' . ($taskInfo[$fieldName] ? ' checked="checked"' : '') . '>';
74
75 $additionalFields[$fieldId] = [
76 'code' => $fieldHtml,
77 'label' => 'LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationAll',
78 'cshKey' => '',
79 'cshLabel' => $fieldId
80 ];
81
82 return $additionalFields;
83 }
84
85 /**
86 * Validates the additional fields' values
87 *
88 * @param array $submittedData An array containing the data submitted by the add/edit task form
89 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
90 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
91 */
92 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
93 {
94 $validInput = true;
95 $notificationEmails = GeneralUtility::trimExplode(LF, $submittedData[$this->fieldPrefix . 'NotificationEmail'], true);
96 foreach ($notificationEmails as $notificationEmail) {
97 if (!GeneralUtility::validEmail($notificationEmail)) {
98 $validInput = false;
99 break;
100 }
101 }
102 if (empty($submittedData[$this->fieldPrefix . 'NotificationEmail']) || !$validInput) {
103 $schedulerModule->addMessage($this->getLanguageService()->sL('LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationEmails_invalid'), FlashMessage::ERROR);
104 $validInput = false;
105 }
106 return $validInput;
107 }
108
109 /**
110 * Takes care of saving the additional fields' values in the task's object
111 *
112 * @param array $submittedData An array containing the data submitted by the add/edit task form
113 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the scheduler backend module
114 * @return void
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 }