1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Models; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Extensions\ElementalAreasExtension; |
6
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
7
|
|
|
use SilverStripe\Core\ClassInfo; |
8
|
|
|
use SilverStripe\Core\Extensible; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\ORM\ArrayList; |
11
|
|
|
use SilverStripe\ORM\DataObject; |
12
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
13
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
14
|
|
|
use SilverStripe\ORM\HasManyList; |
15
|
|
|
use SilverStripe\Versioned\Versioned; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ElementalArea |
19
|
|
|
* @package DNADesign\Elemental\Models |
20
|
|
|
* |
21
|
|
|
* @property string $OwnerClassName |
22
|
|
|
* |
23
|
|
|
* @method HasManyList|BaseElement[] Elements() |
24
|
|
|
*/ |
25
|
|
|
class ElementalArea extends DataObject |
26
|
|
|
{ |
27
|
|
|
private static $db = [ |
|
|
|
|
28
|
|
|
'OwnerClassName' => 'Varchar(255)', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private static $has_many = [ |
|
|
|
|
32
|
|
|
'Elements' => BaseElement::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
private static $extensions = [ |
|
|
|
|
36
|
|
|
Versioned::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
private static $owns = [ |
|
|
|
|
40
|
|
|
'Elements', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
private static $cascade_deletes = [ |
|
|
|
|
44
|
|
|
'Elements', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
private static $cascade_duplicates = [ |
|
|
|
|
48
|
|
|
'Elements', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
private static $summary_fields = [ |
|
|
|
|
52
|
|
|
'Title' => 'Title', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
private static $table_name = 'ElementalArea'; |
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function supportedPageTypes() |
61
|
|
|
{ |
62
|
|
|
$elementalClasses = []; |
63
|
|
|
|
64
|
|
|
foreach (ClassInfo::getValidSubClasses(SiteTree::class) as $class) { |
65
|
|
|
if (Extensible::has_extension($class, ElementalAreasExtension::class)) { |
66
|
|
|
$elementalClasses[] = $class; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $elementalClasses; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return DBHTMLText |
75
|
|
|
*/ |
76
|
|
|
public function forTemplate() |
77
|
|
|
{ |
78
|
|
|
return $this->renderWith(static::class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Necessary to display results in CMS site search. |
83
|
|
|
* |
84
|
|
|
* @return DBField |
85
|
|
|
*/ |
86
|
|
|
public function Breadcrumbs() |
87
|
|
|
{ |
88
|
|
|
$ownerClassName = $this->OwnerClassName; |
89
|
|
|
|
90
|
|
|
if ($owner = $ownerClassName::get()->filter('ElementalAreaID', $this->ID)->first()) { |
91
|
|
|
return DBField::create_field('HTMLText', sprintf( |
92
|
|
|
'<a href="%s">%s</a>', |
93
|
|
|
$owner->CMSEditLink(), |
94
|
|
|
$owner->Title |
95
|
|
|
)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Used in template instead of {@link Elements()} to wrap each element in |
103
|
|
|
* its' controller, making it easier to access and process form logic and |
104
|
|
|
* actions stored in {@link ElementController}. |
105
|
|
|
* |
106
|
|
|
* @return ArrayList |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function ElementControllers() |
110
|
|
|
{ |
111
|
|
|
$controllers = new ArrayList(); |
112
|
|
|
$items = $this->Elements(); |
113
|
|
|
|
114
|
|
|
if (!is_null($items)) { |
|
|
|
|
115
|
|
|
foreach ($items as $element) { |
116
|
|
|
$controller = $element->getController(); |
117
|
|
|
$controllers->push($controller); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $controllers; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return null|DataObject |
126
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
127
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
128
|
|
|
*/ |
129
|
|
|
public function getOwnerPage() |
130
|
|
|
{ |
131
|
|
|
if ($this->OwnerClassName) { |
132
|
|
|
$class = $this->OwnerClassName; |
133
|
|
|
$instance = Injector::inst()->get($class); |
134
|
|
|
if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
$elementalAreaRelations = $instance->getElementalRelations(); |
138
|
|
|
|
139
|
|
|
foreach ($elementalAreaRelations as $eaRelationship) { |
140
|
|
|
$areaID = $eaRelationship . 'ID'; |
141
|
|
|
|
142
|
|
|
$currentStage = Versioned::get_stage() ?: Versioned::DRAFT; |
143
|
|
|
$page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID); |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
if ($page && $page->exists()) { |
147
|
|
|
return $page->first(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
foreach ($this->supportedPageTypes() as $class) { |
153
|
|
|
$instance = Injector::inst()->get($class); |
154
|
|
|
if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
$elementalAreaRelations = $instance->getElementalRelations(); |
158
|
|
|
|
159
|
|
|
foreach ($elementalAreaRelations as $eaRelationship) { |
160
|
|
|
$areaID = $eaRelationship . 'ID'; |
161
|
|
|
$page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID); |
162
|
|
|
|
163
|
|
|
if ($page && $page->exists()) { |
164
|
|
|
if ($this->OwnerClassName !== $class) { |
165
|
|
|
$this->OwnerClassName = $class; |
166
|
|
|
$this->write(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $page->first(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param null $member |
|
|
|
|
179
|
|
|
* @return bool |
180
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
181
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
182
|
|
|
*/ |
183
|
|
|
public function canEdit($member = null) |
184
|
|
|
{ |
185
|
|
|
if (parent::canEdit($member)) { |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$ownerPage = $this->getOwnerPage(); |
190
|
|
|
if ($ownerPage !== null) { |
191
|
|
|
return $this->getOwnerPage()->canEdit($member); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param null $member |
|
|
|
|
199
|
|
|
* @return bool |
200
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
201
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
202
|
|
|
*/ |
203
|
|
|
public function canView($member = null) |
204
|
|
|
{ |
205
|
|
|
if (parent::canEdit($member)) { |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$ownerPage = $this->getOwnerPage(); |
210
|
|
|
if ($ownerPage !== null) { |
211
|
|
|
return $this->getOwnerPage()->canView($member); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|