|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* A country can only be sent goods to from 1 distributor + the default distributor which can send anywhere in the world. |
|
5
|
|
|
* The default distributor shows prices of the default currency. |
|
6
|
|
|
* Precondition : There is always a default distributor. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Distributor extends DataObject implements PermissionProvider |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
private static $db = array( |
|
|
|
|
|
|
11
|
|
|
'Name' => 'Varchar(255)', |
|
12
|
|
|
'IsDefault' => 'Boolean', |
|
13
|
|
|
'Email' => 'Varchar(200)', |
|
14
|
|
|
'Address1' => "Varchar((255)", |
|
15
|
|
|
'Address2' => "Varchar(255)", |
|
16
|
|
|
'Address3' => "Varchar(255)", |
|
17
|
|
|
'Address4' => "Varchar(255)", |
|
18
|
|
|
'Address5' => "Varchar(255)", |
|
19
|
|
|
'Phone' => "Varchar(50)", |
|
20
|
|
|
'DisplayEmail' => "Varchar(50)", |
|
21
|
|
|
'WebAddress' => "Varchar(255)", |
|
22
|
|
|
'DeliveryCostNote' => 'Varchar(255)', |
|
23
|
|
|
'ShippingEstimation' => 'Varchar(255)', |
|
24
|
|
|
'ReturnInformation' => 'Varchar(255)' |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
28
|
|
|
'PrimaryCountry' => 'EcommerceCountry' |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
32
|
|
|
'Countries' => 'EcommerceCountry', |
|
33
|
|
|
'Members' => 'Member', |
|
34
|
|
|
'Updates' => 'CountryPrice_DistributorManagementTool_Log' |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
private static $field_labels = array( |
|
|
|
|
|
|
38
|
|
|
'IsDefault' => 'Is Default Distributor?' |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
private static $field_labels_right = array( |
|
|
|
|
|
|
42
|
|
|
'IsDefault' => 'Use this only for the distributor that is applicable when no other distributor applies (e.g. a country that does not have a distributor).', |
|
43
|
|
|
'Phone' => 'Please format as +64 8 555 5555' |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
private static $extensions = array("Versioned('Stage')"); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
49
|
|
|
"Name" => "Name", |
|
50
|
|
|
"IsDefault.Nice" => "Default" |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
private static $default_sort = array( |
|
|
|
|
|
|
54
|
|
|
"IsDefault" => "DESC", |
|
55
|
|
|
"Name" => "Asc" |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
private static $singular_name = "Distributor"; |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return the translated Singular name. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function i18n_singular_name() |
|
67
|
|
|
{ |
|
68
|
|
|
return _t('Distributor.SINGULAR_NAME', 'Distributor'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
private static $plural_name = "Distributors"; |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return the translated Singular name. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function i18n_plural_name() |
|
80
|
|
|
{ |
|
81
|
|
|
return _t('Distributor.PLURAL_NAME', 'Distributors'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* returns the Distributor for the country OR the default Distributor. |
|
87
|
|
|
* @param String $countryCode = the country code. |
|
88
|
|
|
* |
|
89
|
|
|
* @return Distributor |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function get_one_for_country($countryCode = '') |
|
92
|
|
|
{ |
|
93
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country($countryCode); |
|
94
|
|
|
if ($countryObject) { |
|
95
|
|
|
$distributor = $countryObject->Distributor(); |
|
96
|
|
|
if ($distributor && $distributor->exists()) { |
|
97
|
|
|
return $distributor; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
return self::get_default_distributor(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function get_default_distributor() |
|
104
|
|
|
{ |
|
105
|
|
|
return DataObject::get_one('Distributor', array("IsDefault" => 1)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getCMSFields() |
|
109
|
|
|
{ |
|
110
|
|
|
$fields = parent::getCMSFields(); |
|
111
|
|
|
$fieldLabels = $this->FieldLabels(); |
|
|
|
|
|
|
112
|
|
|
$fieldLabelsRight = $this->Config()->get("field_labels_right"); |
|
113
|
|
|
$listOfCountriesCovered = EcommerceCountry::get()->exclude(array("DistributorID" => 0))->map("ID", "Title"); |
|
114
|
|
|
//secondary for another country |
|
115
|
|
|
$fields->removeByName('Versions'); |
|
116
|
|
|
if ($listOfCountriesCovered && $listOfCountriesCovered->count()) { |
|
117
|
|
|
$countryArray = array(" -- please select --") + $listOfCountriesCovered->toArray(); |
|
118
|
|
|
$fields->addFieldToTab("Root.CountryDetails", DropdownField::create("PrimaryCountryID", "Primary Country", $countryArray)); |
|
119
|
|
|
} else { |
|
120
|
|
|
$fields->removeByName('PrimaryCountryID'); |
|
121
|
|
|
} |
|
122
|
|
|
if ($this->IsDefault) { |
|
|
|
|
|
|
123
|
|
|
$fields->removeByName('Countries'); |
|
124
|
|
|
} else { |
|
125
|
|
|
if (empty($this->ID)) { |
|
126
|
|
|
$id = 0; |
|
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
|
|
$id = $this->ID; |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
if ($this->ID) { |
|
131
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
|
132
|
|
|
$config->removeComponentsByType("GridFieldAddNewButton"); |
|
133
|
|
|
$gridField = new GridField('pages', 'All pages', SiteTree::get(), $config); |
|
|
|
|
|
|
134
|
|
|
$countryField = new GridField( |
|
135
|
|
|
'Countries', |
|
136
|
|
|
'Countries', |
|
137
|
|
|
$this->Countries(), |
|
|
|
|
|
|
138
|
|
|
$config |
|
139
|
|
|
); |
|
140
|
|
|
$fields->addFieldToTab("Root.CountryDetails", $countryField); |
|
141
|
|
|
if ($this->Version > 1) { |
|
|
|
|
|
|
142
|
|
|
$columns = array( |
|
143
|
|
|
'Version' => 'Version', |
|
144
|
|
|
'LastEdited' => 'Date', |
|
145
|
|
|
'Name' => 'Name', |
|
146
|
|
|
'IsDefault' => 'Default', |
|
147
|
|
|
'Email' => 'Email' |
|
148
|
|
|
); |
|
149
|
|
|
$table = '<table class="versions"><thead><tr><th>' . implode('</th><th>', $columns) . '</th></tr></thead><tbody>'; |
|
150
|
|
|
$version = $this->Version - 1; |
|
|
|
|
|
|
151
|
|
|
while ($version > 0) { |
|
152
|
|
|
$versionDO = Versioned::get_version('Distributor', $this->ID, $version--); |
|
153
|
|
|
$values = array(); |
|
154
|
|
|
foreach ($columns as $column => $title) { |
|
155
|
|
|
$values[] = $versionDO->$column; |
|
156
|
|
|
} |
|
157
|
|
|
$table .= '<tr><td>' . implode('</td><td>', $values) . '</td></tr>'; |
|
158
|
|
|
} |
|
159
|
|
|
$table .= '</tbody></table>'; |
|
160
|
|
|
$table .= "<style type=\"text/css\"> |
|
161
|
|
|
table.versions {border: 1px solid black; width:100%; border-collapse: collapse;} |
|
162
|
|
|
table.versions tr {border: 1px solid black;} |
|
163
|
|
|
table.versions tr th, table.versions tr td {border: 1px solid black; width: auto; text-align: center;} |
|
164
|
|
|
</style>"; |
|
165
|
|
|
$fields->addFieldToTab('Root.Versions', new LiteralField('VersionsTable', $table)); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
$fields->addFieldsToTab( |
|
170
|
|
|
"Root.ContactDetails", |
|
171
|
|
|
array( |
|
172
|
|
|
new TextField("DisplayEmail"), |
|
173
|
|
|
new TextField("Phone"), |
|
174
|
|
|
new TextField("Address1"), |
|
175
|
|
|
new TextField("Address2"), |
|
176
|
|
|
new TextField("Address3"), |
|
177
|
|
|
new TextField("Address4"), |
|
178
|
|
|
new TextField("Address5") |
|
179
|
|
|
) |
|
180
|
|
|
); |
|
181
|
|
|
$fields->addFieldsToTab( |
|
182
|
|
|
"Root.EcomInfo", |
|
183
|
|
|
array( |
|
184
|
|
|
new TextField("DeliveryCostNote"), |
|
185
|
|
|
new TextField("ShippingEstimation"), |
|
186
|
|
|
new TextField("ReturnInformation"), |
|
187
|
|
|
new HTMLEditorField("ProductNotAvailableNote") |
|
188
|
|
|
) |
|
189
|
|
|
); |
|
190
|
|
|
foreach ($fieldLabelsRight as $key => $value) { |
|
191
|
|
|
$field = $fields->dataFieldByName($key); |
|
192
|
|
|
if ($field) { |
|
193
|
|
|
$field->setRightTitle($value); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
$fields->removeFieldFromTab( |
|
197
|
|
|
'Root', |
|
198
|
|
|
'Countries' |
|
199
|
|
|
); |
|
200
|
|
|
return $fields; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* returns EcommerceCountries that this Distributor is responsible for. |
|
205
|
|
|
* return ArrayList |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getCountryList() |
|
208
|
|
|
{ |
|
209
|
|
|
$filter = $this->IsDefault ? array("ID" => 0) : array("DistributorID" => $this->ID); |
|
|
|
|
|
|
210
|
|
|
return EcommerceCountry::get() |
|
211
|
|
|
->filter($array); |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* link to edit the record |
|
216
|
|
|
* @param String | Null $action - e.g. edit |
|
217
|
|
|
* @return String |
|
218
|
|
|
*/ |
|
219
|
|
|
public function CMSEditLink($action = null) |
|
220
|
|
|
{ |
|
221
|
|
|
return Controller::join_links( |
|
222
|
|
|
Director::baseURL(), |
|
223
|
|
|
"/admin/shop/".$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/", |
|
224
|
|
|
$action |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* ensure there is one default Distributor. |
|
230
|
|
|
* |
|
231
|
|
|
*/ |
|
232
|
|
|
public function onBeforeWrite() |
|
233
|
|
|
{ |
|
234
|
|
|
parent::onBeforeWrite(); |
|
235
|
|
|
if (Distributor::get()->filter(array("IsDefault" => 1))->count() == 0) { |
|
236
|
|
|
$this->IsDefault = 1; |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
if ($this->PrimaryCountryID > 0 && EcommerceCountry::get()->byID(intval($this->PrimaryCountryID))) { |
|
|
|
|
|
|
239
|
|
|
$primaryCountry = $this->PrimaryCountry(); |
|
|
|
|
|
|
240
|
|
|
if (! $this->Countries()->byID($this->PrimaryCountryID)) { |
|
|
|
|
|
|
241
|
|
|
$this->Countries()->add($primaryCountry); |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
} else { |
|
244
|
|
|
if ($firstCountry = $this->Countries()->First()) { |
|
|
|
|
|
|
245
|
|
|
self::$_ran_after_write = true; |
|
246
|
|
|
$this->PrimaryCountryID = $firstCountry->ID; |
|
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
private static $_ran_after_write = false; |
|
252
|
|
|
/** |
|
253
|
|
|
* ensure there is one default Distributor. |
|
254
|
|
|
* |
|
255
|
|
|
*/ |
|
256
|
|
|
public function onAfterWrite() |
|
257
|
|
|
{ |
|
258
|
|
|
parent::onAfterWrite(); |
|
259
|
|
|
if (! self::$_ran_after_write) { |
|
260
|
|
|
self::$_ran_after_write = true; |
|
261
|
|
|
$this->setupUser(); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param Member $member |
|
|
|
|
|
|
267
|
|
|
* @return Boolean |
|
|
|
|
|
|
268
|
|
|
*/ |
|
269
|
|
|
public function canDelete($member = null) |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->IsDefault ? false : parent::canEdit($member); |
|
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function requireDefaultRecords() |
|
275
|
|
|
{ |
|
276
|
|
|
parent::RequireDefaultRecords(); |
|
277
|
|
|
$distributorTitleSingular = _t('Distributor.SINGULAR_NAME', 'Distributor'); |
|
278
|
|
|
$distributorTitlePlural = _t('Distributor.PLURAL_NAME', 'Distributors'); |
|
279
|
|
|
$filter = array("Title" => $distributorTitleSingular); |
|
280
|
|
|
$role = PermissionRole::get()->filter($filter)->first(); |
|
281
|
|
View Code Duplication |
if (!$role) { |
|
|
|
|
|
|
282
|
|
|
$role = PermissionRole::create($filter); |
|
283
|
|
|
$role->write(); |
|
284
|
|
|
DB::alteration_message("Creating ".$distributorTitleSingular." role", 'created'); |
|
285
|
|
|
} |
|
286
|
|
|
$codes = array( |
|
287
|
|
|
'CMS_ACCESS_SalesAdmin', |
|
288
|
|
|
'CMS_ACCESS_SalesAdminExtras', |
|
289
|
|
|
'CMS_ACCESS_SalesAdmin_PROCESS' |
|
290
|
|
|
); |
|
291
|
|
|
foreach ($codes as $code) { |
|
292
|
|
|
$filter = array( |
|
293
|
|
|
"RoleID" => $role->ID, |
|
294
|
|
|
"Code" => $code |
|
295
|
|
|
); |
|
296
|
|
|
$code = PermissionRoleCode::get()->filter($filter)->first(); |
|
297
|
|
View Code Duplication |
if (!$code) { |
|
|
|
|
|
|
298
|
|
|
DB::alteration_message("Adding code to ".$distributorTitleSingular." role", 'created'); |
|
299
|
|
|
$code = PermissionRoleCode::create($filter); |
|
300
|
|
|
$code->write(); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
$distributorGroup = self::get_distributor_group(); |
|
305
|
|
|
$distributorPermissionCode = Config::inst()->get('Distributor', 'distributor_permission_code'); |
|
306
|
|
|
if (!$distributorGroup) { |
|
307
|
|
|
$distributorGroup = new Group(); |
|
308
|
|
|
$distributorGroup->Code = $distributorPermissionCode; |
|
309
|
|
|
$distributorGroup->Title = $distributorTitlePlural; |
|
310
|
|
|
$distributorGroup->write(); |
|
311
|
|
|
DB::alteration_message($distributorTitlePlural.' Group created', "created"); |
|
312
|
|
|
Permission::grant($distributorGroup->ID, $distributorPermissionCode); |
|
313
|
|
|
} elseif (DB::query("SELECT * FROM \"Permission\" WHERE \"GroupID\" = '".$distributorGroup->ID."' AND \"Code\" LIKE '".$distributorPermissionCode."'")->numRecords() == 0) { |
|
314
|
|
|
Permission::grant($distributorGroup->ID, $distributorPermissionCode); |
|
315
|
|
|
DB::alteration_message($distributorTitlePlural.' group permissions granted', "created"); |
|
316
|
|
|
} |
|
317
|
|
|
$distributorGroup->Roles()->add($role); |
|
318
|
|
|
$distributorGroup = self::get_distributor_group(); |
|
319
|
|
|
if (!$distributorGroup) { |
|
320
|
|
|
user_error("could not create user group"); |
|
321
|
|
|
} else { |
|
322
|
|
|
DB::alteration_message('distributor group is ready for use'); |
|
323
|
|
|
} |
|
324
|
|
|
$distributors = Distributor::get(); |
|
325
|
|
|
if ($distributors && $distributors->count()) { |
|
326
|
|
|
foreach ($distributors as $distributor) { |
|
327
|
|
|
$distributor->setupUser(); |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @return DataObject (Group) |
|
|
|
|
|
|
335
|
|
|
**/ |
|
336
|
|
|
public static function get_distributor_group() |
|
337
|
|
|
{ |
|
338
|
|
|
$distributorPermissionCode = Config::inst()->get('Distributor', 'distributor_permission_code'); |
|
339
|
|
|
return Group::get() |
|
340
|
|
|
->where("\"Code\" = '".$distributorPermissionCode."'") |
|
341
|
|
|
->First(); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @var string |
|
347
|
|
|
*/ |
|
348
|
|
|
private static $distributor_permission_code = "distributors"; |
|
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* {@inheritdoc} |
|
352
|
|
|
*/ |
|
353
|
|
|
public function providePermissions() |
|
354
|
|
|
{ |
|
355
|
|
|
return array( |
|
356
|
|
|
$perms[Config::inst()->get('Distributor', 'distributor_permission_code')] = array( |
|
|
|
|
|
|
357
|
|
|
'name' => _t('Distributor.PLURAL_NAME', 'Distributors'), |
|
358
|
|
|
'category' => 'E-commerce', |
|
359
|
|
|
'help' => _t('Distributor.SINGULAR_NAME', 'Distributor').' access to relevant products and sales data.', |
|
360
|
|
|
'sort' => 98, |
|
361
|
|
|
) |
|
362
|
|
|
); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
public function setupUser() |
|
366
|
|
|
{ |
|
367
|
|
|
$group = Group::get()->filter(array("Code" => $this->Config()->get('distributor_permission_code')))->first(); |
|
368
|
|
|
if ($this->Email) { |
|
|
|
|
|
|
369
|
|
|
$filter = array("Email" => $this->Email); |
|
|
|
|
|
|
370
|
|
|
$member = Member::get() |
|
371
|
|
|
->filter($filter) |
|
372
|
|
|
->First(); |
|
373
|
|
|
if (!$member) { |
|
374
|
|
|
$member = Member::create($filter); |
|
375
|
|
|
//$thisMember->SetPassword = substr(session_id, 0, 8); |
|
|
|
|
|
|
376
|
|
|
} |
|
377
|
|
|
$member->FirstName = trim(_t('Distributor.SINGULAR_NAME', 'Distributor') . _t('Distributor.FOR', ' for ')); |
|
378
|
|
|
$member->Surname = $this->Name; |
|
|
|
|
|
|
379
|
|
|
$member->DistributorID = $this->ID; |
|
380
|
|
|
$member->write(); |
|
381
|
|
|
if ($group) { |
|
382
|
|
|
$member->addToGroupByCode($group->Code); |
|
383
|
|
|
} |
|
384
|
|
|
$member->write(); |
|
385
|
|
|
} |
|
386
|
|
|
if ($group) { |
|
387
|
|
|
foreach ($group->Members() as $oldGroupMember) { |
|
388
|
|
|
$distributor = $oldGroupMember->Distributor(); |
|
389
|
|
|
if ($distributor && $distributor->exists()) { |
|
|
|
|
|
|
390
|
|
|
} else { |
|
391
|
|
|
$group->Members()->remove($oldGroupMember); |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
foreach ($this->Members() as $member) { |
|
|
|
|
|
|
395
|
|
|
$member->addToGroupByCode($group->Code); |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.