Passed
Push — master ( 3e6704...db24af )
by Rafael
43:26 queued 10s
created

CoreSelectorField::renderSelectCheckbox()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 24
rs 9.7333
cc 2
nc 2
nop 2
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Backend;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
19
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
20
use TYPO3\CMS\Backend\Form\Exception;
21
use TYPO3\CMS\Backend\Form\FormResultCompiler;
22
use TYPO3\CMS\Backend\Form\NodeFactory;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Core selector form field.
27
 *
28
 * @author Jens Jacobsen <[email protected]>
29
 */
30
class CoreSelectorField
31
{
32
    /**
33
     * Site used to determine cores
34
     *
35
     * @var Site
36
     */
37
    protected $site;
38
39
    /**
40
     * Form element name
41
     *
42
     * @var string
43
     */
44
    protected $formElementName = 'tx_solr-index-optimize-core-selector';
45
46
    /**
47
     * Selected values
48
     *
49
     * @var array
50
     */
51
    protected $selectedValues = [];
52
53
    /**
54
     * Constructor
55
     *
56
     * @param Site|null $site The site to use to determine cores
57
     */
58
    public function __construct(Site $site = null)
59
    {
60
        $this->site = $site;
61
    }
62
63
    /**
64
     * Sets the form element name.
65
     *
66
     * @param string $formElementName Form element name
67
     */
68
    public function setFormElementName(string $formElementName)
69
    {
70
        $this->formElementName = $formElementName;
71
    }
72
73
    /**
74
     * Gets the form element name.
75
     *
76
     * @return string form element name
77
     */
78
    public function getFormElementName(): string
79
    {
80
        return $this->formElementName;
81
    }
82
83
    /**
84
     * Sets the selected values.
85
     *
86
     * @param array $selectedValues
87
     */
88
    public function setSelectedValues(array $selectedValues)
89
    {
90
        $this->selectedValues = $selectedValues;
91
    }
92
93
    /**
94
     * Gets the selected values.
95
     *
96
     * @return array
97
     */
98
    public function getSelectedValues(): array
99
    {
100
        return $this->selectedValues;
101
    }
102
103
    /**
104
     * Renders a field to select which cores to optimize.
105
     *
106
     * Uses \TYPO3\CMS\Backend\Form\FormEngine.
107
     *
108
     * @return string Markup for the select field
109
     * @throws Exception
110
     * @throws NoSolrConnectionFoundException
111
     */
112
    public function render(): string
113
    {
114
        // transform selected values into the format used by TCEforms
115
        $selectedValues = $this->selectedValues;
116
        $cores = $this->getLanguageUidCoreMap();
117
118
        $formField = $this->renderSelectCheckbox($this->buildSelectorItems($cores), $selectedValues);
119
120
        // need to wrap the field in a TCEforms table to make the CSS apply
121
        $form = [];
122
        $form[] = '<div class="typo3-TCEforms tx_solr-TCEforms">';
123
        $form[] = $formField;
124
        $form[] = '</div>';
125
126
        return implode(LF, $form);
127
    }
128
129
    /**
130
     * Builds a map of language uids to corepaths to optimize.
131
     *
132
     * @return array language uids to core paths map
133
     * @throws NoSolrConnectionFoundException
134
     */
135
    protected function getLanguageUidCoreMap(): array
136
    {
137
        $coreTableMap = [];
138
        $cores = $this->site->getAllSolrConnectionConfigurations();
139
        foreach ($cores as $languageUid => $core) {
140
            $corePath = $core['write']['path'];
141
            $coreTableMap[$languageUid] = $corePath;
142
        }
143
        return $coreTableMap;
144
    }
145
146
    /**
147
     * Builds the items to render in the TCEforms select field.
148
     *
149
     * @param array $coresToOptimize A map of indexing configuration to database tables
150
     *
151
     * @return array Selectable items for the TCEforms select field
152
     */
153
    protected function buildSelectorItems(array $coresToOptimize): array
154
    {
155
        $selectorItems = [];
156
157
        foreach ($coresToOptimize as $corePath) {
158
            $icon = 'module-searchbackend_SolrCoreoptimization';
159
            $corePath = rtrim($corePath, '/');
160
            $selectorItems[] = [$corePath, $corePath, $icon];
161
        }
162
163
        return $selectorItems;
164
    }
165
166
    /**
167
     * @param array $items
168
     * @param array $selectedValues
169
     *
170
     * @return string
171
     * @throws Exception
172
     */
173
    protected function renderSelectCheckbox(array $items, array $selectedValues): string
174
    {
175
        $parameterArray = [
176
            'fieldChangeFunc' => [],
177
            'itemFormElName' => $this->formElementName,
178
            'itemFormElValue' => $selectedValues,
179
            'fieldConf' => ['config' => ['items' => $items]],
180
            'fieldTSConfig' => ['noMatchingValue_label' => '']
181
        ];
182
183
        $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
184
        $options = [
185
            'renderType' => 'selectCheckBox', 'table' => 'tx_solr_classes_backend_coreselector',
186
            'fieldName' => 'additionalFields', 'databaseRow' => [], 'parameterArray' => $parameterArray
187
        ];
188
189
        $selectCheckboxResult = $nodeFactory->create($options)->render();
190
        $formResultCompiler = GeneralUtility::makeInstance(FormResultCompiler::class);
191
        $formResultCompiler->mergeResult($selectCheckboxResult);
192
193
        $formHtml = isset($selectCheckboxResult['html']) ? $selectCheckboxResult['html'] : '';
194
        $content = $formResultCompiler->addCssFiles() . $formHtml . $formResultCompiler->printNeededJSFunctions();
195
196
        return $content;
197
    }
198
}
199