FileStorageIndexingAdditionalFieldProvider.php 5.13 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
use TYPO3\CMS\Core\Resource\StorageRepository;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
22
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
23

24
25
/**
 * Additional BE fields for tasks which indexes files in a storage
26
 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
27
 */
28
class FileStorageIndexingAdditionalFieldProvider implements AdditionalFieldProviderInterface
29
30
31
32
33
{
    /**
     * Add additional fields
     *
     * @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
36
37
38
     * @param \TYPO3\CMS\Scheduler\Controller\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
     */
39
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $parentObject)
40
41
42
43
    {
        if ($task !== null && !$task instanceof FileStorageIndexingTask) {
            throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275696);
        }
44
        $additionalFields = [];
45
        $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task, $taskInfo);
46
47
        return $additionalFields;
    }
48

49
50
51
52
    /**
     * Add a select field of available storages.
     *
     * @param FileStorageIndexingTask $task When editing, reference to the current task object. NULL when adding.
53
     * @param array $taskInfo
54
55
     * @return array Array containing all the information pertaining to the additional fields
     */
56
    protected function getAllStoragesField(FileStorageIndexingTask $task = null, $taskInfo)
57
58
    {
        /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
59
        $storages = GeneralUtility::makeInstance(StorageRepository::class)->findAll();
60
        $options = [];
61
        foreach ($storages as $storage) {
62
63
            $selected = '';
            if ($task !== null && $task->storageUid === $storage->getUid()) {
Christian Kuhn's avatar
Christian Kuhn committed
64
                $selected = ' selected="selected"';
65
            } elseif ((int)$taskInfo['scheduler_fileStorageIndexing_storage'] === $storage->getUid()) {
Christian Kuhn's avatar
Christian Kuhn committed
66
                $selected = ' selected="selected"';
67
            }
68
            $options[] = '<option value="' . $storage->getUid() . '" ' . $selected . ' >' . $storage->getName() . '</option>';
69
        }
70

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

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

84
85
86
87
88
89
90
    /**
     * Validate additional fields
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
     * @param \TYPO3\CMS\Scheduler\Controller\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
     */
91
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $parentObject)
92
93
    {
        $value = $submittedData['scheduler_fileStorageIndexing_storage'];
94
        if (!MathUtility::canBeInterpretedAsInteger($value)) {
95
            return false;
96
        }
97
        if (GeneralUtility::makeInstance(StorageRepository::class)->findByUid($submittedData['scheduler_fileStorageIndexing_storage']) !== null) {
98
99
100
101
            return true;
        }
        return false;
    }
102

103
104
105
106
107
108
109
    /**
     * Save additional field in task
     *
     * @param array $submittedData Contains data submitted by the user
     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
     * @throws \InvalidArgumentException
     */
110
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
111
112
113
114
115
116
    {
        if (!$task instanceof FileStorageIndexingTask) {
            throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275697);
        }
        $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
    }
117
}