|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\GeoZones\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Locale; |
|
6
|
|
|
use ZoneMigrationTask; |
|
7
|
|
|
use SilverStripe\i18n\i18n; |
|
8
|
|
|
use SilverStripe\ORM\DataObject; |
|
9
|
|
|
use SilverStripe\Forms\ListboxField; |
|
10
|
|
|
use SilverStripe\Forms\DropdownField; |
|
11
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A container of multiple regions |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
class Zone extends DataObject |
|
18
|
|
|
{ |
|
19
|
|
|
private static $table_name = 'GeoZoneZone'; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
private static $db = [ |
|
|
|
|
|
|
22
|
|
|
"Name" => "Varchar", |
|
23
|
|
|
"Country" => "Varchar", |
|
24
|
|
|
"AllRegions" => "Boolean", |
|
25
|
|
|
"Enabled" => "Boolean" |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
29
|
|
|
"Site" => SiteConfig::class |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
33
|
|
|
"Regions" => Region::class |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
private static $casting = [ |
|
|
|
|
|
|
37
|
|
|
"CountriesList" => "Varchar" |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
41
|
|
|
"Name", |
|
42
|
|
|
"CountriesList", |
|
43
|
|
|
"Regions.Count", |
|
44
|
|
|
"Enabled" |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
private static $searchable_fields = [ |
|
|
|
|
|
|
48
|
|
|
"Name", |
|
49
|
|
|
"Country", |
|
50
|
|
|
"Regions.Name", |
|
51
|
|
|
"Regions.Code", |
|
52
|
|
|
"Enabled" |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function populateDefaults() |
|
59
|
|
|
{ |
|
60
|
|
|
parent::populateDefaults(); |
|
61
|
|
|
|
|
62
|
|
|
$current_region = Locale::getRegion(i18n::get_locale()); |
|
63
|
|
|
$this->Country = i18n::get_locale($current_region); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Return an array of all associated countries |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCountriesArray() |
|
72
|
|
|
{ |
|
73
|
|
|
$return = json_decode($this->Country); |
|
74
|
|
|
|
|
75
|
|
|
if (empty($return) && isset($this->Country)) { |
|
76
|
|
|
$return = [$this->Country]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Return a simple, comma seperated list of associated countries |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getCountriesList() |
|
88
|
|
|
{ |
|
89
|
|
|
return implode(",", $this->getCountriesArray()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getCMSFields() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
98
|
|
|
$fields->replaceField( |
|
99
|
|
|
"Country", |
|
100
|
|
|
ListboxField::create( |
|
101
|
|
|
'Country', |
|
102
|
|
|
$this->fieldLabel("Country"), |
|
103
|
|
|
array_change_key_case( |
|
104
|
|
|
i18n::getData()->getCountries(), |
|
105
|
|
|
CASE_UPPER |
|
106
|
|
|
) |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
return parent::getCMSFields(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function requireDefaultRecords() { |
|
118
|
|
|
parent::requireDefaultRecords(); |
|
119
|
|
|
|
|
120
|
|
|
if(ZoneMigrationTask::config()->run_during_dev_build) { |
|
121
|
|
|
$task = new ZoneMigrationTask(); |
|
122
|
|
|
$task->up(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function onAfterWrite() |
|
130
|
|
|
{ |
|
131
|
|
|
parent::onAfterWrite(); |
|
132
|
|
|
|
|
133
|
|
|
// If this applies to all regions in the country, |
|
134
|
|
|
// then add them all on save |
|
135
|
|
|
if ($this->AllRegions && isset($this->Country)) { |
|
|
|
|
|
|
136
|
|
|
foreach ($this->getCountriesArray() as $country) { |
|
137
|
|
|
$regions = Region::get() |
|
138
|
|
|
->filter("CountryCode", $country); |
|
139
|
|
|
|
|
140
|
|
|
foreach ($regions as $region) { |
|
141
|
|
|
$this |
|
142
|
|
|
->Regions() |
|
|
|
|
|
|
143
|
|
|
->add($region); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |