[TASK] Use ServerRequestInterface in SchedulerModuleController
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / RecyclerGarbageCollectionAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\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
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
19 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
20 use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
21
22 /**
23 * Additional BE fields for recycler garbage collection task.
24 *
25 * Creates an integer input field for difference between scheduler run time
26 * and file modification time in days to select from.
27 */
28 class RecyclerGarbageCollectionAdditionalFieldProvider extends AbstractAdditionalFieldProvider
29 {
30 /**
31 * Default period in days to remove a recycled file
32 *
33 * @var int Default number of days
34 */
35 protected $defaultNumberOfDays = 30;
36
37 /**
38 * Add an integer input field for difference between scheduler run time
39 * and file modification time in days to select from
40 *
41 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
42 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
43 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
44 * @return array Array containing all the information pertaining to the additional fields
45 */
46 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
47 {
48 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
49
50 // Initialize selected fields
51 if (!isset($taskInfo['scheduler_recyclerGarbageCollection_numberOfDays'])) {
52 $taskInfo['scheduler_recyclerGarbageCollection_numberOfDays'] = $this->defaultNumberOfDays;
53 if ($currentSchedulerModuleAction->equals(Action::EDIT)) {
54 $taskInfo['scheduler_recyclerGarbageCollection_numberOfDays'] = $task->numberOfDays;
55 }
56 }
57 $fieldName = 'tx_scheduler[scheduler_recyclerGarbageCollection_numberOfDays]';
58 $fieldId = 'task_recyclerGarbageCollection_numberOfDays';
59 $fieldValue = (int)$taskInfo['scheduler_recyclerGarbageCollection_numberOfDays'];
60 $fieldHtml = '<input class="form-control" type="text" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '">';
61 $additionalFields[$fieldId] = [
62 'code' => $fieldHtml,
63 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.recyclerGarbageCollection.numberOfDays',
64 'cshKey' => '_MOD_system_txschedulerM1',
65 'cshLabel' => $fieldId
66 ];
67 return $additionalFields;
68 }
69
70 /**
71 * Checks if the given value is an integer
72 *
73 * @param array $submittedData Reference to the array containing the data submitted by the user
74 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
75 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
76 */
77 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
78 {
79 $result = true;
80 // Check if number of days is indeed a number and greater or equals to 0
81 // If not, fail validation and issue error message
82 if (!is_numeric($submittedData['scheduler_recyclerGarbageCollection_numberOfDays']) || (int)$submittedData['scheduler_recyclerGarbageCollection_numberOfDays'] < 0) {
83 $result = false;
84 $this->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), FlashMessage::ERROR);
85 }
86 return $result;
87 }
88
89 /**
90 * Saves given integer value in task object
91 *
92 * @param array $submittedData Contains data submitted by the user
93 * @param AbstractTask $task Reference to the current task object
94 */
95 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
96 {
97 $task->numberOfDays = (int)$submittedData['scheduler_recyclerGarbageCollection_numberOfDays'];
98 }
99 }