Completed
Push — master ( 799397...1fa3fa )
by Axel
05:12
created

AccountController::menuAction()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 15
c 2
b 1
f 0
nc 3
nop 3
dl 0
loc 28
rs 8.8333
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\UsersModule\Api\ApiInterface\CurrentUserApiInterface;
28
use Zikula\UsersModule\Constant;
29
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
30
use Zikula\UsersModule\Entity\UserEntity;
31
use Zikula\UsersModule\Form\Type\ChangeLanguageType;
32
33
/**
34
 * @Route("/account")
35
 */
36
class AccountController extends AbstractController
37
{
38
    /**
39
     * @Route("")
40
     * @Template("@ZikulaUsersModule/Account/menu.html.twig")
41
     *
42
     * @throws AccessDeniedException Thrown if the user isn't logged in or hasn't read permissions for the module
43
     */
44
    public function menuAction(
45
        CurrentUserApiInterface $currentUserApi,
46
        ExtensionMenuCollector $extensionMenuCollector,
47
        VariableApiInterface $variableApi
48
    ): array {
49
        if (!$currentUserApi->isLoggedIn() && !$this->hasPermission('ZikulaUsersModule::', '::', ACCESS_READ)) {
50
            throw new AccessDeniedException();
51
        }
52
53
        $accountMenus = [];
54
        if ($currentUserApi->isLoggedIn()) {
55
            $extensionMenuCollector->getAllByType(ExtensionMenuInterface::TYPE_ACCOUNT);
56
            $accountMenus = $extensionMenuCollector->getAllByType(ExtensionMenuInterface::TYPE_ACCOUNT);
57
            $displayIcon = $variableApi->get('ZikulaUsersModule', Constant::MODVAR_ACCOUNT_DISPLAY_GRAPHICS, Constant::DEFAULT_ACCOUNT_DISPLAY_GRAPHICS);
58
59
            foreach ($accountMenus as $accountMenu) {
60
                /** @var \Knp\Menu\ItemInterface $accountMenu */
61
                $accountMenu->setChildrenAttribute('class', 'list-group');
62
                foreach ($accountMenu->getChildren() as $child) {
63
                    $child->setAttribute('class', 'list-group-item');
64
                    $icon = $child->getAttribute('icon');
65
                    $icon = $displayIcon ? $icon . ' fa-fw fa-2x' : null;
66
                    $child->setAttribute('icon', $icon);
67
                }
68
            }
69
        }
70
71
        return ['accountMenus' => $accountMenus];
72
    }
73
74
    /**
75
     * @Route("/change-language")
76
     * @Template("@ZikulaUsersModule/Account/changeLanguage.html.twig")
77
     *
78
     * @return array|RedirectResponse
79
     */
80
    public function changeLanguageAction(
81
        Request $request,
82
        CurrentUserApiInterface $currentUserApi,
83
        UserRepositoryInterface $userRepository
84
    ) {
85
        if (!$currentUserApi->isLoggedIn()) {
86
            throw new AccessDeniedException();
87
        }
88
        $form = $this->createForm(ChangeLanguageType::class, [
89
            'locale' => $currentUserApi->get('locale')
90
        ]);
91
        $form->handleRequest($request);
92
        if ($form->isSubmitted()) {
93
            $locale = $this->getParameter('locale');
94
            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

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