1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SheaDawson\Blocks\Forms; |
4
|
|
|
|
5
|
|
|
use SheaDawson\Blocks\Model\Block; |
6
|
|
|
use SheaDawson\Blocks\Model\BlockSet; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Forms\DropdownField; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldFilterHeader; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldCopyButton; |
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
19
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
20
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; |
21
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
22
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* GridFieldConfig_BlockManager |
26
|
|
|
* Provides a reusable GridFieldConfig for managing Blocks. |
27
|
|
|
* |
28
|
|
|
* @author Shea Dawson <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class GridFieldConfigBlockManager extends GridFieldConfig |
31
|
|
|
{ |
32
|
|
|
public $blockManager; |
33
|
|
|
|
34
|
|
|
public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false) |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
|
38
|
|
|
$this->blockManager = Injector::inst()->get('SheaDawson\\Blocks\\BlockManager'); |
39
|
|
|
$controllerClass = Controller::curr()->class; |
40
|
|
|
// Get available Areas (for page) or all in case of ModelAdmin |
41
|
|
|
if ($controllerClass == 'CMSPageEditController') { |
42
|
|
|
$currentPage = Controller::curr()->currentPage(); |
43
|
|
|
$areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName); |
44
|
|
|
} else { |
45
|
|
|
$areasFieldSource = $this->blockManager->getAreas(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved |
49
|
|
|
if ($editableRows) { |
50
|
|
|
$this->addComponent($editable = new GridFieldEditableColumns()); |
51
|
|
|
$displayfields = array( |
52
|
|
|
'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
53
|
|
|
'Title' => array('title' => _t('Block.Title', 'Title'), 'field' => 'Silverstripe\\Forms\\ReadonlyField'), |
54
|
|
|
'BlockArea' => array( |
55
|
|
|
'title' => _t('Block.BlockArea', 'Block Area'), |
56
|
|
|
'callback' => function () use ($areasFieldSource) { |
57
|
|
|
$areasField = DropdownField::create('BlockArea', 'Block Area', $areasFieldSource); |
58
|
|
|
if (count($areasFieldSource) > 1) { |
59
|
|
|
$areasField->setHasEmptyDefault(true); |
60
|
|
|
} |
61
|
|
|
return $areasField; |
62
|
|
|
}, |
63
|
|
|
), |
64
|
|
|
'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
65
|
|
|
'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if ($aboveOrBelow) { |
69
|
|
|
$displayfields['AboveOrBelow'] = array( |
70
|
|
|
'title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), |
71
|
|
|
'callback' => function () { |
72
|
|
|
return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options')); |
73
|
|
|
}, |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
$editable->setDisplayFields($displayfields); |
77
|
|
|
} else { |
78
|
|
|
$this->addComponent($dcols = new GridFieldDataColumns()); |
79
|
|
|
|
80
|
|
|
$displayfields = array( |
81
|
|
|
'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
82
|
|
|
'Title' => _t('Block.Title', 'Title'), |
83
|
|
|
'BlockArea' => _t('Block.BlockArea', 'Block Area'), |
84
|
|
|
'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
85
|
|
|
'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'), |
86
|
|
|
); |
87
|
|
|
$dcols->setDisplayFields($displayfields); |
88
|
|
|
$dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
$this->addComponent(new GridFieldButtonRow('before')); |
93
|
|
|
$this->addComponent(new GridFieldToolbarHeader()); |
94
|
|
|
$this->addComponent(new GridFieldDetailForm()); |
95
|
|
|
$this->addComponent($sort = new GridFieldSortableHeader()); |
96
|
|
|
$this->addComponent($filter = new GridFieldFilterHeader()); |
97
|
|
|
$this->addComponent(new GridFieldDetailForm()); |
98
|
|
|
if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) { |
99
|
|
|
$this->addComponent(new GridFieldCopyButton()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$filter->setThrowExceptionOnBadDataType(false); |
103
|
|
|
$sort->setThrowExceptionOnBadDataType(false); |
104
|
|
|
|
105
|
|
|
if ($canAdd) { |
106
|
|
|
$multiClass = new GridFieldAddNewMultiClass(); |
107
|
|
|
$classes = $this->blockManager->getBlockClasses(); |
108
|
|
|
$multiClass->setClasses($classes); |
109
|
|
|
$this->addComponent($multiClass); |
110
|
|
|
//$this->addComponent(new GridFieldAddNewButton()); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($canEdit) { |
114
|
|
|
$this->addComponent(new GridFieldEditButton()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($canDelete) { |
118
|
|
|
$this->addComponent(new GridFieldDeleteAction(true)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add the GridFieldAddExistingSearchButton component to this grid config. |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
**/ |
129
|
|
|
public function addExisting() |
130
|
|
|
{ |
131
|
|
|
$classes = $this->blockManager->getBlockClasses(); |
132
|
|
|
|
133
|
|
|
$this->addComponent($add = new GridFieldAddExistingSearchButton()); |
134
|
|
|
$add->setSearchList(Block::get()->filter(array( |
135
|
|
|
'ClassName' => array_keys($classes), |
136
|
|
|
))); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Add the GridFieldBulkManager component to this grid config. |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
**/ |
146
|
|
|
public function addBulkEditing() |
147
|
|
|
{ |
148
|
|
|
if (class_exists('GridFieldBulkManager')) { |
149
|
|
|
$this->addComponent(new GridFieldBulkManager()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.