Completed
Push — master ( a7cf71...06c6ea )
by Craig
06:43
created

AbstractTheme   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getNameType() 0 4 1
A getServiceIds() 0 4 1
A getConfig() 0 4 1
A __construct() 0 10 3
A generateThemedResponse() 0 11 4
A generateThemedBlockContent() 0 16 2
A wrapBlockContentWithUniqueDiv() 0 11 1
A loadThemeVars() 0 6 2
A getThemeVars() 0 15 4
B getDefaultThemeVars() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\ThemeModule;
13
14
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Yaml\Yaml;
17
use Zikula\Core\AbstractBundle;
18
19
abstract class AbstractTheme extends AbstractBundle
20
{
21
    private $config;
22
23
    public function getNameType()
24
    {
25
        return 'Theme';
26
    }
27
28
    public function getServiceIds()
29
    {
30
        return [];
31
    }
32
33
    public function getConfig()
34
    {
35
        return $this->config;
36
    }
37
38
    /**
39
     * load the theme configuration from the config/theme.yml file
40
     */
41
    public function __construct()
42
    {
43
        $configPath = $this->getConfigPath() . '/theme.yml';
44
        if (file_exists($configPath)) {
45
            $this->config = Yaml::parse(file_get_contents($configPath));
46
            if (!isset($this->config['master'])) {
47
                throw new InvalidConfigurationException('Core-2.0 themes must have a defined master realm.');
48
            }
49
        }
50
    }
51
52
    /**
53
     * generate a response wrapped in the theme
54
     *   wrap the maincontent in a unique div.
55
     * @param string $realm
56
     * @param Response $response
57
     * @param string $moduleName
58
     * @return Response
59
     */
60
    public function generateThemedResponse($realm, Response $response, $moduleName = null)
61
    {
62
        $template = $this->config[$realm]['page'];
63
        $classes = $realm == 'home' ? 'z-homepage' : '' . (empty($classes) ? '' : ' ') . (isset($moduleName) ? 'z-module-' . $moduleName : '');
0 ignored issues
show
Bug introduced by
The variable $classes seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
64
        $content = $this->getContainer()->get('templating')->render('ZikulaThemeModule:Default:maincontent.html.twig', [
65
            'classes' => $classes,
66
            'maincontent' => $response->getContent()
67
        ]);
68
69
        return $this->getContainer()->get('templating')->renderResponse($this->name . ':' . $template, ['maincontent' => $content]);
70
    }
71
72
    /**
73
     * convert the block content to a theme-wrapped Response
74
     * @param string $realm
75
     * @param $positionName
76
     * @param string $blockContent
77
     * @param $blockTitle
78
     * @return string
79
     */
80
    public function generateThemedBlockContent($realm, $positionName, $blockContent, $blockTitle)
81
    {
82
        if (isset($this->config[$realm]['block']['positions'][$positionName])) {
83
            $template = $this->name . ':' . $this->config[$realm]['block']['positions'][$positionName];
84
        } else {
85
            // block position not defined, provide a default template
86
            $template = 'ZikulaThemeModule:Default:block.html.twig';
87
        }
88
89
        $templateParameters = [
90
            'title' => $blockTitle,
91
            'content' => $blockContent
92
        ];
93
94
        return $this->getContainer()->get('templating')->render($template, $templateParameters);
95
    }
96
97
    /**
98
     * Enclose themed block content in a unique div which is useful in applying styling.
99
     *
100
     * @param string $content
101
     * @param string $positionName
102
     * @param string $blockType
103
     * @param integer $bid
104
     * @return string
105
     */
106
    public function wrapBlockContentWithUniqueDiv($content, $positionName, $blockType, $bid)
107
    {
108
        $templateParams = [
109
            'position' => $positionName,
110
            'type' => $blockType,
111
            'bid' => $bid,
112
            'content' => $content
113
        ];
114
115
        return $this->getContainer()->get('templating')->render('ZikulaThemeModule:Default:blockwrapper.html.twig', $templateParams);
116
    }
117
118
    /**
119
     * load the themevars into the themeEngine global vars
120
     */
121
    public function loadThemeVars()
122
    {
123
        if ($this->getContainer()->has('zikula_core.common.theme.themevars')) {
124
            $this->getContainer()->get('zikula_core.common.theme.themevars')->replace($this->getThemeVars());
125
        }
126
    }
127
128
    /**
129
     * Get the theme variables from both the DB and the .yml file.
130
     * @return array|string
131
     */
132
    public function getThemeVars()
133
    {
134
        $dbVars = $this->container->get('zikula_extensions_module.api.variable')->getAll($this->name);
135
        if (empty($dbVars) && !is_array($dbVars)) {
136
            $dbVars = [];
137
        }
138
        $defaultVars = $this->getDefaultThemeVars();
139
        $combinedVars = array_merge($defaultVars, $dbVars);
140
        if (array_keys($dbVars) != array_keys($combinedVars)) {
141
            // First load of file or vars have been added to the .yml file.
142
            $this->container->get('zikula_extensions_module.api.variable')->setAll($this->name, $combinedVars);
143
        }
144
145
        return $combinedVars;
146
    }
147
148
    /**
149
     * Get the default values from variables.yml.
150
     * @return array
151
     */
152
    public function getDefaultThemeVars()
153
    {
154
        $defaultVars = [];
155
        $themeVarsPath = $this->getConfigPath() . '/variables.yml';
156
        if (file_exists($themeVarsPath)) {
157
            if ($this->getContainer()) {
158
                $yamlVars = Yaml::parse(file_get_contents($themeVarsPath));
159
                if (!is_array($yamlVars)) {
160
                    $yamlVars = [];
161
                }
162
                foreach ($yamlVars as $name => $definition) {
163
                    $defaultVars[$name] = $definition['default_value'];
164
                }
165
            }
166
        }
167
168
        return $defaultVars;
169
    }
170
}
171