[CLEANUP] Code cleanup in ext:reports
[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 = array('notificationEmail');
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 }
56 // build html for additional email field
57 $fieldName = $this->getFullFieldName('notificationEmail');
58 $fieldId = 'task_' . $fieldName;
59 $fieldHtml = '<textarea class="form-control" ' . 'rows="5" cols="50" name="tx_scheduler[' . $fieldName . ']" ' . 'id="' . $fieldId . '" ' . '>' . htmlspecialchars($taskInfo[$fieldName]) . '</textarea>';
60
61 $additionalFields = array();
62 $additionalFields[$fieldId] = array(
63 'code' => $fieldHtml,
64 'label' => 'LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationEmails',
65 'cshKey' => '',
66 'cshLabel' => $fieldId
67 );
68
69 return $additionalFields;
70 }
71
72 /**
73 * Validates the additional fields' values
74 *
75 * @param array $submittedData An array containing the data submitted by the add/edit task form
76 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
77 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
78 */
79 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
80 {
81 $validInput = true;
82 $notificationEmails = GeneralUtility::trimExplode(LF, $submittedData[$this->fieldPrefix . 'NotificationEmail'], true);
83 foreach ($notificationEmails as $notificationEmail) {
84 if (!GeneralUtility::validEmail($notificationEmail)) {
85 $validInput = false;
86 break;
87 }
88 }
89 if (empty($submittedData[$this->fieldPrefix . 'NotificationEmail']) || !$validInput) {
90 $schedulerModule->addMessage($this->getLanguageService()->sL('LLL:EXT:reports/Resources/Private/Language/locallang_reports.xlf:status_updateTaskField_notificationEmails_invalid'), FlashMessage::ERROR);
91 $validInput = false;
92 }
93 return $validInput;
94 }
95
96 /**
97 * Takes care of saving the additional fields' values in the task's object
98 *
99 * @param array $submittedData An array containing the data submitted by the add/edit task form
100 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the scheduler backend module
101 * @return void
102 */
103 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
104 {
105 if (!$task instanceof SystemStatusUpdateTask) {
106 throw new \InvalidArgumentException('Expected a task of type ' . SystemStatusUpdateTask::class . ', but got ' . get_class($task), 1295012802);
107 }
108 $task->setNotificationEmail($submittedData[$this->fieldPrefix . 'NotificationEmail']);
109 }
110
111 /**
112 * Constructs the full field name which can be used in HTML markup.
113 *
114 * @param string $fieldName A raw field name
115 * @return string Field name ready to use in HTML markup
116 */
117 protected function getFullFieldName($fieldName)
118 {
119 return $this->fieldPrefix . ucfirst($fieldName);
120 }
121
122 /**
123 * @return LanguageService
124 */
125 protected function getLanguageService()
126 {
127 return $GLOBALS['LANG'];
128 }
129 }