97e4301c467ad46995c4e906490d16d5cf9e267a
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / SleepTaskAdditionalFieldProvider.php
1 <?php
2 namespace 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
17 /**
18 * Additional fields provider class for usage with the Scheduler's sleep task
19 *
20 * @author Fran├žois Suter <francois@typo3.org>
21 */
22 class SleepTaskAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
23
24 /**
25 * This method is used to define new fields for adding or editing a task
26 * In this case, it adds an sleep time field
27 *
28 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
29 * @param object $task When editing, reference to the current task object. Null when adding.
30 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
31 * @return array Array containing all the information pertaining to the additional fields
32 */
33 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
34 // Initialize extra field value
35 if (empty($taskInfo['sleepTime'])) {
36 if ($parentObject->CMD === 'add') {
37 // In case of new task and if field is empty, set default sleep time
38 $taskInfo['sleepTime'] = 30;
39 } elseif ($parentObject->CMD === 'edit') {
40 // In case of edit, set to internal value if no data was submitted already
41 $taskInfo['sleepTime'] = $task->sleepTime;
42 } else {
43 // Otherwise set an empty value, as it will not be used anyway
44 $taskInfo['sleepTime'] = '';
45 }
46 }
47 // Write the code for the field
48 $fieldID = 'task_sleepTime';
49 $fieldCode = '<input type="text" name="tx_scheduler[sleepTime]" id="' . $fieldID . '" value="' . $taskInfo['sleepTime'] . '" size="10" />';
50 $additionalFields = array();
51 $additionalFields[$fieldID] = array(
52 'code' => $fieldCode,
53 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.sleepTime',
54 'cshKey' => '_MOD_system_txschedulerM1',
55 'cshLabel' => $fieldID
56 );
57 return $additionalFields;
58 }
59
60 /**
61 * This method checks any additional data that is relevant to the specific task
62 * If the task class is not relevant, the method is expected to return TRUE
63 *
64 * @param array $submittedData Reference to the array containing the data submitted by the user
65 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
66 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
67 */
68 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
69 $submittedData['sleepTime'] = (int)$submittedData['sleepTime'];
70 if ($submittedData['sleepTime'] < 0) {
71 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidSleepTime'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
72 $result = FALSE;
73 } else {
74 $result = TRUE;
75 }
76 return $result;
77 }
78
79 /**
80 * This method is used to save any additional input into the current task object
81 * if the task class matches
82 *
83 * @param array $submittedData Array containing the data submitted by the user
84 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
85 * @return void
86 */
87 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
88 $task->sleepTime = $submittedData['sleepTime'];
89 }
90
91 }