1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SheaDawson\Blocks\Extensions; |
4
|
|
|
|
5
|
|
|
use SheaDawson\Blocks\Model\Block; |
6
|
|
|
use SheaDawson\Blocks\Model\Blockset; |
7
|
|
|
use SheaDawson\Blocks\BlockManager; |
8
|
|
|
use SheaDawson\Blocks\Forms\GridFieldConfigBlockManager; |
9
|
|
|
use SilverStripe\CMS\Model\SiteTreeExtension; |
10
|
|
|
use SilverStripe\Forms\ReadonlyField; |
11
|
|
|
use SilverStripe\Forms\CheckboxField; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
use SilverStripe\Forms\LiteralField; |
14
|
|
|
use SilverStripe\Forms\ListboxField; |
15
|
|
|
use SilverStripe\Forms\Tab; |
16
|
|
|
use SilverStripe\Forms\GridField\GridField; |
17
|
|
|
use SilverStripe\View\SSViewer; |
18
|
|
|
use SilverStripe\ORM\ArrayList; |
19
|
|
|
use SilverStripe\Security\Permission; |
20
|
|
|
use SilverStripe\Control\Controller; |
21
|
|
|
use SilverStripeAustralia\GridFieldExtensions\GridFieldOrderableRows; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* BlocksSiteTreeExtension. |
25
|
|
|
* |
26
|
|
|
* @author Shea Dawson <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class BlocksSiteTreeExtension extends SiteTreeExtension |
29
|
|
|
{ |
30
|
|
|
private static $db = [ |
|
|
|
|
31
|
|
|
'InheritBlockSets' => 'Boolean', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private static $many_many = [ |
|
|
|
|
35
|
|
|
"Blocks" => Block::class, |
36
|
|
|
"DisabledBlocks" => Block::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public static $many_many_extraFields = [ |
40
|
|
|
'Blocks' => [ |
41
|
|
|
'Sort' => 'Int', |
42
|
|
|
'BlockArea' => 'Varchar', |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
private static $defaults = [ |
|
|
|
|
47
|
|
|
'InheritBlockSets' => 1, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
private static $dependencies = [ |
|
|
|
|
51
|
|
|
'blockManager' => '%$blockManager', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
public $blockManager; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if the Blocks CMSFields should be displayed for this Page |
59
|
|
|
* |
60
|
|
|
* @return boolean |
61
|
|
|
**/ |
62
|
|
|
public function showBlocksFields() |
63
|
|
|
{ |
64
|
|
|
$whiteList = $this->blockManager->getWhiteListedPageTypes(); |
65
|
|
|
$blackList = $this->blockManager->getBlackListedPageTypes(); |
66
|
|
|
|
67
|
|
|
if (in_array($this->owner->ClassName, $blackList)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (count($whiteList) && !in_array($this->owner->ClassName, $whiteList)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!Permission::check('BLOCK_EDIT')) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Block manager for Pages. |
84
|
|
|
* */ |
85
|
|
|
public function updateCMSFields(FieldList $fields) |
86
|
|
|
{ |
87
|
|
|
if ($fields->fieldByName('Root.Blocks') || !$this->showBlocksFields()) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$areas = $this->blockManager->getAreasForPageType($this->owner->ClassName); |
92
|
|
|
|
93
|
|
|
if ($areas && count($areas)) { |
94
|
|
|
$fields->addFieldToTab('Root', new Tab('Blocks', _t('Block.PLURALNAME', 'Blocks'))); |
95
|
|
|
if (BlockManager::config()->get('block_area_preview')) { |
96
|
|
|
$fields->addFieldToTab('Root.Blocks', |
97
|
|
|
LiteralField::create('PreviewLink', $this->areasPreviewButton())); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Blocks related directly to this Page |
101
|
|
|
$gridConfig = GridFieldConfigBlockManager::create(true, true, true, true) |
102
|
|
|
->addExisting($this->owner->class) |
|
|
|
|
103
|
|
|
//->addBulkEditing() |
104
|
|
|
// ->addComponent(new GridFieldOrderableRows()) // Comment until below TODO is complete. |
|
|
|
|
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
// TODO it seems this sort is not being applied... |
109
|
|
|
$gridSource = $this->owner->Blocks(); |
110
|
|
|
// ->sort(array( |
|
|
|
|
111
|
|
|
// "FIELD(SiteTree_Blocks.BlockArea, '" . implode("','", array_keys($areas)) . "')" => '', |
|
|
|
|
112
|
|
|
// 'SiteTree_Blocks.Sort' => 'ASC', |
|
|
|
|
113
|
|
|
// 'Name' => 'ASC' |
|
|
|
|
114
|
|
|
// )); |
115
|
|
|
|
116
|
|
|
$fields->addFieldToTab('Root.Blocks', GridField::create('Blocks', _t('Block.PLURALNAME', 'Blocks'), $gridSource, $gridConfig)); |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
// Blocks inherited from BlockSets |
120
|
|
|
if ($this->blockManager->getUseBlockSets()) { |
121
|
|
|
$inheritedBlocks = $this->getBlocksFromAppliedBlockSets(null, true); |
122
|
|
|
|
123
|
|
|
if ($inheritedBlocks->count()) { |
124
|
|
|
$activeInherited = $this->getBlocksFromAppliedBlockSets(null, false); |
125
|
|
|
|
126
|
|
|
if ($activeInherited->count()) { |
127
|
|
|
$fields->addFieldsToTab('Root.Blocks', [ |
128
|
|
|
GridField::create('InheritedBlockList', _t('BlocksSiteTreeExtension.BlocksInheritedFromBlockSets', 'Blocks Inherited from Block Sets'), $activeInherited, |
129
|
|
|
GridFieldConfigBlockManager::create(false, false, false)), |
130
|
|
|
LiteralField::create('InheritedBlockListTip', "<p class='message'>"._t('BlocksSiteTreeExtension.InheritedBlocksEditLink', 'Tip: Inherited blocks can be edited in the {link_start}Block Admin area{link_end}', '', ['link_start' => '<a href="admin/block-admin">', 'link_end' => '</a>']).'<p>'), |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$fields->addFieldToTab('Root.Blocks', |
135
|
|
|
ListboxField::create('DisabledBlocks', _t('BlocksSiteTreeExtension.DisableInheritedBlocks', 'Disable Inherited Blocks'), |
136
|
|
|
$inheritedBlocks->map('ID', 'Title'), null, null, true) |
137
|
|
|
->setDescription(_t('BlocksSiteTreeExtension.DisableInheritedBlocksDescription', 'Select any inherited blocks that you would not like displayed on this page.')) |
138
|
|
|
); |
139
|
|
|
} else { |
140
|
|
|
$fields->addFieldToTab('Root.Blocks', |
141
|
|
|
ReadonlyField::create('DisabledBlocksReadOnly', _t('BlocksSiteTreeExtension.DisableInheritedBlocks', 'Disable Inherited Blocks'), |
142
|
|
|
_t('BlocksSiteTreeExtension.NoInheritedBlocksToDisable','This page has no inherited blocks to disable.'))); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$fields->addFieldToTab('Root.Blocks', |
146
|
|
|
CheckboxField::create('InheritBlockSets', _t('BlocksSiteTreeExtension.InheritBlocksFromBlockSets', 'Inherit Blocks from Block Sets'))); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Called from templates to get rendered blocks for the given area. |
153
|
|
|
* |
154
|
|
|
* @param string $area |
155
|
|
|
* @param int $limit Limit the items to this number, or null for no limit |
156
|
|
|
*/ |
157
|
|
|
public function BlockArea($area, $limit = null) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
if ($this->owner->ID <= 0) { |
160
|
|
|
return; |
161
|
|
|
} // blocks break on fake pages ie Security/login |
162
|
|
|
|
163
|
|
|
$list = $this->getBlockList($area); |
164
|
|
|
|
165
|
|
|
foreach ($list as $block) { |
166
|
|
|
if (!$block->canView()) { |
167
|
|
|
$list->remove($block); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($limit !== null) { |
172
|
|
|
$list = $list->limit($limit); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$data = []; |
176
|
|
|
$data['HasBlockArea'] = ($this->owner->canEdit() && isset($_REQUEST['block_preview']) && $_REQUEST['block_preview']) || $list->Count() > 0; |
177
|
|
|
$data['BlockArea'] = $list; |
178
|
|
|
$data['AreaID'] = $area; |
179
|
|
|
|
180
|
|
|
$data = $this->owner->customise($data); |
181
|
|
|
|
182
|
|
|
$template = ['BlockArea_'.$area]; |
183
|
|
|
|
184
|
|
|
if (SSViewer::hasTemplate($template)) { |
185
|
|
|
return $data->renderWith($template); |
186
|
|
|
} else { |
187
|
|
|
return $data->renderWith('BlockArea'); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function HasBlockArea($area) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
if ($this->owner->canEdit() && isset($_REQUEST['block_preview']) && $_REQUEST['block_preview']) { |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$list = $this->getBlockList($area); |
198
|
|
|
|
199
|
|
|
foreach ($list as $block) { |
200
|
|
|
if (!$block->canView()) { |
201
|
|
|
$list->remove($block); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $list->Count() > 0; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get a merged list of all blocks on this page and ones inherited from BlockSets. |
210
|
|
|
* |
211
|
|
|
* @param string|null $area filter by block area |
212
|
|
|
* @param bool $includeDisabled Include blocks that have been explicitly excluded from this page |
213
|
|
|
* i.e. blocks from block sets added to the "disable inherited blocks" list |
214
|
|
|
* |
215
|
|
|
* @return ArrayList |
216
|
|
|
* */ |
217
|
|
|
public function getBlockList($area = null, $includeDisabled = false) |
218
|
|
|
{ |
219
|
|
|
$includeSets = $this->blockManager->getUseBlockSets() && $this->owner->InheritBlockSets; |
220
|
|
|
$blocks = ArrayList::create(); |
221
|
|
|
|
222
|
|
|
// get blocks directly linked to this page |
223
|
|
|
$nativeBlocks = $this->owner->Blocks()->sort('Sort'); |
224
|
|
|
if ($area) { |
|
|
|
|
225
|
|
|
$nativeBlocks = $nativeBlocks->filter('BlockArea', $area); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if ($nativeBlocks->count()) { |
229
|
|
|
foreach ($nativeBlocks as $block) { |
230
|
|
|
$blocks->add($block); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// get blocks from BlockSets |
235
|
|
|
if ($includeSets) { |
236
|
|
|
$blocksFromSets = $this->getBlocksFromAppliedBlockSets($area, $includeDisabled); |
237
|
|
|
if ($blocksFromSets->count()) { |
238
|
|
|
// merge set sources |
239
|
|
|
foreach ($blocksFromSets as $block) { |
240
|
|
|
if (!$blocks->find('ID', $block->ID)) { |
241
|
|
|
if ($block->AboveOrBelow == 'Above') { |
242
|
|
|
$blocks->unshift($block); |
243
|
|
|
} else { |
244
|
|
|
$blocks->push($block); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $blocks; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get Any BlockSets that apply to this page. |
256
|
|
|
* |
257
|
|
|
* @return ArrayList |
258
|
|
|
* */ |
259
|
|
|
public function getAppliedSets() |
260
|
|
|
{ |
261
|
|
|
$list = ArrayList::create(); |
262
|
|
|
if (!$this->owner->InheritBlockSets) { |
263
|
|
|
return $list; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$sets = BlockSet::get()->where("(PageTypesValue IS NULL) OR (PageTypesValue LIKE '%:\"{$this->owner->ClassName}%')"); |
267
|
|
|
$ancestors = $this->owner->getAncestors()->column('ID'); |
268
|
|
|
|
269
|
|
|
foreach ($sets as $set) { |
270
|
|
|
$restrictedToParerentIDs = $set->PageParents()->column('ID'); |
271
|
|
|
if (count($restrictedToParerentIDs)) { |
272
|
|
|
// check whether the set should include selected parent, in which case check whether |
273
|
|
|
// it was in the restricted parents list. If it's not, or if include parentpage |
274
|
|
|
// wasn't selected, we check the ancestors of this page. |
275
|
|
|
if ($set->IncludePageParent && in_array($this->owner->ID, $restrictedToParerentIDs)) { |
276
|
|
|
$list->add($set); |
277
|
|
|
} else { |
278
|
|
|
if (count($ancestors)) { |
279
|
|
|
foreach ($ancestors as $ancestor) { |
280
|
|
|
if (in_array($ancestor, $restrictedToParerentIDs)) { |
281
|
|
|
$list->add($set); |
282
|
|
|
continue; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} else { |
288
|
|
|
$list->add($set); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $list; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get all Blocks from BlockSets that apply to this page. |
297
|
|
|
* |
298
|
|
|
* @return ArrayList |
299
|
|
|
* */ |
300
|
|
|
public function getBlocksFromAppliedBlockSets($area = null, $includeDisabled = false) |
301
|
|
|
{ |
302
|
|
|
$blocks = ArrayList::create(); |
303
|
|
|
$sets = $this->getAppliedSets(); |
304
|
|
|
|
305
|
|
|
if (!$sets->count()) { |
306
|
|
|
return $blocks; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
foreach ($sets as $set) { |
310
|
|
|
$setBlocks = $set->Blocks()->sort('Sort DESC'); |
311
|
|
|
|
312
|
|
|
if (!$includeDisabled) { |
313
|
|
|
$setBlocks = $setBlocks->exclude('ID', $this->owner->DisabledBlocks()->column('ID')); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
if ($area) { |
317
|
|
|
$setBlocks = $setBlocks->filter('BlockArea', $area); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$blocks->merge($setBlocks); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$blocks->removeDuplicates(); |
324
|
|
|
|
325
|
|
|
return $blocks; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Get's the link for a block area preview button. |
330
|
|
|
* |
331
|
|
|
* @return string |
332
|
|
|
* */ |
333
|
|
|
public function areasPreviewLink() |
334
|
|
|
{ |
335
|
|
|
return Controller::join_links($this->owner->Link(), '?block_preview=1'); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Get's html for a block area preview button. |
340
|
|
|
* |
341
|
|
|
* @return string |
342
|
|
|
* */ |
343
|
|
|
public function areasPreviewButton() |
344
|
|
|
{ |
345
|
|
|
return "<a class='btn btn-primary' style='font-style:normal;' href='".$this->areasPreviewLink()."' target='_blank'>"._t('BlocksSiteTreeExtension.PreviewBlockAreasLink', 'Preview Block Areas for this page').'</a>'; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.