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\Security\PermissionProvider; |
||
11 | use SilverCommerce\VersionHistoryField\Forms\VersionHistoryField; |
||
12 | use SilverStripe\Security\Security; |
||
13 | |||
14 | /** |
||
15 | * Details on a particular contact |
||
16 | * |
||
17 | * @property string Title |
||
18 | * @property string Address |
||
19 | * @property string Address1 |
||
20 | * @property string Address2 |
||
21 | * @property string City |
||
22 | * @property string Country |
||
23 | * @property string County |
||
24 | * @property string PostCode |
||
25 | * @property bool Default |
||
26 | * |
||
27 | * @method Contact Contact |
||
28 | * |
||
29 | * @author ilateral |
||
30 | * @package Contacts |
||
31 | */ |
||
32 | class ContactLocation extends DataObject |
||
33 | { |
||
34 | private static $table_name = 'ContactLocation'; |
||
35 | |||
36 | private static $db = [ |
||
37 | "Address1" => "Varchar(255)", |
||
38 | "Address2" => "Varchar(255)", |
||
39 | "City" => "Varchar(255)", |
||
40 | "Country" => "Varchar(255)", |
||
41 | "County" => "Varchar(255)", |
||
42 | "PostCode" => "Varchar(10)", |
||
43 | "Default" => "Boolean" |
||
44 | ]; |
||
45 | |||
46 | private static $has_one = [ |
||
47 | "Contact" => Contact::class |
||
48 | ]; |
||
49 | |||
50 | private static $casting = [ |
||
51 | "Title" => "Varchar", |
||
52 | "Address" => "Text" |
||
53 | ]; |
||
54 | |||
55 | private static $frontend_fields = [ |
||
56 | "Address1", |
||
57 | "Address2", |
||
58 | "City", |
||
59 | "Country", |
||
60 | "County", |
||
61 | "PostCode", |
||
62 | "Default" |
||
63 | ]; |
||
64 | |||
65 | private static $summary_fields = [ |
||
66 | "Address1", |
||
67 | "Address2", |
||
68 | "City", |
||
69 | "County", |
||
70 | "Country", |
||
71 | "PostCode", |
||
72 | "Default" |
||
73 | ]; |
||
74 | |||
75 | private static $export_fields = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
76 | "Address1", |
||
77 | "Address2", |
||
78 | "City", |
||
79 | "County", |
||
80 | "Country", |
||
81 | "PostCode", |
||
82 | "Default" |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Add extension classes |
||
87 | * |
||
88 | * @var array |
||
89 | * @config |
||
90 | */ |
||
91 | private static $extensions = [ |
||
92 | Versioned::class . '.versioned', |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * Declare version history |
||
97 | * |
||
98 | * @var array |
||
99 | * @config |
||
100 | */ |
||
101 | private static $versioning = [ |
||
102 | "History" |
||
103 | ]; |
||
104 | |||
105 | /** |
||
106 | * Generate a title for this location |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getTitle() |
||
111 | { |
||
112 | $title = $this->Address1 . " (" . $this->PostCode . ")"; |
||
113 | |||
114 | $this->extend("updateTitle", $title); |
||
115 | |||
116 | return $title; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get a list of address fields as an array |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | protected function getAddressArray() |
||
125 | { |
||
126 | $array = [$this->Address1]; |
||
127 | |||
128 | if (!empty($this->Address2)) { |
||
129 | $array[] = $this->Address2; |
||
130 | } |
||
131 | |||
132 | $array[] = $this->City; |
||
133 | |||
134 | if (!empty($this->County)) { |
||
135 | $array[] = $this->County; |
||
136 | } |
||
137 | |||
138 | $array[] = $this->Country; |
||
139 | $array[] = $this->PostCode; |
||
140 | |||
141 | $this->extend("updateAddressArray", $array); |
||
142 | |||
143 | return $array; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Get the address from this location as a string |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getAddress() |
||
152 | { |
||
153 | $return = $this->getAddressArray(); |
||
154 | |||
155 | $this->extend("updateAddress", $return); |
||
156 | |||
157 | return implode(",\n", $return); |
||
158 | } |
||
159 | |||
160 | public function getCMSFields() |
||
161 | { |
||
162 | $self = $this; |
||
163 | $this->beforeUpdateCMSFields( |
||
164 | function ($fields) use ($self) { |
||
165 | if ($self->exists()) { |
||
166 | $fields->addFieldToTab( |
||
167 | "Root.History", |
||
168 | VersionHistoryField::create( |
||
169 | "History", |
||
170 | _t("SilverCommerce\VersionHistoryField.History", "History"), |
||
171 | $this |
||
172 | )->addExtraClass("stacked") |
||
173 | ); |
||
174 | } |
||
175 | } |
||
176 | ); |
||
177 | |||
178 | return parent::getCMSFields(); |
||
179 | } |
||
180 | |||
181 | public function getCMSValidator() |
||
182 | { |
||
183 | $validator = new RequiredFields( |
||
184 | [ |
||
185 | "Address1", |
||
186 | "City", |
||
187 | "Country", |
||
188 | "PostCode" |
||
189 | ] |
||
190 | ); |
||
191 | |||
192 | $this->extend('updateCMSValidator', $validator); |
||
193 | |||
194 | return $validator; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Get the default export fields for this object |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getExportFields() |
||
203 | { |
||
204 | $raw_fields = $this->config()->get('export_fields'); |
||
205 | |||
206 | // Merge associative / numeric keys |
||
207 | $fields = []; |
||
208 | foreach ($raw_fields as $key => $value) { |
||
209 | if (is_int($key)) { |
||
210 | $key = $value; |
||
211 | } |
||
212 | $fields[$key] = $value; |
||
213 | } |
||
214 | |||
215 | $this->extend("updateExportFields", $fields); |
||
216 | |||
217 | // Final fail-over, just list ID field |
||
218 | if (!$fields) { |
||
219 | $fields['ID'] = 'ID'; |
||
220 | } |
||
221 | |||
222 | return $fields; |
||
223 | } |
||
224 | |||
225 | public function canView($member = null) |
||
226 | { |
||
227 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
228 | |||
229 | if ($extended !== null) { |
||
230 | return $extended; |
||
231 | } |
||
232 | |||
233 | return $this->Contact()->canView($member); |
||
234 | } |
||
235 | |||
236 | public function canCreate($member = null, $context = []) |
||
237 | { |
||
238 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
239 | |||
240 | if ($extended !== null) { |
||
241 | return $extended; |
||
242 | } |
||
243 | |||
244 | if (!$member) { |
||
245 | $member = Security::getCurrentUser(); |
||
246 | } |
||
247 | |||
248 | if ($member && Permission::checkMember($member->ID, "CONTACTS_MANAGE")) { |
||
249 | return true; |
||
250 | } |
||
251 | |||
252 | return false; |
||
253 | } |
||
254 | |||
255 | public function canEdit($member = null) |
||
256 | { |
||
257 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
258 | |||
259 | if ($extended !== null) { |
||
260 | return $extended; |
||
261 | } |
||
262 | |||
263 | return $this->Contact()->canEdit($member); |
||
264 | } |
||
265 | |||
266 | public function canDelete($member = null, $context = []) |
||
267 | { |
||
268 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
269 | |||
270 | if ($extended !== null) { |
||
271 | return $extended; |
||
272 | } |
||
273 | |||
274 | return $this->Contact()->canDelete($member); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * If we have assigned this as a default location, loop through |
||
279 | * other locations and disable default. |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | public function onAfterWrite() |
||
284 | { |
||
285 | parent::onAfterWrite(); |
||
286 | |||
287 | if ($this->Default) { |
||
288 | foreach ($this->Contact()->Locations() as $location) { |
||
289 | if ($location->ID != $this->ID && $location->Default) { |
||
290 | $location->Default = false; |
||
291 | $location->write(); |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 |