Completed
Pull Request — master (#520)
by
unknown
02:26
created

BackendController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 0
loc 144
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A optionAction() 0 6 1
A listAction() 0 23 2
A getPids() 0 10 2
A getOptions() 0 14 3
A getDifferentTypesAndLocations() 0 17 3
A isPageAllowed() 0 15 4
A getAllowedDbMounts() 0 9 2
A getBackendUser() 0 4 1
1
<?php
2
3
/**
4
 * BackendController.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Controller;
9
10
use HDNET\Calendarize\Domain\Model\Request\OptionRequest;
11
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
12
use TYPO3\CMS\Backend\Utility\BackendUtility;
13
use TYPO3\CMS\Core\Messaging\FlashMessage;
14
15
/**
16
 * BackendController.
17
 */
18
class BackendController extends AbstractController
19
{
20
    /**
21
     * Basic backend list.
22
     */
23
    public function listAction()
24
    {
25
        $this->settings['timeFormat'] = 'H:i';
26
        $this->settings['dateFormat'] = 'd.m.Y';
27
28
        $options = $this->getOptions();
29
        $typeLocations = $this->getDifferentTypesAndLocations();
30
31
        $pids = $this->getPids($typeLocations);
32
        if ($pids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            $indices = $this->indexRepository->findAllForBackend($options, $pids);
34
        } else {
35
            $indices = [];
36
        }
37
38
        $this->view->assignMultiple([
39
            'indices' => $indices,
40
            'typeLocations' => $typeLocations,
41
            'pids' => $pids,
42
            'settings' => $this->settings,
43
            'options' => $options,
44
        ]);
45
    }
46
47
    /**
48
     * Option action.
49
     *
50
     * @param \HDNET\Calendarize\Domain\Model\Request\OptionRequest $options
51
     */
52
    public function optionAction(OptionRequest $options)
53
    {
54
        $GLOBALS['BE_USER']->setAndSaveSessionData('calendarize_be', serialize($options));
55
        $this->addFlashMessage('Options saved', '', FlashMessage::OK, true);
56
        $this->forward('list');
57
    }
58
59
    protected function getPids(array $typeLocations)
60
    {
61
        $pids = [];
62
        foreach ($typeLocations as $locations) {
63
            $pids = array_merge($pids, array_keys($locations));
64
        }
65
        $pids = array_unique($pids);
66
67
        return array_combine($pids, $pids);
68
    }
69
70
    /**
71
     * Get option request.
72
     *
73
     * @return OptionRequest
74
     */
75
    protected function getOptions()
76
    {
77
        try {
78
            $info = $GLOBALS['BE_USER']->getSessionData('calendarize_be');
79
            $object = @unserialize((string)$info);
80
            if ($object instanceof OptionRequest) {
81
                return $object;
82
            }
83
84
            return new OptionRequest();
85
        } catch (\Exception $exception) {
86
            return new OptionRequest();
87
        }
88
    }
89
90
    /**
91
     * Get the differnet locations for new entries.
92
     *
93
     * @return array
94
     */
95
    protected function getDifferentTypesAndLocations()
96
    {
97
        /**
98
         * @var array<int>
99
         */
100
        $mountPoints = $this->getAllowedDbMounts();
101
102
        $typeLocations = [];
103
        foreach ($this->indexRepository->findDifferentTypesAndLocations() as $entry) {
104
            $pageId = $entry['pid'];
105
            if ($this->isPageAllowed($pageId, $mountPoints)) {
106
                $typeLocations[$entry['foreign_table']][$pageId] = $entry['unique_register_key'];
107
            }
108
        }
109
110
        return $typeLocations;
111
    }
112
113
    /**
114
     * Check if access to page is allowed for current user.
115
     *
116
     * @param int $pageId
117
     * @param array $mountPoints
118
     * @return bool
119
     */
120
    protected function isPageAllowed(int $pageId, array $mountPoints):bool
121
    {
122
        if ($this->getBackendUser()->isAdmin()) {
123
            return true;
124
        }
125
126
        // check if any mountpoint is in rootline
127
        $rootline = BackendUtility::BEgetRootLine($pageId, '');
128
        foreach ($rootline as $entry) {
129
            if (in_array((int)$entry['uid'], $mountPoints)) {
130
                return true;
131
            }
132
        }
133
        return false;
134
    }
135
136
    /**
137
     * Get allowed mountpoints. Returns temporary mountpoint when temporary mountpoint is used.
138
     *
139
     * copied from core TreeController
140
     *
141
     * @return int[]
142
     */
143
    protected function getAllowedDbMounts(): array
144
    {
145
        $dbMounts = (int)($this->getBackendUser()->uc['pageTree_temporaryMountPoint'] ?? 0);
146
        if (!$dbMounts) {
147
            $dbMounts = array_map('intval', $this->getBackendUser()->returnWebmounts());
148
            return array_unique($dbMounts);
149
        }
150
        return [$dbMounts];
151
    }
152
153
    /**
154
     * @return BackendUserAuthentication
155
     */
156
    protected function getBackendUser(): BackendUserAuthentication
157
    {
158
        return $GLOBALS['BE_USER'];
159
    }
160
161
}
162