[CLEANUP] The correct case must be used for standard PHP types in 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\Resource\Index\ExtractorInterface;
18 use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\MathUtility;
22 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
23 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
24
25 /**
26 * Additional BE fields for task which extracts metadata from storage
27 */
28 class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldProviderInterface
29 {
30 /**
31 * Add additional fields
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 $parentObject Reference to the calling object (Scheduler's BE module)
36 * @return array Array containing all the information pertaining to the additional fields
37 * @throws \InvalidArgumentException
38 */
39 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $parentObject)
40 {
41 if ($task !== null && !$task instanceof FileStorageExtractionTask) {
42 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275695);
43 }
44 $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task);
45 $additionalFields['scheduler_fileStorageIndexing_fileCount'] = $this->getFileCountField($task);
46 $additionalFields['scheduler_fileStorageIndexing_registeredExtractors'] = $this->getRegisteredExtractorsField($task);
47 return $additionalFields;
48 }
49
50 /**
51 * Returns a field configuration including a selectbox for available storages
52 *
53 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
54 * @return array Array containing all the information pertaining to the additional fields
55 */
56 protected function getAllStoragesField(FileStorageExtractionTask $task = null)
57 {
58 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
59 $storages = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
60 $options = [];
61 foreach ($storages as $storage) {
62 if ($task !== null && $task->storageUid === $storage->getUid()) {
63 $options[] = '<option value="' . $storage->getUid() . '" selected="selected">' . $storage->getName() . '</option>';
64 } else {
65 $options[] = '<option value="' . $storage->getUid() . '">' . $storage->getName() . '</option>';
66 }
67 }
68
69 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_storage]';
70 $fieldId = 'scheduler_fileStorageIndexing_storage';
71 $fieldHtml = '<select class="form-control" name="' . $fieldName . '" id="' . $fieldId . '">' . implode("\n", $options) . '</select>';
72
73 $fieldConfiguration = [
74 'code' => $fieldHtml,
75 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageIndexing.storage',
76 'cshKey' => '_MOD_system_txschedulerM1',
77 'cshLabel' => $fieldId
78 ];
79 return $fieldConfiguration;
80 }
81
82 /**
83 * Returns a field configuration including an input field for the file count
84 *
85 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
86 * @return array Array containing all the information pertaining to the additional fields
87 */
88 protected function getFileCountField(FileStorageExtractionTask $task = null)
89 {
90 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_fileCount]';
91 $fieldId = 'scheduler_fileStorageIndexing_fileCount';
92 $fieldValue = $task !== null ? (int)$task->maxFileCount : 100;
93 $fieldHtml = '<input type="text" class="form-control" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '">';
94
95 $fieldConfiguration = [
96 'code' => $fieldHtml,
97 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.fileCount',
98 'cshKey' => '_MOD_system_txschedulerM1',
99 'cshLabel' => $fieldId
100 ];
101 return $fieldConfiguration;
102 }
103
104 /**
105 * Returns a field configuration telling about the status of registered extractors.
106 *
107 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
108 * @return array Array containing all the information pertaining to the additional fields
109 */
110 protected function getRegisteredExtractorsField(FileStorageExtractionTask $task = null)
111 {
112 $extractors = ExtractorRegistry::getInstance()->getExtractors();
113
114 if (empty($extractors)) {
115 $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.without_extractors';
116 $content = '<span class="label label-warning">'
117 . htmlspecialchars($this->getLanguageService()->sL($labelKey))
118 . '</span>';
119 } else {
120 // Assemble the extractor bullet list first.
121 $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.extractor';
122 $bullets = [];
123 foreach ($extractors as $extractor) {
124 $bullets[] = sprintf(
125 '<li title="%s">%s</li>',
126 get_class($extractor),
127 sprintf($this->getLanguageService()->sL($labelKey), $this->formatExtractorClassName($extractor), $extractor->getPriority())
128 );
129 }
130
131 // Finalize content assembling.
132 $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.with_extractors';
133 $title = $this->getLanguageService()->sL($labelKey);
134 $content = '<p>' . htmlspecialchars($title) . '</p>';
135 $content .= '<ul>' . implode(LF, $bullets) . '</ul>';
136 }
137
138 $fieldConfiguration = [
139 'code' => $content,
140 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors',
141 'cshKey' => '_MOD_system_txschedulerM1',
142 'cshLabel' => 'scheduler_fileStorageIndexing_registeredExtractors'
143 ];
144 return $fieldConfiguration;
145 }
146
147 /**
148 * Validate additional fields
149 *
150 * @param array $submittedData Reference to the array containing the data submitted by the user
151 * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
152 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
153 */
154 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $parentObject)
155 {
156 if (
157 !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_storage'])
158 || !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_fileCount'])
159 ) {
160 return false;
161 }
162 if (ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) === null) {
163 return false;
164 }
165 if (!MathUtility::isIntegerInRange($submittedData['scheduler_fileStorageIndexing_fileCount'], 1, 9999)) {
166 return false;
167 }
168 return true;
169 }
170
171 /**
172 * Save additional field in task
173 *
174 * @param array $submittedData Contains data submitted by the user
175 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
176 * @throws \InvalidArgumentException
177 */
178 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
179 {
180 if ($task !== null && !$task instanceof FileStorageExtractionTask) {
181 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275698);
182 }
183 $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
184 $task->maxFileCount = (int)$submittedData['scheduler_fileStorageIndexing_fileCount'];
185 }
186
187 /**
188 * Since the class name can be very long considering the namespace, only take the final
189 * part for better readability. The FQN of the class will be displayed as tooltip.
190 *
191 * @param ExtractorInterface $extractor
192 * @return string
193 */
194 protected function formatExtractorClassName(ExtractorInterface $extractor)
195 {
196 $extractorParts = explode('\\', get_class($extractor));
197 return array_pop($extractorParts);
198 }
199
200 /**
201 * @return \TYPO3\CMS\Core\Localization\LanguageService
202 */
203 protected function getLanguageService()
204 {
205 return $GLOBALS['LANG'];
206 }
207 }