1 | <?php |
||
2 | |||
3 | namespace DNADesign\Elemental\Models; |
||
4 | |||
5 | use DNADesign\Elemental\Controllers\ElementController; |
||
6 | use DNADesign\Elemental\Forms\EditFormFactory; |
||
7 | use DNADesign\Elemental\Forms\TextCheckboxGroupField; |
||
8 | use DNADesign\Elemental\ORM\FieldType\DBObjectType; |
||
9 | use Exception; |
||
10 | use SilverStripe\CMS\Controllers\CMSPageEditController; |
||
11 | use SilverStripe\CMS\Model\SiteTree; |
||
12 | use SilverStripe\Control\Controller; |
||
13 | use SilverStripe\Control\Director; |
||
14 | use SilverStripe\Core\ClassInfo; |
||
15 | use SilverStripe\Core\Injector\Injector; |
||
16 | use SilverStripe\Forms\CheckboxField; |
||
17 | use SilverStripe\Forms\DropdownField; |
||
18 | use SilverStripe\Forms\FieldList; |
||
19 | use SilverStripe\Forms\HiddenField; |
||
20 | use SilverStripe\Forms\NumericField; |
||
21 | use SilverStripe\Forms\TextField; |
||
22 | use SilverStripe\GraphQL\Scaffolding\StaticSchema; |
||
23 | use SilverStripe\ORM\DataObject; |
||
24 | use SilverStripe\ORM\FieldType\DBBoolean; |
||
25 | use SilverStripe\ORM\FieldType\DBField; |
||
26 | use SilverStripe\ORM\FieldType\DBHTMLText; |
||
27 | use SilverStripe\Security\Member; |
||
28 | use SilverStripe\Security\Permission; |
||
29 | use SilverStripe\Versioned\Versioned; |
||
30 | use SilverStripe\View\ArrayData; |
||
31 | use SilverStripe\View\Parsers\URLSegmentFilter; |
||
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); |
||
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 | |||
337 | return parent::getCMSFields(); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Get the type of the current block, for use in GridField summaries, block |
||
342 | * type dropdowns etc. Examples are "Content", "File", "Media", etc. |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | public function getType() |
||
347 | { |
||
348 | return _t(__CLASS__ . '.BlockType', 'Block'); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Proxy through to configuration setting 'inline_editable' |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function inlineEditable() |
||
357 | { |
||
358 | return static::config()->get('inline_editable'); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param ElementController $controller |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setController($controller) |
||
367 | { |
||
368 | $this->controller = $controller; |
||
369 | |||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @throws Exception If the specified controller class doesn't exist |
||
375 | * |
||
376 | * @return ElementController |
||
377 | */ |
||
378 | public function getController() |
||
379 | { |
||
380 | if ($this->controller) { |
||
381 | return $this->controller; |
||
382 | } |
||
383 | |||
384 | $controllerClass = self::config()->controller_class; |
||
385 | |||
386 | if (!class_exists($controllerClass)) { |
||
387 | throw new Exception( |
||
388 | 'Could not find controller class ' . $controllerClass . ' as defined in ' . static::class |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | $this->controller = Injector::inst()->create($controllerClass, $this); |
||
393 | $this->controller->doInit(); |
||
394 | |||
395 | return $this->controller; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @param string $name |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function setAreaRelationNameCache($name) |
||
403 | { |
||
404 | $this->cacheData['area_relation_name'] = $name; |
||
405 | |||
406 | return $this; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @return Controller |
||
411 | */ |
||
412 | public function Top() |
||
413 | { |
||
414 | return (Controller::has_curr()) ? Controller::curr() : null; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Default way to render element in templates. Note that all blocks should |
||
419 | * be rendered through their {@link ElementController} class as this |
||
420 | * contains the holder styles. |
||
421 | * |
||
422 | * @return string|null HTML |
||
423 | */ |
||
424 | public function forTemplate($holder = true) |
||
425 | { |
||
426 | $templates = $this->getRenderTemplates(); |
||
427 | |||
428 | if ($templates) { |
||
429 | return $this->renderWith($templates); |
||
430 | } |
||
431 | |||
432 | return null; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @param string $suffix |
||
437 | * |
||
438 | * @return array |
||
439 | */ |
||
440 | public function getRenderTemplates($suffix = '') |
||
441 | { |
||
442 | $classes = ClassInfo::ancestry($this->ClassName); |
||
443 | $classes[static::class] = static::class; |
||
444 | $classes = array_reverse($classes); |
||
445 | $templates = []; |
||
446 | |||
447 | foreach ($classes as $key => $class) { |
||
448 | if ($class == BaseElement::class) { |
||
449 | continue; |
||
450 | } |
||
451 | |||
452 | if ($class == DataObject::class) { |
||
453 | break; |
||
454 | } |
||
455 | |||
456 | if ($style = $this->Style) { |
||
457 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName() . '_' . $style; |
||
458 | $templates[$class][] = $class . $suffix . '_' . $style; |
||
459 | } |
||
460 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName(); |
||
461 | $templates[$class][] = $class . $suffix; |
||
462 | } |
||
463 | |||
464 | $this->extend('updateRenderTemplates', $templates, $suffix); |
||
465 | |||
466 | $templateFlat = []; |
||
467 | foreach ($templates as $class => $variations) { |
||
468 | $templateFlat = array_merge($templateFlat, $variations); |
||
469 | } |
||
470 | |||
471 | return $templateFlat; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Given form data (wit |
||
476 | * |
||
477 | * @param $data |
||
478 | */ |
||
479 | public function updateFromFormData($data) |
||
480 | { |
||
481 | $cmsFields = $this->getCMSFields(); |
||
482 | |||
483 | foreach ($data as $field => $datum) { |
||
484 | $field = $cmsFields->dataFieldByName($field); |
||
485 | |||
486 | if (!$field) { |
||
487 | continue; |
||
488 | } |
||
489 | |||
490 | $field->setSubmittedValue($datum); |
||
491 | $field->saveInto($this); |
||
492 | } |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Strip all namespaces from class namespace. |
||
497 | * |
||
498 | * @param string $classname e.g. "\Fully\Namespaced\Class" |
||
499 | * |
||
500 | * @return string following the param example, "Class" |
||
501 | */ |
||
502 | protected function stripNamespacing($classname) |
||
503 | { |
||
504 | $classParts = explode('\\', $classname); |
||
505 | return array_pop($classParts); |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * @return string |
||
510 | */ |
||
511 | public function getSimpleClassName() |
||
512 | { |
||
513 | return strtolower($this->sanitiseClassName($this->ClassName, '__')); |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * @return null|SiteTree |
||
518 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
519 | * @throws \SilverStripe\ORM\ValidationException |
||
520 | */ |
||
521 | public function getPage() |
||
522 | { |
||
523 | // Allow for repeated calls to be cached |
||
524 | if (isset($this->cacheData['page'])) { |
||
525 | return $this->cacheData['page']; |
||
526 | } |
||
527 | |||
528 | |||
529 | $class = DataObject::getSchema()->hasOneComponent($this, 'Parent'); |
||
530 | $area = ($this->ParentID) ? DataObject::get_by_id($class, $this->ParentID) : null; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
531 | if ($area instanceof ElementalArea && $area->exists()) { |
||
532 | $this->cacheData['page'] = $area->getOwnerPage(); |
||
533 | return $this->cacheData['page']; |
||
534 | } |
||
535 | |||
536 | return null; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Get a unique anchor name |
||
541 | * |
||
542 | * @return string |
||
543 | */ |
||
544 | public function getAnchor() |
||
545 | { |
||
546 | if ($this->anchor !== null) { |
||
547 | return $this->anchor; |
||
548 | } |
||
549 | |||
550 | $anchorTitle = ''; |
||
551 | |||
552 | if (!$this->config()->disable_pretty_anchor_name) { |
||
553 | if ($this->hasMethod('getAnchorTitle')) { |
||
554 | $anchorTitle = $this->getAnchorTitle(); |
||
555 | } elseif ($this->config()->enable_title_in_template) { |
||
556 | $anchorTitle = $this->getField('Title'); |
||
557 | } |
||
558 | } |
||
559 | |||
560 | if (!$anchorTitle) { |
||
561 | $anchorTitle = 'e'.$this->ID; |
||
562 | } |
||
563 | |||
564 | $filter = URLSegmentFilter::create(); |
||
565 | $titleAsURL = $filter->filter($anchorTitle); |
||
566 | |||
567 | // Ensure that this anchor name isn't already in use |
||
568 | // ie. If two elemental blocks have the same title, it'll append '-2', '-3' |
||
569 | $result = $titleAsURL; |
||
570 | $count = 1; |
||
571 | while (isset(self::$used_anchors[$result]) && self::$used_anchors[$result] !== $this->ID) { |
||
572 | ++$count; |
||
573 | $result = $titleAsURL . '-' . $count; |
||
574 | } |
||
575 | self::$used_anchors[$result] = $this->ID; |
||
576 | return $this->anchor = $result; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * @param string|null $action |
||
581 | * @return string|null |
||
582 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
583 | * @throws \SilverStripe\ORM\ValidationException |
||
584 | */ |
||
585 | public function AbsoluteLink($action = null) |
||
586 | { |
||
587 | if ($page = $this->getPage()) { |
||
588 | $link = $page->AbsoluteLink($action) . '#' . $this->getAnchor(); |
||
589 | |||
590 | return $link; |
||
591 | } |
||
592 | |||
593 | return null; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @param string|null $action |
||
598 | * @return string |
||
599 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
600 | * @throws \SilverStripe\ORM\ValidationException |
||
601 | */ |
||
602 | public function Link($action = null) |
||
603 | { |
||
604 | if ($page = $this->getPage()) { |
||
605 | $link = $page->Link($action) . '#' . $this->getAnchor(); |
||
606 | |||
607 | $this->extend('updateLink', $link); |
||
608 | |||
609 | return $link; |
||
610 | } |
||
611 | |||
612 | return null; |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * @param string|null $action |
||
617 | * @return string |
||
618 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
619 | * @throws \SilverStripe\ORM\ValidationException |
||
620 | */ |
||
621 | public function PreviewLink($action = null) |
||
622 | { |
||
623 | $action = $action . '?ElementalPreview=' . mt_rand(); |
||
624 | $link = $this->Link($action); |
||
625 | $this->extend('updatePreviewLink', $link); |
||
626 | |||
627 | return $link; |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * @return boolean |
||
632 | */ |
||
633 | public function isCMSPreview() |
||
634 | { |
||
635 | if (Controller::has_curr()) { |
||
636 | $controller = Controller::curr(); |
||
637 | |||
638 | if ($controller->getRequest()->requestVar('CMSPreview')) { |
||
639 | return true; |
||
640 | } |
||
641 | } |
||
642 | |||
643 | return false; |
||
644 | } |
||
645 | |||
646 | /** |
||
647 | * @return null|string |
||
648 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
649 | * @throws \SilverStripe\ORM\ValidationException |
||
650 | */ |
||
651 | public function CMSEditLink() |
||
652 | { |
||
653 | // Allow for repeated calls to be returned from cache |
||
654 | if (isset($this->cacheData['cms_edit_link'])) { |
||
655 | return $this->cacheData['cms_edit_link']; |
||
656 | } |
||
657 | |||
658 | $relationName = $this->getAreaRelationName(); |
||
659 | $page = $this->getPage(); |
||
660 | |||
661 | if (!$page) { |
||
662 | return null; |
||
663 | } |
||
664 | |||
665 | if (!$page instanceof SiteTree && method_exists($page, 'CMSEditLink')) { |
||
666 | $link = Controller::join_links($page->CMSEditLink(), 'ItemEditForm'); |
||
667 | } else { |
||
668 | $link = $page->CMSEditLink(); |
||
669 | } |
||
670 | |||
671 | // In-line editable blocks should just take you to the page. Editable ones should add the suffix for detail form |
||
672 | if (!$this->inlineEditable()) { |
||
673 | $link = Controller::join_links( |
||
674 | singleton(CMSPageEditController::class)->Link('EditForm'), |
||
675 | $page->ID, |
||
676 | 'field/' . $relationName . '/item/', |
||
677 | $this->ID, |
||
678 | 'edit' |
||
679 | ); |
||
680 | } |
||
681 | |||
682 | $this->extend('updateCMSEditLink', $link); |
||
683 | |||
684 | $this->cacheData['cms_edit_link'] = $link; |
||
685 | return $link; |
||
686 | } |
||
687 | |||
688 | /** |
||
689 | * Retrieve a elemental area relation for creating cms links |
||
690 | * |
||
691 | * @return int|string The name of a valid elemental area relation |
||
692 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
693 | * @throws \SilverStripe\ORM\ValidationException |
||
694 | */ |
||
695 | public function getAreaRelationName() |
||
696 | { |
||
697 | // Allow repeated calls to return from internal cache |
||
698 | if (isset($this->cacheData['area_relation_name'])) { |
||
699 | return $this->cacheData['area_relation_name']; |
||
700 | } |
||
701 | |||
702 | $page = $this->getPage(); |
||
703 | |||
704 | $result = 'ElementalArea'; |
||
705 | |||
706 | if ($page) { |
||
707 | $has_one = $page->config()->get('has_one'); |
||
708 | $area = $this->Parent(); |
||
709 | |||
710 | foreach ($has_one as $relationName => $relationClass) { |
||
711 | if ($page instanceof BaseElement && $relationName === 'Parent') { |
||
712 | continue; |
||
713 | } |
||
714 | if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) { |
||
715 | $result = $relationName; |
||
716 | break; |
||
717 | } |
||
718 | } |
||
719 | } |
||
720 | |||
721 | $this->setAreaRelationNameCache($result); |
||
722 | |||
723 | return $result; |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Sanitise a model class' name for inclusion in a link. |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function sanitiseClassName($class, $delimiter = '-') |
||
732 | { |
||
733 | return str_replace('\\', $delimiter, $class); |
||
734 | } |
||
735 | |||
736 | public function unsanitiseClassName($class, $delimiter = '-') |
||
737 | { |
||
738 | return str_replace($delimiter, '\\', $class); |
||
739 | } |
||
740 | |||
741 | /** |
||
742 | * @return null|string |
||
743 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
744 | * @throws \SilverStripe\ORM\ValidationException |
||
745 | */ |
||
746 | public function getEditLink() |
||
747 | { |
||
748 | return $this->CMSEditLink(); |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * @return DBField|null |
||
753 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
754 | * @throws \SilverStripe\ORM\ValidationException |
||
755 | */ |
||
756 | public function PageCMSEditLink() |
||
757 | { |
||
758 | if ($page = $this->getPage()) { |
||
759 | return DBField::create_field('HTMLText', sprintf( |
||
760 | '<a href="%s">%s</a>', |
||
761 | $page->CMSEditLink(), |
||
762 | $page->Title |
||
763 | )); |
||
764 | } |
||
765 | |||
766 | return null; |
||
767 | } |
||
768 | |||
769 | /** |
||
770 | * @return string |
||
771 | */ |
||
772 | public function getMimeType() |
||
773 | { |
||
774 | return 'text/html'; |
||
775 | } |
||
776 | |||
777 | /** |
||
778 | * This can be overridden on child elements to create a summary for display |
||
779 | * in GridFields. |
||
780 | * |
||
781 | * @return string |
||
782 | */ |
||
783 | public function getSummary() |
||
784 | { |
||
785 | return ''; |
||
786 | } |
||
787 | |||
788 | /** |
||
789 | * The block config defines a set of data (usually set through config on the element) that will be made available in |
||
790 | * client side config. Individual element types may choose to add config variable for use in React code |
||
791 | * |
||
792 | * @return array |
||
793 | */ |
||
794 | public static function getBlockConfig() |
||
795 | { |
||
796 | return []; |
||
797 | } |
||
798 | |||
799 | /** |
||
800 | * The block actions is an associative array available for providing data to the client side to be used to describe |
||
801 | * actions that may be performed. This is available as a plain "ObjectType" in the GraphQL schema. |
||
802 | * |
||
803 | * By default the only action is "edit" which is simply the URL where the block may be edited. |
||
804 | * |
||
805 | * To modify the actions, either use the extension point or overload the `provideBlockSchema` method. |
||
806 | * |
||
807 | * @internal This API may change in future. Treat this as a `final` method. |
||
808 | * @return array |
||
809 | */ |
||
810 | public function getBlockSchema() |
||
811 | { |
||
812 | $blockSchema = $this->provideBlockSchema(); |
||
813 | |||
814 | $this->extend('updateBlockSchema', $blockSchema); |
||
815 | |||
816 | return $blockSchema; |
||
817 | } |
||
818 | |||
819 | /** |
||
820 | * Provide block schema data, which will be serialised and sent via GraphQL to the editor client. |
||
821 | * |
||
822 | * Overload this method in child element classes to augment, or use the extension point on `getBlockSchema` |
||
823 | * to update it from an `Extension`. |
||
824 | * |
||
825 | * @return array |
||
826 | */ |
||
827 | protected function provideBlockSchema() |
||
828 | { |
||
829 | return [ |
||
830 | // Currently GraphQL doesn't expose the correct type name and just returns "base element"s. This is a |
||
831 | // workaround until we can scaffold a query client side that specifies by type name |
||
832 | 'typeName' => StaticSchema::inst()->typeNameForDataObject(static::class), |
||
833 | 'actions' => [ |
||
834 | 'edit' => $this->getEditLink(), |
||
835 | ], |
||
836 | ]; |
||
837 | } |
||
838 | |||
839 | /** |
||
840 | * Generate markup for element type icons suitable for use in GridFields. |
||
841 | * |
||
842 | * @return null|DBHTMLText |
||
843 | */ |
||
844 | public function getIcon() |
||
845 | { |
||
846 | $data = ArrayData::create([]); |
||
847 | |||
848 | $iconClass = $this->config()->get('icon'); |
||
849 | if ($iconClass) { |
||
850 | $data->IconClass = $iconClass; |
||
851 | |||
852 | // Add versioned states (rendered as a circle over the icon) |
||
853 | if ($this->hasExtension(Versioned::class)) { |
||
854 | $data->IsVersioned = true; |
||
855 | if ($this->isOnDraftOnly()) { |
||
856 | $data->VersionState = 'draft'; |
||
857 | $data->VersionStateTitle = _t( |
||
858 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.ADDEDTODRAFTHELP', |
||
859 | 'Item has not been published yet' |
||
860 | ); |
||
861 | } elseif ($this->isModifiedOnDraft()) { |
||
862 | $data->VersionState = 'modified'; |
||
863 | $data->VersionStateTitle = $data->VersionStateTitle = _t( |
||
864 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.MODIFIEDONDRAFTHELP', |
||
865 | 'Item has unpublished changes' |
||
866 | ); |
||
867 | } |
||
868 | } |
||
869 | |||
870 | return $data->renderWith(__CLASS__ . '/PreviewIcon'); |
||
871 | } |
||
872 | |||
873 | return null; |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * Get a description for this content element, if available |
||
878 | * |
||
879 | * @return string |
||
880 | */ |
||
881 | public function getDescription() |
||
882 | { |
||
883 | $description = $this->config()->uninherited('description'); |
||
884 | if ($description) { |
||
885 | return _t(__CLASS__ . '.Description', $description); |
||
886 | } |
||
887 | return ''; |
||
888 | } |
||
889 | |||
890 | /** |
||
891 | * Generate markup for element type, with description suitable for use in |
||
892 | * GridFields. |
||
893 | * |
||
894 | * @return DBField |
||
895 | */ |
||
896 | public function getTypeNice() |
||
897 | { |
||
898 | $description = $this->getDescription(); |
||
899 | $desc = ($description) ? ' <span class="element__note"> — ' . $description . '</span>' : ''; |
||
900 | |||
901 | return DBField::create_field( |
||
902 | 'HTMLVarchar', |
||
903 | $this->getType() . $desc |
||
904 | ); |
||
905 | } |
||
906 | |||
907 | /** |
||
908 | * @return \SilverStripe\ORM\FieldType\DBHTMLText |
||
909 | */ |
||
910 | public function getEditorPreview() |
||
911 | { |
||
912 | $templates = $this->getRenderTemplates('_EditorPreview'); |
||
913 | $templates[] = BaseElement::class . '_EditorPreview'; |
||
914 | |||
915 | return $this->renderWith($templates); |
||
916 | } |
||
917 | |||
918 | /** |
||
919 | * @return Member |
||
920 | */ |
||
921 | public function getAuthor() |
||
922 | { |
||
923 | if ($this->AuthorID) { |
||
924 | return Member::get()->byId($this->AuthorID); |
||
925 | } |
||
926 | |||
927 | return null; |
||
928 | } |
||
929 | |||
930 | /** |
||
931 | * Get a user defined style variant for this element, if available |
||
932 | * |
||
933 | * @return string |
||
934 | */ |
||
935 | public function getStyleVariant() |
||
936 | { |
||
937 | $style = $this->Style; |
||
938 | $styles = $this->config()->get('styles'); |
||
939 | |||
940 | if (isset($styles[$style])) { |
||
941 | $style = strtolower($style); |
||
942 | } else { |
||
943 | $style = ''; |
||
944 | } |
||
945 | |||
946 | $this->extend('updateStyleVariant', $style); |
||
947 | |||
948 | return $style; |
||
949 | } |
||
950 | |||
951 | /** |
||
952 | * @return mixed|null |
||
953 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
954 | * @throws \SilverStripe\ORM\ValidationException |
||
955 | */ |
||
956 | public function getPageTitle() |
||
957 | { |
||
958 | $page = $this->getPage(); |
||
959 | |||
960 | if ($page) { |
||
961 | return $page->Title; |
||
962 | } |
||
963 | |||
964 | return null; |
||
965 | } |
||
966 | |||
967 | /** |
||
968 | * @return boolean |
||
969 | */ |
||
970 | public function First() |
||
971 | { |
||
972 | return ($this->Parent()->Elements()->first()->ID === $this->ID); |
||
973 | } |
||
974 | |||
975 | /** |
||
976 | * @return boolean |
||
977 | */ |
||
978 | public function Last() |
||
979 | { |
||
980 | return ($this->Parent()->Elements()->last()->ID === $this->ID); |
||
981 | } |
||
982 | |||
983 | /** |
||
984 | * @return int |
||
985 | */ |
||
986 | public function TotalItems() |
||
987 | { |
||
988 | return $this->Parent()->Elements()->count(); |
||
989 | } |
||
990 | |||
991 | /** |
||
992 | * Returns the position of the current element. |
||
993 | * |
||
994 | * @return int |
||
995 | */ |
||
996 | public function Pos() |
||
997 | { |
||
998 | return ($this->Parent()->Elements()->filter('Sort:LessThan', $this->Sort)->count() + 1); |
||
999 | } |
||
1000 | |||
1001 | /** |
||
1002 | * @return string |
||
1003 | */ |
||
1004 | public function EvenOdd() |
||
1005 | { |
||
1006 | $odd = (bool) ($this->Pos() % 2); |
||
1007 | |||
1008 | return ($odd) ? 'odd' : 'even'; |
||
1009 | } |
||
1010 | } |
||
1011 |