Passed
Push — master ( f179a5...b52f67 )
by
unknown
20:41 queued 07:39
created

BackendUserGroupController::compareAction()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 23
rs 9.7998
c 0
b 0
f 0
cc 4
nc 6
nop 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Beuser\Controller;
17
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use TYPO3\CMS\Beuser\Domain\Model\BackendUserGroup;
21
use TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository;
22
use TYPO3\CMS\Beuser\Service\UserInformationService;
23
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
24
use TYPO3\CMS\Core\Pagination\SimplePagination;
25
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
26
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
27
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
28
29
/**
30
 * Backend module user group administration controller
31
 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
32
 */
33
class BackendUserGroupController extends ActionController
34
{
35
    /**
36
     * @var \TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository
37
     */
38
    protected $backendUserGroupRepository;
39
40
    /**
41
     * @param \TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository
42
     */
43
    public function injectBackendUserGroupRepository(BackendUserGroupRepository $backendUserGroupRepository)
44
    {
45
        $this->backendUserGroupRepository = $backendUserGroupRepository;
46
    }
47
48
    /**
49
     * @var UserInformationService
50
     */
51
    protected $userInformationService;
52
53
    public function __construct(
54
        UserInformationService $userInformationService
55
    ) {
56
        $this->userInformationService = $userInformationService;
57
    }
58
59
    /**
60
     * Displays all BackendUserGroups
61
     * @param int $currentPage
62
     */
63
    public function indexAction(int $currentPage = 1): ResponseInterface
64
    {
65
        /** @var QueryResultInterface<int, BackendUserGroup> $backendUsers */
66
        $backendUsers = $this->backendUserGroupRepository->findAll();
67
        $paginator = new QueryResultPaginator($backendUsers, $currentPage, 50);
68
        $pagination = new SimplePagination($paginator);
69
        $compareGroupUidList = array_keys($this->getBackendUser()->uc['beuser']['compareGroupUidList'] ?? []);
70
        $this->view->assignMultiple(
71
            [
72
                'shortcutLabel' => 'backendUserGroupsMenu',
73
                'paginator' => $paginator,
74
                'pagination' => $pagination,
75
                'totalAmountOfBackendUserGroups' => $backendUsers->count(),
76
                'route' => $this->getRequest()->getAttribute('route')->getPath(),
77
                'action' => $this->request->getControllerActionName(),
78
                'controller' => $this->request->getControllerName(),
79
                'compareGroupUidList' => array_map(static function ($value) { // uid as key and force value to 1
80
                    return 1;
81
                }, array_flip($compareGroupUidList)),
82
                'compareGroupList' => !empty($compareGroupUidList) ? $this->backendUserGroupRepository->findByUidList($compareGroupUidList) : [],
83
            ]
84
        );
85
86
        return $this->htmlResponse($this->view->render());
87
    }
88
89
    public function compareAction(): ResponseInterface
90
    {
91
        $compareGroupUidList = array_keys($this->getBackendUser()->uc['beuser']['compareGroupUidList'] ?? []);
92
93
        $compareData = [];
94
        foreach ($compareGroupUidList as $uid) {
95
            if ($compareInformation = $this->userInformationService->getGroupInformation($uid)) {
96
                $compareData[] = $compareInformation;
97
            }
98
        }
99
        if (empty($compareData)) {
100
            $this->redirect('index');
101
        }
102
103
        $this->view->assignMultiple([
104
            'shortcutLabel' => 'compareBackendUsersGroups',
105
            'route' => $this->getRequest()->getAttribute('route')->getPath(),
106
            'action' => $this->request->getControllerActionName(),
107
            'controller' => $this->request->getControllerName(),
108
            'compareGroupList' => $compareData,
109
        ]);
110
111
        return $this->htmlResponse($this->view->render());
112
    }
113
114
    /**
115
     * Attaches one backend user group to the compare list
116
     *
117
     * @param int $uid
118
     */
119
    public function addToCompareListAction(int $uid): void
120
    {
121
        $list = $this->getBackendUser()->uc['beuser']['compareGroupUidList'] ?? [];
122
        $list[$uid] = true;
123
        $this->getBackendUser()->uc['beuser']['compareGroupUidList'] = $list;
124
        $this->getBackendUser()->writeUC();
125
126
        $this->redirect('index');
127
    }
128
129
    /**
130
     * Removes given backend user group to the compare list
131
     *
132
     * @param int $uid
133
     * @param int $redirectToCompare
134
     */
135
    public function removeFromCompareListAction(int $uid, int $redirectToCompare = 0): void
136
    {
137
        $list = $this->getBackendUser()->uc['beuser']['compareGroupUidList'] ?? [];
138
        unset($list[$uid]);
139
        $this->getBackendUser()->uc['beuser']['compareGroupUidList'] = $list;
140
        $this->getBackendUser()->writeUC();
141
142
        if ($redirectToCompare) {
143
            $this->redirect('compare');
144
        } else {
145
            $this->redirect('index');
146
        }
147
    }
148
149
    /**
150
     * Removes all backend user groups from the compare list
151
     */
152
    public function removeAllFromCompareListAction(): void
153
    {
154
        $this->getBackendUser()->uc['beuser']['compareGroupUidList'] = [];
155
        $this->getBackendUser()->writeUC();
156
        $this->redirect('index');
157
    }
158
159
    protected function getBackendUser(): BackendUserAuthentication
160
    {
161
        return $GLOBALS['BE_USER'];
162
    }
163
164
    /**
165
     * @return ServerRequestInterface
166
     */
167
    protected function getRequest(): ServerRequestInterface
168
    {
169
        return $GLOBALS['TYPO3_REQUEST'];
170
    }
171
}
172