-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
LinkSiteTree.php
146 lines (133 loc) · 4.13 KB
/
LinkSiteTree.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
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace gorriecoe\Link\Extensions;
use gorriecoe\Link\Models\Link;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataExtension;
use UncleCheese\DisplayLogic\Forms\Wrapper;
/**
* Add sitetree type to link object
*
* @package silverstripe-link
*
* @property Link|$this owner
* @property int SiteTreeID
* @method SiteTree SiteTree()
*/
class LinkSiteTree extends DataExtension
{
/**
* Database fields
* @var array
*/
private static $db = [
'Anchor' => 'Varchar(255)',
];
/**
* Has_one relationship
* @var array
*/
private static $has_one = [
'SiteTree' => SiteTree::class,
];
/**
* A map of object types that can be linked to
* Custom dataobjects can be added to this
*
* @var array
**/
private static $types = [
'SiteTree' => 'Page on this website',
];
/**
* Defines the label used in the sitetree dropdown.
* @param String $sitetree_field_label
*/
private static $sitetree_field_label = 'MenuTitle';
/**
* Update Fields
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
$owner = $this->owner;
$config = $owner->config();
$sitetree_field_label = $config->get('sitetree_field_label') ? : 'MenuTitle';
// Insert site tree field after the file selection field
$fields->insertAfter(
'Type',
Wrapper::create(
$sitetreeField = TreeDropdownField::create(
'SiteTreeID',
_t(__CLASS__ . '.PAGE', 'Page'),
SiteTree::class
)
->setTitleField($sitetree_field_label),
TextField::create(
'Anchor',
_t(__CLASS__ . '.ANCHOR', 'Anchor/Querystring')
)
->setDescription(_t(__CLASS__ . '.ANCHORINFO', 'Include # at the start of your anchor name or, ? at the start of your querystring'))
)
->displayIf('Type')->isEqualTo('SiteTree')->end()
);
// Display warning if the selected page is deleted or unpublished
if ($owner->SiteTreeID && !$owner->SiteTree()->isPublished()) {
$sitetreeField->setDescription(_t(__CLASS__ . '.DELETEDWARNING', 'Warning: The selected page appears to have been deleted or unpublished. This link may not appear or may be broken in the frontend'));
}
}
/**
* @return bool
*/
public function updateIsCurrent(&$status)
{
$owner = $this->owner;
if (
$owner->Type == 'SiteTree' &&
isset($owner->SiteTreeID) &&
$owner->CurrentPage instanceof SiteTree &&
$currentPage = $owner->CurrentPage
){
$status = $currentPage === $owner->SiteTree() || $currentPage->ID === $owner->SiteTreeID;
}
}
/**
* @return bool
*/
public function updateIsSection(&$status)
{
$owner = $this->owner;
if (
$owner->Type == 'SiteTree' &&
isset($owner->SiteTreeID) &&
$owner->CurrentPage instanceof SiteTree &&
$currentPage = $owner->CurrentPage
){
$status = $owner->isCurrent() || in_array($owner->SiteTreeID, $currentPage->getAncestors()->column());
}
}
/**
* @return bool
*/
public function updateIsOrphaned(&$status)
{
$owner = $this->owner;
if (
$owner->Type == 'SiteTree' &&
isset($owner->SiteTreeID) &&
$owner->CurrentPage instanceof SiteTree &&
$currentPage = $owner->CurrentPage
){
// Always false for root pages
if (empty($owner->SiteTree()->ParentID)) {
$status = false;
return;
}
// Parent must exist and not be an orphan itself
$parent = $this->Parent();
$status = !$parent || !$parent->exists() || $parent->isOrphaned();
}
}
}