This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractTheme.php
184 lines (158 loc) · 5.56 KB
/
AbstractTheme.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\ExtensionsModule;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Yaml;
use Twig\Environment;
use Zikula\ExtensionsModule\Api\VariableApi;
use Zikula\UsersModule\Api\CurrentUserApi;
abstract class AbstractTheme extends AbstractExtension
{
/**
* @var array
*/
private $config;
public function getNameType(): string
{
return 'Theme';
}
public function getConfig(): array
{
return $this->config;
}
/**
* Load the theme configuration from the config/theme.yaml file.
*/
public function __construct()
{
$this->config = [];
$configPath = $this->getConfigPath() . '/theme.yaml';
if (!file_exists($configPath)) {
return;
}
$this->config = Yaml::parse(file_get_contents($configPath));
if (!isset($this->config['master'])) {
throw new InvalidConfigurationException('Core-2.0 themes must have a defined master realm.');
}
}
/**
* Generate a response wrapped in the theme; wrap the main content in a unique div.
*/
public function generateThemedResponse(
string $realm,
Response $response,
string $moduleName = null
): Response {
$template = $this->config[$realm]['page'];
$classes = 'home' === $realm ? 'z-homepage' : '';
$classes .= (empty($classes) ? '' : ' ') . (isset($moduleName) ? 'z-module-' . $moduleName : '');
/* @var Environment $twig */
$twig = $this->getContainer()->get('twig');
$content = $twig->render('@ZikulaThemeModule/Default/maincontent.html.twig', [
'classes' => $classes,
'maincontent' => $response->getContent()
]);
$content = $twig->render('@' . $this->name . '/' . $template, ['maincontent' => $content]);
$response = new Response($content);
$isLoggedIn = $this->getContainer()->get(CurrentUserApi::class)->isLoggedIn();
if ($isLoggedIn) {
$response->headers->set('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', 'Sun, 02 Jan 1990 00:00:00 GMT');
}
return $response;
}
/**
* Convert the block content to a theme-wrapped response.
*/
public function generateThemedBlockContent(
string $realm,
string $positionName,
string $blockContent,
string $blockTitle
): string {
if (isset($this->config[$realm]['block']['positions'][$positionName])) {
$template = '@' . $this->name . '/' . $this->config[$realm]['block']['positions'][$positionName];
} else {
// block position not defined, provide a default template
$template = '@ZikulaThemeModule/Default/block.html.twig';
}
return $this->getContainer()->get('twig')->render($template, [
'title' => $blockTitle,
'content' => $blockContent
]);
}
/**
* Enclose themed block content in a unique div which is useful in applying styling.
*/
public function wrapBlockContentWithUniqueDiv(
string $content,
string $positionName,
string $blockType,
int $blockId
): string {
/* @var Environment $twig */
$twig = $this->getContainer()->get('twig');
return $twig->render('@ZikulaThemeModule/Default/blockwrapper.html.twig', [
'position' => $positionName,
'type' => $blockType,
'bid' => $blockId,
'content' => $content
]);
}
/**
* Load the theme variables into the theme engine global vars.
*/
public function loadThemeVars(): void
{
if ($this->getContainer()->has('zikula_core.common.theme.themevars')) {
$this->getContainer()->get('zikula_core.common.theme.themevars')->replace($this->getThemeVars());
}
}
/**
* Get the theme variables from both the DB and the YAML file.
*/
public function getThemeVars(): array
{
$variableApi = $this->container->get(VariableApi::class);
$dbVars = $variableApi->getAll($this->name);
if (empty($dbVars) && !is_array($dbVars)) {
$dbVars = [];
}
$defaultVars = $this->getDefaultThemeVars();
$combinedVars = array_merge($defaultVars, $dbVars);
if (array_keys($dbVars) !== array_keys($combinedVars)) {
// First load of file or vars have been added to the YAML file.
$variableApi->setAll($this->name, $combinedVars);
}
return $combinedVars;
}
/**
* Get the default values from variables.yaml.
*/
public function getDefaultThemeVars(): array
{
$defaultVars = [];
$themeVarsPath = $this->getConfigPath() . '/variables.yaml';
if (!file_exists($themeVarsPath)) {
return $defaultVars;
}
$yamlVars = Yaml::parse(file_get_contents($themeVarsPath));
if (!is_array($yamlVars)) {
$yamlVars = [];
}
foreach ($yamlVars as $name => $definition) {
$defaultVars[$name] = $definition['default_value'];
}
return $defaultVars;
}
}