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