1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BlockManager. |
4
|
|
|
* |
5
|
|
|
* @author Shea Dawson <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
class BlockManager extends Object |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Define areas and config on a per theme basis. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
**/ |
14
|
|
|
private static $themes = array(); |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Use default ContentBlock class. |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
**/ |
21
|
|
|
private static $use_default_blocks = true; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Show a block area preview button in CMS |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
**/ |
28
|
|
|
private static $block_area_preview = true; |
|
|
|
|
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Gets an array of all areas defined for the current theme. |
37
|
|
|
* |
38
|
|
|
* @param string $theme |
39
|
|
|
* @param bool $keyAsValue |
40
|
|
|
* |
41
|
|
|
* @return array $areas |
42
|
|
|
**/ |
43
|
|
|
public function getAreasForTheme($theme = null, $keyAsValue = true) |
44
|
|
|
{ |
45
|
|
|
$theme = $theme ? $theme : $this->getTheme(); |
46
|
|
|
if (!$theme) { |
47
|
|
|
return false; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
$config = $this->config()->get('themes'); |
50
|
|
|
if (!isset($config[$theme]['areas'])) { |
51
|
|
|
return false; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
$areas = $config[$theme]['areas']; |
54
|
|
|
$areas = $keyAsValue ? ArrayLib::valuekey(array_keys($areas)) : $areas; |
55
|
|
|
if (count($areas)) { |
56
|
|
|
foreach ($areas as $k => $v) { |
57
|
|
|
$areas[$k] = $keyAsValue ? FormField::name_to_label($k) : $v; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $areas; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets an array of all areas defined for the current theme that are compatible |
66
|
|
|
* with pages of type $class. |
67
|
|
|
* |
68
|
|
|
* @param string $class |
69
|
|
|
* |
70
|
|
|
* @return array $areas |
71
|
|
|
**/ |
72
|
|
|
public function getAreasForPageType($class) |
73
|
|
|
{ |
74
|
|
|
$areas = $this->getAreasForTheme(null, false); |
75
|
|
|
|
76
|
|
|
if (!$areas) { |
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
foreach ($areas as $area => $config) { |
81
|
|
|
if (!is_array($config)) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
if (isset($config['except'])) { |
|
|
|
|
86
|
|
|
$except = $config['except']; |
87
|
|
|
if (is_array($except) |
88
|
|
|
? in_array($class, $except) |
89
|
|
|
: $except == $class |
90
|
|
|
) { |
91
|
|
|
unset($areas[$area]); |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
if (isset($config['only'])) { |
|
|
|
|
97
|
|
|
$only = $config['only']; |
98
|
|
|
if (is_array($only) |
99
|
|
|
? !in_array($class, $only) |
100
|
|
|
: $only != $class |
101
|
|
|
) { |
102
|
|
|
unset($areas[$area]); |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (count($areas)) { |
109
|
|
|
foreach ($areas as $k => $v) { |
110
|
|
|
$areas[$k] = _t('Block.BlockAreaName.'.$k, FormField::name_to_label($k)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $areas; |
114
|
|
|
} else { |
115
|
|
|
return $areas; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getBlockClasses() |
120
|
|
|
{ |
121
|
|
|
$classes = ArrayLib::valuekey(ClassInfo::subclassesFor('Block')); |
122
|
|
|
array_shift($classes); |
123
|
|
|
foreach ($classes as $k => $v) { |
124
|
|
|
$classes[$k] = singleton($k)->singular_name(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!Config::inst()->get('BlockManager', 'use_default_blocks')) { |
128
|
|
|
unset($classes['ContentBlock']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($disabledArr = Config::inst()->get('BlockManager', 'disabled_blocks')) { |
132
|
|
|
foreach ($disabledArr as $k => $v) { |
133
|
|
|
unset($classes[$v]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $classes; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/* |
141
|
|
|
* Get the current/active theme or 'default' to support theme-less sites |
142
|
|
|
*/ |
143
|
|
|
private function getTheme() |
144
|
|
|
{ |
145
|
|
|
$currentTheme = Config::inst()->get('SSViewer', 'theme'); |
146
|
|
|
|
147
|
|
|
// check directly on SiteConfig incase ContentController hasn't set |
148
|
|
|
// the theme yet in ContentController->init() |
149
|
|
|
if (!$currentTheme && class_exists('SiteConfig')) { |
150
|
|
|
$currentTheme = SiteConfig::current_site_config()->Theme; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $currentTheme ? $currentTheme : 'default'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/* |
157
|
|
|
* Get the block config for the current theme |
158
|
|
|
*/ |
159
|
|
|
private function getThemeConfig() |
160
|
|
|
{ |
161
|
|
|
$theme = $this->getTheme(); |
162
|
|
|
$config = $this->config()->get('themes'); |
163
|
|
|
|
164
|
|
|
return $theme && isset($config[$theme]) ? $config[$theme] : null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/* |
168
|
|
|
* Usage of BlockSets configurable from yaml |
169
|
|
|
*/ |
170
|
|
|
public function getUseBlockSets() |
171
|
|
|
{ |
172
|
|
|
$config = $this->getThemeConfig(); |
173
|
|
|
|
174
|
|
|
return isset($config['use_blocksets']) ? $config['use_blocksets'] : true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/* |
178
|
|
|
* Exclusion of blocks from page types defined in yaml |
179
|
|
|
*/ |
180
|
|
|
public function getExcludeFromPageTypes() |
181
|
|
|
{ |
182
|
|
|
$config = $this->getThemeConfig(); |
183
|
|
|
|
184
|
|
|
return isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : array(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/* |
188
|
|
|
* getWhiteListedPageTypes optionally configured by the developer |
189
|
|
|
*/ |
190
|
|
|
public function getWhiteListedPageTypes() |
191
|
|
|
{ |
192
|
|
|
$config = $this->getThemeConfig(); |
193
|
|
|
return isset($config['pagetype_whitelist']) ? $config['pagetype_whitelist'] : array(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/* |
197
|
|
|
* getBlackListedPageTypes optionally configured by the developer |
198
|
|
|
* Includes blacklisted page types defined in the old exclude_from_page_types array |
199
|
|
|
*/ |
200
|
|
|
public function getBlackListedPageTypes() |
201
|
|
|
{ |
202
|
|
|
$config = $this->getThemeConfig(); |
203
|
|
|
$legacy = isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : array(); |
204
|
|
|
$current = isset($config['pagetype_blacklist']) ? $config['pagetype_blacklist'] : array(); |
205
|
|
|
return array_merge($legacy, $current); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/* |
209
|
|
|
* Usage of extra css classes configurable from yaml |
210
|
|
|
*/ |
211
|
|
|
public function getUseExtraCSSClasses() |
212
|
|
|
{ |
213
|
|
|
$config = $this->getThemeConfig(); |
214
|
|
|
|
215
|
|
|
return isset($config['use_extra_css_classes']) ? $config['use_extra_css_classes'] : false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/* |
219
|
|
|
* Prefix for the default CSSClasses |
220
|
|
|
*/ |
221
|
|
|
public function getPrefixDefaultCSSClasses() |
222
|
|
|
{ |
223
|
|
|
$config = $this->getThemeConfig(); |
224
|
|
|
|
225
|
|
|
return isset($config['prefix_default_css_classes']) ? $config['prefix_default_css_classes'] : false; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.