1<?php
2namespace TYPO3\CMS\Scheduler\Example;
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
17use TYPO3\CMS\Core\Messaging\FlashMessage;
18use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
19use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
20use TYPO3\CMS\Scheduler\Task\AbstractTask;
21use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
22
23/**
24 * Additional fields provider class for usage with the Scheduler's test task
25 * @internal This class is an example is not considered part of the Public TYPO3 API.
26 */
27class TestTaskAdditionalFieldProvider extends AbstractAdditionalFieldProvider
28{
29    /**
30     * This method is used to define new fields for adding or editing a task
31     * In this case, it adds an email field
32     *
33     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
34     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
35     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
36     * @return array Array containing all the information pertaining to the additional fields
37     */
38    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
39    {
40        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
41
42        // Initialize extra field value
43        if (empty($taskInfo['email'])) {
44            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
45                // In case of new task and if field is empty, set default email address
46                $taskInfo['email'] = $GLOBALS['BE_USER']->user['email'];
47            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
48                // In case of edit, and editing a test task, set to internal value if not data was submitted already
49                $taskInfo['email'] = $task->email;
50            } else {
51                // Otherwise set an empty value, as it will not be used anyway
52                $taskInfo['email'] = '';
53            }
54        }
55        // Write the code for the field
56        $fieldID = 'task_email';
57        $fieldCode = '<input type="text" class="form-control" name="tx_scheduler[email]" id="' . $fieldID . '" value="' . htmlspecialchars($taskInfo['email']) . '" size="30">';
58        $additionalFields = [];
59        $additionalFields[$fieldID] = [
60            'code' => $fieldCode,
61            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.email',
62            'cshKey' => '_MOD_system_txschedulerM1',
63            'cshLabel' => $fieldID
64        ];
65        return $additionalFields;
66    }
67
68    /**
69     * This method checks any additional data that is relevant to the specific task
70     * If the task class is not relevant, the method is expected to return TRUE
71     *
72     * @param array	 $submittedData Reference to the array containing the data submitted by the user
73     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
74     * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
75     */
76    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
77    {
78        $submittedData['email'] = trim($submittedData['email']);
79        if (empty($submittedData['email'])) {
80            $this->addMessage(
81                $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.noEmail'),
82                FlashMessage::ERROR
83            );
84            $result = false;
85        } else {
86            $result = true;
87        }
88        return $result;
89    }
90
91    /**
92     * This method is used to save any additional input into the current task object
93     * if the task class matches
94     *
95     * @param array $submittedData Array containing the data submitted by the user
96     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
97     */
98    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
99    {
100        $task->email = $submittedData['email'];
101    }
102}
103