1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2010-2017 dkd Internet Service GmbH <[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 2 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\SiteRepository; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Site; |
29
|
|
|
use ApacheSolrForTypo3\Solr\SolrService as SolrCoreConnection; |
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Mvc\Backend\Component\Exception\InvalidViewObjectNameException; |
31
|
|
|
use ApacheSolrForTypo3\Solr\Utility\StringUtility; |
32
|
|
|
use TYPO3\CMS\Backend\Template\Components\Menu\Menu; |
33
|
|
|
use TYPO3\CMS\Backend\Utility\BackendUtility; |
34
|
|
|
use TYPO3\CMS\Backend\View\BackendTemplateView; |
35
|
|
|
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; |
36
|
|
|
use TYPO3\CMS\Core\Messaging\AbstractMessage; |
37
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
38
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; |
39
|
|
|
use TYPO3\CMS\Extbase\Mvc\View\NotFoundView; |
40
|
|
|
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; |
41
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Abstract Module |
45
|
|
|
* |
46
|
|
|
* @property BackendTemplateView $view |
47
|
|
|
*/ |
48
|
|
|
abstract class AbstractModuleController extends ActionController |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Backend Template Container |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $defaultViewObjectName = BackendTemplateView::class; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \ApacheSolrForTypo3\Solr\Utility\StringUtility |
59
|
|
|
*/ |
60
|
|
|
protected $stringUtility; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* In the pagetree selected page UID |
64
|
|
|
* |
65
|
|
|
* @var int |
66
|
|
|
*/ |
67
|
|
|
protected $selectedPageUID; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var SiteRepository |
71
|
|
|
*/ |
72
|
|
|
protected $siteRepository; |
73
|
|
|
/** |
74
|
|
|
* @var Site |
75
|
|
|
*/ |
76
|
|
|
protected $selectedSite; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var SolrCoreConnection |
80
|
|
|
*/ |
81
|
|
|
protected $selectedSolrCoreConnection; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var Menu |
85
|
|
|
*/ |
86
|
|
|
protected $coreSelectorMenu = null; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var \ApacheSolrForTypo3\Solr\ConnectionManager |
90
|
|
|
* @inject |
91
|
|
|
*/ |
92
|
|
|
protected $solrConnectionManager = null; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var \ApacheSolrForTypo3\Solr\System\Mvc\Backend\Service\ModuleDataStorageService |
96
|
|
|
* @inject |
97
|
|
|
*/ |
98
|
|
|
protected $moduleDataStorageService = null; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Method to pass a StringUtil object. |
102
|
|
|
* Use to overwrite injected object in unit test context. |
103
|
|
|
* |
104
|
|
|
* @param \ApacheSolrForTypo3\Solr\Utility\StringUtility $stringUtility |
105
|
|
|
*/ |
106
|
|
|
public function injectStringHelper(StringUtility $stringUtility) |
107
|
|
|
{ |
108
|
|
|
$this->stringUtility = $stringUtility; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Initializes the controller and sets needed vars. |
113
|
|
|
*/ |
114
|
|
|
protected function initializeAction() |
115
|
|
|
{ |
116
|
|
|
parent::initializeAction(); |
117
|
|
|
$this->selectedPageUID = (int)GeneralUtility::_GP('id'); |
118
|
|
|
if ($this->request->hasArgument('id')) { |
119
|
|
|
$this->selectedPageUID = (int)$this->request->getArgument('id'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($this->selectedPageUID < 1) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->siteRepository = $this->objectManager->get(SiteRepository::class); |
127
|
|
|
$this->selectedSite = $this->siteRepository->getSiteByPageId($this->selectedPageUID); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set up the doc header properly here |
132
|
|
|
* |
133
|
|
|
* @param ViewInterface $view |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function initializeView(ViewInterface $view) |
137
|
|
|
{ |
138
|
|
|
parent::initializeView($view); |
139
|
|
|
if ($view instanceof NotFoundView || $this->selectedPageUID < 1) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
/* @var BackendUserAuthentication $beUser */ |
143
|
|
|
$beUser = $GLOBALS['BE_USER']; |
144
|
|
|
$permissionClause = $beUser->getPagePermsClause(1); |
145
|
|
|
$pageRecord = BackendUtility::readPageAccess($this->selectedSite->getRootPageId(), $permissionClause); |
146
|
|
|
|
147
|
|
|
if (false === $pageRecord) { |
148
|
|
|
throw new \InvalidArgumentException(vsprintf('There is something wrong with permissions for page "%s" for backend user "%s".', [$this->selectedSite->getRootPageId(), $beUser->user['username']]), 1496146317); |
149
|
|
|
} |
150
|
|
|
$this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($pageRecord); |
151
|
|
|
|
152
|
|
|
$this->view->getModuleTemplate()->addJavaScriptCode('mainJsFunctions', ' |
153
|
|
|
top.fsMod.recentIds["searchbackend"] = ' . (int)$this->selectedPageUID . ';' |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Generates selector menu in backends doc header using selected page from page tree. |
159
|
|
|
* |
160
|
|
|
* @param string|null $uriToRedirectTo |
161
|
|
|
*/ |
162
|
|
|
public function generateCoreSelectorMenuUsingPageTree(string $uriToRedirectTo = null) |
163
|
|
|
{ |
164
|
|
|
if ($this->selectedPageUID < 1) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($this->view instanceof NotFoundView) { |
169
|
|
|
$this->initializeSelectedSolrCoreConnection(); |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->generateCoreSelectorMenu($this->selectedSite, $uriToRedirectTo); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Generates Core selector Menu for given Site. |
178
|
|
|
* |
179
|
|
|
* @param Site $site |
180
|
|
|
* @param string|null $uriToRedirectTo |
181
|
|
|
* @throws InvalidViewObjectNameException |
182
|
|
|
*/ |
183
|
|
|
protected function generateCoreSelectorMenu(Site $site, string $uriToRedirectTo = null) |
184
|
|
|
{ |
185
|
|
|
if (!$this->view instanceof BackendTemplateView) { |
186
|
|
|
throw new InvalidViewObjectNameException(vsprintf( |
187
|
|
|
'The controller "%s" must use BackendTemplateView to be able to generate menu for backends docheader. \ |
188
|
|
|
Please set `protected $defaultViewObjectName = BackendTemplateView::class;` field in your controller.', |
189
|
|
|
[static::class]), 1493804179); |
190
|
|
|
} |
191
|
|
|
$this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue()); |
192
|
|
|
|
193
|
|
|
$this->coreSelectorMenu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu(); |
194
|
|
|
$this->coreSelectorMenu->setIdentifier('component_core_selector_menu'); |
195
|
|
|
|
196
|
|
|
if (!isset($uriToRedirectTo)) { |
197
|
|
|
$uriToRedirectTo = $this->uriBuilder->reset()->uriFor(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->initializeSelectedSolrCoreConnection(); |
201
|
|
|
$cores = $this->solrConnectionManager->getConnectionsBySite($site); |
202
|
|
|
foreach ($cores as $core) { |
203
|
|
|
$menuItem = $this->coreSelectorMenu->makeMenuItem(); |
204
|
|
|
$menuItem->setTitle($core->getPath()); |
205
|
|
|
$uri = $this->uriBuilder->reset()->uriFor('switchCore', |
206
|
|
|
[ |
207
|
|
|
'corePath' => $core->getPath(), |
208
|
|
|
'uriToRedirectTo' => $uriToRedirectTo |
209
|
|
|
] |
210
|
|
|
); |
211
|
|
|
$menuItem->setHref($uri); |
212
|
|
|
|
213
|
|
|
if ($core->getPath() == $this->selectedSolrCoreConnection->getPath()) { |
214
|
|
|
$menuItem->setActive(true); |
215
|
|
|
} |
216
|
|
|
$this->coreSelectorMenu->addMenuItem($menuItem); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($this->coreSelectorMenu); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Switches used core. |
224
|
|
|
* |
225
|
|
|
* Note: Does not check availability of core in site. All this stuff is done in the generation step. |
226
|
|
|
* |
227
|
|
|
* @param string $corePath |
228
|
|
|
* @param string $uriToRedirectTo |
229
|
|
|
*/ |
230
|
|
|
public function switchCoreAction(string $corePath, string $uriToRedirectTo) |
231
|
|
|
{ |
232
|
|
|
$moduleData = $this->moduleDataStorageService->loadModuleData(); |
233
|
|
|
$moduleData->setCore($corePath); |
234
|
|
|
|
235
|
|
|
$this->moduleDataStorageService->persistModuleData($moduleData); |
236
|
|
|
$message = LocalizationUtility::translate('coreselector_switched_successfully', 'solr', [$corePath]); |
237
|
|
|
$this->addFlashMessage($message); |
238
|
|
|
$this->redirectToUri($uriToRedirectTo); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Initializes the solr core connection considerately to the components state. |
243
|
|
|
* Uses and persists default core connection if persisted core in Site does not exist. |
244
|
|
|
* |
245
|
|
|
*/ |
246
|
|
|
private function initializeSelectedSolrCoreConnection() |
247
|
|
|
{ |
248
|
|
|
$moduleData = $this->moduleDataStorageService->loadModuleData(); |
249
|
|
|
|
250
|
|
|
$solrCoreConnections = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite); |
251
|
|
|
$currentSolrCorePath = $moduleData->getCore(); |
252
|
|
|
if (empty($currentSolrCorePath)) { |
253
|
|
|
$this->initializeFirstAvailableSolrCoreConnection($solrCoreConnections, $moduleData); |
254
|
|
|
} |
255
|
|
|
foreach ($solrCoreConnections as $solrCoreConnection) { |
256
|
|
|
if ($solrCoreConnection->getPath() == $currentSolrCorePath) { |
257
|
|
|
$this->selectedSolrCoreConnection = $solrCoreConnection; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
if (!$this->selectedSolrCoreConnection instanceof SolrCoreConnection && count($solrCoreConnections) > 0) { |
261
|
|
|
$this->initializeFirstAvailableSolrCoreConnection($solrCoreConnections, $moduleData); |
262
|
|
|
$message = LocalizationUtility::translate('coreselector_switched_to_default_core', 'solr', [$currentSolrCorePath, $this->selectedSite->getLabel(), $this->selectedSolrCoreConnection->getPath()]); |
263
|
|
|
$this->addFlashMessage($message, '', AbstractMessage::NOTICE); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param SolrCoreConnection[] $solrCoreConnections |
269
|
|
|
*/ |
270
|
|
|
private function initializeFirstAvailableSolrCoreConnection(array $solrCoreConnections, $moduleData) |
271
|
|
|
{ |
272
|
|
|
$this->selectedSolrCoreConnection = $solrCoreConnections[0]; |
273
|
|
|
$moduleData->setCore($this->selectedSolrCoreConnection->getPath()); |
274
|
|
|
$this->moduleDataStorageService->persistModuleData($moduleData); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|