Completed
Push — master ( 8fc13e...c19aa3 )
by Axel
06:21
created

AccountController::menuAction()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 15
nc 3
nop 3
dl 0
loc 28
rs 9.2222
c 2
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersModule\Controller;
15
16
use Locale;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Intl\Languages;
21
use Symfony\Component\Routing\Annotation\Route;
22
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
23
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
24
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
25
use Zikula\MenuModule\ExtensionMenu\ExtensionMenuCollector;
26
use Zikula\MenuModule\ExtensionMenu\ExtensionMenuInterface;
27
use Zikula\PermissionsModule\Annotation\PermissionCheck;
28
use Zikula\UsersModule\Api\ApiInterface\CurrentUserApiInterface;
29
use Zikula\UsersModule\Constant;
30
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
31
use Zikula\UsersModule\Entity\UserEntity;
32
use Zikula\UsersModule\Form\Type\ChangeLanguageType;
33
34
/**
35
 * @Route("/account")
36
 * @PermissionCheck("read")
37
 */
38
class AccountController extends AbstractController
39
{
40
    /**
41
     * @Route("")
42
     * @Template("@ZikulaUsersModule/Account/menu.html.twig")
43
     *
44
     * @throws AccessDeniedException Thrown if the user isn't logged in
45
     */
46
    public function menuAction(
47
        CurrentUserApiInterface $currentUserApi,
48
        ExtensionMenuCollector $extensionMenuCollector,
49
        VariableApiInterface $variableApi
50
    ): array {
51
        if (!$currentUserApi->isLoggedIn()) {
52
            throw new AccessDeniedException();
53
        }
54
55
        $accountMenus = [];
56
        if ($currentUserApi->isLoggedIn()) {
57
            $extensionMenuCollector->getAllByType(ExtensionMenuInterface::TYPE_ACCOUNT);
58
            $accountMenus = $extensionMenuCollector->getAllByType(ExtensionMenuInterface::TYPE_ACCOUNT);
59
            $displayIcon = $variableApi->get('ZikulaUsersModule', Constant::MODVAR_ACCOUNT_DISPLAY_GRAPHICS, Constant::DEFAULT_ACCOUNT_DISPLAY_GRAPHICS);
60
61
            foreach ($accountMenus as $accountMenu) {
62
                /** @var \Knp\Menu\ItemInterface $accountMenu */
63
                $accountMenu->setChildrenAttribute('class', 'list-group');
64
                foreach ($accountMenu->getChildren() as $child) {
65
                    $child->setAttribute('class', 'list-group-item');
66
                    $icon = $child->getAttribute('icon');
67
                    $icon = $displayIcon ? $icon . ' fa-fw fa-2x' : null;
68
                    $child->setAttribute('icon', $icon);
69
                }
70
            }
71
        }
72
73
        return ['accountMenus' => $accountMenus];
74
    }
75
76
    /**
77
     * @Route("/change-language")
78
     * @Template("@ZikulaUsersModule/Account/changeLanguage.html.twig")
79
     *
80
     * @return array|RedirectResponse
81
     *
82
     * @throws AccessDeniedException Thrown if the user isn't logged in
83
     */
84
    public function changeLanguageAction(
85
        Request $request,
86
        CurrentUserApiInterface $currentUserApi,
87
        UserRepositoryInterface $userRepository
88
    ) {
89
        if (!$currentUserApi->isLoggedIn()) {
90
            throw new AccessDeniedException();
91
        }
92
        $form = $this->createForm(ChangeLanguageType::class, [
93
            'locale' => $currentUserApi->get('locale')
94
        ]);
95
        $form->handleRequest($request);
96
        if ($form->isSubmitted()) {
97
            $locale = $this->getParameter('locale');
98
            if ($form->get('submit')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

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

98
            if ($form->get('submit')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
99
                $data = $form->getData();
100
                $locale = !empty($data['locale']) ? $data['locale'] : $locale;
101
                /** @var UserEntity $userEntity */
102
                $userEntity = $userRepository->find($currentUserApi->get('uid'));
103
                $userEntity->setLocale($locale);
104
                $userRepository->persistAndFlush($userEntity);
105
                if ($request->hasSession() && ($session = $request->getSession())) {
106
                    $session->set('_locale', $locale);
107
                }
108
                Locale::setDefault($locale);
109
                $langText = Languages::getName($locale);
110
                $this->addFlash('success', $this->trans('Language changed to %lang%', ['%lang%' => $langText], 'messages', $locale));
111
            } elseif ($form->get('cancel')->isClicked()) {
112
                $this->addFlash('status', 'Operation cancelled.');
113
            }
114
115
            return $this->redirectToRoute('zikulausersmodule_account_menu', ['_locale' => $locale]);
116
        }
117
118
        return [
119
            'form' => $form->createView()
120
        ];
121
    }
122
}
123