1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\GeoZones\Model; |
4
|
|
|
|
5
|
|
|
use SilverCommerce\GeoZones\Helpers\GeoZonesHelper; |
6
|
|
|
use ZoneMigrationTask; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\Forms\ListboxField; |
9
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A container of multiple regions (usually a country) |
13
|
|
|
* |
14
|
|
|
* @property string Name |
15
|
|
|
* @property string Country |
16
|
|
|
* @property string RegionCodes |
17
|
|
|
* @property bool AllRegions |
18
|
|
|
* @property bool Enabled |
19
|
|
|
* @property string CountriesList |
20
|
|
|
* |
21
|
|
|
* @method SiteConfig Site |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
class Zone extends DataObject |
25
|
|
|
{ |
26
|
|
|
private static $table_name = 'GeoZoneZone'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
private static $db = [ |
|
|
|
|
29
|
|
|
"Name" => "Varchar", |
30
|
|
|
"Country" => "Varchar", |
31
|
|
|
"RegionCodes" => "Text", |
32
|
|
|
"AllRegions" => "Boolean", |
33
|
|
|
"Enabled" => "Boolean" |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private static $has_one = [ |
|
|
|
|
37
|
|
|
"Site" => SiteConfig::class |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private static $many_many = [ |
|
|
|
|
41
|
|
|
"Regions" => Region::class // Remaining for legacy support |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
private static $casting = [ |
|
|
|
|
45
|
|
|
"CountriesList" => "Varchar" |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private static $field_labels = [ |
|
|
|
|
49
|
|
|
"RegionsCount" => "No. of regions" |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
private static $summary_fields = [ |
|
|
|
|
53
|
|
|
"Name", |
54
|
|
|
"CountriesList", |
55
|
|
|
"RegionsCount", |
56
|
|
|
"Enabled" |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
private static $searchable_fields = [ |
|
|
|
|
60
|
|
|
"Name", |
61
|
|
|
"Country", |
62
|
|
|
"RegionCodes", |
63
|
|
|
"Enabled" |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return an array of all associated countries |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function getCountriesArray(): array |
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
|
|
|
* Get an array of region codes saved against this object |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getRegionCodesArray(): array |
88
|
|
|
{ |
89
|
|
|
$return = json_decode($this->RegionCodes); |
90
|
|
|
|
91
|
|
|
if (empty($return) && isset($this->RegionCodes)) { |
92
|
|
|
$return = [$this->RegionCodes]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return a simple, comma seperated list of associated countries |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getCountriesList(): string |
104
|
|
|
{ |
105
|
|
|
return implode(",", $this->getCountriesArray()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get an array of regions for the current zone, or an empty |
110
|
|
|
* array if no countries selected |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getRegionsArray(): array |
115
|
|
|
{ |
116
|
|
|
$countries = $this->getCountriesArray(); |
117
|
|
|
$region_codes = $this->getRegionCodesArray(); |
118
|
|
|
$helper = GeoZonesHelper::create(); |
119
|
|
|
|
120
|
|
|
if (count($countries) > 0) { |
121
|
|
|
$helper->setCountriesList($countries); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (count($region_codes) > 0) { |
125
|
|
|
$helper->setLimitRegionCodes($region_codes); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $helper->getRegionArray(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getRegionsCount(): int |
132
|
|
|
{ |
133
|
|
|
return count($this->getRegionsArray()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function getCMSFields() |
140
|
|
|
{ |
141
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
142
|
|
|
$fields->removeByName("Regions"); |
143
|
|
|
|
144
|
|
|
$helper = GeoZonesHelper::create($this->getCountriesArray()); |
145
|
|
|
|
146
|
|
|
$fields->replaceField( |
147
|
|
|
"Country", |
148
|
|
|
ListboxField::create( |
149
|
|
|
'Country', |
150
|
|
|
$this->fieldLabel("Country"), |
151
|
|
|
$helper->getISOCountries() |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$fields->replaceField( |
156
|
|
|
"RegionCodes", |
157
|
|
|
ListboxField::create( |
158
|
|
|
'RegionCodes', |
159
|
|
|
$this->fieldLabel("RegionCodes"), |
160
|
|
|
$helper |
161
|
|
|
->getRegionsAsObjects() |
162
|
|
|
->map('RegionCode', 'Name') |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
}); |
166
|
|
|
|
167
|
|
|
return parent::getCMSFields(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function requireDefaultRecords() { |
174
|
|
|
parent::requireDefaultRecords(); |
175
|
|
|
|
176
|
|
|
if(ZoneMigrationTask::config()->run_during_dev_build) { |
177
|
|
|
$task = new ZoneMigrationTask(); |
178
|
|
|
$task->up(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function onBeforeWrite() |
186
|
|
|
{ |
187
|
|
|
parent::onBeforeWrite(); |
188
|
|
|
|
189
|
|
|
// If this applies to all regions in the country, |
190
|
|
|
// then add them all on save |
191
|
|
|
if ($this->AllRegions && isset($this->Country)) { |
192
|
|
|
$helper = GeoZonesHelper::create($this->getCountriesArray()); |
193
|
|
|
$codes = []; |
194
|
|
|
foreach ($helper->getRegionArray() as $region) { |
195
|
|
|
$codes[] = $region['region_code']; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (count($codes) > 0) { |
199
|
|
|
$this->RegionCodes = json_encode($codes); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|