FileStorageExtractionAdditionalFieldProvider.php 9.52 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Scheduler\Task;

18
19
use TYPO3\CMS\Core\Resource\Index\ExtractorInterface;
use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry;
20
use TYPO3\CMS\Core\Resource\StorageRepository;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Core\Utility\MathUtility;
23
24
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
25
26
27

/**
 * Additional BE fields for task which extracts metadata from storage
28
 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
29
 */
30
31
32
33
34
35
class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldProviderInterface
{
    /**
     * Add additional fields
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
36
     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
37
38
39
40
41
42
43
44
45
     * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
     * @return array Array containing all the information pertaining to the additional fields
     * @throws \InvalidArgumentException
     */
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $parentObject)
    {
        if ($task !== null && !$task instanceof FileStorageExtractionTask) {
            throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275695);
        }
46
        $additionalFields = [];
47
48
49
50
51
52
53
54
55
        $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task);
        $additionalFields['scheduler_fileStorageIndexing_fileCount'] = $this->getFileCountField($task);
        $additionalFields['scheduler_fileStorageIndexing_registeredExtractors'] = $this->getRegisteredExtractorsField($task);
        return $additionalFields;
    }

    /**
     * Returns a field configuration including a selectbox for available storages
     *
56
     * @param FileStorageExtractionTask|null $task When editing, reference to the current task object. NULL when adding.
57
58
59
60
61
     * @return array Array containing all the information pertaining to the additional fields
     */
    protected function getAllStoragesField(FileStorageExtractionTask $task = null)
    {
        /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
62
        $storages = GeneralUtility::makeInstance(StorageRepository::class)->findAll();
63
        $options = [];
64
65
66
67
68
69
70
71
72
73
        foreach ($storages as $storage) {
            if ($task !== null && $task->storageUid === $storage->getUid()) {
                $options[] = '<option value="' . $storage->getUid() . '" selected="selected">' . $storage->getName() . '</option>';
            } else {
                $options[] = '<option value="' . $storage->getUid() . '">' . $storage->getName() . '</option>';
            }
        }

        $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_storage]';
        $fieldId = 'scheduler_fileStorageIndexing_storage';
74
        $fieldHtml = '<select class="form-select" name="' . $fieldName . '" id="' . $fieldId . '">' . implode("\n", $options) . '</select>';
75

76
        $fieldConfiguration = [
77
78
79
80
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageIndexing.storage',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
81
        ];
82
83
84
85
86
87
        return $fieldConfiguration;
    }

    /**
     * Returns a field configuration including an input field for the file count
     *
88
     * @param FileStorageExtractionTask|null $task When editing, reference to the current task object. NULL when adding.
89
90
91
92
93
94
     * @return array Array containing all the information pertaining to the additional fields
     */
    protected function getFileCountField(FileStorageExtractionTask $task = null)
    {
        $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_fileCount]';
        $fieldId = 'scheduler_fileStorageIndexing_fileCount';
95
        $fieldValue = (string)($task !== null ? (int)$task->maxFileCount : 100);
96
97
        $fieldHtml = '<input type="text" class="form-control" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '">';

98
        $fieldConfiguration = [
99
100
101
102
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.fileCount',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
103
        ];
104
105
106
107
108
109
        return $fieldConfiguration;
    }

    /**
     * Returns a field configuration telling about the status of registered extractors.
     *
110
     * @param FileStorageExtractionTask|null $task When editing, reference to the current task object. NULL when adding.
111
112
113
114
115
116
117
118
     * @return array Array containing all the information pertaining to the additional fields
     */
    protected function getRegisteredExtractorsField(FileStorageExtractionTask $task = null)
    {
        $extractors = ExtractorRegistry::getInstance()->getExtractors();

        if (empty($extractors)) {
            $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.without_extractors';
119
120
121
            $content = '<span class="label label-warning">'
                . htmlspecialchars($this->getLanguageService()->sL($labelKey))
                . '</span>';
122
123
124
        } else {
            // Assemble the extractor bullet list first.
            $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.extractor';
125
            $bullets = [];
126
127
128
129
130
131
132
133
134
135
            foreach ($extractors as $extractor) {
                $bullets[] = sprintf(
                    '<li title="%s">%s</li>',
                    get_class($extractor),
                    sprintf($this->getLanguageService()->sL($labelKey), $this->formatExtractorClassName($extractor), $extractor->getPriority())
                );
            }

            // Finalize content assembling.
            $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.with_extractors';
136
137
138
            $title = $this->getLanguageService()->sL($labelKey);
            $content = '<p>' . htmlspecialchars($title) . '</p>';
            $content .= '<ul>' . implode(LF, $bullets) . '</ul>';
139
140
        }

141
        $fieldConfiguration = [
142
143
144
145
            'code' => $content,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => 'scheduler_fileStorageIndexing_registeredExtractors'
146
        ];
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        return $fieldConfiguration;
    }

    /**
     * Validate additional fields
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
     * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
     * @return bool True if validation was ok (or selected class is not relevant), false otherwise
     */
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $parentObject)
    {
        if (
            !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_storage'])
            || !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_fileCount'])
        ) {
            return false;
164
        }
165
        if (GeneralUtility::makeInstance(StorageRepository::class)->findByUid($submittedData['scheduler_fileStorageIndexing_storage']) === null) {
166
            return false;
167
168
        }
        if (!MathUtility::isIntegerInRange($submittedData['scheduler_fileStorageIndexing_fileCount'], 1, 9999)) {
169
170
171
172
173
174
175
176
177
            return false;
        }
        return true;
    }

    /**
     * Save additional field in task
     *
     * @param array $submittedData Contains data submitted by the user
178
     * @param AbstractTask $task Reference to the current task object
179
180
     * @throws \InvalidArgumentException
     */
181
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
182
    {
183
        if (!$task instanceof FileStorageExtractionTask) {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
            throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275698);
        }
        $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
        $task->maxFileCount = (int)$submittedData['scheduler_fileStorageIndexing_fileCount'];
    }

    /**
     * Since the class name can be very long considering the namespace, only take the final
     * part for better readability. The FQN of the class will be displayed as tooltip.
     *
     * @param ExtractorInterface $extractor
     * @return string
     */
    protected function formatExtractorClassName(ExtractorInterface $extractor)
    {
        $extractorParts = explode('\\', get_class($extractor));
        return array_pop($extractorParts);
    }

    /**
204
     * @return \TYPO3\CMS\Core\Localization\LanguageService
205
206
207
208
209
     */
    protected function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }
210
}