Passed
Push — wip/remove-deprecations-for-v1... ( f97f5b )
by Tomas Norre
05:15
created

BackendModule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 0 Features 1
Metric Value
eloc 64
c 9
b 0
f 1
dl 0
loc 144
ccs 0
cts 83
cp 0
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A __construct() 0 8 1
A renderForm() 0 7 1
A initializeView() 0 8 1
A getLanguageService() 0 3 1
A getModuleMenu() 0 29 1
A main() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Backend;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2020 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use AOE\Crawler\Backend\RequestForm\RequestFormFactory;
32
use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
33
use AOE\Crawler\Domain\Repository\QueueRepository;
34
use AOE\Crawler\Service\ProcessService;
35
use AOE\Crawler\Value\CrawlAction;
36
use TYPO3\CMS\Backend\Utility\BackendUtility;
37
use TYPO3\CMS\Core\Database\ConnectionPool;
38
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
39
use TYPO3\CMS\Core\Localization\LanguageService;
40
use TYPO3\CMS\Core\Utility\GeneralUtility;
41
use TYPO3\CMS\Extbase\Object\ObjectManager;
42
use TYPO3\CMS\Fluid\View\StandaloneView;
43
use TYPO3\CMS\Info\Controller\InfoModuleController;
44
45
/**
46
 * Function for Info module, containing three main actions:
47
 * - List of all queued items
48
 * - Log functionality
49
 * - Process overview
50
 */
51
class BackendModule
52
{
53
    /**
54
     * @var InfoModuleController Contains a reference to the parent calling object
55
     */
56
    protected $pObj;
57
58
    /**
59
     * The current page ID
60
     * @var int
61
     */
62
    protected $id;
63
64
    /**
65
     * Holds the configuration from ext_conf_template loaded by getExtensionConfiguration()
66
     *
67
     * @var array
68
     */
69
    protected $extensionSettings = [];
70
71
    /**
72
     * @var ProcessService
73
     */
74
    protected $processManager;
75
76
    /**
77
     * @var QueryBuilder
78
     */
79
    protected $queryBuilder;
80
81
    /**
82
     * @var QueueRepository
83
     */
84
    protected $queueRepository;
85
86
    /**
87
     * @var StandaloneView
88
     */
89
    protected $view;
90
91
    public function __construct()
92
    {
93
        $objectManger = GeneralUtility::makeInstance(ObjectManager::class);
94
        $this->processManager = $objectManger->get(ProcessService::class);
95
        $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_crawler_queue');
96
        $this->queueRepository = $objectManger->get(QueueRepository::class);
97
        $this->initializeView();
98
        $this->extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
99
    }
100
101
    /**
102
     * Called by the InfoModuleController
103
     */
104
    public function init(InfoModuleController $pObj): void
105
    {
106
        $this->pObj = $pObj;
107
        $this->id = (int) GeneralUtility::_GP('id');
108
        // Setting MOD_MENU items as we need them for logging:
109
        $this->pObj->MOD_MENU = array_merge($this->pObj->MOD_MENU, $this->getModuleMenu());
110
    }
111
112
    public function main(): string
113
    {
114
        if (empty($this->pObj->MOD_SETTINGS['processListMode'])) {
115
            $this->pObj->MOD_SETTINGS['processListMode'] = 'simple';
116
        }
117
        $this->view->assign('currentPageId', $this->id);
118
119
        $selectedAction = new CrawlAction($this->pObj->MOD_SETTINGS['crawlaction'] ?? 'start');
120
121
        // Type function menu:
122
        $actionDropdown = BackendUtility::getFuncMenu(
123
            $this->id,
124
            'SET[crawlaction]',
125
            $selectedAction->__toString(),
126
            $this->pObj->MOD_MENU['crawlaction']
127
        );
128
129
        $theOutput = '<h2>' . htmlspecialchars($this->getLanguageService()->getLL('title'), ENT_QUOTES | ENT_HTML5) . '</h2>' . $actionDropdown;
130
131
        return $theOutput . $this->renderForm($selectedAction);
132
    }
133
134
    private function getLanguageService(): LanguageService
135
    {
136
        return $GLOBALS['LANG'];
137
    }
138
139
    /*****************************
140
     *
141
     * General Helper Functions
142
     *
143
     *****************************/
144
145
    private function initializeView(): void
146
    {
147
        $view = GeneralUtility::makeInstance(StandaloneView::class);
148
        $view->setLayoutRootPaths(['EXT:crawler/Resources/Private/Layouts']);
149
        $view->setPartialRootPaths(['EXT:crawler/Resources/Private/Partials']);
150
        $view->setTemplateRootPaths(['EXT:crawler/Resources/Private/Templates/Backend']);
151
        $view->getRequest()->setControllerExtensionName('Crawler');
152
        $this->view = $view;
153
    }
154
155
    private function getModuleMenu(): array
156
    {
157
        return [
158
            'depth' => [
159
                0 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
160
                1 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
161
                2 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
162
                3 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
163
                4 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
164
                99 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
165
            ],
166
            'crawlaction' => [
167
                'start' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.start'),
168
                'log' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.log'),
169
                'multiprocess' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.multiprocess'),
170
            ],
171
            'log_resultLog' => '',
172
            'log_feVars' => '',
173
            'processListMode' => '',
174
            'log_display' => [
175
                'all' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.all'),
176
                'pending' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.pending'),
177
                'finished' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.finished'),
178
            ],
179
            'itemsPerPage' => [
180
                '5' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.5'),
181
                '10' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.10'),
182
                '50' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.50'),
183
                '0' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.0'),
184
            ],
185
        ];
186
    }
187
188
    private function renderForm(CrawlAction $selectedAction): string
189
    {
190
        $requestForm = RequestFormFactory::create($selectedAction, $this->view, $this->pObj, $this->extensionSettings);
191
        return $requestForm->render(
192
            $this->id,
193
            $this->pObj->MOD_SETTINGS['depth'],
194
            $this->pObj->MOD_MENU['depth']
195
        );
196
    }
197
}
198