configureAssets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
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 - 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\UserMenu;
20
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
21
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
22
use Symfony\Component\DependencyInjection\Attribute\Autowire;
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\CoreBundle\Api\ApiInterface\LocaleApiInterface;
28
use Zikula\CoreBundle\Site\SiteDefinitionInterface;
29
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuCollector;
30
use Zikula\ThemeBundle\Helper\AdminCategoryHelper;
31
use Zikula\ThemeBundle\Helper\UserMenuExtensionHelper;
32
33
abstract class AbstractThemedDashboardController extends AbstractDashboardController
34
{
35
    protected const INVALID_LOCALE = 'invalid';
36
    protected array $enabledLocales;
37
    protected string $defaultLocale;
38
39
    public function __construct(
40
        protected readonly KernelInterface $kernel,
41
        protected readonly AdminUrlGenerator $urlGenerator,
42
        protected readonly AdminCategoryHelper $adminCategoryHelper,
43
        protected readonly ExtensionMenuCollector $extensionMenuCollector,
44
        protected readonly UserMenuExtensionHelper $userMenuExtensionHelper,
45
        protected readonly SiteDefinitionInterface $site,
46
        protected readonly array $themeConfig,
47
        #[Autowire(param: 'kernel.enabled_locales')]
48
        array $enabledLocales,
49
        #[Autowire(param: 'kernel.default_locale')]
50
        string $defaultLocale,
51
        protected readonly LocaleApiInterface $localeApi
52
    ) {
53
        $this->enabledLocales = $enabledLocales ?: $localeApi->getSupportedLocales();
54
        $this->defaultLocale = $defaultLocale;
55
    }
56
57
    abstract protected function getName(): string;
58
59
    protected function getDashboardWithBranding(bool $showLogo, ?TranslatableMessage $title = null): Dashboard
60
    {
61
        $siteName = $this->site->getName();
62
        if ($showLogo) {
63
            $logoPath = $this->site->getMobileLogoPath() ?? $this->site->getLogoPath();
64
            $logo = $logoPath ? '<img src="' . $logoPath . '" alt="' . str_replace('"', '', $siteName) . '" /><br />' : '';
65
        } else {
66
            $logo = '';
67
        }
68
69
        $titlePrefix = $logo ?: $siteName;
70
        $titleSuffix = $title ? ' ' . $title : '';
71
72
        $dashboard = parent::configureDashboard()
73
            ->setTitle($titlePrefix . $titleSuffix)
74
            ->renderContentMaximized($this->themeConfig['view']['content_maximized'])
75
            ->renderSidebarMinimized($this->themeConfig['view']['sidebar_minimized'])
76
            ->setTranslationDomain('dashboard')
77
            ->setLocales($this->enabledLocales);
78
79
        return $dashboard;
80
    }
81
82
    public function configureAssets(): Assets
83
    {
84
        return parent::configureAssets()
85
            ->addCssFile('bundles/zikulatheme/dashboard/' . $this->getName() . '.css')
86
            ->addJsFile('bundles/zikulatheme/dashboard/' . $this->getName() . '.js')
87
        ;
88
    }
89
90
    public function configureCrud(): Crud
91
    {
92
        return parent::configureCrud()
93
            ->overrideTemplate('layout', '@ZikulaTheme/Dashboard/layout_' . $this->getName() . '.html.twig')
94
        ;
95
    }
96
97
    public function configureUserMenu(UserInterface $user): UserMenu
98
    {
99
        return $this->userMenuExtensionHelper->configureUserMenu(parent::configureUserMenu($user), $user);
100
    }
101
102
    public function index(): Response
103
    {
104
        $contentConfig = $this->themeConfig['content'];
105
        if (null !== $contentConfig['template']) {
106
            // render a custom template
107
            return $this->render($contentConfig['template']);
108
        }
109
110
        if (null !== $contentConfig['redirect']['crud']) {
111
            // redirect to a CRUD controller page
112
            return $this->redirect($this->urlGenerator->setController($contentConfig['redirect']['crud'])->generateUrl());
113
        }
114
115
        if (null !== $contentConfig['redirect']['route']) {
116
            // redirect to a Symfony route
117
            return $this->redirect($this->urlGenerator->setRoute($contentConfig['redirect']['route'], $contentConfig['redirect']['route_parameters'])->generateUrl());
118
        }
119
120
        // render EAB welcome page
121
        return parent::index();
122
    }
123
}
124