1
<?php
2

3
namespace CWP\CWP\Model;
4

5
use CWP\CWP\PageTypes\BaseHomePage;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Forms\CompositeField;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\TreeDropdownField;
10
use SilverStripe\ORM\DataObject;
11

12
/**
13
 * @method BaseHomePage Parent()
14
 * @method SiteTree InternalLink()
15
 */
16
class Quicklink extends DataObject
17
{
18
    private static $db = [
19
        'Name' => 'Varchar(255)',
20
        'ExternalLink' => 'Varchar(255)',
21
        'SortOrder' => 'Int',
22
    ];
23

24
    private static $has_one = [
25
        'Parent' => BaseHomePage::class,
26
        'InternalLink' => SiteTree::class,
27
    ];
28

29
    private static $summary_fields = [
30
        'Name' => 'Name',
31
        'InternalLink.Title' => 'Internal Link',
32
        'ExternalLink' => 'External Link',
33
    ];
34

35
    private static $table_name = 'Quicklink';
36

37 0
    public function fieldLabels($includerelations = true)
38
    {
39 0
        $labels = parent::fieldLabels($includerelations);
40 0
        $labels['Name'] = _t(__CLASS__ . '.NameLabel', 'Name');
41 0
        $labels['ExternalLink'] = _t(__CLASS__ . '.ExternalLinkLabel', 'External Link');
42 0
        $labels['SortOrder'] = _t(__CLASS__ . '.SortOrderLabel', 'Sort Order');
43 0
        $labels['ParentID'] = _t(__CLASS__ . '.ParentRelationLabel', 'Parent');
44 0
        $labels['InternalLinkID'] = _t(__CLASS__ . '.InternalLinkLabel', 'Internal Link');
45

46 0
        return $labels;
47
    }
48

49 0
    public function getLink()
50
    {
51 0
        if ($this->ExternalLink) {
52 0
            $url = parse_url($this->ExternalLink);
53

54
            // if no scheme set in the link, default to http
55 0
            if (!isset($url['scheme'])) {
56 0
                return 'http://' . $this->ExternalLink;
57
            }
58

59 0
            return $this->ExternalLink;
60
        }
61

62 0
        if ($this->InternalLinkID) {
63 0
            return $this->InternalLink()->Link();
64
        }
65
    }
66

67 0
    public function canCreate($member = null, $context = [])
68
    {
69
        // Creating quick links should not be the same permission level as creating parent pages for them, they're
70
        // essentially content in the context of the page, so use the edit permission instead.
71 0
        return $this->canEdit($member);
72
    }
73

74 0
    public function canEdit($member = null)
75
    {
76 0
        return $this->Parent()->canEdit($member);
77
    }
78

79 0
    public function canDelete($member = null)
80
    {
81 0
        return $this->Parent()->canDelete($member);
82
    }
83

84 0
    public function canView($member = null)
85
    {
86 0
        return $this->Parent()->canView($member);
87
    }
88

89 0
    public function getCMSFields()
90
    {
91 0
        $fields = parent::getCMSFields();
92

93 0
        $fields->removeByName('ParentID');
94

95 0
        $externalLinkField = $fields->fieldByName('Root.Main.ExternalLink');
96

97 0
        $fields->removeByName('ExternalLink');
98 0
        $fields->removeByName('InternalLinkID');
99 0
        $fields->removeByName('SortOrder');
100 0
        $externalLinkField->addExtraClass('noBorder');
101

102 0
        $fields->addFieldToTab('Root.Main', CompositeField::create(
103
            array(
104 0
                TreeDropdownField::create(
105 0
                    'InternalLinkID',
106 0
                    $this->fieldLabel('InternalLinkID'),
107 0
                    SiteTree::class
108
                ),
109 0
                $externalLinkField,
110 0
                $wrap = CompositeField::create(
111 0
                    $extraLabel = LiteralField::create(
112 0
                        'NoteOverride',
113 0
                        sprintf('<div class="message good notice">%s</div>', _t(
114 0
                            __CLASS__ . '.Note',
115 0
                            'Note: If you specify an External Link, the Internal Link will be ignored.'
116
                        ))
117
                    )
118
                )
119
            )
120
        ));
121 0
        $fields->insertBefore(
122 0
            'Name',
123 0
            LiteralField::create(
124 0
                'Note',
125 0
                sprintf('<p>%s</p>', _t(
126 0
                    __CLASS__ . '.Note2',
127
                    'Use this to specify a link to a page either on this site '
128 0
                        . '(Internal Link) or another site (External Link).'
129
                ))
130
            )
131
        );
132

133 0
        return $fields;
134
    }
135
}

Read our documentation on viewing source code .

Loading