Passed
Push — main ( 32046f...b819d2 )
by Axel
04:42
created

AbstractThemedDashboardController::index()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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