Passed
Push — refactor/backendModule-ValueOb... ( ab393e...9011ea )
by Tomas Norre
16:36
created

BackendModule::getInfoModuleUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 6
rs 10
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\Controller\CrawlerController;
34
use AOE\Crawler\Converter\JsonCompatibilityConverter;
35
use AOE\Crawler\Domain\Repository\QueueRepository;
36
use AOE\Crawler\Service\ProcessService;
37
use AOE\Crawler\Value\CrawlAction;
38
use AOE\Crawler\Value\ModuleSettings;
39
use TYPO3\CMS\Backend\Utility\BackendUtility;
40
use TYPO3\CMS\Core\Database\ConnectionPool;
41
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
42
use TYPO3\CMS\Core\Imaging\IconFactory;
43
use TYPO3\CMS\Core\Localization\LanguageService;
44
use TYPO3\CMS\Core\Utility\GeneralUtility;
45
use TYPO3\CMS\Extbase\Object\ObjectManager;
46
use TYPO3\CMS\Fluid\View\StandaloneView;
47
use TYPO3\CMS\Info\Controller\InfoModuleController;
48
49
/**
50
 * Function for Info module, containing three main actions:
51
 * - List of all queued items
52
 * - Log functionality
53
 * - Process overview
54
 */
55
class BackendModule
56
{
57
    /**
58
     * @var InfoModuleController Contains a reference to the parent calling object
59
     */
60
    protected $pObj;
61
62
    /**
63
     * The current page ID
64
     * @var int
65
     */
66
    protected $id;
67
68
    // Internal, dynamic:
69
70
    /**
71
     * @var array
72
     */
73
    protected $duplicateTrack = [];
74
75
    /**
76
     * @var bool
77
     */
78
    protected $submitCrawlUrls = false;
79
80
    /**
81
     * @var bool
82
     */
83
    protected $downloadCrawlUrls = false;
84
85
    /**
86
     * @var int
87
     */
88
    protected $scheduledTime = 0;
89
90
    /**
91
     * @var int
92
     */
93
    protected $reqMinute = 1000;
94
95
    /**
96
     * @var array holds the selection of configuration from the configuration selector box
97
     */
98
    protected $incomingConfigurationSelection = [];
99
100
    /**
101
     * @var CrawlerController
102
     */
103
    protected $crawlerController;
104
105
    /**
106
     * @var array
107
     */
108
    protected $CSVaccu = [];
109
110
    /**
111
     * If true the user requested a CSV export of the queue
112
     *
113
     * @var boolean
114
     */
115
    protected $CSVExport = false;
116
117
    /**
118
     * @var array
119
     */
120
    protected $downloadUrls = [];
121
122
    /**
123
     * Holds the configuration from ext_conf_template loaded by getExtensionConfiguration()
124
     *
125
     * @var array
126
     */
127
    protected $extensionSettings = [];
128
129
    /**
130
     * Indicate that an flash message with an error is present.
131
     *
132
     * @var boolean
133
     */
134
    protected $isErrorDetected = false;
135
136
    /**
137
     * @var ProcessService
138
     */
139
    protected $processManager;
140
141
    /**
142
     * @var QueryBuilder
143
     */
144
    protected $queryBuilder;
145
146
    /**
147
     * @var QueueRepository
148
     */
149
    protected $queueRepository;
150
151
    /**
152
     * @var StandaloneView
153
     */
154
    protected $view;
155
156
    /**
157
     * @var IconFactory
158
     */
159
    protected $iconFactory;
160
161
    /**
162
     * @var JsonCompatibilityConverter
163
     */
164
    protected $jsonCompatibilityConverter;
165
166
    /**
167
     * @var LanguageService
168
     */
169
    private $languageService;
170
171
    /**
172
     * @var ModuleSettings
173
     */
174
    private $moduleSettings;
175
176
    public function __construct()
177
    {
178
        $this->languageService = $GLOBALS['LANG'];
179
        $objectManger = GeneralUtility::makeInstance(ObjectManager::class);
180
        $this->processManager = $objectManger->get(ProcessService::class);
181
        $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_crawler_queue');
182
        $this->queueRepository = $objectManger->get(QueueRepository::class);
183
        $this->initializeView();
184
        $this->extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
185
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
186
        $this->jsonCompatibilityConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
187
    }
188
189
    /**
190
     * Called by the InfoModuleController
191
     */
192
    public function init(InfoModuleController $pObj): void
193
    {
194
        $this->pObj = $pObj;
195
        $this->id = (int) GeneralUtility::_GP('id');
196
        // Setting MOD_MENU items as we need them for logging:
197
        $this->pObj->MOD_MENU = array_merge($this->pObj->MOD_MENU, $this->getModuleMenu());
198
        $this->moduleSettings = ModuleSettings::fromArray($this->pObj->MOD_SETTINGS);
199
    }
200
201
    /**
202
     * Additions to the function menu array
203
     *
204
     * @return array Menu array
205
     * @deprecated Using BackendModule->modMenu() is deprecated since 9.1.1 and will be removed in v11.x
206
     */
207
    public function modMenu(): array
208
    {
209
        return $this->getModuleMenu();
210
    }
211
212 1
    public function main(): string
213
    {
214 1
        if (empty($this->pObj->MOD_SETTINGS['processListMode'])) {
215
            $this->pObj->MOD_SETTINGS['processListMode'] = 'simple';
216
        }
217
        $this->view->assign('currentPageId', $this->id);
218
219
        $selectedAction = new CrawlAction($this->pObj->MOD_SETTINGS['crawlaction'] ?? 'start');
220
221
        // Type function menu:
222
        $actionDropdown = BackendUtility::getFuncMenu(
223
            $this->id,
224
            'SET[crawlaction]',
225
            $selectedAction,
226
            $this->pObj->MOD_MENU['crawlaction']
227
        );
228
229
        $theOutput = '<h2>' . htmlspecialchars($this->getLanguageService()->getLL('title'), ENT_QUOTES | ENT_HTML5) . '</h2>' . $actionDropdown;
230
        $theOutput .= $this->renderForm($selectedAction);
231
232
        return $theOutput;
233
    }
234
235
    /*****************************
236
     *
237
     * General Helper Functions
238
     *
239
     *****************************/
240
241
    private function initializeView(): void
242
    {
243
        $view = GeneralUtility::makeInstance(StandaloneView::class);
244
        $view->setLayoutRootPaths(['EXT:crawler/Resources/Private/Layouts']);
245
        $view->setPartialRootPaths(['EXT:crawler/Resources/Private/Partials']);
246
        $view->setTemplateRootPaths(['EXT:crawler/Resources/Private/Templates/Backend']);
247
        $view->getRequest()->setControllerExtensionName('Crawler');
248
        $this->view = $view;
249
    }
250
251
    private function getLanguageService(): LanguageService
252
    {
253
        return $GLOBALS['LANG'];
254
    }
255
256
    private function getModuleMenu(): array
257
    {
258
        return [
259
            'depth' => [
260
                0 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
261
                1 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
262
                2 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
263
                3 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
264
                4 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
265
                99 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
266
            ],
267
            'crawlaction' => [
268
                'start' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.start'),
269
                'log' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.log'),
270
                'multiprocess' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.multiprocess'),
271
            ],
272
            'log_resultLog' => '',
273
            'log_feVars' => '',
274
            'processListMode' => '',
275
            'log_display' => [
276
                'all' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.all'),
277
                'pending' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.pending'),
278
                'finished' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.finished'),
279
            ],
280
            'itemsPerPage' => [
281
                '5' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.5'),
282
                '10' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.10'),
283
                '50' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.50'),
284
                '0' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage.0'),
285
            ],
286
        ];
287 4
    }
288
289 4
    private function renderForm(CrawlAction $selectedAction): string
290 1
    {
291
        $requestForm = RequestFormFactory::create($selectedAction, $this->view, $this->moduleSettings, $this->pObj);
292 3
        return $requestForm->render(
293 3
            $this->id,
294
            $this->pObj->MOD_SETTINGS['depth'],
295
            $this->pObj->MOD_MENU['depth']
296
        );
297
    }
298
}
299