Completed
Push — master ( e93629...dcd410 )
by
unknown
19s queued 15s
created

NewTenantController::injectStructureRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Kitodo\Dlf\Controller\Backend;
13
14
use Kitodo\Dlf\Common\Solr;
15
use Kitodo\Dlf\Controller\AbstractController;
16
use Kitodo\Dlf\Domain\Model\Format;
17
use Kitodo\Dlf\Domain\Model\Metadata;
18
use Kitodo\Dlf\Domain\Model\MetadataFormat;
19
use Kitodo\Dlf\Domain\Model\SolrCore;
20
use Kitodo\Dlf\Domain\Model\Structure;
21
use Kitodo\Dlf\Domain\Repository\FormatRepository;
22
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
23
use Kitodo\Dlf\Domain\Repository\StructureRepository;
24
use Kitodo\Dlf\Domain\Repository\SolrCoreRepository;
25
26
use TYPO3\CMS\Backend\Utility\BackendUtility;
27
use TYPO3\CMS\Core\Localization\LocalizationFactory;
28
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
31
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
32
33
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
34
use TYPO3\CMS\Core\Site\Entity\NullSite;
35
use TYPO3\CMS\Core\Site\Entity\Site;
36
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
37
use TYPO3\CMS\Core\Site\SiteFinder;
38
39
/**
40
 * Controller class for the backend module 'New Tenant'.
41
 *
42
 * @author Christopher Timm <[email protected]>
43
 * @author Alexander Bigga <[email protected]>
44
 * @package TYPO3
45
 * @subpackage dlf
46
 * @access public
47
 */
48
class NewTenantController extends AbstractController
49
{
50
    /**
51
     * @var int
52
     */
53
    protected $pid;
54
55
    /**
56
     * @var array
57
     */
58
    protected $pageInfo;
59
60
    /**
61
     * All configured site languages
62
     *
63
     * @var array
64
     */
65
    protected $siteLanguages;
66
67
    /**
68
     * LanguageFactory to get language key/values by our own.
69
     *
70
     * @var \TYPO3\CMS\Core\Localization\LocalizationFactory
71
     */
72
    protected $languageFactory;
73
74
    /**
75
     * Backend Template Container
76
     *
77
     * @var string
78
     */
79
    protected $defaultViewObjectName = \TYPO3\CMS\Backend\View\BackendTemplateView::class;
80
81
    /**
82
     * @var FormatRepository
83
     */
84
    protected $formatRepository;
85
86
    /**
87
     * @param FormatRepository $formatRepository
88
     */
89
    public function injectFormatRepository(FormatRepository $formatRepository)
90
    {
91
        $this->formatRepository = $formatRepository;
92
    }
93
94
    /**
95
     * @var MetadataRepository
96
     */
97
    protected $metadataRepository;
98
99
    /**
100
     * @param MetadataRepository $metadataRepository
101
     */
102
    public function injectMetadataRepository(MetadataRepository $metadataRepository)
103
    {
104
        $this->metadataRepository = $metadataRepository;
105
    }
106
107
    /**
108
     * @var StructureRepository
109
     */
110
    protected $structureRepository;
111
112
    /**
113
     * @param StructureRepository $structureRepository
114
     */
115
    public function injectStructureRepository(StructureRepository $structureRepository)
116
    {
117
        $this->structureRepository = $structureRepository;
118
    }
119
120
    /**
121
     * @var SolrCoreRepository
122
     */
123
    protected $solrCoreRepository;
124
125
    /**
126
     * @param SolrCoreRepository $solrCoreRepository
127
     */
128
    public function injectSolrCoreRepository(SolrCoreRepository $solrCoreRepository)
129
    {
130
        $this->solrCoreRepository = $solrCoreRepository;
131
    }
132
133
    /**
134
     * Initialization for all actions
135
     *
136
     */
137
    protected function initializeAction()
138
    {
139
        $this->pid = (int) GeneralUtility::_GP('id');
140
141
        $frameworkConfiguration = $this->configurationManager->getConfiguration($this->configurationManager::CONFIGURATION_TYPE_FRAMEWORK);
142
        $frameworkConfiguration['persistence']['storagePid'] = $this->pid;
143
        $this->configurationManager->setConfiguration($frameworkConfiguration);
144
145
        $this->languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
146
147
        try {
148
            $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($this->pid);
149
        } catch (SiteNotFoundException $e) {
150
            $site = new NullSite();
151
        }
152
        $this->siteLanguages = $site->getLanguages();
153
    }
154
155
156
    /**
157
     * Action adding formats records
158
     */
159
    public function addFormatAction()
160
    {
161
        // Include formats definition file.
162
        $formatsDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/FormatDefaults.php');
163
164
        $frameworkConfiguration = $this->configurationManager->getConfiguration($this->configurationManager::CONFIGURATION_TYPE_FRAMEWORK);
165
        // tx_dlf_formats are stored on PID = 0
166
        $frameworkConfiguration['persistence']['storagePid'] = 0;
167
        $this->configurationManager->setConfiguration($frameworkConfiguration);
168
169
        $doPersist = false;
170
171
        foreach ($formatsDefaults as $type => $values) {
172
            // if default format record is not found, add it to the repository
173
            if ($this->formatRepository->findOneByType($type) === null) {
0 ignored issues
show
Bug introduced by
The method findOneByType() does not exist on Kitodo\Dlf\Domain\Repository\FormatRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
            if ($this->formatRepository->/** @scrutinizer ignore-call */ findOneByType($type) === null) {
Loading history...
174
                $newRecord = GeneralUtility::makeInstance(Format::class);
175
                $newRecord->setType($type);
176
                $newRecord->setRoot($values['root']);
177
                $newRecord->setNamespace($values['namespace']);
178
                $newRecord->setClass($values['class']);
179
                $this->formatRepository->add($newRecord);
180
181
                $doPersist = true;
182
            }
183
        }
184
185
        // We must persist here, if we changed anything.
186
        if ($doPersist === true) {
187
            $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
188
            $persistenceManager->persistAll();
189
        }
190
191
        $this->forward('index');
192
    }
193
194
    /**
195
     * Action adding metadata records
196
     */
197
    public function addMetadataAction()
198
    {
199
        // Include metadata definition file.
200
        $metadataDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/MetadataDefaults.php');
201
202
        $doPersist = false;
203
204
        // load language file in own array
205
        $metadataLabels = $this->languageFactory->getParsedData('EXT:dlf/Resources/Private/Language/locallang_metadata.xlf', $this->siteLanguages[0]->getTypo3Language());
206
207
        foreach ($metadataDefaults as $indexName => $values) {
208
            // if default format record is not found, add it to the repository
209
            if ($this->metadataRepository->findOneByIndexName($indexName) === null) {
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\MetadataRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

209
            if ($this->metadataRepository->/** @scrutinizer ignore-call */ findOneByIndexName($indexName) === null) {
Loading history...
210
211
                $newRecord = GeneralUtility::makeInstance(Metadata::class);
212
                $newRecord->setLabel($this->getLLL('metadata.' . $indexName, $this->siteLanguages[0]->getTypo3Language(), $metadataLabels));
213
                $newRecord->setIndexName($indexName);
214
                $newRecord->setDefaultValue($values['default_value']);
215
                $newRecord->setWrap($values['wrap'] ? : $GLOBALS['TCA']['tx_dlf_metadata']['columns']['wrap']['config']['default']);
216
                $newRecord->setIndexTokenized($values['index_tokenized']);
217
                $newRecord->setIndexStored((int) $values['index_stored']);
218
                $newRecord->setIndexIndexed((int) $values['index_indexed']);
219
                $newRecord->setIndexBoost((float) $values['index_boost']);
220
                $newRecord->setIsSortable((int) $values['is_sortable']);
221
                $newRecord->setIsFacet((int) $values['is_facet']);
222
                $newRecord->setIsListed((int) $values['is_listed']);
223
                $newRecord->setIndexAutocomplete((int) $values['index_autocomplete']);
224
225
                if (is_array($values['format'])) {
226
                    foreach ($values['format'] as $format) {
227
                        $formatRecord = $this->formatRepository->findOneByRoot($format['format_root']);
0 ignored issues
show
Bug introduced by
The method findOneByRoot() does not exist on Kitodo\Dlf\Domain\Repository\FormatRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
                        /** @scrutinizer ignore-call */ 
228
                        $formatRecord = $this->formatRepository->findOneByRoot($format['format_root']);
Loading history...
228
                        // If formatRecord is null, we cannot create a MetadataFormat record.
229
                        if ($formatRecord !== null) {
230
                            $newMetadataFormat = GeneralUtility::makeInstance(MetadataFormat::class);
231
                            $newMetadataFormat->setEncoded($formatRecord->getUid());
0 ignored issues
show
Bug introduced by
The method getUid() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

231
                            $newMetadataFormat->setEncoded($formatRecord->/** @scrutinizer ignore-call */ getUid());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
232
                            $newMetadataFormat->setXpath($format['xpath']);
233
                            $newMetadataFormat->setXpathSorting($format['xpath_sorting']);
234
                            $newRecord->addFormat($newMetadataFormat);
235
                        }
236
                    }
237
                }
238
239
                foreach ($this->siteLanguages as $siteLanguage) {
240
                    if ($siteLanguage->getLanguageId() === 0) {
241
                        // skip default language
242
                        continue;
243
                    }
244
                    $translatedRecord = GeneralUtility::makeInstance(Metadata::class);
245
                    $translatedRecord->setL18nParent($newRecord);
246
                    $translatedRecord->_setProperty('_languageUid', $siteLanguage->getLanguageId());
247
                    $translatedRecord->setLabel($this->getLLL('metadata.' . $indexName, $siteLanguage->getTypo3Language(), $metadataLabels));
248
                    $translatedRecord->setIndexName($indexName);
249
                    $translatedRecord->setWrap($newRecord->getWrap());
250
251
                    $this->metadataRepository->add($translatedRecord);
252
                }
253
254
                $this->metadataRepository->add($newRecord);
255
256
                $doPersist = true;
257
            }
258
        }
259
260
        // We must persist here, if we changed anything.
261
        if ($doPersist === true) {
262
            $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
263
            $persistenceManager->persistAll();
264
        }
265
266
        $this->forward('index');
267
    }
268
269
    /**
270
     * Action adding Solr core records
271
     */
272
    public function addSolrCoreAction()
273
    {
274
        $doPersist = false;
275
276
        // load language file in own array
277
        $beLabels = $this->languageFactory->getParsedData('EXT:dlf/Resources/Private/Language/locallang_be.xlf', $this->siteLanguages[0]->getTypo3Language());
278
279
        if ($this->solrCoreRepository->findOneByPid($this->pid) === null) {
0 ignored issues
show
Bug introduced by
The method findOneByPid() does not exist on Kitodo\Dlf\Domain\Repository\SolrCoreRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

279
        if ($this->solrCoreRepository->/** @scrutinizer ignore-call */ findOneByPid($this->pid) === null) {
Loading history...
280
            $newRecord = GeneralUtility::makeInstance(SolrCore::class);
281
            $newRecord->setLabel($this->getLLL('flexform.solrcore', $this->siteLanguages[0]->getTypo3Language(), $beLabels). ' (PID ' . $this->pid . ')');
282
            $indexName = Solr::createCore('');
283
            $newRecord->setIndexName($indexName);
284
285
            $this->solrCoreRepository->add($newRecord);
286
287
            $doPersist = true;
288
        }
289
290
        // We must persist here, if we changed anything.
291
        if ($doPersist === true) {
292
            $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
293
            $persistenceManager->persistAll();
294
        }
295
296
        $this->forward('index');
297
    }
298
299
    /**
300
     * Action adding structure records
301
     */
302
    public function addStructureAction()
303
    {
304
        // Include structure definition file.
305
        $structureDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/StructureDefaults.php');
306
307
        $doPersist = false;
308
309
        // load language file in own array
310
        $structLabels = $this->languageFactory->getParsedData('EXT:dlf/Resources/Private/Language/locallang_structure.xlf', $this->siteLanguages[0]->getTypo3Language());
311
312
        foreach ($structureDefaults as $indexName => $values) {
313
            // if default format record is not found, add it to the repository
314
            if ($this->structureRepository->findOneByIndexName($indexName) === null) {
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\StructureRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

314
            if ($this->structureRepository->/** @scrutinizer ignore-call */ findOneByIndexName($indexName) === null) {
Loading history...
315
                $newRecord = GeneralUtility::makeInstance(Structure::class);
316
                $newRecord->setLabel($this->getLLL('structure.' . $indexName, $this->siteLanguages[0]->getTypo3Language(), $structLabels));
317
                $newRecord->setIndexName($indexName);
318
                $newRecord->setToplevel($values['toplevel']);
319
                $newRecord->setOaiName($values['oai_name']);
320
                $this->structureRepository->add($newRecord);
321
322
                foreach ($this->siteLanguages as $siteLanguage) {
323
                    if ($siteLanguage->getLanguageId() === 0) {
324
                        // skip default language
325
                        continue;
326
                    }
327
                    $translatedRecord = GeneralUtility::makeInstance(Structure::class);
328
                    $translatedRecord->setL18nParent($newRecord);
329
                    $translatedRecord->_setProperty('_languageUid', $siteLanguage->getLanguageId());
330
                    $translatedRecord->setLabel($this->getLLL('structure.' . $indexName, $siteLanguage->getTypo3Language(), $structLabels));
331
                    $translatedRecord->setIndexName($indexName);
332
333
                    $this->structureRepository->add($translatedRecord);
334
                }
335
336
                $doPersist = true;
337
            }
338
        }
339
340
        // We must persist here, if we changed anything.
341
        if ($doPersist === true) {
342
            $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
343
            $persistenceManager->persistAll();
344
        }
345
346
        $this->forward('index');
347
    }
348
349
    /**
350
     * Set up the doc header properly here
351
     *
352
     * @param ViewInterface $view
353
     * @return void
354
     */
355
    protected function initializeView(ViewInterface $view)
356
    {
357
        /** @var BackendTemplateView $view */
358
        parent::initializeView($view);
359
        if ($this->actionMethodName == 'indexAction') {
360
            $this->pageInfo = BackendUtility::readPageAccess($this->pid, $GLOBALS['BE_USER']->getPagePermsClause(1));
0 ignored issues
show
Documentation Bug introduced by
It seems like TYPO3\CMS\Backend\Utilit...>getPagePermsClause(1)) can also be of type false. However, the property $pageInfo is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
361
            $view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
362
        }
363
        if ($view instanceof BackendTemplateView) {
0 ignored issues
show
Bug introduced by
The type Kitodo\Dlf\Controller\Backend\BackendTemplateView was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
364
            $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
365
        }
366
    }
367
368
    /**
369
     * Main function of the module
370
     *
371
     * @access public
372
     *
373
     */
374
    public function indexAction()
375
    {
376
        $recordInfos = [];
377
378
        if ($this->pageInfo['doktype'] != 254) {
379
            $this->forward('error');
380
        }
381
382
        $formatsDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/FormatDefaults.php');
383
        $recordInfos['formats']['numCurrent'] = $this->formatRepository->countAll();
384
        $recordInfos['formats']['numDefault'] = count($formatsDefaults);
385
386
        $structuresDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/StructureDefaults.php');
387
        $recordInfos['structures']['numCurrent'] = $this->structureRepository->countByPid($this->pid);
0 ignored issues
show
Bug introduced by
The method countByPid() does not exist on Kitodo\Dlf\Domain\Repository\StructureRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

387
        /** @scrutinizer ignore-call */ 
388
        $recordInfos['structures']['numCurrent'] = $this->structureRepository->countByPid($this->pid);
Loading history...
388
        $recordInfos['structures']['numDefault'] = count($structuresDefaults);
389
390
        $metadataDefaults = include(ExtensionManagementUtility::extPath('dlf') . 'Resources/Private/Data/MetadataDefaults.php');
391
        $recordInfos['metadata']['numCurrent'] = $this->metadataRepository->countByPid($this->pid);
0 ignored issues
show
Bug introduced by
The method countByPid() does not exist on Kitodo\Dlf\Domain\Repository\MetadataRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

391
        /** @scrutinizer ignore-call */ 
392
        $recordInfos['metadata']['numCurrent'] = $this->metadataRepository->countByPid($this->pid);
Loading history...
392
        $recordInfos['metadata']['numDefault'] = count($metadataDefaults);
393
394
        $recordInfos['solrcore']['numCurrent'] = $this->solrCoreRepository->countByPid($this->pid);
0 ignored issues
show
Bug introduced by
The method countByPid() does not exist on Kitodo\Dlf\Domain\Repository\SolrCoreRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

394
        /** @scrutinizer ignore-call */ 
395
        $recordInfos['solrcore']['numCurrent'] = $this->solrCoreRepository->countByPid($this->pid);
Loading history...
395
396
        $this->view->assign('recordInfos', $recordInfos);
397
    }
398
399
    /**
400
     * Error function - there is nothing to do at the moment.
401
     *
402
     * @access public
403
     *
404
     */
405
    public function errorAction()
406
    {
407
    }
408
409
    /**
410
     * Get language label for given key and language.
411
     *
412
     * @param string $index
413
     * @param string $lang
414
     * @param array $langArray
415
     *
416
     * @access public
417
     */
418
    protected function getLLL($index, $lang, $langArray)
419
    {
420
        if (isset($langArray[$lang][$index][0]['target'])) {
421
            return $langArray[$lang][$index][0]['target'];
422
        } else {
423
            return $langArray['default'][$index][0]['target'];
424
        }
425
    }
426
}
427