8a33bd91f788b8689c05a227b927e9e0e33001e3
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / FileStorageIndexingAdditionalFieldProvider.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 /**
18 * Additional BE fields for tasks which indexes files in a storage
19 */
20 class FileStorageIndexingAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface
21 {
22 /**
23 * Add additional fields
24 *
25 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
26 * @param AbstractTask|NULL $task When editing, reference to the current task. NULL when adding.
27 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
28 * @return array Array containing all the information pertaining to the additional fields
29 * @throws \InvalidArgumentException
30 */
31 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
32 {
33 if ($task !== null && !$task instanceof FileStorageIndexingTask) {
34 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275696);
35 }
36 $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task, $taskInfo);
37 return $additionalFields;
38 }
39
40 /**
41 * Add a select field of available storages.
42 *
43 * @param FileStorageIndexingTask $task When editing, reference to the current task object. NULL when adding.
44 * @param array $taskInfo
45 * @return array Array containing all the information pertaining to the additional fields
46 */
47 protected function getAllStoragesField(FileStorageIndexingTask $task = null, $taskInfo)
48 {
49 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
50 $storages = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
51 $options = [];
52 foreach ($storages as $storage) {
53 $selected = '';
54 if ($task !== null && $task->storageUid === $storage->getUid()) {
55 $selected =' selected="selected"';
56 } elseif ((int)$taskInfo['scheduler_fileStorageIndexing_storage'] === $storage->getUid()) {
57 $selected =' selected="selected"';
58 }
59 $options[] = '<option value="' . $storage->getUid() . '" ' . $selected . ' >' . $storage->getName() . '</option>';
60 }
61
62 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_storage]';
63 $fieldId = 'scheduler_fileStorageIndexing_storage';
64 $fieldHtml = '<select class="form-control" name="' . $fieldName . '" id="' . $fieldId . '">' . implode("\n", $options) . '</select>';
65
66 $fieldConfiguration = [
67 'code' => $fieldHtml,
68 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageIndexing.storage',
69 'cshKey' => '_MOD_system_txschedulerM1',
70 'cshLabel' => $fieldId
71 ];
72 return $fieldConfiguration;
73 }
74
75 /**
76 * Validate additional fields
77 *
78 * @param array $submittedData Reference to the array containing the data submitted by the user
79 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
80 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
81 */
82 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
83 {
84 $value = $submittedData['scheduler_fileStorageIndexing_storage'];
85 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($value)) {
86 return false;
87 }
88 if (\TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) !== null) {
89 return true;
90 }
91 return false;
92 }
93
94 /**
95 * Save additional field in task
96 *
97 * @param array $submittedData Contains data submitted by the user
98 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
99 * @throws \InvalidArgumentException
100 */
101 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
102 {
103 if (!$task instanceof FileStorageIndexingTask) {
104 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275697);
105 }
106 $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
107 }
108 }