Issues (202)

IndexQueueWorkerTaskAdditionalFieldProvider.php (1 issue)

Labels
Severity
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Backend\SiteSelectorField;
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
31
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
32
use TYPO3\CMS\Scheduler\Task\AbstractTask;
33
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
34
35
/**
36
 * Additional field provider for the index queue worker task
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class IndexQueueWorkerTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
41
{
42
43
    /**
44
     * SiteRepository
45
     *
46
     * @var SiteRepository
47
     */
48
    protected $siteRepository;
49
50
    public function __construct()
51
    {
52
        $this->siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
53
    }
54
55
    /**
56
     * Used to define fields to provide the TYPO3 site to index and number of
57
     * items to index per run when adding or editing a task.
58
     *
59
     * @param array $taskInfo reference to the array containing the info used in the add/edit form
60
     * @param AbstractTask $task when editing, reference to the current task object. Null when adding.
61
     * @param SchedulerModuleController $schedulerModule : reference to the calling object (Scheduler's BE module)
62
     * @return array Array containing all the information pertaining to the additional fields
63
     *                    The array is multidimensional, keyed to the task class name and each field's id
64
     *                    For each field it provides an associative sub-array with the following:
65
     */
66
    public function getAdditionalFields(
67
        array &$taskInfo,
68
        $task,
69
        SchedulerModuleController $schedulerModule
70
    ) {
71
        /** @var $task IndexQueueWorkerTask */
72
        $additionalFields = [];
73
        $siteSelectorField = GeneralUtility::makeInstance(SiteSelectorField::class);
74
75
        if (!$this->isTaskInstanceofIndexQueueWorkerTask($task)) {
76
            return $additionalFields;
77
        }
78
79
        $currentAction = $schedulerModule->getCurrentAction();
80
        if ($currentAction->equals(Action::ADD)) {
81
            $taskInfo['site'] = null;
82
            $taskInfo['documentsToIndexLimit'] = 50;
83
            $taskInfo['forcedWebRoot'] = '';
84
        }
85
86
        if ($currentAction->equals(Action::EDIT)) {
87
            $taskInfo['site'] = $this->siteRepository->getSiteByRootPageId($task->getRootPageId());
88
            $taskInfo['documentsToIndexLimit'] = $task->getDocumentsToIndexLimit();
89
            $taskInfo['forcedWebRoot'] = $task->getForcedWebRoot();
90
        }
91
92
        $additionalFields['site'] = [
93
            'code' => $siteSelectorField->getAvailableSitesSelector('tx_scheduler[site]',
94
                $taskInfo['site']),
95
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:field_site',
96
            'cshKey' => '',
97
            'cshLabel' => ''
98
        ];
99
100
        $additionalFields['documentsToIndexLimit'] = [
101
            'code' => '<input type="number" class="form-control" name="tx_scheduler[documentsToIndexLimit]" value="' . htmlspecialchars($taskInfo['documentsToIndexLimit']) . '" />',
102
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_documentsToIndexLimit',
103
            'cshKey' => '',
104
            'cshLabel' => ''
105
        ];
106
107
        $additionalFields['forcedWebRoot'] = [
108
            'code' => '<input type="text" class="form-control" name="tx_scheduler[forcedWebRoot]" value="' . htmlspecialchars($taskInfo['forcedWebRoot']) . '" />',
109
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_forcedWebRoot',
110
            'cshKey' => '',
111
            'cshLabel' => ''
112
        ];
113
114
        return $additionalFields;
115
    }
116
117
    /**
118
     * Checks any additional data that is relevant to this task. If the task
119
     * class is not relevant, the method is expected to return TRUE
120
     *
121
     * @param array $submittedData reference to the array containing the data submitted by the user
122
     * @param SchedulerModuleController $schedulerModule reference to the calling object (Scheduler's BE module)
123
     * @return bool True if validation was ok (or selected class is not relevant), FALSE otherwise
124
     */
125
    public function validateAdditionalFields(
126
        array &$submittedData,
127
        SchedulerModuleController $schedulerModule
128
    ) {
129
        $result = false;
130
131
        // validate site
132
        $sites = $this->siteRepository->getAvailableSites();
133
        if (array_key_exists($submittedData['site'], $sites)) {
134
            $result = true;
135
        }
136
137
        // escape limit
138
        $submittedData['documentsToIndexLimit'] = intval($submittedData['documentsToIndexLimit']);
139
140
        return $result;
141
    }
142
143
    /**
144
     * Saves any additional input into the current task object if the task
145
     * class matches.
146
     *
147
     * @param array $submittedData array containing the data submitted by the user
148
     * @param AbstractTask $task reference to the current task object
149
     */
150
    public function saveAdditionalFields(
151
        array $submittedData,
152
        AbstractTask $task
153
    ) {
154
        if (!$this->isTaskInstanceofIndexQueueWorkerTask($task)) {
155
            return;
156
        }
157
158
        $task->setRootPageId($submittedData['site']);
0 ignored issues
show
The method setRootPageId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        $task->/** @scrutinizer ignore-call */ 
159
               setRootPageId($submittedData['site']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
        $task->setDocumentsToIndexLimit($submittedData['documentsToIndexLimit']);
160
        $task->setForcedWebRoot($submittedData['forcedWebRoot']);
161
    }
162
163
    /**
164
     * Check that a task is an instance of IndexQueueWorkerTask
165
     *
166
     * @param AbstractTask $task
167
     * @return boolean
168
     * @throws \LogicException
169
     */
170
    protected function isTaskInstanceofIndexQueueWorkerTask($task)
171
    {
172
        if ((!is_null($task)) && (!($task instanceof IndexQueueWorkerTask))) {
173
            throw new \LogicException(
174
                '$task must be an instance of IndexQueueWorkerTask, '
175
                .'other instances are not supported.', 1487499814
176
            );
177
        }
178
        return true;
179
    }
180
}
181