[CLEANUP] Improve the @param/@return/@var PHPDoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / FileStorageExtractionAdditionalFieldProvider.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\Utility\MathUtility;
18
19 /**
20 * Additional BE fields for task which extracts metadata from storage
21 *
22 */
23 class FileStorageExtractionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
24
25 /**
26 * Add additional fields
27 *
28 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
29 * @param AbstractTask|NULL $task When editing, reference to the current task. 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 * @throws \InvalidArgumentException
33 */
34 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
35 if ($task !== NULL && !$task instanceof FileStorageExtractionTask) {
36 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275695);
37 }
38 $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task);
39 $additionalFields['scheduler_fileStorageIndexing_fileCount'] = $this->getFileCountField($task);
40 return $additionalFields;
41 }
42
43 /**
44 * Returns a field configuration including a selectbox for available storages
45 *
46 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
47 * @return array Array containing all the information pertaining to the additional fields
48 */
49 protected function getAllStoragesField(FileStorageExtractionTask $task = NULL) {
50 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
51 $storages = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
52 $options = array();
53 foreach ($storages as $storage) {
54 if ($task !== NULL && $task->storageUid === $storage->getUid()) {
55 $options[] = '<option value="' . $storage->getUid() . '" selected="selected">' . $storage->getName() . '</option>';
56 } else {
57 $options[] = '<option value="' . $storage->getUid() . '">' . $storage->getName() . '</option>';
58 }
59 }
60
61 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_storage]';
62 $fieldId = 'scheduler_fileStorageIndexing_storage';
63 $fieldHtml = '<select name="' . $fieldName . '" id="' . $fieldId . '">' . implode("\n", $options) . '</select>';
64
65 $fieldConfiguration = array(
66 'code' => $fieldHtml,
67 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageIndexing.storage',
68 'cshKey' => '_MOD_system_txschedulerM1',
69 'cshLabel' => $fieldId
70 );
71 return $fieldConfiguration;
72 }
73
74 /**
75 * Returns a field configuration including a input field for the file count
76 *
77 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
78 * @return array Array containing all the information pertaining to the additional fields
79 */
80 protected function getFileCountField(FileStorageExtractionTask $task = NULL) {
81 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_fileCount]';
82 $fieldId = 'scheduler_fileStorageIndexing_fileCount';
83 $fieldValue = $task !== NULL ? (int)$task->maxFileCount : 100;
84 $fieldHtml = '<input type="text" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '" />';
85
86 $fieldConfiguration = array(
87 'code' => $fieldHtml,
88 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.fileCount',
89 'cshKey' => '_MOD_system_txschedulerM1',
90 'cshLabel' => $fieldId
91 );
92 return $fieldConfiguration;
93 }
94
95 /**
96 * Validate additional fields
97 *
98 * @param array $submittedData Reference to the array containing the data submitted by the user
99 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
100 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
101 */
102 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
103 if (
104 !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_storage'])
105 || !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_fileCount'])
106 ) {
107 return FALSE;
108 } elseif(\TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) === NULL) {
109 return FALSE;
110 } elseif (!MathUtility::isIntegerInRange($submittedData['scheduler_fileStorageIndexing_fileCount'], 1, 9999)) {
111 return FALSE;
112 }
113 return TRUE;
114 }
115
116 /**
117 * Save additional field in task
118 *
119 * @param array $submittedData Contains data submitted by the user
120 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
121 * @return void
122 * @throws \InvalidArgumentException
123 */
124 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
125 if ($task !== NULL && !$task instanceof FileStorageExtractionTask) {
126 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275698);
127 }
128 $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
129 $task->maxFileCount = (int)$submittedData['scheduler_fileStorageIndexing_fileCount'];
130 }
131
132 }