Passed
Push — main ( 674e66...68245a )
by Axel
04:12
created

configureUserMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - 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\ThemeBundle\Controller\Dashboard;
15
16
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
17
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
18
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
19
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
20
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
21
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
22
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\KernelInterface;
25
use Symfony\Component\Security\Core\User\UserInterface;
26
use Symfony\Component\Translation\TranslatableMessage;
27
use Zikula\Bundle\CoreBundle\Site\SiteDefinitionInterface;
28
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuCollector;
29
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuInterface;
30
use Zikula\ThemeBundle\Helper\AdminBundleHelper;
31
use Zikula\ThemeBundle\Helper\AdminCategoryHelper;
32
use Zikula\ThemeBundle\Helper\UserMenuExtensionHelper;
33
34
abstract class AbstractThemedDashboardController extends AbstractDashboardController
35
{
36
    public function __construct(
37
        protected readonly KernelInterface $kernel,
38
        protected readonly AdminUrlGenerator $urlGenerator,
39
        protected readonly AdminCategoryHelper $adminCategoryHelper,
40
        protected readonly AdminBundleHelper $adminBundleHelper,
41
        protected readonly ExtensionMenuCollector $extensionMenuCollector,
42
        protected readonly UserMenuExtensionHelper $userMenuExtensionHelper,
43
        protected readonly SiteDefinitionInterface $site,
44
        protected readonly array $themeConfig
45
    ) {
46
    }
47
48
    abstract protected function getName(): string;
49
50
    protected function getDashboardWithBranding(bool $showLogo, ?TranslatableMessage $title = null): Dashboard
51
    {
52
        $siteName = $this->site->getName();
53
        if ($showLogo) {
54
            $logoPath = $this->site->getMobileLogoPath() ?? $this->site->getLogoPath();
55
            $logo = $logoPath ? '<img src="' . $logoPath . '" alt="' . str_replace('"', '', $siteName) . '" /><br />' : '';
56
        } else {
57
            $logo = '';
58
        }
59
60
        $titlePrefix = $logo ?: $siteName;
61
        $titleSuffix = $title ? ' ' . $title : '';
62
63
        $dashboard = parent::configureDashboard()
64
            ->setTitle($titlePrefix . $titleSuffix);
65
66
        $iconPath = $this->site->getIconPath();
67
        if (null !== $iconPath) {
68
            $dashboard->setFaviconPath($iconPath);
69
        }
70
71
        $dashboard->renderContentMaximized($this->themeConfig['view']['content_maximized'])
72
            ->renderSidebarMinimized($this->themeConfig['view']['sidebar_minimized'])
73
            ->setTranslationDomain('dashboard');
74
75
        return $dashboard;
76
    }
77
78
    public function configureAssets(): Assets
79
    {
80
        return parent::configureAssets()
81
            ->addCssFile('bundles/zikulatheme/dashboard/' . $this->getName() . '.css')
82
            ->addJsFile('bundles/zikulatheme/dashboard/' . $this->getName() . '.js')
83
        ;
84
    }
85
86
    public function configureCrud(): Crud
87
    {
88
        return parent::configureCrud()
89
            ->overrideTemplate('layout', '@ZikulaTheme/Dashboard/layout_' . $this->getName() . '.html.twig')
90
        ;
91
    }
92
93
    public function configureUserMenu(UserInterface $user): UserMenu
94
    {
95
        return $this->userMenuExtensionHelper->configureUserMenu(parent::configureUserMenu($user), $user);
96
    }
97
98
    public function index(): Response
99
    {
100
        $contentConfig = $this->themeConfig['content'];
101
        if (null !== $contentConfig['template']) {
102
            // render a custom template
103
            return $this->render($contentConfig['template']);
104
        }
105
106
        if (null !== $contentConfig['redirect']['crud']) {
107
            // redirect to a CRUD controller page
108
            return $this->redirect($this->urlGenerator->setController($contentConfig['redirect']['crud'])->generateUrl());
109
        }
110
111
        if (null !== $contentConfig['redirect']['route']) {
112
            // redirect to a Symfony route
113
            return $this->redirect($this->urlGenerator->setRoute($contentConfig['redirect']['route'], $contentConfig['redirect']['route_parameters'])->generateUrl());
114
        }
115
116
        // render EAB welcome page
117
        return parent::index();
118
    }
119
}
120