1 | <?php |
||
2 | namespace ApacheSolrForTypo3\Solr\Backend; |
||
3 | |||
4 | /*************************************************************** |
||
5 | * Copyright notice |
||
6 | * |
||
7 | * (c) 2013-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\Domain\Site\Site; |
||
28 | use TYPO3\CMS\Backend\Form\FormResultCompiler; |
||
29 | use TYPO3\CMS\Backend\Form\NodeFactory; |
||
30 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
31 | |||
32 | /** |
||
33 | * Index Queue indexing configuration selector form field. |
||
34 | * |
||
35 | * @author Ingo Renner <[email protected]> |
||
36 | */ |
||
37 | class IndexingConfigurationSelectorField |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * Site used to determine indexing configurations |
||
42 | * |
||
43 | * @var Site |
||
44 | */ |
||
45 | protected $site; |
||
46 | |||
47 | /** |
||
48 | * Form element name |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $formElementName = 'tx_solr-index-queue-indexing-configuration-selector'; |
||
53 | |||
54 | /** |
||
55 | * Selected values |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $selectedValues = []; |
||
60 | |||
61 | /** |
||
62 | * Constructor |
||
63 | * |
||
64 | * @param Site $site The site to use to determine indexing configurations |
||
65 | */ |
||
66 | public function __construct(Site $site = null) |
||
67 | { |
||
68 | $this->site = $site; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Sets the form element name. |
||
73 | * |
||
74 | * @param string $formElementName Form element name |
||
75 | */ |
||
76 | public function setFormElementName($formElementName) |
||
77 | { |
||
78 | $this->formElementName = $formElementName; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Gets the form element name. |
||
83 | * |
||
84 | * @return string form element name |
||
85 | */ |
||
86 | public function getFormElementName() |
||
87 | { |
||
88 | return $this->formElementName; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Sets the selected values. |
||
93 | * |
||
94 | * @param array $selectedValues |
||
95 | */ |
||
96 | public function setSelectedValues(array $selectedValues) |
||
97 | { |
||
98 | $this->selectedValues = $selectedValues; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Gets the selected values. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function getSelectedValues() |
||
107 | { |
||
108 | return $this->selectedValues; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Renders a field to select which indexing configurations to initialize. |
||
113 | * |
||
114 | * Uses \TYPO3\CMS\Backend\Form\FormEngine. |
||
115 | * |
||
116 | * @return string Markup for the select field |
||
117 | */ |
||
118 | public function render() |
||
119 | { |
||
120 | // transform selected values into the format used by TCEforms |
||
121 | $selectedValues = $this->selectedValues; |
||
122 | $tablesToIndex = $this->getIndexQueueConfigurationTableMap(); |
||
123 | |||
124 | $formField = $this->renderSelectCheckbox($this->buildSelectorItems($tablesToIndex), $selectedValues); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
125 | |||
126 | // need to wrap the field in a TCEforms table to make the CSS apply |
||
127 | $form[] = '<div class="typo3-TCEforms tx_solr-TCEforms">'; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
128 | $form[] = $formField; |
||
129 | $form[] = '</div>'; |
||
130 | |||
131 | return implode(LF, $form); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Builds a map of indexing configuration names to tables to to index. |
||
136 | * |
||
137 | * @return array Indexing configuration to database table map |
||
138 | */ |
||
139 | protected function getIndexQueueConfigurationTableMap() |
||
140 | { |
||
141 | $indexingTableMap = []; |
||
142 | |||
143 | $solrConfiguration = $this->site->getSolrConfiguration(); |
||
144 | $configurationNames = $solrConfiguration->getEnabledIndexQueueConfigurationNames(); |
||
145 | foreach ($configurationNames as $configurationName) { |
||
146 | $indexingTableMap[$configurationName] = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($configurationName); |
||
147 | } |
||
148 | |||
149 | return $indexingTableMap; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Builds the items to render in the TCEforms select field. |
||
154 | * |
||
155 | * @param array $tablesToIndex A map of indexing configuration to database tables |
||
156 | * |
||
157 | * @return array Selectable items for the TCEforms select field |
||
158 | */ |
||
159 | protected function buildSelectorItems(array $tablesToIndex) |
||
160 | { |
||
161 | $selectorItems = []; |
||
162 | |||
163 | foreach ($tablesToIndex as $configurationName => $tableName) { |
||
164 | $icon = 'tcarecords-' . $tableName . '-default'; |
||
165 | if ($tableName === 'pages') { |
||
166 | $icon = 'apps-pagetree-page-default'; |
||
167 | } |
||
168 | |||
169 | $labelTableName = ''; |
||
170 | if ($configurationName != $tableName) { |
||
171 | $labelTableName = ' (' . $tableName . ')'; |
||
172 | } |
||
173 | |||
174 | $selectorItems[] = [$configurationName . $labelTableName, $configurationName, $icon]; |
||
175 | } |
||
176 | |||
177 | return $selectorItems; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param array $items |
||
182 | * @param string $selectedValues |
||
183 | * |
||
184 | * @return string |
||
185 | * @throws \TYPO3\CMS\Backend\Form\Exception |
||
186 | */ |
||
187 | protected function renderSelectCheckbox($items, $selectedValues) |
||
188 | { |
||
189 | $parameterArray = [ |
||
190 | 'fieldChangeFunc' => [], |
||
191 | 'itemFormElName' => $this->formElementName, |
||
192 | 'itemFormElValue' => $selectedValues, |
||
193 | 'fieldConf' => ['config' => ['items' => $items]], |
||
194 | 'fieldTSConfig' => ['noMatchingValue_label' => ''] |
||
195 | ]; |
||
196 | |||
197 | $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class); |
||
198 | $options = [ |
||
199 | 'renderType' => 'selectCheckBox', 'table' => 'tx_solr_classes_backend_indexingconfigurationselector', |
||
200 | 'fieldName' => 'additionalFields', 'databaseRow' => [], 'parameterArray' => $parameterArray |
||
201 | ]; |
||
202 | $options['parameterArray']['fieldConf']['config']['items'] = $items; |
||
203 | $options['parameterArray']['fieldTSConfig']['noMatchingValue_label'] = ''; |
||
204 | |||
205 | $selectCheckboxResult = $nodeFactory->create($options)->render(); |
||
206 | $formResultCompiler = GeneralUtility::makeInstance(FormResultCompiler::class); |
||
207 | $formResultCompiler->mergeResult($selectCheckboxResult); |
||
208 | |||
209 | $formHtml = isset($selectCheckboxResult['html']) ? $selectCheckboxResult['html'] : ''; |
||
210 | $content = $formResultCompiler->addCssFiles() . $formHtml . $formResultCompiler->printNeededJSFunctions(); |
||
211 | |||
212 | return $content; |
||
213 | } |
||
214 | } |
||
215 |