[FEATURE] Enable multiple emails in reports task
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Task / SystemStatusUpdateTaskNotificationEmailField.php
1 <?php
2 namespace TYPO3\CMS\Reports\Task;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Additional field to set the notification email address(es) for system health
28 * issue notifications.
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 */
32 class SystemStatusUpdateTaskNotificationEmailField implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
33
34 /**
35 * Additional fields
36 *
37 * @var array
38 */
39 protected $fields = array('notificationEmail');
40
41 /**
42 * Field prefix.
43 *
44 * @var string
45 */
46 protected $fieldPrefix = 'SystemStatusUpdate';
47
48 /**
49 * Gets additional fields to render in the form to add/edit a task
50 *
51 * @param array $taskInfo Values of the fields from the add/edit task form
52 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task The task object being eddited. Null when adding a task!
53 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
54 * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
55 */
56 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
57 $fields = array('notificationEmail' => 'textarea');
58 if ($schedulerModule->CMD == 'edit') {
59 $taskInfo[$this->fieldPrefix . 'NotificationEmail'] = $task->getNotificationEmail();
60 }
61 // build html for additional email field
62 $fieldName = $this->getFullFieldName('notificationEmail');
63 $fieldId = 'task_' . $fieldName;
64 $fieldHtml = '<textarea ' . 'rows="5" cols="50" name="tx_scheduler[' . $fieldName . ']" ' . 'id="' . $fieldId . '" ' . '>' . htmlspecialchars($taskInfo[$fieldName]) . '</textarea>';
65
66 $additionalFields = array();
67 $additionalFields[$fieldId] = array(
68 'code' => $fieldHtml,
69 'label' => 'LLL:EXT:reports/reports/locallang.xml:status_updateTaskField_notificationEmails',
70 'cshKey' => '',
71 'cshLabel' => $fieldId
72 );
73
74 return $additionalFields;
75 }
76
77 /**
78 * Validates the additional fields' values
79 *
80 * @param array $submittedData An array containing the data submitted by the add/edit task form
81 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
82 * @return boolean TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
83 */
84 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
85 $validInput = TRUE;
86 $notificationEmails = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $submittedData[$this->fieldPrefix . 'NotificationEmail'], TRUE);
87 foreach ($notificationEmails as $notificationEmail) {
88 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($notificationEmail)) {
89 $validInput = FALSE;
90 break;
91 }
92 }
93 if (empty($submittedData[$this->fieldPrefix . 'NotificationEmail']) || !$validInput) {
94 $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:reports/reports/locallang.xml:status_updateTaskField_notificationEmails_invalid'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
95 $validInput = FALSE;
96 }
97 return $validInput;
98 }
99
100 /**
101 * Takes care of saving the additional fields' values in the task's object
102 *
103 * @param array $submittedData An array containing the data submitted by the add/edit task form
104 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the scheduler backend module
105 * @return void
106 */
107 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
108 if (!$task instanceof \TYPO3\CMS\Reports\Task\SystemStatusUpdateTask) {
109 throw new \InvalidArgumentException('Expected a task of type TYPO3\\CMS\\Reports\\Task\\SystemStatusUpdateTask, but got ' . get_class($task), 1295012802);
110 }
111 $task->setNotificationEmail($submittedData[$this->fieldPrefix . 'NotificationEmail']);
112 }
113
114 /**
115 * Constructs the full field name which can be used in HTML markup.
116 *
117 * @param string $fieldName A raw field name
118 * @return string Field name ready to use in HTML markup
119 */
120 protected function getFullFieldName($fieldName) {
121 return $this->fieldPrefix . ucfirst($fieldName);
122 }
123
124 }
125
126 ?>