Passed
Pull Request — master (#123)
by
unknown
05:01
created

ReindexAdditionalFieldProvider::getCollections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.9
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
namespace Kitodo\Dlf\Task;
13
14
use TYPO3\CMS\Core\Database\Connection;
15
use TYPO3\CMS\Core\Database\ConnectionPool;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
18
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
19
20
/**
21
 * Additional fields for reindex documents task.
22
 *
23
 * @package TYPO3
24
 * @subpackage dlf
25
 *
26
 * @access public
27
 */
28
class ReindexAdditionalFieldProvider extends BaseAdditionalFieldProvider
29
{
30
    /**
31
     * Gets additional fields to render in the form to add/edit a task
32
     *
33
     * @param array $taskInfo Values of the fields from the add/edit task form
34
     * @param BaseTask $task The task object being edited. Null when adding a task!
35
     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
36
     * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
37
     */
38
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
39
    {
40
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
41
42
        /** @var BaseTask $task */
43
        if ($currentSchedulerModuleAction->equals(Action::EDIT)) {
44
            $taskInfo['dryRun'] = $task->isDryRun();
45
            $taskInfo['coll'] = $task->getColl();
46
            $taskInfo['pid'] = $task->getPid();
47
            $taskInfo['solr'] = $task->getSolr();
48
            $taskInfo['owner'] = $task->getOwner();
49
            $taskInfo['all'] = $task->isAll();
50
        } else {
51
            $taskInfo['dryRun'] = false;
52
            $taskInfo['coll'] = [];
53
            $taskInfo['pid'] = - 1;
54
            $taskInfo['solr'] = - 1;
55
            $taskInfo['owner'] = '';
56
            $taskInfo['all'] = false;
57
        }
58
59
        $additionalFields = [];
60
61
        // Checkbox for dry-run
62
        $additionalFields['dryRun'] = $this->getDryRunField($taskInfo['dryRun']);
63
64
        // Select for collection(s)
65
        $fieldName = 'coll';
66
        $fieldId = 'task_' . $fieldName;
67
        $options = $this->getCollOptions($taskInfo['coll'], $taskInfo['pid']);
68
        ;
69
        $fieldHtml = '<select name="tx_scheduler[' . $fieldName . '][]" id="' . $fieldId . '" size="10" multiple="multiple">' . implode("\n", $options) . '</select>';
70
        $additionalFields[$fieldId] = [
71
            'code' => $fieldHtml,
72
            'label' => 'LLL:EXT:dlf/Resources/Private/Language/locallang_tasks.xlf:additionalFields.coll',
73
            'cshKey' => '_MOD_system_txschedulerM1',
74
            'cshLabel' => $fieldId
75
        ];
76
77
        // DropDown for storage page
78
        $additionalFields['pid'] = $this->getPidField($taskInfo['pid']);
79
80
        // DropDown for Solr core
81
        $additionalFields['solr'] = $this->getSolrField($taskInfo['solr'], $taskInfo['pid']);
82
83
        // Text field for owner
84
        $additionalFields['owner'] = $this->getOwnerField($taskInfo['owner']);
85
86
        // Checkbox for all
87
        $fieldName = 'all';
88
        $fieldId = 'task_' . $fieldName;
89
        $fieldHtml = '<input type="checkbox" name="tx_scheduler[' . $fieldName . ']" id="' . $fieldId . '" value="1"' .
90
            ($taskInfo['all'] ? ' checked="checked"' : '') . '>';
91
        $additionalFields[$fieldId] = [
92
            'code' => $fieldHtml,
93
            'label' => 'LLL:EXT:dlf/Resources/Private/Language/locallang_tasks.xlf:additionalFields.all',
94
            'cshKey' => '_MOD_system_txschedulerM1',
95
            'cshLabel' => $fieldId
96
        ];
97
98
        return $additionalFields;
99
    }
100
101
    /**
102
     * Generates HTML options for collections
103
     *
104
     * @param array $coll Selected collections
105
     * @param int $pid UID of storage page
106
     *
107
     * @return array HTML of selectbox options
108
     */
109
    private function getCollOptions(array $coll, int $pid): array
110
    {
111
        $options = [];
112
        $collections = $this->getCollections($pid);
113
        foreach ($collections as $label => $uid) {
114
            $options[] = '<option value="' . $uid . '" ' . (in_array($uid, $coll) ? 'selected' : '') . ' >' . $label . '</option>';
115
        }
116
        return $options;
117
    }
118
119
    /**
120
     * Fetches all collections on given storage page.
121
     *
122
     * @access protected
123
     *
124
     * @param int $pid The UID of the storage page
125
     *
126
     * @return array Array of collections
127
     */
128
    private function getCollections(int $pid): array
129
    {
130
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_collections');
131
132
        $collections = [];
133
        $result = $queryBuilder->select('uid', 'label')
134
            ->from('tx_dlf_collections')
135
            ->where(
136
                $queryBuilder->expr()
137
                    ->eq('pid', $queryBuilder->createNamedParameter((int) $pid, Connection::PARAM_INT))
138
            )
139
            ->execute();
140
141
        while ($record = $result->fetchAssociative()) {
142
            $collections[$record['label']] = $record['uid'];
143
        }
144
145
        return $collections;
146
    }
147
}
148