1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\ContactAdmin\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Security\Member; |
7
|
|
|
use SilverStripe\Security\Permission; |
8
|
|
|
use SilverStripe\Versioned\Versioned; |
9
|
|
|
use SilverStripe\Forms\RequiredFields; |
10
|
|
|
use SilverStripe\Forms\GridField\GridField; |
11
|
|
|
use SilverStripe\Security\PermissionProvider; |
12
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText as HTMLText; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
14
|
|
|
use SilverCommerce\VersionHistoryField\Forms\VersionHistoryField; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Details on a particular contact |
18
|
|
|
* |
19
|
|
|
* @author ilateral |
20
|
|
|
* @package Contacts |
21
|
|
|
*/ |
22
|
|
|
class ContactLocation extends DataObject implements PermissionProvider |
23
|
|
|
{ |
24
|
|
|
private static $table_name = 'ContactLocation'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
private static $db = [ |
|
|
|
|
27
|
|
|
"Address1" => "Varchar(255)", |
28
|
|
|
"Address2" => "Varchar(255)", |
29
|
|
|
"City" => "Varchar(255)", |
30
|
|
|
"County" => "Varchar(255)", |
31
|
|
|
"Country" => "Varchar(255)", |
32
|
|
|
"PostCode" => "Varchar(10)", |
33
|
|
|
"Default" => "Boolean" |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private static $has_one = [ |
|
|
|
|
37
|
|
|
"Contact" => Contact::class |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private static $casting = [ |
|
|
|
|
41
|
|
|
"Title" => "Varchar", |
42
|
|
|
"Address" => "Text" |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
private static $frontend_fields = [ |
|
|
|
|
46
|
|
|
"Address1", |
47
|
|
|
"Address2", |
48
|
|
|
"City", |
49
|
|
|
"County", |
50
|
|
|
"Country", |
51
|
|
|
"PostCode", |
52
|
|
|
"Default" |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
private static $summary_fields = [ |
|
|
|
|
56
|
|
|
"Address1", |
57
|
|
|
"Address2", |
58
|
|
|
"City", |
59
|
|
|
"County", |
60
|
|
|
"Country", |
61
|
|
|
"PostCode", |
62
|
|
|
"Default" |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Add extension classes |
67
|
|
|
* |
68
|
|
|
* @var array |
69
|
|
|
* @config |
70
|
|
|
*/ |
71
|
|
|
private static $extensions = [ |
|
|
|
|
72
|
|
|
Versioned::class . '.versioned', |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Declare version history |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
* @config |
80
|
|
|
*/ |
81
|
|
|
private static $versioning = [ |
|
|
|
|
82
|
|
|
"History" |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
public function getTitle() |
86
|
|
|
{ |
87
|
|
|
$title = $this->Address1 . " (" . $this->PostCode . ")"; |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->extend("updateTitle", $title); |
90
|
|
|
|
91
|
|
|
return $title; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getAddress() |
95
|
|
|
{ |
96
|
|
|
$return = []; |
97
|
|
|
$return[] = $this->Address1; |
|
|
|
|
98
|
|
|
|
99
|
|
|
if (!empty($this->Address2)) { |
|
|
|
|
100
|
|
|
$return[] = $this->Address2; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$return[] = $this->City; |
|
|
|
|
104
|
|
|
|
105
|
|
|
if (!empty($this->County)) { |
|
|
|
|
106
|
|
|
$return[] = $this->County; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$return[] = $this->Country; |
|
|
|
|
110
|
|
|
$return[] = $this->PostCode; |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->extend("updateAddress", $return); |
113
|
|
|
|
114
|
|
|
return implode(",\n", $return); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getCMSFields() |
118
|
|
|
{ |
119
|
|
|
$self = $this; |
120
|
|
|
$this->beforeUpdateCMSFields(function ($fields) use ($self) { |
121
|
|
|
if ($self->exists()) { |
122
|
|
|
$fields->addFieldToTab( |
123
|
|
|
"Root.History", |
124
|
|
|
VersionHistoryField::create( |
125
|
|
|
"History", |
126
|
|
|
_t("SilverCommerce\VersionHistoryField.History", "History"), |
127
|
|
|
$this |
128
|
|
|
)->addExtraClass("stacked") |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
}); |
132
|
|
|
|
133
|
|
|
return parent::getCMSFields(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getCMSValidator() |
137
|
|
|
{ |
138
|
|
|
return new RequiredFields(array( |
139
|
|
|
"Address1", |
140
|
|
|
"City", |
141
|
|
|
"Country", |
142
|
|
|
"PostCode" |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function providePermissions() |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
"CONTACTS_MANAGE" => [ |
150
|
|
|
'name' => _t( |
151
|
|
|
'Contacts.PERMISSION_MANAGE_CONTACTS_DESCRIPTION', |
152
|
|
|
'Manage contacts' |
153
|
|
|
), |
154
|
|
|
'help' => _t( |
155
|
|
|
'Contacts.PERMISSION_MANAGE_CONTACTS_HELP', |
156
|
|
|
'Allow creation and editing of contacts' |
157
|
|
|
), |
158
|
|
|
'category' => _t('Contacts.Contacts', 'Contacts') |
159
|
|
|
], |
160
|
|
|
"CONTACTS_DELETE" => [ |
161
|
|
|
'name' => _t( |
162
|
|
|
'Contacts.PERMISSION_DELETE_CONTACTS_DESCRIPTION', |
163
|
|
|
'Delete contacts' |
164
|
|
|
), |
165
|
|
|
'help' => _t( |
166
|
|
|
'Contacts.PERMISSION_DELETE_CONTACTS_HELP', |
167
|
|
|
'Allow deleting of contacts' |
168
|
|
|
), |
169
|
|
|
'category' => _t('Contacts.Contacts', 'Contacts') |
170
|
|
|
] |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function canView($member = null) |
175
|
|
|
{ |
176
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
177
|
|
|
|
178
|
|
|
if ($extended !== null) { |
179
|
|
|
return $extended; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->Contact()->canView($member); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function canCreate($member = null, $context = []) |
186
|
|
|
{ |
187
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member, $context); |
188
|
|
|
|
189
|
|
|
if ($extended !== null) { |
190
|
|
|
return $extended; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (!$member) { |
194
|
|
|
$member = Member::currentUser(); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($member && Permission::checkMember($member->ID, "CONTACTS_MANAGE")) { |
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function canEdit($member = null) |
205
|
|
|
{ |
206
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
207
|
|
|
|
208
|
|
|
if ($extended !== null) { |
209
|
|
|
return $extended; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $this->Contact()->canEdit($member); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
218
|
|
|
|
219
|
|
|
if ($extended !== null) { |
220
|
|
|
return $extended; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this->Contact()->canDelete($member); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* If we have assigned this as a default location, loop through |
228
|
|
|
* other locations and disable default. |
229
|
|
|
* |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
public function onAfterWrite() |
233
|
|
|
{ |
234
|
|
|
parent::onAfterWrite(); |
235
|
|
|
|
236
|
|
|
if ($this->Default) { |
237
|
|
|
foreach ($this->Contact()->Locations() as $location) { |
238
|
|
|
if ($location->ID != $this->ID && $location->Default) { |
239
|
|
|
$location->Default = false; |
240
|
|
|
$location->write(); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|