|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\BaseObject\Model; |
|
4
|
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Forms\TextCheckboxGroupField; |
|
6
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
|
7
|
|
|
use Sheadawson\Linkable\Forms\LinkField; |
|
8
|
|
|
use Sheadawson\Linkable\Models\Link; |
|
9
|
|
|
use SilverStripe\Assets\Image; |
|
10
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
11
|
|
|
use SilverStripe\Control\Director; |
|
12
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
13
|
|
|
use SilverStripe\Forms\FieldList; |
|
14
|
|
|
use SilverStripe\Forms\TextField; |
|
15
|
|
|
use SilverStripe\ORM\DataObject; |
|
16
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
17
|
|
|
use SilverStripe\Security\Permission; |
|
18
|
|
|
use SilverStripe\Versioned\Versioned; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class BaseElementObject. |
|
22
|
|
|
* |
|
23
|
|
|
* @property string $Title |
|
24
|
|
|
* @property booelan $ShowTitle |
|
25
|
|
|
* @property string $Content |
|
26
|
|
|
* |
|
27
|
|
|
* @property int $ImageID |
|
28
|
|
|
* @property int $ElementLinkID |
|
29
|
|
|
* |
|
30
|
|
|
* @method Image Image() |
|
31
|
|
|
* @method Link ElementLink() |
|
32
|
|
|
* |
|
33
|
|
|
* @mixin Versioned |
|
34
|
|
|
*/ |
|
35
|
|
|
class BaseElementObject extends DataObject |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $db = array( |
|
|
|
|
|
|
41
|
|
|
'Title' => 'Varchar(255)', |
|
42
|
|
|
'ShowTitle' => 'Boolean', |
|
43
|
|
|
'Content' => 'HTMLText', |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
50
|
|
|
'Image' => Image::class, |
|
51
|
|
|
'ElementLink' => Link::class, |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
private static $owns = array( |
|
|
|
|
|
|
58
|
|
|
'Image', |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private static $default_sort = 'Name ASC'; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
70
|
|
|
'Image.CMSThumbnail' => 'Image', |
|
71
|
|
|
'Name' => 'Name', |
|
72
|
|
|
'Title' => 'Title', |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
private static $searchable_fields = array( |
|
|
|
|
|
|
79
|
|
|
'Name' => 'Name', |
|
80
|
|
|
'Title' => 'Headline', |
|
81
|
|
|
'Content' => 'Description', |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var array |
|
86
|
|
|
*/ |
|
87
|
|
|
private static $extensions = [ |
|
|
|
|
|
|
88
|
|
|
Versioned::class, |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Adds Publish button. |
|
93
|
|
|
* |
|
94
|
|
|
* @var bool |
|
95
|
|
|
*/ |
|
96
|
|
|
private static $versioned_gridfield_extensions = true; |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @var string |
|
100
|
|
|
*/ |
|
101
|
|
|
private static $table_name = 'BaseElementObject'; |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return FieldList |
|
105
|
|
|
* |
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getCMSFields() |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
111
|
|
|
/** @var FieldList $fields */ |
|
112
|
1 |
|
$fields->replaceField( |
|
113
|
1 |
|
'ElementLinkID', |
|
114
|
1 |
|
LinkField::create('ElementLinkID') |
|
115
|
1 |
|
->setTitle('Link') |
|
116
|
1 |
|
->setDescription('Optional. Add a call to action link.') |
|
117
|
|
|
); |
|
118
|
1 |
|
$fields->insertBefore($fields->dataFieldByName('ElementLinkID'), 'Content'); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
1 |
|
$fields->removeByName(array( |
|
121
|
1 |
|
'ElementFeaturesID', |
|
122
|
|
|
'Sort', |
|
123
|
|
|
)); |
|
124
|
|
|
|
|
125
|
|
|
// Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group |
|
126
|
1 |
|
$fields->removeByName('ShowTitle'); |
|
127
|
1 |
|
$fields->replaceField( |
|
128
|
1 |
|
'Title', |
|
129
|
1 |
|
TextCheckboxGroupField::create( |
|
130
|
1 |
|
TextField::create('Title', _t(BaseElement::class . '.TitleLabel', 'Title (displayed if checked)')), |
|
131
|
1 |
|
CheckboxField::create('ShowTitle', _t(BaseElement::class . '.ShowTitleLabel', 'Displayed')) |
|
132
|
1 |
|
)->setName('TitleAndDisplayed') |
|
133
|
|
|
); |
|
134
|
|
|
// $fields->dataFieldByName('Title')->setDescription('Optional. Display a Title with this feature.'); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
1 |
|
$image = $fields->dataFieldByName('Image') |
|
137
|
1 |
|
->setDescription('Optional. Display an image with this feature.') |
|
138
|
1 |
|
->setFolderName('Uploads/Elements/Objects'); |
|
139
|
1 |
|
$fields->insertBefore($image, 'Content'); |
|
140
|
|
|
|
|
141
|
1 |
|
$fields->dataFieldByName('Content') |
|
142
|
1 |
|
->setTitle('Description') |
|
143
|
1 |
|
->setDescription('Optional. Set a description for this feature.') |
|
144
|
1 |
|
->setRows(8); |
|
145
|
1 |
|
}); |
|
146
|
|
|
|
|
147
|
1 |
|
return parent::getCMSFields(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return SiteTree|null |
|
152
|
|
|
*/ |
|
153
|
4 |
|
public function getPage() |
|
154
|
|
|
{ |
|
155
|
4 |
|
$page = Director::get_current_page(); |
|
156
|
|
|
// because $page can be a SiteTree or Controller |
|
157
|
4 |
|
return $page instanceof SiteTree ? $page : null; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
162
|
|
|
* |
|
163
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
164
|
|
|
* @return boolean |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function canView($member = null) |
|
167
|
|
|
{ |
|
168
|
1 |
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
169
|
1 |
|
if ($extended !== null) { |
|
170
|
|
|
return $extended; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
if ($page = $this->getPage()) { |
|
174
|
|
|
return $page->canView($member); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
182
|
|
|
* |
|
183
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
184
|
|
|
* |
|
185
|
|
|
* @return boolean |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function canEdit($member = null) |
|
188
|
|
|
{ |
|
189
|
1 |
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
190
|
1 |
|
if ($extended !== null) { |
|
191
|
|
|
return $extended; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
if ($page = $this->getPage()) { |
|
195
|
|
|
return $page->canEdit($member); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
1 |
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
203
|
|
|
* |
|
204
|
|
|
* Uses archive not delete so that current stage is respected i.e if a |
|
205
|
|
|
* element is not published, then it can be deleted by someone who doesn't |
|
206
|
|
|
* have publishing permissions. |
|
207
|
|
|
* |
|
208
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
209
|
|
|
* |
|
210
|
|
|
* @return boolean |
|
211
|
|
|
*/ |
|
212
|
1 |
|
public function canDelete($member = null) |
|
213
|
|
|
{ |
|
214
|
1 |
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
215
|
1 |
|
if ($extended !== null) { |
|
216
|
|
|
return $extended; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
1 |
|
if ($page = $this->getPage()) { |
|
220
|
|
|
return $page->canArchive($member); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
1 |
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
228
|
|
|
* |
|
229
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
230
|
|
|
* @param array $context |
|
231
|
|
|
* |
|
232
|
|
|
* @return boolean |
|
233
|
|
|
*/ |
|
234
|
1 |
|
public function canCreate($member = null, $context = array()) |
|
235
|
|
|
{ |
|
236
|
1 |
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
237
|
1 |
|
if ($extended !== null) { |
|
238
|
|
|
return $extended; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
1 |
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|