|
1
|
|
|
<?php
|
|
2
|
|
|
class BlockinPage extends Extension {
|
|
3
|
|
|
|
|
4
|
|
|
protected static $belongs_many_many = array (
|
|
5
|
|
|
'Blocks' => 'Block'
|
|
6
|
|
|
);
|
|
7
|
|
|
|
|
8
|
|
|
public function updateCMSFields( FieldList $fields ) {
|
|
9
|
|
|
$blocks = $this->owner->Blocks();
|
|
10
|
|
|
if ($blocks->count() > 0) {
|
|
11
|
|
|
$blocks = Block::get()->filter(array(
|
|
12
|
|
|
'ID' => $blocks->column('ID'),
|
|
13
|
|
|
'showBlockbyClass' => false
|
|
14
|
|
|
))->sort('SortOrder','ASC');
|
|
15
|
|
|
}
|
|
16
|
|
|
$blocks_grid = $this->gridBuilder('Blocks', $blocks, '', true,'GridFieldConfig_RelationEditor');
|
|
17
|
|
|
$docked_grid = $this->gridBuilder('DockedBlocks', $this->dockedBlocks(), '');
|
|
18
|
|
|
|
|
19
|
|
|
$fields->addFieldToTab('Root.MyBlocks', $blocks_grid);
|
|
20
|
|
|
$fields->addFieldToTab('Root.DockedBlocks', $docked_grid);
|
|
21
|
|
|
}
|
|
22
|
|
|
|
|
23
|
|
|
private function gridBuilder($name, $source, $label = '', $canAdd = false, $gridHeaderType = 'GridFieldConfig_RecordEditor') {
|
|
24
|
|
|
/*
|
|
25
|
|
|
GridFieldConfig_Base
|
|
26
|
|
|
GridFieldConfig_RecordViewer
|
|
27
|
|
|
GridFieldConfig_RecordEditor
|
|
28
|
|
|
GridFieldConfig_RelationEditor
|
|
29
|
|
|
*/
|
|
30
|
|
|
if ($label == '') { $label = $name; }
|
|
31
|
|
|
$grid = new GridField($name, $label, $source);
|
|
32
|
|
|
$config = $gridHeaderType::create();
|
|
33
|
|
|
$config->removeComponentsByType('GridFieldAddNewButton');
|
|
34
|
|
|
if ( $canAdd ) {
|
|
35
|
|
|
$config->addComponents(
|
|
36
|
|
|
$multiClass = new GridFieldAddNewMultiClass(),
|
|
37
|
|
|
$sortable = new GridFieldSortableRows('SortOrder')
|
|
38
|
|
|
);
|
|
39
|
|
|
$subBlocks = ClassInfo::subclassesFor('Block');
|
|
40
|
|
|
unset($subBlocks['Block']);
|
|
41
|
|
|
$multiClass->setClasses($subBlocks);
|
|
42
|
|
|
}
|
|
43
|
|
|
$grid->setConfig($config);
|
|
44
|
|
|
return $grid;
|
|
45
|
|
|
}
|
|
46
|
|
|
|
|
47
|
|
|
private function dockedBlocks($baseOn = 'Class') {
|
|
48
|
|
|
$blocks = Block::get();
|
|
49
|
|
|
$IDs = array();
|
|
50
|
|
|
$ClassName = $this->owner->ClassName;
|
|
51
|
|
|
$Classes = $blocks->map('ID', 'shownInClass');
|
|
52
|
|
|
foreach ($Classes as $BlockID => $Class) {
|
|
53
|
|
|
$listedClasses = explode(',', $Class);
|
|
54
|
|
|
if (in_array($ClassName, $listedClasses)) {
|
|
55
|
|
|
$IDs[] = $BlockID;
|
|
56
|
|
|
}
|
|
57
|
|
|
}
|
|
58
|
|
|
$blocks = Block::get()->filter('ID', $IDs)->sort('SortOrder','ASC');
|
|
59
|
|
|
return $blocks;
|
|
60
|
|
|
}
|
|
61
|
|
|
} |