1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalVirtual\Extensions; |
4
|
|
|
|
5
|
|
|
use DNADesign\ElementalVirtual\Forms\ElementalGridFieldDeleteAction; |
6
|
|
|
use DNADesign\ElementalVirtual\Model\ElementVirtual; |
7
|
|
|
use SilverStripe\ORM\DataExtension; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Versioned\Versioned; |
10
|
|
|
use SilverStripe\Forms\LiteralField; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_Base; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
16
|
|
|
use DNADesign\Elemental\Models\ElementalArea; |
17
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
18
|
|
|
use SilverStripe\ORM\ArrayList; |
19
|
|
|
|
20
|
|
|
class BaseElementExtension extends DataExtension |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $virtualOwner; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @config |
29
|
|
|
* |
30
|
|
|
* @var boolean |
31
|
|
|
*/ |
32
|
|
|
private static $default_global_elements = true; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private static $db = [ |
|
|
|
|
38
|
|
|
'AvailableGlobally' => 'Boolean(1)' |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array $has_many |
43
|
|
|
*/ |
44
|
|
|
private static $has_many = [ |
|
|
|
|
45
|
|
|
'VirtualClones' => ElementVirtual::class |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
public function populateDefaults() |
49
|
|
|
{ |
50
|
|
|
$default = $this->owner->config()->get('default_global_elements'); |
51
|
|
|
|
52
|
|
|
$this->AvailableGlobally = $default; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ElementVirtual |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setVirtualOwner(ElementVirtual $owner) |
61
|
|
|
{ |
62
|
|
|
$this->virtualOwner = $owner; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return ElementVirtual |
68
|
|
|
*/ |
69
|
|
|
public function getVirtualOwner() |
70
|
|
|
{ |
71
|
|
|
return $this->virtualOwner; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Finds and returns elements that are virtual elements which link to this |
76
|
|
|
* element. |
77
|
|
|
* |
78
|
|
|
* @return DataList |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
public function getVirtualElements() |
81
|
|
|
{ |
82
|
|
|
return ElementVirtual::get()->filter([ |
|
|
|
|
83
|
|
|
'LinkedElementID' => $this->owner->ID |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getVirtualLinkedSummary() |
91
|
|
|
{ |
92
|
|
|
return sprintf('%s (%s #%s)', $this->owner->Title, $this->owner->getType(), $this->owner->ID); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return DataList |
97
|
|
|
*/ |
98
|
|
|
public function getPublishedVirtualElements() |
99
|
|
|
{ |
100
|
|
|
return ElementVirtual::get()->filter([ |
|
|
|
|
101
|
|
|
'LinkedElementID' => $this->owner->ID |
102
|
|
|
])->setDataQueryParam([ |
103
|
|
|
'Versioned.mode' => 'stage', |
104
|
|
|
'Versioned.stage' => 'Live' |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param FieldList $fields |
110
|
|
|
* |
111
|
|
|
* @return FieldList |
112
|
|
|
*/ |
113
|
|
|
public function updateCMSFields(FieldList $fields) |
114
|
|
|
{ |
115
|
|
|
$global = $fields->dataFieldByName('AvailableGlobally'); |
116
|
|
|
|
117
|
|
|
if ($global) { |
|
|
|
|
118
|
|
|
$fields->removeByName('AvailableGlobally'); |
119
|
|
|
$fields->addFieldToTab('Root.Settings', $global); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($virtual = $fields->dataFieldByName('VirtualClones')) { |
123
|
|
|
if ($this->owner->VirtualClones()->Count() > 0) { |
124
|
|
|
$tab = $fields->findOrMakeTab('Root.VirtualClones'); |
125
|
|
|
$tab->setTitle(_t(__CLASS__ . '.LinkedTo', 'Linked To')); |
126
|
|
|
|
127
|
|
|
if ($ownerPage = $this->owner->getPage()) { |
128
|
|
|
$fields->addFieldToTab( |
129
|
|
|
'Root.VirtualClones', |
130
|
|
|
LiteralField::create( |
131
|
|
|
'DisplaysOnPage', |
132
|
|
|
sprintf( |
133
|
|
|
"<p>" |
134
|
|
|
. _t(__CLASS__ . '.OriginalContentFrom', 'The original content element appears on') |
135
|
|
|
. " <a href='%s'>%s</a></p>", |
136
|
|
|
($ownerPage->hasMethod('CMSEditLink') && $ownerPage->canEdit()) ? $ownerPage->CMSEditLink() : $ownerPage->Link(), |
137
|
|
|
$ownerPage->MenuTitle |
138
|
|
|
) |
139
|
|
|
), |
140
|
|
|
'VirtualClones' |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$virtual->setConfig(new GridFieldConfig_Base()); |
145
|
|
|
$virtual |
146
|
|
|
->setTitle(_t(__CLASS__ . '.OtherPages', 'Other pages')) |
147
|
|
|
->getConfig() |
148
|
|
|
->removeComponentsByType(GridFieldAddExistingAutocompleter::class) |
149
|
|
|
->removeComponentsByType(GridFieldAddNewButton::class) |
150
|
|
|
->removeComponentsByType(GridFieldDeleteAction::class) |
|
|
|
|
151
|
|
|
->removeComponentsByType(GridFieldDetailForm::class) |
152
|
|
|
->addComponent(new ElementalGridFieldDeleteAction()); |
153
|
|
|
|
154
|
|
|
$virtual->getConfig() |
155
|
|
|
->getComponentByType(GridFieldDataColumns::class) |
156
|
|
|
->setDisplayFields([ |
157
|
|
|
'getPage.Title' => _t(__CLASS__ . '.GridFieldTitle', 'Title'), |
158
|
|
|
'ParentPageCMSEditLink' => _t(__CLASS__ . '.GridFieldUsedOn', 'Used on'), |
159
|
|
|
]); |
160
|
|
|
} else { |
161
|
|
|
$fields->removeByName('VirtualClones'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Client specific requirement https://mbiessd.atlassian.net/browse/MWP-390 |
168
|
|
|
* Delete all references of this Virtual element |
169
|
|
|
* |
170
|
|
|
*/ |
171
|
|
|
public function onBeforeDelete() |
172
|
|
|
{ |
173
|
|
|
foreach ($this->getVirtualElements() as $virtualElement) { |
174
|
|
|
$virtualElement->delete(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $classes |
180
|
|
|
*/ |
181
|
|
|
public function updateAllowedElementClasses(&$classes) |
182
|
|
|
{ |
183
|
|
|
if (isset($classes[ElementVirtual::class])) { |
184
|
|
|
unset($classes[ElementVirtual::class]); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* get all pages where this element is used |
191
|
|
|
* |
192
|
|
|
* @return ArrayList |
193
|
|
|
*/ |
194
|
|
|
public function getUsage() |
195
|
|
|
{ |
196
|
|
|
$usage = new ArrayList(); |
197
|
|
|
|
198
|
|
|
if ($page = $this->owner->getPage()) { |
199
|
|
|
$usage->push($page); |
200
|
|
|
if ($this->virtualOwner) { |
201
|
|
|
$page->setField('ElementType', 'Linked'); |
202
|
|
|
} else { |
203
|
|
|
$page->setField('ElementType', 'Master'); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$linkedElements = ElementVirtual::get()->filter('LinkedElementID', $this->ID); |
|
|
|
|
208
|
|
|
|
209
|
|
|
foreach ($linkedElements as $element) { |
210
|
|
|
$area = $element->Parent(); |
211
|
|
|
|
212
|
|
|
if ($area instanceof ElementalArea && $page = $area->getOwnerPage()) { |
213
|
|
|
$page->setField('ElementType', 'Linked'); |
214
|
|
|
$usage->push($page); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$usage->removeDuplicates(); |
219
|
|
|
return $usage; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return DBHTMLText |
224
|
|
|
*/ |
225
|
|
|
public function UsageSummary() |
226
|
|
|
{ |
227
|
|
|
$usage = $this->getUsage(); |
228
|
|
|
$arr = []; |
229
|
|
|
foreach ($usage as $page) { |
230
|
|
|
$type = ($page->ElementType) ? sprintf("<em> - %s</em>", $page->ElementType) : null; |
231
|
|
|
$arr[] = sprintf("<a href=\"%s\" target=\"blank\">%s</a> %s", $page->CMSEditLink(), $page->Title, $type); |
232
|
|
|
} |
233
|
|
|
$html = DBHTMLText::create('UsageSummary'); |
234
|
|
|
$html->setValue(implode('<br>', $arr)); |
235
|
|
|
|
236
|
|
|
return $html; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|