Passed
Pull Request — master (#650)
by Loz
04:12
created

src/Models/BaseElement.php (1 issue)

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 Loz Calver
The property ParentID does not exist on DNADesign\Elemental\Models\BaseElement. Since you implemented __get, consider adding a @property annotation.
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
        $editLinkPrefix = '';
666
        if (!$page instanceof SiteTree && method_exists($page, 'CMSEditLink')) {
667
            $link = Controller::join_links($page->CMSEditLink(), 'ItemEditForm');
668
        } else {
669
            $link = Controller::join_links(
670
                singleton(CMSPageEditController::class)->Link('EditForm'),
671
                $page->ID
672
            );
673
        }
674
675
        // In-line editable blocks should just take you to the page. Editable ones should add the suffix for detail form
676
        if (!$this->inlineEditable()) {
677
            $link = Controller::join_links(
678
                $link,
679
                'field/' . $relationName . '/item/',
680
                $this->ID,
681
                'edit'
682
            );
683
        }
684
685
        $this->extend('updateCMSEditLink', $link);
686
687
        $this->cacheData['cms_edit_link'] = $link;
688
        return $link;
689
    }
690
691
    /**
692
     * Retrieve a elemental area relation for creating cms links
693
     *
694
     * @return int|string The name of a valid elemental area relation
695
     * @throws \Psr\Container\NotFoundExceptionInterface
696
     * @throws \SilverStripe\ORM\ValidationException
697
     */
698
    public function getAreaRelationName()
699
    {
700
        // Allow repeated calls to return from internal cache
701
        if (isset($this->cacheData['area_relation_name'])) {
702
            return $this->cacheData['area_relation_name'];
703
        }
704
705
        $page = $this->getPage();
706
707
        $result = 'ElementalArea';
708
709
        if ($page) {
710
            $has_one = $page->config()->get('has_one');
711
            $area = $this->Parent();
712
713
            foreach ($has_one as $relationName => $relationClass) {
714
                if ($page instanceof BaseElement && $relationName === 'Parent') {
715
                    continue;
716
                }
717
                if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) {
718
                    $result = $relationName;
719
                    break;
720
                }
721
            }
722
        }
723
724
        $this->setAreaRelationNameCache($result);
725
726
        return $result;
727
    }
728
729
    /**
730
     * Sanitise a model class' name for inclusion in a link.
731
     *
732
     * @return string
733
     */
734
    public function sanitiseClassName($class, $delimiter = '-')
735
    {
736
        return str_replace('\\', $delimiter, $class);
737
    }
738
739
    public function unsanitiseClassName($class, $delimiter = '-')
740
    {
741
        return str_replace($delimiter, '\\', $class);
742
    }
743
744
    /**
745
     * @return null|string
746
     * @throws \Psr\Container\NotFoundExceptionInterface
747
     * @throws \SilverStripe\ORM\ValidationException
748
     */
749
    public function getEditLink()
750
    {
751
        return $this->CMSEditLink();
752
    }
753
754
    /**
755
     * @return DBField|null
756
     * @throws \Psr\Container\NotFoundExceptionInterface
757
     * @throws \SilverStripe\ORM\ValidationException
758
     */
759
    public function PageCMSEditLink()
760
    {
761
        if ($page = $this->getPage()) {
762
            return DBField::create_field('HTMLText', sprintf(
763
                '<a href="%s">%s</a>',
764
                $page->CMSEditLink(),
765
                $page->Title
766
            ));
767
        }
768
769
        return null;
770
    }
771
772
    /**
773
     * @return string
774
     */
775
    public function getMimeType()
776
    {
777
        return 'text/html';
778
    }
779
780
    /**
781
     * This can be overridden on child elements to create a summary for display
782
     * in GridFields.
783
     *
784
     * @return string
785
     */
786
    public function getSummary()
787
    {
788
        return '';
789
    }
790
791
    /**
792
     * The block config defines a set of data (usually set through config on the element) that will be made available in
793
     * client side config. Individual element types may choose to add config variable for use in React code
794
     *
795
     * @return array
796
     */
797
    public static function getBlockConfig()
798
    {
799
        return [];
800
    }
801
802
    /**
803
     * The block actions is an associative array available for providing data to the client side to be used to describe
804
     * actions that may be performed. This is available as a plain "ObjectType" in the GraphQL schema.
805
     *
806
     * By default the only action is "edit" which is simply the URL where the block may be edited.
807
     *
808
     * To modify the actions, either use the extension point or overload the `provideBlockSchema` method.
809
     *
810
     * @internal This API may change in future. Treat this as a `final` method.
811
     * @return array
812
     */
813
    public function getBlockSchema()
814
    {
815
        $blockSchema = $this->provideBlockSchema();
816
817
        $this->extend('updateBlockSchema', $blockSchema);
818
819
        return $blockSchema;
820
    }
821
822
    /**
823
     * Provide block schema data, which will be serialised and sent via GraphQL to the editor client.
824
     *
825
     * Overload this method in child element classes to augment, or use the extension point on `getBlockSchema`
826
     * to update it from an `Extension`.
827
     *
828
     * @return array
829
     */
830
    protected function provideBlockSchema()
831
    {
832
        return [
833
            // Currently GraphQL doesn't expose the correct type name and just returns "base element"s. This is a
834
            // workaround until we can scaffold a query client side that specifies by type name
835
            'typeName' => StaticSchema::inst()->typeNameForDataObject(static::class),
836
            'actions' => [
837
                'edit' => $this->getEditLink(),
838
            ],
839
        ];
840
    }
841
842
    /**
843
     * Generate markup for element type icons suitable for use in GridFields.
844
     *
845
     * @return null|DBHTMLText
846
     */
847
    public function getIcon()
848
    {
849
        $data = ArrayData::create([]);
850
851
        $iconClass = $this->config()->get('icon');
852
        if ($iconClass) {
853
            $data->IconClass = $iconClass;
854
855
            // Add versioned states (rendered as a circle over the icon)
856
            if ($this->hasExtension(Versioned::class)) {
857
                $data->IsVersioned = true;
858
                if ($this->isOnDraftOnly()) {
859
                    $data->VersionState = 'draft';
860
                    $data->VersionStateTitle = _t(
861
                        'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.ADDEDTODRAFTHELP',
862
                        'Item has not been published yet'
863
                    );
864
                } elseif ($this->isModifiedOnDraft()) {
865
                    $data->VersionState = 'modified';
866
                    $data->VersionStateTitle = $data->VersionStateTitle = _t(
867
                        'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.MODIFIEDONDRAFTHELP',
868
                        'Item has unpublished changes'
869
                    );
870
                }
871
            }
872
873
            return $data->renderWith(__CLASS__ . '/PreviewIcon');
874
        }
875
876
        return null;
877
    }
878
879
    /**
880
     * Get a description for this content element, if available
881
     *
882
     * @return string
883
     */
884
    public function getDescription()
885
    {
886
        $description = $this->config()->uninherited('description');
887
        if ($description) {
888
            return _t(__CLASS__ . '.Description', $description);
889
        }
890
        return '';
891
    }
892
893
    /**
894
     * Generate markup for element type, with description suitable for use in
895
     * GridFields.
896
     *
897
     * @return DBField
898
     */
899
    public function getTypeNice()
900
    {
901
        $description = $this->getDescription();
902
        $desc = ($description) ? ' <span class="element__note"> &mdash; ' . $description . '</span>' : '';
903
904
        return DBField::create_field(
905
            'HTMLVarchar',
906
            $this->getType() . $desc
907
        );
908
    }
909
910
    /**
911
     * @return \SilverStripe\ORM\FieldType\DBHTMLText
912
     */
913
    public function getEditorPreview()
914
    {
915
        $templates = $this->getRenderTemplates('_EditorPreview');
916
        $templates[] = BaseElement::class . '_EditorPreview';
917
918
        return $this->renderWith($templates);
919
    }
920
921
    /**
922
     * @return Member
923
     */
924
    public function getAuthor()
925
    {
926
        if ($this->AuthorID) {
927
            return Member::get()->byId($this->AuthorID);
928
        }
929
930
        return null;
931
    }
932
933
    /**
934
     * Get a user defined style variant for this element, if available
935
     *
936
     * @return string
937
     */
938
    public function getStyleVariant()
939
    {
940
        $style = $this->Style;
941
        $styles = $this->config()->get('styles');
942
943
        if (isset($styles[$style])) {
944
            $style = strtolower($style);
945
        } else {
946
            $style = '';
947
        }
948
949
        $this->extend('updateStyleVariant', $style);
950
951
        return $style;
952
    }
953
954
    /**
955
     * @return mixed|null
956
     * @throws \Psr\Container\NotFoundExceptionInterface
957
     * @throws \SilverStripe\ORM\ValidationException
958
     */
959
    public function getPageTitle()
960
    {
961
        $page = $this->getPage();
962
963
        if ($page) {
964
            return $page->Title;
965
        }
966
967
        return null;
968
    }
969
970
    /**
971
     * @return boolean
972
     */
973
    public function First()
974
    {
975
        return ($this->Parent()->Elements()->first()->ID === $this->ID);
976
    }
977
978
    /**
979
     * @return boolean
980
     */
981
    public function Last()
982
    {
983
        return ($this->Parent()->Elements()->last()->ID === $this->ID);
984
    }
985
986
    /**
987
     * @return int
988
     */
989
    public function TotalItems()
990
    {
991
        return $this->Parent()->Elements()->count();
992
    }
993
994
    /**
995
     * Returns the position of the current element.
996
     *
997
     * @return int
998
     */
999
    public function Pos()
1000
    {
1001
        return ($this->Parent()->Elements()->filter('Sort:LessThan', $this->Sort)->count() + 1);
1002
    }
1003
1004
    /**
1005
     * @return string
1006
     */
1007
    public function EvenOdd()
1008
    {
1009
        $odd = (bool) ($this->Pos() % 2);
1010
1011
        return  ($odd) ? 'odd' : 'even';
1012
    }
1013
}
1014