Completed
Push — master ( dfc64f...43826b )
by Fabien
51:13
created

ModuleService::getFirstModuleForPid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Fab\Vidi\Module;
3
4
/*
5
 * This file is part of the Fab/Vidi project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use Fab\Vidi\Service\DataService;
12
use Fab\Vidi\Utility\BackendUtility;
13
use TYPO3\CMS\Core\SingletonInterface;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use Fab\Vidi\Tca\Tca;
16
17
/**
18
 * Service related to data type (AKA tablename)
19
 * @deprecated this class is not used anymore
20
 */
21
class ModuleService implements SingletonInterface
22
{
23
24
    /**
25
     * @var array
26
     */
27
    protected $storage = [];
28
29
    /**
30
     * Returns a class instance
31
     *
32
     * @return \Fab\Vidi\Module\ModuleService|object
33
     */
34
    static public function getInstance()
35
    {
36
        return GeneralUtility::makeInstance(\Fab\Vidi\Module\ModuleService::class);
37
    }
38
39
    /**
40
     * Fetch all modules to be displayed on the current pid.
41
     *
42
     * @return array
43
     */
44
    public function getModulesForCurrentPid(): array
45
    {
46
        $pid = $this->getModuleLoader()->getCurrentPid();
47
        return $this->getModulesForPid($pid);
48
    }
49
50
    /**
51
     * Fetch all modules displayed given a pid.
52
     *
53
     * @param int $pid
54
     * @return array
55
     */
56
    public function getModulesForPid($pid = null): array
57
    {
58
        if (!isset($this->storage[$pid])) {
59
60
            $modules = [];
61
            foreach ($GLOBALS['TCA'] as $dataType => $configuration) {
62
                if (Tca::table($dataType)->isNotHidden()) {
63
                    $record = $this->getDataService()->getRecord(
64
                        $dataType,
65
                        [
66
                            'pid' => $pid
67
                        ]
68
                    );
69
                    if (!empty($record)) {
70
                        $moduleName = 'Vidi' . GeneralUtility::underscoredToUpperCamelCase($dataType) . 'M1';
71
                        $title = Tca::table($dataType)->getTitle();
72
                        $modules[$moduleName] = $title;
73
                    }
74
                }
75
            }
76
            $this->storage[$pid] = $modules;
77
        }
78
        return $this->storage[$pid];
79
    }
80
81
    /**
82
     * Fetch the first module for the current pid.
83
     *
84
     * @return string
85
     */
86
    public function getFirstModuleForCurrentPid(): string
87
    {
88
        $pid = $this->getModuleLoader()->getCurrentPid();
89
        return $this->getFirstModuleForPid($pid);
90
    }
91
92
    /**
93
     * Fetch the module given a pid.
94
     *
95
     * @param int $pid
96
     * @return string
97
     */
98
    public function getFirstModuleForPid($pid): string
99
    {
100
        $firstModule = '';
101
        $modules = $this->getModulesForPid($pid);
102
        if (!empty($modules)) {
103
            $firstModule = key($modules);
104
        }
105
106
        return $firstModule;
107
    }
108
109
    /**
110
     * @return object|DataService
111
     */
112
    protected function getDataService(): DataService
113
    {
114
        return GeneralUtility::makeInstance(DataService::class);
115
    }
116
117
    /**
118
     * Get the Vidi Module Loader.
119
     *
120
     * @return \Fab\Vidi\Module\ModuleLoader|object
121
     */
122
    protected function getModuleLoader()
123
    {
124
        return GeneralUtility::makeInstance(\Fab\Vidi\Module\ModuleLoader::class);
125
    }
126
}
127