-
Notifications
You must be signed in to change notification settings - Fork 27
/
Quicklink.php
135 lines (113 loc) · 4.03 KB
/
Quicklink.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace CWP\CWP\Model;
use CWP\CWP\PageTypes\BaseHomePage;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject;
/**
* @method BaseHomePage Parent()
* @method SiteTree InternalLink()
*/
class Quicklink extends DataObject
{
private static $db = [
'Name' => 'Varchar(255)',
'ExternalLink' => 'Varchar(255)',
'SortOrder' => 'Int',
];
private static $has_one = [
'Parent' => BaseHomePage::class,
'InternalLink' => SiteTree::class,
];
private static $summary_fields = [
'Name' => 'Name',
'InternalLink.Title' => 'Internal Link',
'ExternalLink' => 'External Link',
];
private static $table_name = 'Quicklink';
public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);
$labels['Name'] = _t(__CLASS__ . '.NameLabel', 'Name');
$labels['ExternalLink'] = _t(__CLASS__ . '.ExternalLinkLabel', 'External Link');
$labels['SortOrder'] = _t(__CLASS__ . '.SortOrderLabel', 'Sort Order');
$labels['ParentID'] = _t(__CLASS__ . '.ParentRelationLabel', 'Parent');
$labels['InternalLinkID'] = _t(__CLASS__ . '.InternalLinkLabel', 'Internal Link');
return $labels;
}
public function getLink()
{
if ($this->ExternalLink) {
$url = parse_url($this->ExternalLink);
// if no scheme set in the link, default to http
if (!isset($url['scheme'])) {
return 'http://' . $this->ExternalLink;
}
return $this->ExternalLink;
}
if ($this->InternalLinkID) {
return $this->InternalLink()->Link();
}
}
public function canCreate($member = null, $context = [])
{
// Creating quick links should not be the same permission level as creating parent pages for them, they're
// essentially content in the context of the page, so use the edit permission instead.
return $this->canEdit($member);
}
public function canEdit($member = null)
{
return $this->Parent()->canEdit($member);
}
public function canDelete($member = null)
{
return $this->Parent()->canDelete($member);
}
public function canView($member = null)
{
return $this->Parent()->canView($member);
}
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('ParentID');
$externalLinkField = $fields->fieldByName('Root.Main.ExternalLink');
$fields->removeByName('ExternalLink');
$fields->removeByName('InternalLinkID');
$fields->removeByName('SortOrder');
$externalLinkField->addExtraClass('noBorder');
$fields->addFieldToTab('Root.Main', CompositeField::create(
array(
TreeDropdownField::create(
'InternalLinkID',
$this->fieldLabel('InternalLinkID'),
SiteTree::class
),
$externalLinkField,
$wrap = CompositeField::create(
$extraLabel = LiteralField::create(
'NoteOverride',
sprintf('<div class="message good notice">%s</div>', _t(
__CLASS__ . '.Note',
'Note: If you specify an External Link, the Internal Link will be ignored.'
))
)
)
)
));
$fields->insertBefore(
'Name',
LiteralField::create(
'Note',
sprintf('<p>%s</p>', _t(
__CLASS__ . '.Note2',
'Use this to specify a link to a page either on this site '
. '(Internal Link) or another site (External Link).'
))
)
);
return $fields;
}
}