1 | <?php |
||
2 | |||
3 | namespace DNADesign\Elemental\Models; |
||
4 | |||
5 | use DNADesign\Elemental\Controllers\ElementController; |
||
6 | use DNADesign\Elemental\Forms\TextCheckboxGroupField; |
||
7 | use DNADesign\Elemental\ORM\FieldType\DBObjectType; |
||
8 | use Exception; |
||
9 | use SilverStripe\CMS\Controllers\CMSPageEditController; |
||
10 | use SilverStripe\CMS\Model\SiteTree; |
||
11 | use SilverStripe\Control\Controller; |
||
12 | use SilverStripe\Control\Director; |
||
13 | use SilverStripe\Core\ClassInfo; |
||
14 | use SilverStripe\Core\Injector\Injector; |
||
15 | use SilverStripe\Forms\DropdownField; |
||
16 | use SilverStripe\Forms\FieldList; |
||
17 | use SilverStripe\Forms\HiddenField; |
||
18 | use SilverStripe\Forms\NumericField; |
||
19 | use SilverStripe\Forms\TextField; |
||
20 | use SilverStripe\GraphQL\Scaffolding\StaticSchema; |
||
21 | use SilverStripe\ORM\DataObject; |
||
22 | use SilverStripe\ORM\FieldType\DBBoolean; |
||
23 | use SilverStripe\ORM\FieldType\DBField; |
||
24 | use SilverStripe\ORM\FieldType\DBHTMLText; |
||
25 | use SilverStripe\Security\Member; |
||
26 | use SilverStripe\Security\Permission; |
||
27 | use SilverStripe\Versioned\Versioned; |
||
28 | use SilverStripe\VersionedAdmin\Forms\HistoryViewerField; |
||
29 | use SilverStripe\View\ArrayData; |
||
30 | use SilverStripe\View\Parsers\URLSegmentFilter; |
||
31 | use SilverStripe\View\Requirements; |
||
32 | |||
33 | /** |
||
34 | * Class BaseElement |
||
35 | * @package DNADesign\Elemental\Models |
||
36 | * |
||
37 | * @property string $Title |
||
38 | * @property bool $ShowTitle |
||
39 | * @property int $Sort |
||
40 | * @property string $ExtraClass |
||
41 | * @property string $Style |
||
42 | * |
||
43 | * @method ElementalArea Parent() |
||
44 | */ |
||
45 | class BaseElement extends DataObject |
||
46 | { |
||
47 | /** |
||
48 | * Override this on your custom elements to specify a CSS icon class |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private static $icon = 'font-icon-block-layout'; |
||
53 | |||
54 | /** |
||
55 | * Describe the purpose of this element |
||
56 | * |
||
57 | * @config |
||
58 | * @var string |
||
59 | */ |
||
60 | private static $description = 'Base element class'; |
||
61 | |||
62 | private static $db = [ |
||
63 | 'Title' => 'Varchar(255)', |
||
64 | 'ShowTitle' => 'Boolean', |
||
65 | 'Sort' => 'Int', |
||
66 | 'ExtraClass' => 'Varchar(255)', |
||
67 | 'Style' => 'Varchar(255)' |
||
68 | ]; |
||
69 | |||
70 | private static $has_one = [ |
||
71 | 'Parent' => ElementalArea::class |
||
72 | ]; |
||
73 | |||
74 | private static $extensions = [ |
||
75 | Versioned::class |
||
76 | ]; |
||
77 | |||
78 | private static $casting = [ |
||
79 | 'BlockSchema' => DBObjectType::class, |
||
80 | 'IsLiveVersion' => DBBoolean::class, |
||
81 | 'IsPublished' => DBBoolean::class, |
||
82 | ]; |
||
83 | |||
84 | private static $versioned_gridfield_extensions = true; |
||
85 | |||
86 | private static $table_name = 'Element'; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | private static $controller_class = ElementController::class; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | private static $controller_template = 'ElementHolder'; |
||
97 | |||
98 | /** |
||
99 | * @var ElementController |
||
100 | */ |
||
101 | protected $controller; |
||
102 | |||
103 | /** |
||
104 | * Cache various data to improve CMS load time |
||
105 | * |
||
106 | * @internal |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $cacheData; |
||
110 | |||
111 | private static $default_sort = 'Sort'; |
||
112 | |||
113 | private static $singular_name = 'block'; |
||
114 | |||
115 | private static $plural_name = 'blocks'; |
||
116 | |||
117 | private static $summary_fields = [ |
||
118 | 'EditorPreview' => 'Summary' |
||
119 | ]; |
||
120 | |||
121 | /** |
||
122 | * @config |
||
123 | * @var array |
||
124 | */ |
||
125 | private static $styles = []; |
||
126 | |||
127 | private static $searchable_fields = [ |
||
128 | 'ID' => [ |
||
129 | 'field' => NumericField::class, |
||
130 | ], |
||
131 | 'Title', |
||
132 | 'LastEdited' |
||
133 | ]; |
||
134 | |||
135 | /** |
||
136 | * Enable for backwards compatibility |
||
137 | * |
||
138 | * @var boolean |
||
139 | */ |
||
140 | private static $disable_pretty_anchor_name = false; |
||
141 | |||
142 | /** |
||
143 | * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be |
||
144 | * clickable and a GridFieldDetailForm will be used. |
||
145 | * |
||
146 | * @config |
||
147 | * @var bool |
||
148 | */ |
||
149 | private static $inline_editable = true; |
||
150 | |||
151 | /** |
||
152 | * Store used anchor names, this is to avoid title clashes |
||
153 | * when calling 'getAnchor' |
||
154 | * |
||
155 | * @var array |
||
156 | */ |
||
157 | protected static $used_anchors = []; |
||
158 | |||
159 | /** |
||
160 | * For caching 'getAnchor' |
||
161 | * |
||
162 | * @var string |
||
163 | */ |
||
164 | protected $anchor = null; |
||
165 | |||
166 | /** |
||
167 | * Basic permissions, defaults to page perms where possible. |
||
168 | * |
||
169 | * @param Member $member |
||
170 | * @return boolean |
||
171 | */ |
||
172 | public function canView($member = null) |
||
173 | { |
||
174 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
175 | if ($extended !== null) { |
||
176 | return $extended; |
||
177 | } |
||
178 | |||
179 | if ($this->hasMethod('getPage')) { |
||
180 | if ($page = $this->getPage()) { |
||
181 | return $page->canView($member); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Basic permissions, defaults to page perms where possible. |
||
190 | * |
||
191 | * @param Member $member |
||
192 | * |
||
193 | * @return boolean |
||
194 | */ |
||
195 | public function canEdit($member = null) |
||
196 | { |
||
197 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
198 | if ($extended !== null) { |
||
199 | return $extended; |
||
200 | } |
||
201 | |||
202 | if ($this->hasMethod('getPage')) { |
||
203 | if ($page = $this->getPage()) { |
||
204 | return $page->canEdit($member); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Basic permissions, defaults to page perms where possible. |
||
213 | * |
||
214 | * Uses archive not delete so that current stage is respected i.e if a |
||
215 | * element is not published, then it can be deleted by someone who doesn't |
||
216 | * have publishing permissions. |
||
217 | * |
||
218 | * @param Member $member |
||
219 | * |
||
220 | * @return boolean |
||
221 | */ |
||
222 | public function canDelete($member = null) |
||
223 | { |
||
224 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
225 | if ($extended !== null) { |
||
226 | return $extended; |
||
227 | } |
||
228 | |||
229 | if ($this->hasMethod('getPage')) { |
||
230 | if ($page = $this->getPage()) { |
||
231 | return $page->canArchive($member); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Basic permissions, defaults to page perms where possible. |
||
240 | * |
||
241 | * @param Member $member |
||
242 | * @param array $context |
||
243 | * |
||
244 | * @return boolean |
||
245 | */ |
||
246 | public function canCreate($member = null, $context = array()) |
||
247 | { |
||
248 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
249 | if ($extended !== null) { |
||
250 | return $extended; |
||
251 | } |
||
252 | |||
253 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Increment the sort order if one hasn't been already defined. This |
||
258 | * ensures that new elements are created at the end of the list by default. |
||
259 | * |
||
260 | * {@inheritDoc} |
||
261 | */ |
||
262 | public function onBeforeWrite() |
||
263 | { |
||
264 | parent::onBeforeWrite(); |
||
265 | |||
266 | if (!$this->Sort) { |
||
267 | if ($this->hasExtension(Versioned::class)) { |
||
268 | $records = Versioned::get_by_stage(BaseElement::class, Versioned::DRAFT); |
||
269 | } else { |
||
270 | $records = BaseElement::get(); |
||
271 | } |
||
272 | |||
273 | $records = $records->filter('ParentID', $this->ParentID); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
274 | |||
275 | $this->Sort = $records->max('Sort') + 1; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | public function getCMSFields() |
||
280 | { |
||
281 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
282 | // Remove relationship fields |
||
283 | $fields->removeByName('ParentID'); |
||
284 | $fields->removeByName('Sort'); |
||
285 | |||
286 | // Remove link and file tracking tabs |
||
287 | $fields->removeByName(['LinkTracking', 'FileTracking']); |
||
288 | |||
289 | $fields->addFieldToTab( |
||
290 | 'Root.Settings', |
||
291 | TextField::create('ExtraClass', _t(__CLASS__ . '.ExtraCssClassesLabel', 'Custom CSS classes')) |
||
292 | ->setAttribute( |
||
293 | 'placeholder', |
||
294 | _t(__CLASS__ . '.ExtraCssClassesPlaceholder', 'my_class another_class') |
||
295 | ) |
||
296 | ); |
||
297 | |||
298 | // Rename the "Settings" tab |
||
299 | $fields->fieldByName('Root.Settings') |
||
300 | ->setTitle(_t(__CLASS__ . '.SettingsTabLabel', 'Settings')); |
||
301 | |||
302 | // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group |
||
303 | $fields->removeByName('ShowTitle'); |
||
304 | $fields->replaceField( |
||
305 | 'Title', |
||
306 | TextCheckboxGroupField::create() |
||
307 | ->setName('Title') |
||
308 | ); |
||
309 | |||
310 | // Rename the "Main" tab |
||
311 | $fields->fieldByName('Root.Main') |
||
312 | ->setTitle(_t(__CLASS__ . '.MainTabLabel', 'Content')); |
||
313 | |||
314 | $fields->addFieldsToTab('Root.Main', [ |
||
315 | HiddenField::create('AbsoluteLink', false, Director::absoluteURL($this->PreviewLink())), |
||
316 | HiddenField::create('LiveLink', false, Director::absoluteURL($this->Link())), |
||
317 | HiddenField::create('StageLink', false, Director::absoluteURL($this->PreviewLink())), |
||
318 | ]); |
||
319 | |||
320 | $styles = $this->config()->get('styles'); |
||
321 | |||
322 | if ($styles && count($styles) > 0) { |
||
323 | $styleDropdown = DropdownField::create('Style', _t(__CLASS__.'.STYLE', 'Style variation'), $styles); |
||
324 | |||
325 | $fields->insertBefore($styleDropdown, 'ExtraClass'); |
||
326 | |||
327 | $styleDropdown->setEmptyString(_t(__CLASS__.'.CUSTOM_STYLES', 'Select a style..')); |
||
328 | } else { |
||
329 | $fields->removeByName('Style'); |
||
330 | } |
||
331 | |||
332 | // Hide the navigation section of the tabs in the React component {@see silverstripe/admin Tabs} |
||
333 | $rootTabset = $fields->fieldByName('Root'); |
||
334 | $rootTabset->setSchemaState(['hideNav' => true]); |
||
335 | |||
336 | if ($this->isInDB()) { |
||
337 | $fields->addFieldsToTab('Root.History', [ |
||
338 | HistoryViewerField::create('ElementHistory'), |
||
339 | ]); |
||
340 | // Add class to containing tab |
||
341 | $fields->fieldByName('Root.History') |
||
342 | ->addExtraClass('elemental-block__history-tab tab--history-viewer'); |
||
343 | |||
344 | // Hack: automatically navigate to the History tab with `#Root_History` is in the URL |
||
345 | // To unhack, fix this: https://github.com/silverstripe/silverstripe-admin/issues/911 |
||
346 | Requirements::customScript(<<<JS |
||
347 | document.addEventListener('DOMContentLoaded', () => { |
||
348 | var hash = window.location.hash.substr(1); |
||
349 | if (hash !== 'Root_History') { |
||
350 | return null; |
||
351 | } |
||
352 | jQuery('.cms-tabset-nav-primary li[aria-controls="Root_History"] a').trigger('click') |
||
353 | }); |
||
354 | JS |
||
355 | ); |
||
356 | } |
||
357 | }); |
||
358 | |||
359 | return parent::getCMSFields(); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Get the type of the current block, for use in GridField summaries, block |
||
364 | * type dropdowns etc. Examples are "Content", "File", "Media", etc. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getType() |
||
369 | { |
||
370 | return _t(__CLASS__ . '.BlockType', 'Block'); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Proxy through to configuration setting 'inline_editable' |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function inlineEditable() |
||
379 | { |
||
380 | return static::config()->get('inline_editable'); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @param ElementController $controller |
||
385 | * |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function setController($controller) |
||
389 | { |
||
390 | $this->controller = $controller; |
||
391 | |||
392 | return $this; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @throws Exception If the specified controller class doesn't exist |
||
397 | * |
||
398 | * @return ElementController |
||
399 | */ |
||
400 | public function getController() |
||
401 | { |
||
402 | if ($this->controller) { |
||
403 | return $this->controller; |
||
404 | } |
||
405 | |||
406 | $controllerClass = self::config()->controller_class; |
||
407 | |||
408 | if (!class_exists($controllerClass)) { |
||
409 | throw new Exception( |
||
410 | 'Could not find controller class ' . $controllerClass . ' as defined in ' . static::class |
||
411 | ); |
||
412 | } |
||
413 | |||
414 | $this->controller = Injector::inst()->create($controllerClass, $this); |
||
415 | $this->controller->doInit(); |
||
416 | |||
417 | return $this->controller; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @param string $name |
||
422 | * @return $this |
||
423 | */ |
||
424 | public function setAreaRelationNameCache($name) |
||
425 | { |
||
426 | $this->cacheData['area_relation_name'] = $name; |
||
427 | |||
428 | return $this; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @return Controller |
||
433 | */ |
||
434 | public function Top() |
||
435 | { |
||
436 | return (Controller::has_curr()) ? Controller::curr() : null; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Default way to render element in templates. Note that all blocks should |
||
441 | * be rendered through their {@link ElementController} class as this |
||
442 | * contains the holder styles. |
||
443 | * |
||
444 | * @return string|null HTML |
||
445 | */ |
||
446 | public function forTemplate($holder = true) |
||
447 | { |
||
448 | $templates = $this->getRenderTemplates(); |
||
449 | |||
450 | if ($templates) { |
||
451 | return $this->renderWith($templates); |
||
452 | } |
||
453 | |||
454 | return null; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * @param string $suffix |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | public function getRenderTemplates($suffix = '') |
||
463 | { |
||
464 | $classes = ClassInfo::ancestry($this->ClassName); |
||
465 | $classes[static::class] = static::class; |
||
466 | $classes = array_reverse($classes); |
||
467 | $templates = []; |
||
468 | |||
469 | foreach ($classes as $key => $class) { |
||
470 | if ($class == BaseElement::class) { |
||
471 | continue; |
||
472 | } |
||
473 | |||
474 | if ($class == DataObject::class) { |
||
475 | break; |
||
476 | } |
||
477 | |||
478 | if ($style = $this->Style) { |
||
479 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName() . '_' . $style; |
||
480 | $templates[$class][] = $class . $suffix . '_' . $style; |
||
481 | } |
||
482 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName(); |
||
483 | $templates[$class][] = $class . $suffix; |
||
484 | } |
||
485 | |||
486 | $this->extend('updateRenderTemplates', $templates, $suffix); |
||
487 | |||
488 | $templateFlat = []; |
||
489 | foreach ($templates as $class => $variations) { |
||
490 | $templateFlat = array_merge($templateFlat, $variations); |
||
491 | } |
||
492 | |||
493 | return $templateFlat; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Given form data (wit |
||
498 | * |
||
499 | * @param $data |
||
500 | */ |
||
501 | public function updateFromFormData($data) |
||
502 | { |
||
503 | $cmsFields = $this->getCMSFields(); |
||
504 | |||
505 | foreach ($data as $field => $datum) { |
||
506 | $field = $cmsFields->dataFieldByName($field); |
||
507 | |||
508 | if (!$field) { |
||
509 | continue; |
||
510 | } |
||
511 | |||
512 | $field->setSubmittedValue($datum); |
||
513 | $field->saveInto($this); |
||
514 | } |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Strip all namespaces from class namespace. |
||
519 | * |
||
520 | * @param string $classname e.g. "\Fully\Namespaced\Class" |
||
521 | * |
||
522 | * @return string following the param example, "Class" |
||
523 | */ |
||
524 | protected function stripNamespacing($classname) |
||
525 | { |
||
526 | $classParts = explode('\\', $classname); |
||
527 | return array_pop($classParts); |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * @return string |
||
532 | */ |
||
533 | public function getSimpleClassName() |
||
534 | { |
||
535 | return strtolower($this->sanitiseClassName($this->ClassName, '__')); |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * @return null|SiteTree |
||
540 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
541 | * @throws \SilverStripe\ORM\ValidationException |
||
542 | */ |
||
543 | public function getPage() |
||
544 | { |
||
545 | // Allow for repeated calls to be cached |
||
546 | if (isset($this->cacheData['page'])) { |
||
547 | return $this->cacheData['page']; |
||
548 | } |
||
549 | |||
550 | |||
551 | $class = DataObject::getSchema()->hasOneComponent($this, 'Parent'); |
||
552 | $area = ($this->ParentID) ? DataObject::get_by_id($class, $this->ParentID) : null; |
||
553 | if ($area instanceof ElementalArea && $area->exists()) { |
||
554 | $this->cacheData['page'] = $area->getOwnerPage(); |
||
555 | return $this->cacheData['page']; |
||
556 | } |
||
557 | |||
558 | return null; |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Get a unique anchor name |
||
563 | * |
||
564 | * @return string |
||
565 | */ |
||
566 | public function getAnchor() |
||
567 | { |
||
568 | if ($this->anchor !== null) { |
||
569 | return $this->anchor; |
||
570 | } |
||
571 | |||
572 | $anchorTitle = ''; |
||
573 | |||
574 | if (!$this->config()->disable_pretty_anchor_name) { |
||
575 | if ($this->hasMethod('getAnchorTitle')) { |
||
576 | $anchorTitle = $this->getAnchorTitle(); |
||
577 | } elseif ($this->config()->enable_title_in_template) { |
||
578 | $anchorTitle = $this->getField('Title'); |
||
579 | } |
||
580 | } |
||
581 | |||
582 | if (!$anchorTitle) { |
||
583 | $anchorTitle = 'e'.$this->ID; |
||
584 | } |
||
585 | |||
586 | $filter = URLSegmentFilter::create(); |
||
587 | $titleAsURL = $filter->filter($anchorTitle); |
||
588 | |||
589 | // Ensure that this anchor name isn't already in use |
||
590 | // ie. If two elemental blocks have the same title, it'll append '-2', '-3' |
||
591 | $result = $titleAsURL; |
||
592 | $count = 1; |
||
593 | while (isset(self::$used_anchors[$result]) && self::$used_anchors[$result] !== $this->ID) { |
||
594 | ++$count; |
||
595 | $result = $titleAsURL . '-' . $count; |
||
596 | } |
||
597 | self::$used_anchors[$result] = $this->ID; |
||
598 | return $this->anchor = $result; |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * @param string|null $action |
||
603 | * @return string|null |
||
604 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
605 | * @throws \SilverStripe\ORM\ValidationException |
||
606 | */ |
||
607 | public function AbsoluteLink($action = null) |
||
608 | { |
||
609 | if ($page = $this->getPage()) { |
||
610 | $link = $page->AbsoluteLink($action) . '#' . $this->getAnchor(); |
||
611 | |||
612 | return $link; |
||
613 | } |
||
614 | |||
615 | return null; |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * @param string|null $action |
||
620 | * @return string |
||
621 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
622 | * @throws \SilverStripe\ORM\ValidationException |
||
623 | */ |
||
624 | public function Link($action = null) |
||
625 | { |
||
626 | if ($page = $this->getPage()) { |
||
627 | $link = $page->Link($action) . '#' . $this->getAnchor(); |
||
628 | |||
629 | $this->extend('updateLink', $link); |
||
630 | |||
631 | return $link; |
||
632 | } |
||
633 | |||
634 | return null; |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * @param string|null $action |
||
639 | * @return string |
||
640 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
641 | * @throws \SilverStripe\ORM\ValidationException |
||
642 | */ |
||
643 | public function PreviewLink($action = null) |
||
644 | { |
||
645 | $action = $action . '?ElementalPreview=' . mt_rand(); |
||
646 | $link = $this->Link($action); |
||
647 | $this->extend('updatePreviewLink', $link); |
||
648 | |||
649 | return $link; |
||
650 | } |
||
651 | |||
652 | /** |
||
653 | * @return boolean |
||
654 | */ |
||
655 | public function isCMSPreview() |
||
656 | { |
||
657 | if (Controller::has_curr()) { |
||
658 | $controller = Controller::curr(); |
||
659 | |||
660 | if ($controller->getRequest()->requestVar('CMSPreview')) { |
||
661 | return true; |
||
662 | } |
||
663 | } |
||
664 | |||
665 | return false; |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * @param bool $directLink Indicates that the GridFieldDetailEdit form link should be given even if the block can be |
||
670 | * edited in-line. |
||
671 | * @return null|string |
||
672 | * @throws \SilverStripe\ORM\ValidationException |
||
673 | */ |
||
674 | public function CMSEditLink($directLink = false) |
||
675 | { |
||
676 | // Allow for repeated calls to be returned from cache |
||
677 | if (isset($this->cacheData['cms_edit_link'])) { |
||
678 | return $this->cacheData['cms_edit_link']; |
||
679 | } |
||
680 | |||
681 | $relationName = $this->getAreaRelationName(); |
||
682 | $page = $this->getPage(); |
||
683 | |||
684 | if (!$page) { |
||
685 | $link = null; |
||
686 | $this->extend('updateCMSEditLink', $link); |
||
687 | return $link; |
||
688 | } |
||
689 | |||
690 | if (!$page instanceof SiteTree && method_exists($page, 'CMSEditLink')) { |
||
691 | $link = Controller::join_links($page->CMSEditLink(), 'ItemEditForm'); |
||
692 | } else { |
||
693 | $link = $page->CMSEditLink(); |
||
694 | } |
||
695 | |||
696 | // In-line editable blocks should just take you to the page. Editable ones should add the suffix for detail form |
||
697 | if (!$this->inlineEditable() || $directLink) { |
||
698 | $link = Controller::join_links( |
||
699 | singleton(CMSPageEditController::class)->Link('EditForm'), |
||
700 | $page->ID, |
||
701 | 'field/' . $relationName . '/item/', |
||
702 | $this->ID, |
||
703 | 'edit' |
||
704 | ); |
||
705 | } |
||
706 | |||
707 | $this->extend('updateCMSEditLink', $link); |
||
708 | |||
709 | $this->cacheData['cms_edit_link'] = $link; |
||
710 | return $link; |
||
711 | } |
||
712 | |||
713 | /** |
||
714 | * Retrieve a elemental area relation for creating cms links |
||
715 | * |
||
716 | * @return int|string The name of a valid elemental area relation |
||
717 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
718 | * @throws \SilverStripe\ORM\ValidationException |
||
719 | */ |
||
720 | public function getAreaRelationName() |
||
721 | { |
||
722 | // Allow repeated calls to return from internal cache |
||
723 | if (isset($this->cacheData['area_relation_name'])) { |
||
724 | return $this->cacheData['area_relation_name']; |
||
725 | } |
||
726 | |||
727 | $page = $this->getPage(); |
||
728 | |||
729 | $result = 'ElementalArea'; |
||
730 | |||
731 | if ($page) { |
||
732 | $has_one = $page->config()->get('has_one'); |
||
733 | $area = $this->Parent(); |
||
734 | |||
735 | foreach ($has_one as $relationName => $relationClass) { |
||
736 | if ($page instanceof BaseElement && $relationName === 'Parent') { |
||
737 | continue; |
||
738 | } |
||
739 | if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) { |
||
740 | $result = $relationName; |
||
741 | break; |
||
742 | } |
||
743 | } |
||
744 | } |
||
745 | |||
746 | $this->setAreaRelationNameCache($result); |
||
747 | |||
748 | return $result; |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * Sanitise a model class' name for inclusion in a link. |
||
753 | * |
||
754 | * @return string |
||
755 | */ |
||
756 | public function sanitiseClassName($class, $delimiter = '-') |
||
757 | { |
||
758 | return str_replace('\\', $delimiter, $class); |
||
759 | } |
||
760 | |||
761 | public function unsanitiseClassName($class, $delimiter = '-') |
||
762 | { |
||
763 | return str_replace($delimiter, '\\', $class); |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * @return null|string |
||
768 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
769 | * @throws \SilverStripe\ORM\ValidationException |
||
770 | */ |
||
771 | public function getEditLink() |
||
772 | { |
||
773 | return Director::absoluteURL($this->CMSEditLink()); |
||
774 | } |
||
775 | |||
776 | /** |
||
777 | * @return DBField|null |
||
778 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
779 | * @throws \SilverStripe\ORM\ValidationException |
||
780 | */ |
||
781 | public function PageCMSEditLink() |
||
782 | { |
||
783 | if ($page = $this->getPage()) { |
||
784 | return DBField::create_field('HTMLText', sprintf( |
||
785 | '<a href="%s">%s</a>', |
||
786 | $page->CMSEditLink(), |
||
787 | $page->Title |
||
788 | )); |
||
789 | } |
||
790 | |||
791 | return null; |
||
792 | } |
||
793 | |||
794 | /** |
||
795 | * @return string |
||
796 | */ |
||
797 | public function getMimeType() |
||
798 | { |
||
799 | return 'text/html'; |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * This can be overridden on child elements to create a summary for display |
||
804 | * in GridFields. |
||
805 | * |
||
806 | * @return string |
||
807 | */ |
||
808 | public function getSummary() |
||
809 | { |
||
810 | return ''; |
||
811 | } |
||
812 | |||
813 | /** |
||
814 | * The block config defines a set of data (usually set through config on the element) that will be made available in |
||
815 | * client side config. Individual element types may choose to add config variable for use in React code |
||
816 | * |
||
817 | * @return array |
||
818 | */ |
||
819 | public static function getBlockConfig() |
||
820 | { |
||
821 | return []; |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * The block actions is an associative array available for providing data to the client side to be used to describe |
||
826 | * actions that may be performed. This is available as a plain "ObjectType" in the GraphQL schema. |
||
827 | * |
||
828 | * By default the only action is "edit" which is simply the URL where the block may be edited. |
||
829 | * |
||
830 | * To modify the actions, either use the extension point or overload the `provideBlockSchema` method. |
||
831 | * |
||
832 | * @internal This API may change in future. Treat this as a `final` method. |
||
833 | * @return array |
||
834 | */ |
||
835 | public function getBlockSchema() |
||
836 | { |
||
837 | $blockSchema = $this->provideBlockSchema(); |
||
838 | |||
839 | $this->extend('updateBlockSchema', $blockSchema); |
||
840 | |||
841 | return $blockSchema; |
||
842 | } |
||
843 | |||
844 | /** |
||
845 | * Provide block schema data, which will be serialised and sent via GraphQL to the editor client. |
||
846 | * |
||
847 | * Overload this method in child element classes to augment, or use the extension point on `getBlockSchema` |
||
848 | * to update it from an `Extension`. |
||
849 | * |
||
850 | * @return array |
||
851 | */ |
||
852 | protected function provideBlockSchema() |
||
853 | { |
||
854 | return [ |
||
855 | // Currently GraphQL doesn't expose the correct type name and just returns "base element"s. This is a |
||
856 | // workaround until we can scaffold a query client side that specifies by type name |
||
857 | 'typeName' => StaticSchema::inst()->typeNameForDataObject(static::class), |
||
858 | 'actions' => [ |
||
859 | 'edit' => $this->getEditLink(), |
||
860 | ], |
||
861 | ]; |
||
862 | } |
||
863 | |||
864 | /** |
||
865 | * Generate markup for element type icons suitable for use in GridFields. |
||
866 | * |
||
867 | * @return null|DBHTMLText |
||
868 | */ |
||
869 | public function getIcon() |
||
870 | { |
||
871 | $data = ArrayData::create([]); |
||
872 | |||
873 | $iconClass = $this->config()->get('icon'); |
||
874 | if ($iconClass) { |
||
875 | $data->IconClass = $iconClass; |
||
876 | |||
877 | // Add versioned states (rendered as a circle over the icon) |
||
878 | if ($this->hasExtension(Versioned::class)) { |
||
879 | $data->IsVersioned = true; |
||
880 | if ($this->isOnDraftOnly()) { |
||
881 | $data->VersionState = 'draft'; |
||
882 | $data->VersionStateTitle = _t( |
||
883 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.ADDEDTODRAFTHELP', |
||
884 | 'Item has not been published yet' |
||
885 | ); |
||
886 | } elseif ($this->isModifiedOnDraft()) { |
||
887 | $data->VersionState = 'modified'; |
||
888 | $data->VersionStateTitle = $data->VersionStateTitle = _t( |
||
889 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.MODIFIEDONDRAFTHELP', |
||
890 | 'Item has unpublished changes' |
||
891 | ); |
||
892 | } |
||
893 | } |
||
894 | |||
895 | return $data->renderWith(__CLASS__ . '/PreviewIcon'); |
||
896 | } |
||
897 | |||
898 | return null; |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * Get a description for this content element, if available |
||
903 | * |
||
904 | * @return string |
||
905 | */ |
||
906 | public function getDescription() |
||
907 | { |
||
908 | $description = $this->config()->uninherited('description'); |
||
909 | if ($description) { |
||
910 | return _t(__CLASS__ . '.Description', $description); |
||
911 | } |
||
912 | return ''; |
||
913 | } |
||
914 | |||
915 | /** |
||
916 | * Generate markup for element type, with description suitable for use in |
||
917 | * GridFields. |
||
918 | * |
||
919 | * @return DBField |
||
920 | */ |
||
921 | public function getTypeNice() |
||
922 | { |
||
923 | $description = $this->getDescription(); |
||
924 | $desc = ($description) ? ' <span class="element__note"> — ' . $description . '</span>' : ''; |
||
925 | |||
926 | return DBField::create_field( |
||
927 | 'HTMLVarchar', |
||
928 | $this->getType() . $desc |
||
929 | ); |
||
930 | } |
||
931 | |||
932 | /** |
||
933 | * @return \SilverStripe\ORM\FieldType\DBHTMLText |
||
934 | */ |
||
935 | public function getEditorPreview() |
||
936 | { |
||
937 | $templates = $this->getRenderTemplates('_EditorPreview'); |
||
938 | $templates[] = BaseElement::class . '_EditorPreview'; |
||
939 | |||
940 | return $this->renderWith($templates); |
||
941 | } |
||
942 | |||
943 | /** |
||
944 | * @return Member |
||
945 | */ |
||
946 | public function getAuthor() |
||
947 | { |
||
948 | if ($this->AuthorID) { |
||
0 ignored issues
–
show
The property
AuthorID does not exist on DNADesign\Elemental\Models\BaseElement . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||
949 | return Member::get()->byId($this->AuthorID); |
||
950 | } |
||
951 | |||
952 | return null; |
||
953 | } |
||
954 | |||
955 | /** |
||
956 | * Get a user defined style variant for this element, if available |
||
957 | * |
||
958 | * @return string |
||
959 | */ |
||
960 | public function getStyleVariant() |
||
961 | { |
||
962 | $style = $this->Style; |
||
963 | $styles = $this->config()->get('styles'); |
||
964 | |||
965 | if (isset($styles[$style])) { |
||
966 | $style = strtolower($style); |
||
967 | } else { |
||
968 | $style = ''; |
||
969 | } |
||
970 | |||
971 | $this->extend('updateStyleVariant', $style); |
||
972 | |||
973 | return $style; |
||
974 | } |
||
975 | |||
976 | /** |
||
977 | * @return mixed|null |
||
978 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
979 | * @throws \SilverStripe\ORM\ValidationException |
||
980 | */ |
||
981 | public function getPageTitle() |
||
982 | { |
||
983 | $page = $this->getPage(); |
||
984 | |||
985 | if ($page) { |
||
986 | return $page->Title; |
||
987 | } |
||
988 | |||
989 | return null; |
||
990 | } |
||
991 | |||
992 | /** |
||
993 | * @return boolean |
||
994 | */ |
||
995 | public function First() |
||
996 | { |
||
997 | return ($this->Parent()->Elements()->first()->ID === $this->ID); |
||
998 | } |
||
999 | |||
1000 | /** |
||
1001 | * @return boolean |
||
1002 | */ |
||
1003 | public function Last() |
||
1004 | { |
||
1005 | return ($this->Parent()->Elements()->last()->ID === $this->ID); |
||
1006 | } |
||
1007 | |||
1008 | /** |
||
1009 | * @return int |
||
1010 | */ |
||
1011 | public function TotalItems() |
||
1012 | { |
||
1013 | return $this->Parent()->Elements()->count(); |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * Returns the position of the current element. |
||
1018 | * |
||
1019 | * @return int |
||
1020 | */ |
||
1021 | public function Pos() |
||
1022 | { |
||
1023 | return ($this->Parent()->Elements()->filter('Sort:LessThan', $this->Sort)->count() + 1); |
||
1024 | } |
||
1025 | |||
1026 | /** |
||
1027 | * @return string |
||
1028 | */ |
||
1029 | public function EvenOdd() |
||
1030 | { |
||
1031 | $odd = (bool) ($this->Pos() % 2); |
||
1032 | |||
1033 | return ($odd) ? 'odd' : 'even'; |
||
1034 | } |
||
1035 | } |
||
1036 |