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\Dev\TestOnly; |
||
11 | use SilverStripe\ORM\ArrayList; |
||
12 | use SilverStripe\ORM\DataList; |
||
13 | use SilverStripe\ORM\DataObject; |
||
14 | use SilverStripe\ORM\FieldType\DBField; |
||
15 | use SilverStripe\ORM\FieldType\DBHTMLText; |
||
16 | use SilverStripe\ORM\UnsavedRelationList; |
||
17 | use SilverStripe\Versioned\Versioned; |
||
18 | |||
19 | /** |
||
20 | * Class ElementalArea |
||
21 | * @package DNADesign\Elemental\Models |
||
22 | * |
||
23 | * @property string $OwnerClassName |
||
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 = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
48 | 'Elements', |
||
49 | ]; |
||
50 | |||
51 | private static $summary_fields = [ |
||
52 | 'Title' => 'Title', |
||
53 | ]; |
||
54 | |||
55 | private static $table_name = 'ElementalArea'; |
||
56 | |||
57 | /** |
||
58 | * Cache various data to improve CMS load time |
||
59 | * |
||
60 | * @internal |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $cacheData = []; |
||
64 | |||
65 | /** |
||
66 | * @return array |
||
67 | */ |
||
68 | public function supportedPageTypes() |
||
69 | { |
||
70 | $elementalClasses = []; |
||
71 | |||
72 | foreach (ClassInfo::getValidSubClasses(SiteTree::class) as $class) { |
||
73 | if (Extensible::has_extension($class, ElementalAreasExtension::class)) { |
||
74 | $elementalClasses[] = $class; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | return $elementalClasses; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return DBHTMLText |
||
83 | */ |
||
84 | public function forTemplate() |
||
85 | { |
||
86 | return $this->renderWith(static::class); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param ArrayList $elements |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setElementsCached(ArrayList $elements) |
||
94 | { |
||
95 | $this->cacheData['elements'] = $elements; |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param DataObject $page |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setOwnerPageCached(DataObject $page) |
||
105 | { |
||
106 | $this->cacheData['owner_page'] = $page; |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * A cache-aware accessor for the elements |
||
113 | * @return ArrayList|DataList|BaseElement[] |
||
114 | */ |
||
115 | public function Elements() |
||
116 | { |
||
117 | if (isset($this->cacheData['elements'])) { |
||
118 | return $this->cacheData['elements']; |
||
119 | } |
||
120 | |||
121 | return parent::Elements(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Necessary to display results in CMS site search. |
||
126 | * |
||
127 | * @return DBField |
||
128 | */ |
||
129 | public function Breadcrumbs() |
||
130 | { |
||
131 | $ownerClassName = $this->OwnerClassName; |
||
132 | |||
133 | if ($owner = $ownerClassName::get()->filter('ElementalAreaID', $this->ID)->first()) { |
||
134 | return DBField::create_field('HTMLText', sprintf( |
||
135 | '<a href="%s">%s</a>', |
||
136 | $owner->CMSEditLink(), |
||
137 | $owner->Title |
||
138 | )); |
||
139 | } |
||
140 | |||
141 | return null; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Used in template instead of {@link Elements()} to wrap each element in |
||
146 | * its' controller, making it easier to access and process form logic and |
||
147 | * actions stored in {@link ElementController}. |
||
148 | * |
||
149 | * @return ArrayList |
||
150 | * @throws \Exception |
||
151 | */ |
||
152 | public function ElementControllers() |
||
153 | { |
||
154 | // Don't try and process unsaved lists |
||
155 | if ($this->Elements() instanceof UnsavedRelationList) { |
||
156 | return ArrayList::create(); |
||
157 | } |
||
158 | |||
159 | $controllers = ArrayList::create(); |
||
160 | $items = $this->Elements()->filterByCallback(function (BaseElement $item) { |
||
161 | return $item->canView(); |
||
162 | }); |
||
163 | |||
164 | if (!is_null($items)) { |
||
165 | foreach ($items as $element) { |
||
166 | $controller = $element->getController(); |
||
167 | $controllers->push($controller); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return $controllers; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return null|DataObject |
||
176 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
177 | * @throws \SilverStripe\ORM\ValidationException |
||
178 | */ |
||
179 | public function getOwnerPage() |
||
180 | { |
||
181 | // You can't find the owner page of a area that hasn't been save yet |
||
182 | if (!$this->isInDB()) { |
||
183 | return null; |
||
184 | } |
||
185 | |||
186 | // Allow for repeated calls to read from cache |
||
187 | if (isset($this->cacheData['owner_page'])) { |
||
188 | return $this->cacheData['owner_page']; |
||
189 | } |
||
190 | |||
191 | if ($this->OwnerClassName) { |
||
192 | $class = $this->OwnerClassName; |
||
193 | $instance = Injector::inst()->get($class); |
||
194 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
195 | return null; |
||
196 | } |
||
197 | $elementalAreaRelations = $instance->getElementalRelations(); |
||
198 | |||
199 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
200 | $areaID = $eaRelationship . 'ID'; |
||
201 | |||
202 | $currentStage = Versioned::get_stage() ?: Versioned::DRAFT; |
||
203 | $page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID)->first(); |
||
204 | |||
205 | if ($page) { |
||
206 | $this->setOwnerPageCached($page); |
||
207 | return $page; |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | foreach ($this->supportedPageTypes() as $class) { |
||
213 | $instance = Injector::inst()->get($class); |
||
214 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
215 | return null; |
||
216 | } |
||
217 | |||
218 | $areaIDFilters = []; |
||
219 | foreach ($instance->getElementalRelations() as $eaRelationship) { |
||
220 | $areaIDFilters[$eaRelationship . 'ID'] = $this->ID; |
||
221 | } |
||
222 | |||
223 | try { |
||
224 | $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filterAny($areaIDFilters)->first(); |
||
225 | } catch (\Exception $ex) { |
||
226 | // Usually this is catching cases where test stubs from other modules are trying to be loaded |
||
227 | // and failing in unit tests. |
||
228 | if (in_array(TestOnly::class, class_implements($class))) { |
||
229 | continue; |
||
230 | } |
||
231 | // Continue as normal... |
||
232 | throw $ex; |
||
233 | } |
||
234 | |||
235 | if ($page) { |
||
236 | if ($this->OwnerClassName !== $class) { |
||
237 | $this->OwnerClassName = $class; |
||
238 | |||
239 | // Avoid recursion: only write if it's already in the database |
||
240 | if ($this->isInDB()) { |
||
241 | $this->write(); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | $this->cacheData['area_relation_name'] = $page; |
||
246 | return $page; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | return null; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param null $member |
||
255 | * @return bool |
||
256 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
257 | * @throws \SilverStripe\ORM\ValidationException |
||
258 | */ |
||
259 | public function canEdit($member = null) |
||
260 | { |
||
261 | if (parent::canEdit($member)) { |
||
262 | return true; |
||
263 | } |
||
264 | |||
265 | $ownerPage = $this->getOwnerPage(); |
||
266 | if ($ownerPage !== null) { |
||
267 | return $this->getOwnerPage()->canEdit($member); |
||
268 | } |
||
269 | |||
270 | return false; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param null $member |
||
275 | * @return bool |
||
276 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
277 | * @throws \SilverStripe\ORM\ValidationException |
||
278 | */ |
||
279 | public function canView($member = null) |
||
280 | { |
||
281 | if (parent::canEdit($member)) { |
||
282 | return true; |
||
283 | } |
||
284 | |||
285 | $ownerPage = $this->getOwnerPage(); |
||
286 | if ($ownerPage !== null) { |
||
287 | return $this->getOwnerPage()->canView($member); |
||
288 | } |
||
289 | |||
290 | return false; |
||
291 | } |
||
292 | } |
||
293 |