/
MenuItem.php
176 lines (154 loc) · 3.99 KB
/
MenuItem.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Class MenuItem
*/
class MenuItem extends DataObject implements PermissionProvider
{
/**
* @var array
*/
private static $db = array(
// If you want to customise the MenuTitle use this field - leaving blank will use MenuTitle of associated Page
'MenuTitle' => 'Varchar(255)',
// This field is used for external links (picking a page from the dropdown will overwrite this link)
'Link' => 'Text',
// Sort order
'Sort' => 'Int',
// Can be used as a check for adding target="_blank"
'IsNewWindow' => 'Boolean'
);
/**
* @var array
*/
private static $has_one = array(
'Page' => 'SiteTree', // page the MenuItem refers to
'MenuSet' => 'MenuSet' // parent MenuSet
);
/**
* @var array
*/
private static $searchable_fields = array(
'MenuTitle',
'Page.Title'
);
/**
* @var array
*/
private static $summary_fields = array(
'Menu Title' => 'MenuTitle',
'Page Title' => 'Page.Title',
'Link',
'IsNewWindow'
);
/**
* @var string
*/
private static $default_sort = 'Sort';
/**
* @return array
*/
public function providePermissions()
{
return array(
'MANAGE_MENU_ITEMS' => 'Manage Menu Items',
);
}
/**
* @param mixed $member
* @return boolean
*/
public function canCreate($member = null)
{
return Permission::check('MANAGE_MENU_ITEMS');
}
/**
* @param mixed $member
* @return boolean
*/
public function canDelete($member = null)
{
return Permission::check('MANAGE_MENU_ITEMS');
}
/**
* @param mixed $member
* @return boolean
*/
public function canEdit($member = null)
{
return Permission::check('MANAGE_MENU_ITEMS');
}
/**
* @param mixed $member
* @return boolean
*/
public function canView($member = null)
{
return Permission::check('MANAGE_MENU_ITEMS');
}
/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = new FieldList();
$fields->push(
new TextField('MenuTitle', 'Menu Title (will default to selected page title)')
);
$fields->push(
new TreeDropdownField(
'PageID',
'Page',
'Page'
)
);
$fields->push(new TextField('Link', 'Link (use when not specifying a page)'));
$fields->push(new CheckboxField('IsNewWindow', 'Open in a new window?'));
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* @return mixed
*/
public function Parent()
{
return $this->MenuSet();
}
/**
* Attempts to return the $field from this MenuItem
* If $field is not found or it is not set then attempts
* to return a similar field on the associated Page
* (if there is one)
*
* @param string $field
* @return mixed
*/
public function __get($field)
{
$default = parent::__get($field);
if ($default || $field === 'ID') {
return $default;
} else {
$page = $this->Page();
if ($page instanceof DataObject) {
if ($page->hasMethod($field)) {
return $page->$field();
} else {
return $page->$field;
}
}
}
}
/**
* Checks to see if a page has been chosen and if so sets Link to null
* This means that used in conjunction with the __get method above
* calling $menuItem->Link won't return the Link field of this MenuItem
* but rather call the Link method on the associated Page
*/
public function onBeforeWrite()
{
parent::onBeforeWrite();
if ($this->PageID != 0) {
$this->Link = null;
}
}
}