1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\Model; |
4
|
|
|
|
5
|
|
|
use Dynamic\Locator\Location; |
6
|
|
|
use Dynamic\Locator\Page\Locator; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
12
|
|
|
use SilverStripe\ORM\DataObject; |
13
|
|
|
use SilverStripe\ORM\ManyManyList; |
14
|
|
|
use SilverStripe\Security\Permission; |
15
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class LocationCategory |
19
|
|
|
* |
20
|
|
|
* @property string $Name |
21
|
|
|
* @method ManyManyList LocationSet() |
22
|
|
|
* @method ManyManyList Locators() |
23
|
|
|
*/ |
24
|
|
|
class LocationCategory extends DataObject |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private static $singular_name = 'Category'; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private static $plural_name = 'Categories'; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $db = [ |
|
|
|
|
40
|
|
|
'Name' => 'Varchar(100)', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private static $belongs_many_many = [ |
|
|
|
|
47
|
|
|
'Locators' => Locator::class, |
48
|
|
|
'LocationSet' => Location::class, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private static $table_name = 'LocationCategory'; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private static $default_sort = 'Name'; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \SilverStripe\Forms\FieldList |
63
|
|
|
*/ |
64
|
|
|
public function getCMSFields() |
65
|
|
|
{ |
66
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
67
|
|
|
$fields->removeByName([ |
68
|
|
|
'Locations', |
69
|
|
|
'LocationSet', |
70
|
|
|
'Locators', |
71
|
|
|
'LinkTracking', |
72
|
|
|
'FileTracking', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
if ($this->ID) { |
76
|
|
|
// Locations |
77
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
78
|
|
|
$config->removeComponentsByType([ |
79
|
|
|
GridFieldAddExistingAutocompleter::class, |
80
|
|
|
GridFieldAddNewButton::class, |
81
|
|
|
]) |
82
|
|
|
->addComponents([ |
83
|
|
|
new GridFieldAddExistingSearchButton(), |
84
|
|
|
]); |
85
|
|
|
$locations = $this->Locations(); |
86
|
|
|
$locationField = GridField::create('Locations', 'Locations', $locations, $config); |
87
|
|
|
|
88
|
|
|
$fields->addFieldsToTab('Root.Main', [ |
89
|
|
|
$locationField, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
$fields = parent::getCMSFields(); |
95
|
|
|
|
96
|
|
|
return $fields; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* For backwards compatability |
101
|
|
|
* @return ManyManyList |
102
|
|
|
*/ |
103
|
|
|
public function Locations() |
104
|
|
|
{ |
105
|
|
|
return $this->LocationSet(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param null $member |
|
|
|
|
110
|
|
|
* @param array $context |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param null $member |
|
|
|
|
120
|
|
|
* @param array $context |
121
|
|
|
* @return bool|int |
122
|
|
|
*/ |
123
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
return Permission::check('Location_EDIT', 'any', $member); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param null $member |
|
|
|
|
130
|
|
|
* @param array $context |
131
|
|
|
* @return bool|int |
132
|
|
|
*/ |
133
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return Permission::check('Location_DELETE', 'any', $member); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param null $member |
|
|
|
|
140
|
|
|
* @param array $context |
141
|
|
|
* @return bool|int |
142
|
|
|
*/ |
143
|
|
|
public function canCreate($member = null, $context = []) |
144
|
|
|
{ |
145
|
|
|
return Permission::check('Location_CREATE', 'any', $member); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|