Passed
Pull Request — master (#1151)
by
unknown
19:19
created

AdministrationController::getSiteRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller;
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 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\Util;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
32
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
33
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
34
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
35
use TYPO3\CMS\Extbase\Mvc\ResponseInterface;
36
use TYPO3\CMS\Extbase\Mvc\Web\Request;
37
use TYPO3\CMS\Extbase\Mvc\Web\Response;
38
39
/**
40
 * Administration module controller
41
 *
42
 * @author Ingo Renner <[email protected]>
43
 */
44
class AdministrationController extends ActionController
45
{
46
47
    /**
48
     * Persistent module data
49
     *
50
     * @var \ApacheSolrForTypo3\Solr\Domain\Model\ModuleData
51
     */
52
    protected $moduleData = null;
53
54
    /**
55
     * @var \ApacheSolrForTypo3\Solr\Service\ModuleDataStorageService
56
     * @inject
57
     */
58
    protected $moduleDataStorageService;
59
60
    /**
61
     * Administration Module Manager
62
     *
63
     * @var \ApacheSolrForTypo3\Solr\Backend\SolrModule\AdministrationModuleManager
64
     * @inject
65
     */
66
    protected $moduleManager = null;
67
68
    /**
69
     * Modules
70
     *
71
     * @var array
72
     */
73
    protected $modules = [];
74
75
    /**
76
     * Name of the currently active module
77
     *
78
     * @var string
79
     */
80
    protected $activeModuleName = 'Overview';
81
82
    /**
83
     * Currently active module
84
     *
85
     * @var null|\ApacheSolrForTypo3\Solr\Backend\SolrModule\AdministrationModuleInterface|ActionController
86
     */
87
    protected $activeModule = null;
88
89
    /**
90
     * The site to work with
91
     *
92
     * @var Site
93
     */
94
    protected $site;
95
96
    /**
97
     * Loads and persists module data
98
     *
99
     * @param RequestInterface $request
100
     * @param ResponseInterface $response
101
     * @throws \Exception|StopActionException
102
     * @return void
103
     */
104
    public function processRequest(
105
        RequestInterface $request,
106
        ResponseInterface $response
107
    ) {
108
        $this->moduleData = $this->moduleDataStorageService->loadModuleData();
109
110
        try {
111
            parent::processRequest($request, $response);
112
            $this->moduleDataStorageService->persistModuleData($this->moduleData);
113
        } catch (StopActionException $e) {
114
            $this->moduleDataStorageService->persistModuleData($this->moduleData);
115
            throw $e;
116
        }
117
    }
118
119
    /**
120
     * Initializes the controller before invoking an action method.
121
     *
122
     * @return void
123
     */
124
    protected function initializeAction()
125
    {
126
        if ($this->request->getControllerActionName() == 'noSiteAvailable') {
127
            return;
128
        }
129
130
        $this->resolveSite();
131
132
        if ($this->site === null) {
133
            // we could not set the site
134
            $this->forwardToNonModuleAction('noSiteAvailable');
135
        }
136
137
        try {
138
            $moduleName = $this->request->getArgument('module');
139
            if ($this->moduleManager->isRegisteredModule($moduleName)) {
140
                $this->activeModuleName = $moduleName;
141
                $this->activeModule = $this->moduleManager->getModule($moduleName);
142
            }
143
        } catch (NoSuchArgumentException $e) {
144
            $this->activeModule = $this->moduleManager->getModule($this->activeModuleName);
145
        }
146
147
        $this->moduleManager->sortModules();
148
        $this->modules = $this->moduleManager->getModules();
149
    }
150
151
    /**
152
     * Index Action / Overview
153
     *
154
     * @return void
155
     */
156
    public function indexAction()
157
    {
158
        $this->view->assign('activeModule', $this->activeModule);
159
        $this->view->assign('modules', $this->modules);
160
        $this->view->assign('site', $this->site);
161
162
        $this->invokeModuleController();
163
    }
164
165
    /**
166
     * No site available
167
     *
168
     * @return void
169
     */
170
    public function noSiteAvailableAction()
171
    {
172
    }
173
174
    /**
175
     * Call a sub-module's controller
176
     *
177
     */
178
    protected function invokeModuleController()
179
    {
180
        $activeModuleDescription = $this->moduleManager->getModuleDescription($this->activeModuleName);
181
182
        $request = $this->objectManager->get(Request::class);
183
        /* @var Request $request */
184
        $request->setControllerExtensionName(ucfirst($activeModuleDescription['extensionKey']));
185
        $request->setControllerName($activeModuleDescription['controller'] . 'Module');
186
        $request->setControllerActionName('index');
187
188
        if (!is_null($this->site)) {
189
            $request->setArgument('site', $this->site);
190
        }
191
192
        $request->setPluginName($this->request->getPluginName());
193
        if ($this->request->hasArgument('moduleAction')) {
194
            // TODO check whether action is registered/allowed
195
            $request->setControllerActionName($this->request->getArgument('moduleAction'));
196
        }
197
198
        // transfer additional parameters
199
        foreach ($this->request->getArguments() as $argumentName => $argumentValue) {
200
            if (in_array($argumentName,
201
                ['module', 'moduleAction', 'controller'])) {
202
                // these have been transferred already
203
                continue;
204
            }
205
            $request->setArgument($argumentName, $argumentValue);
206
        }
207
208
        $response = $this->objectManager->get(Response::class);
209
        /* @var Response $response */
210
211
        while (!$request->isDispatched()) {
212
            try {
213
                $this->activeModule->processRequest($request, $response);
214
            } catch (StopActionException $ignoredException) {
215
                /* Silently ignore exception */
216
            }
217
        }
218
219
        $this->view->assign('moduleContent', $response->getContent());
220
    }
221
222
    /**
223
     * Sets the site to work with
224
     *
225
     * @param int $site Site root page id
226
     * @return void
227
     */
228
    public function setSiteAction($site)
229
    {
230
        $siteRepository = $this->getSiteRepository();
231
        $site = $siteRepository->getSiteByPageId((int)$site);
232
233
        $this->setSiteAndResetCore($site);
234
235
        $this->forwardHome();
236
    }
237
238
    /**
239
     * Sets the core to work with.
240
     *
241
     * @param string $core The core path to use
242
     * @param string $module Module to forward to after setting the core
243
     * @return void
244
     */
245
    public function setCoreAction($core, $module = 'Overview')
246
    {
247
        $this->moduleData->setCore($core);
248
        $this->moduleDataStorageService->persistModuleData($this->moduleData);
249
250
        $this->forwardToModule($module);
251
    }
252
253
    /**
254
     * Forwards to the index action after resetting module and moduleAction
255
     * arguments to prevent execution of module actions.
256
     *
257
     * @return void
258
     */
259
    protected function forwardHome()
260
    {
261
        $this->forwardToNonModuleAction('index');
262
    }
263
264
    /**
265
     * Forwards to an action of the AdministrationController.
266
     *
267
     * @param string $actionName
268
     */
269
    protected function forwardToNonModuleAction($actionName)
270
    {
271
        $requestArguments = $this->request->getArguments();
272
        unset($requestArguments['module'], $requestArguments['moduleAction']);
273
274
        $this->request->setArguments($requestArguments);
275
        $this->forward($actionName);
276
    }
277
278
    /**
279
     * Forwards to a specific module and module action.
280
     *
281
     * @param string $module Module name
282
     * @param string $moduleAction Module action
283
     * @return void
284
     */
285
    protected function forwardToModule($module, $moduleAction = '')
286
    {
287
        $requestArguments = $this->request->getArguments();
288
289
        $requestArguments['module'] = $module;
290
        if (!empty($moduleAction)) {
291
            $requestArguments['moduleAction'] = $moduleAction;
292
        } else {
293
            unset($requestArguments['moduleAction']);
294
        }
295
296
        $this->request->setArguments($requestArguments);
297
298
        $this->forward('index');
299
    }
300
301
    /**
302
     * @param Site $site
303
     */
304
    protected function setSiteAndResetCore($site)
305
    {
306
        $this->moduleData->setSite($site);
307
        // when switching the site, reset the core
308
        $this->moduleData->setCore('');
309
        $this->moduleDataStorageService->persistModuleData($this->moduleData);
310
    }
311
312
    /**
313
     * Resets the stored site to the first available site.
314
     *
315
     * @return void‚
316
     */
317
    protected function initializeSiteFromFirstAvailableAndStoreInModuleData()
318
    {
319
        $siteRepository = $this->getSiteRepository();
320
        $site = $siteRepository->getFirstAvailableSite();
321
        if (!$site instanceof Site) {
322
            return;
323
        }
324
        $this->setSiteAndResetCore($site);
325
        $this->site = $site;
326
    }
327
328
    /**
329
     * @return void
330
     */
331
    protected function resolveSite()
332
    {
333
        $this->site = $this->moduleData->getSite();
334
        if (!$this->site instanceof Site) {
335
            $this->initializeSiteFromFirstAvailableAndStoreInModuleData();
336
        }
337
338
        $rootPageId = ($this->site instanceof Site) ? $this->site->getRootPageId() : 0;
339
        if ($rootPageId > 0 && !Util::pageExists($rootPageId)) {
340
            $this->initializeSiteFromFirstAvailableAndStoreInModuleData();
341
        }
342
    }
343
344
    /**
345
     * Get an instance of the SiteRepository
346
     *
347
     * @return SiteRepository
348
     */
349
    protected function getSiteRepository()
350
    {
351
        return GeneralUtility::makeInstance(SiteRepository::class);
352
    }
353
}
354