CachingFrameworkGarbageCollectionAdditionalFieldProvider.php 6.94 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\Cache\Backend\Typo3DatabaseBackend;
19
use TYPO3\CMS\Core\Localization\LanguageService;
20
21
22
23
24
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;

25
26
27
/**
 * Additional BE fields for caching framework garbage collection task.
 * Creates a multi selectbox with all available cache backends to select from.
28
 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
29
 */
30
class CachingFrameworkGarbageCollectionAdditionalFieldProvider extends AbstractAdditionalFieldProvider
31
32
33
34
35
{
    /**
     * Add a multi select box with all available cache backends.
     *
     * @param array $taskInfo Reference to the array containing the info used in the add/edit form
36
     * @param CachingFrameworkGarbageCollectionTask|null $task When editing, reference to the current task. NULL when adding.
37
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
38
39
     * @return array Array containing all the information pertaining to the additional fields
     */
40
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
41
    {
42
43
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();

44
45
        // Initialize selected fields
        if (empty($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
46
            $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = [];
47
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
48
                // In case of new task, set to dbBackend if it's available
49
50
                if (in_array(Typo3DatabaseBackend::class, $this->getRegisteredBackends())) {
                    $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'][] = Typo3DatabaseBackend::class;
51
                }
52
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
53
54
55
56
57
58
59
                // In case of editing the task, set to currently selected value
                $taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends'] = $task->selectedBackends;
            }
        }
        $fieldName = 'tx_scheduler[scheduler_cachingFrameworkGarbageCollection_selectedBackends][]';
        $fieldId = 'task_cachingFrameworkGarbageCollection_selectedBackends';
        $fieldOptions = $this->getCacheBackendOptions($taskInfo['scheduler_cachingFrameworkGarbageCollection_selectedBackends']);
60
        $fieldHtml = '<select class="form-select" name="' . $fieldName . '" id="' . $fieldId . '" class="from-control" size="10" multiple="multiple">' . $fieldOptions . '</select>';
61
        $additionalFields = [];
62
        $additionalFields[$fieldId] = [
63
64
65
66
            'code' => $fieldHtml,
            'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.cachingFrameworkGarbageCollection.selectBackends',
            'cshKey' => '_MOD_system_txschedulerM1',
            'cshLabel' => $fieldId
67
        ];
68
69
        return $additionalFields;
    }
70

71
72
73
74
    /**
     * Checks that all selected backends exist in available backend list
     *
     * @param array $submittedData Reference to the array containing the data submitted by the user
75
     * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
76
77
     * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
     */
78
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
79
80
81
82
83
84
    {
        $validData = true;
        $availableBackends = $this->getRegisteredBackends();
        if (is_array($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'])) {
            $invalidBackends = array_diff($submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'], $availableBackends);
            if (!empty($invalidBackends)) {
85
                $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.selectionOfNonExistingCacheBackends'), FlashMessage::ERROR);
86
87
88
                $validData = false;
            }
        } else {
89
            $this->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.noCacheBackendSelected'), FlashMessage::ERROR);
90
91
92
93
            $validData = false;
        }
        return $validData;
    }
94

95
96
97
98
    /**
     * Save selected backends in task object
     *
     * @param array $submittedData Contains data submitted by the user
99
     * @param CachingFrameworkGarbageCollectionTask $task Reference to the current task object
100
     */
101
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
102
103
104
    {
        $task->selectedBackends = $submittedData['scheduler_cachingFrameworkGarbageCollection_selectedBackends'];
    }
105

106
107
108
109
110
111
112
113
    /**
     * Build select options of available backends and set currently selected backends
     *
     * @param array $selectedBackends Selected backends
     * @return string HTML of selectbox options
     */
    protected function getCacheBackendOptions(array $selectedBackends)
    {
114
        $options = [];
115
116
117
118
119
120
121
122
123
124
125
        $availableBackends = $this->getRegisteredBackends();
        foreach ($availableBackends as $backendName) {
            if (in_array($backendName, $selectedBackends)) {
                $selected = ' selected="selected"';
            } else {
                $selected = '';
            }
            $options[] = '<option value="' . $backendName . '"' . $selected . '>' . $backendName . '</option>';
        }
        return implode('', $options);
    }
126

127
128
129
130
131
132
133
    /**
     * Get all registered caching framework backends
     *
     * @return array Registered backends
     */
    protected function getRegisteredBackends()
    {
134
        $backends = [];
135
136
137
138
139
140
141
142
143
144
145
        $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
        if (is_array($cacheConfigurations)) {
            foreach ($cacheConfigurations as $cacheConfiguration) {
                $backend = $cacheConfiguration['backend'];
                if (!in_array($backend, $backends)) {
                    $backends[] = $backend;
                }
            }
        }
        return $backends;
    }
146
147
148
149
150
151
152
153

    /**
     * @return LanguageService|null
     */
    protected function getLanguageService(): ?LanguageService
    {
        return $GLOBALS['LANG'] ?? null;
    }
154
}