1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SheaDawson\Blocks\Controllers; |
4
|
|
|
|
5
|
|
|
use SheaDawson\Blocks\Model\Block; |
6
|
|
|
use SheaDawson\Blocks\Model\BlockSet; |
7
|
|
|
use SheaDawson\Blocks\Forms\GridFieldConfigBlockManager; |
8
|
|
|
use SilverStripe\Admin\ModelAdmin; |
9
|
|
|
use SilverStripe\Versioned\Versioned; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* BlockAdmin. |
13
|
|
|
* |
14
|
|
|
* @author Shea Dawson <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class BlockAdmin extends ModelAdmin |
17
|
|
|
{ |
18
|
|
|
private static $managed_models = [ |
|
|
|
|
19
|
|
|
Block::class, |
20
|
|
|
Blockset::class, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
private static $url_segment = "block-admin"; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private static $menu_title = "Blocks"; |
|
|
|
|
26
|
|
|
|
27
|
|
|
public $showImportForm = false; |
28
|
|
|
|
29
|
|
|
private static $dependencies = [ |
|
|
|
|
30
|
|
|
"blockManager" => '%$blockManager', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
public $blockManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
**/ |
38
|
|
|
public function getManagedModels() |
39
|
|
|
{ |
40
|
|
|
$models = parent::getManagedModels(); |
41
|
|
|
|
42
|
|
|
// remove blocksets if not in use (set in config): |
43
|
|
|
if (!$this->blockManager->getUseBlockSets()) { |
44
|
|
|
unset($models['BlockSet']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $models; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Form |
52
|
|
|
**/ |
53
|
|
|
public function getEditForm($id = null, $fields = null) |
54
|
|
|
{ |
55
|
|
|
Versioned::set_stage('Stage'); |
56
|
|
|
$form = parent::getEditForm($id, $fields); |
57
|
|
|
|
58
|
|
|
if ($blockGridField = $form->Fields()->fieldByName('Block')) { |
59
|
|
|
$blockGridField->setConfig(GridFieldConfigBlockManager::create(true, true, false)); |
60
|
|
|
$config = $blockGridField->getConfig(); |
61
|
|
|
$dcols = $config->getComponentByType('GridFieldDataColumns'); |
62
|
|
|
$dfields = $dcols->getDisplayFields($blockGridField); |
63
|
|
|
unset($dfields['BlockArea']); |
64
|
|
|
$dcols->setDisplayFields($dfields); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $form; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getSearchContext() |
71
|
|
|
{ |
72
|
|
|
$context = parent::getSearchContext(); |
73
|
|
|
$fields = $context->getFields(); |
74
|
|
|
$subclasses = $this->blockManager->getBlockClasses(); |
75
|
|
|
if ($fields->dataFieldByName('q[ClassName]') && sizeof($subclasses) > 1) { |
76
|
|
|
$fields->dataFieldByName('q[ClassName]')->setSource($subclasses); |
77
|
|
|
$fields->dataFieldByName('q[ClassName]')->setEmptyString('(any)'); |
78
|
|
|
} else { |
79
|
|
|
$fields->removeByName('q[ClassName]'); |
80
|
|
|
} |
81
|
|
|
return $context; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|