|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Address model using a generic format for storing international addresses. |
|
5
|
|
|
* |
|
6
|
|
|
* Typical Address Hierarcy: |
|
7
|
|
|
* Continent |
|
8
|
|
|
* Country |
|
9
|
|
|
* State / Province / Territory (Island?) |
|
10
|
|
|
* District / Suburb / County / City |
|
11
|
|
|
* Code / Zip (may cross over the above) |
|
12
|
|
|
* Street / Road - name + type: eg Gandalf Cresent |
|
13
|
|
|
* (Premises/Building/Unit/Suite) |
|
14
|
|
|
* (Floor/Level/Side/Wing) |
|
15
|
|
|
* Number / Entrance / Room |
|
16
|
|
|
* Person(s), Company, Department |
|
17
|
|
|
* |
|
18
|
|
|
* Collection of international address formats: |
|
19
|
|
|
* |
|
20
|
|
|
* @see http://bitboost.com/ref/international-address-formats.html |
|
21
|
|
|
* xAL address standard: |
|
22
|
|
|
* @see https://www.oasis-open.org/committees/ciq/ciq.html#6 |
|
23
|
|
|
* Universal Postal Union addressing standards: |
|
24
|
|
|
* @see http://www.upu.int/nc/en/activities/addressing/standards.html |
|
25
|
|
|
*/ |
|
26
|
|
|
class Address extends DataObject |
|
27
|
|
|
{ |
|
28
|
|
|
private static $db = array( |
|
|
|
|
|
|
29
|
|
|
'Country' => 'ShopCountry', |
|
30
|
|
|
//level1: Country = ISO 2-character country code |
|
31
|
|
|
'State' => 'Varchar(100)', |
|
32
|
|
|
//level2: Locality, Administrative Area, State, Province, Region, Territory, Island |
|
33
|
|
|
'City' => 'Varchar(100)', |
|
34
|
|
|
//level3: Dependent Locality, City, Suburb, County, District |
|
35
|
|
|
'PostalCode' => 'Varchar(20)', |
|
36
|
|
|
//code: ZipCode, PostCode (could cross above levels within a country) |
|
37
|
|
|
|
|
38
|
|
|
'Address' => 'Varchar(255)', |
|
39
|
|
|
//Number + type of thoroughfare/street. P.O. box |
|
40
|
|
|
'AddressLine2' => 'Varchar(255)', |
|
41
|
|
|
//Premises, Apartment, Building. Suite, Unit, Floor, Level, Side, Wing. |
|
42
|
|
|
|
|
43
|
|
|
'Company' => 'Varchar(100)', |
|
44
|
|
|
//Business, Organisation, Group, Institution. |
|
45
|
|
|
|
|
46
|
|
|
'FirstName' => 'Varchar(100)', |
|
47
|
|
|
//Individual, Person, Contact, Attention |
|
48
|
|
|
'Surname' => 'Varchar(100)', |
|
49
|
|
|
'Phone' => 'Varchar(100)', |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
53
|
|
|
'Member' => 'Member', |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
private static $casting = array( |
|
|
|
|
|
|
57
|
|
|
'Country' => 'ShopCountry', |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
private static $required_fields = array( |
|
61
|
|
|
'Country', |
|
62
|
|
|
'State', |
|
63
|
|
|
'City', |
|
64
|
|
|
'Address', |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
68
|
|
|
'toString' => 'Address', |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
public function getCMSFields() |
|
72
|
|
|
{ |
|
73
|
|
|
$fields = parent::getCMSFields(); |
|
74
|
|
|
$fields->addFieldToTab( |
|
75
|
|
|
"Root.Main", |
|
76
|
|
|
$this->getCountryField(), |
|
77
|
|
|
'State' |
|
78
|
|
|
); |
|
79
|
|
|
$fields->removeByName("MemberID"); |
|
80
|
|
|
|
|
81
|
|
|
return $fields; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
public function getFrontEndFields($params = null) |
|
85
|
|
|
{ |
|
86
|
5 |
|
$fields = new FieldList( |
|
87
|
5 |
|
$this->getCountryField(), |
|
88
|
5 |
|
$addressfield = TextField::create('Address', _t('Address.db_Address', 'Address')), |
|
89
|
|
|
$address2field = |
|
90
|
5 |
|
TextField::create('AddressLine2', _t('Address.db_AddressLine2', 'Address Line 2 (optional)')), |
|
91
|
5 |
|
$cityfield = TextField::create('City', _t('Address.db_City', 'City')), |
|
92
|
5 |
|
$statefield = TextField::create('State', _t('Address.db_State', 'State')), |
|
93
|
5 |
|
$postcodefield = TextField::create('PostalCode', _t('Address.db_PostalCode', 'Postal Code')), |
|
94
|
5 |
|
$phonefield = TextField::create('Phone', _t('Address.db_Phone', 'Phone Number')) |
|
95
|
5 |
|
); |
|
96
|
5 |
|
if (isset($params['addfielddescriptions']) && !empty($params['addfielddescriptions'])) { |
|
97
|
2 |
|
$addressfield->setDescription( |
|
98
|
2 |
|
_t("Address.AddressHint", "street / thoroughfare number, name, and type or P.O. Box") |
|
99
|
2 |
|
); |
|
100
|
2 |
|
$address2field->setDescription( |
|
101
|
2 |
|
_t("Address.AddressLine2Hint", "premises, building, apartment, unit, floor") |
|
102
|
2 |
|
); |
|
103
|
2 |
|
$cityfield->setDescription(_t("Address.CityHint", "or suburb, county, district")); |
|
104
|
2 |
|
$statefield->setDescription(_t("Address.StateHint", "or province, territory, island")); |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
$this->extend('updateFormFields', $fields); |
|
108
|
5 |
|
return $fields; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
5 |
|
public function getCountryField() |
|
112
|
|
|
{ |
|
113
|
5 |
|
$countries = SiteConfig::current_site_config()->getCountriesList(); |
|
114
|
5 |
|
if (count($countries) == 1) { |
|
115
|
|
|
//field name is Country_readonly so it's value doesn't get updated |
|
116
|
3 |
|
return ReadonlyField::create( |
|
117
|
3 |
|
"Country_readonly", |
|
118
|
3 |
|
_t('Address.db_Country', 'Country'), |
|
119
|
3 |
|
array_pop($countries) |
|
120
|
3 |
|
); |
|
121
|
|
|
} |
|
122
|
2 |
|
$field = DropdownField::create( |
|
123
|
2 |
|
"Country", |
|
124
|
2 |
|
_t('Address.db_Country', 'Country'), |
|
125
|
|
|
$countries |
|
126
|
2 |
|
)->setHasEmptyDefault(true); |
|
127
|
|
|
|
|
128
|
2 |
|
$this->extend('updateCountryField', $field); |
|
129
|
|
|
|
|
130
|
2 |
|
return $field; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get an array of data fields that must be populated for model to be valid. |
|
135
|
|
|
* Required fields can be customised via self::$required_fields |
|
136
|
|
|
*/ |
|
137
|
16 |
|
public function getRequiredFields() |
|
138
|
|
|
{ |
|
139
|
16 |
|
$fields = self::config()->required_fields; |
|
140
|
|
|
//hack to allow overriding arrays in ss config |
|
141
|
16 |
|
if (self::$required_fields != $fields) { |
|
142
|
|
|
foreach (self::$required_fields as $requirement) { |
|
143
|
|
|
if (($key = array_search($requirement, $fields)) !== false) { |
|
144
|
|
|
unset($fields[$key]); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
//set nicer keys for easier processing |
|
149
|
16 |
|
$fields = array_combine($fields, $fields); |
|
150
|
16 |
|
$this->extend('updateRequiredFields', $fields); |
|
151
|
|
|
//don't require country if shop config only specifies a single country |
|
152
|
16 |
|
if (isset($fields['Country']) && SiteConfig::current_site_config()->getSingleCountry()) { |
|
153
|
3 |
|
unset($fields['Country']); |
|
154
|
3 |
|
} |
|
155
|
|
|
|
|
156
|
16 |
|
return $fields; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get full name associated with this Address |
|
161
|
|
|
*/ |
|
162
|
5 |
|
public function getName() |
|
163
|
|
|
{ |
|
164
|
5 |
|
return implode( |
|
165
|
5 |
|
' ', |
|
166
|
5 |
|
array_filter( |
|
167
|
|
|
array( |
|
168
|
5 |
|
$this->FirstName, |
|
|
|
|
|
|
169
|
5 |
|
$this->Surname, |
|
|
|
|
|
|
170
|
|
|
) |
|
171
|
5 |
|
) |
|
172
|
5 |
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Convert address to a single string. |
|
177
|
|
|
*/ |
|
178
|
3 |
|
public function toString($separator = ", ") |
|
179
|
|
|
{ |
|
180
|
|
|
$fields = array( |
|
181
|
3 |
|
$this->Address, |
|
|
|
|
|
|
182
|
3 |
|
$this->AddressLine2, |
|
|
|
|
|
|
183
|
3 |
|
$this->City, |
|
|
|
|
|
|
184
|
3 |
|
$this->State, |
|
|
|
|
|
|
185
|
3 |
|
$this->PostalCode, |
|
|
|
|
|
|
186
|
3 |
|
$this->Country, |
|
|
|
|
|
|
187
|
3 |
|
); |
|
188
|
3 |
|
$this->extend('updateToString', $fields); |
|
189
|
3 |
|
return implode($separator, array_filter($fields)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function getTitle() |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->toString(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
3 |
|
public function forTemplate() |
|
198
|
|
|
{ |
|
199
|
3 |
|
return $this->renderWith('Address'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Add alias setters for fields which are synonymous |
|
204
|
|
|
*/ |
|
205
|
17 |
|
public function setProvince($val) |
|
206
|
|
|
{ |
|
207
|
17 |
|
$this->State = $val; |
|
|
|
|
|
|
208
|
17 |
|
} |
|
209
|
|
|
|
|
210
|
17 |
|
public function setTerritory($val) |
|
211
|
|
|
{ |
|
212
|
17 |
|
$this->State = $val; |
|
|
|
|
|
|
213
|
17 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function setIsland($val) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->State = $val; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
23 |
|
public function setPostCode($val) |
|
221
|
|
|
{ |
|
222
|
23 |
|
$this->PostalCode = $val; |
|
|
|
|
|
|
223
|
23 |
|
} |
|
224
|
|
|
|
|
225
|
17 |
|
public function setZipCode($val) |
|
226
|
|
|
{ |
|
227
|
17 |
|
$this->PostalCode = $val; |
|
|
|
|
|
|
228
|
17 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function setStreet($val) |
|
231
|
|
|
{ |
|
232
|
|
|
$this->Address = $val; |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function setStreet2($val) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->AddressLine2 = $val; |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
23 |
|
public function setAddress2($val) |
|
241
|
|
|
{ |
|
242
|
23 |
|
$this->AddressLine2 = $val; |
|
|
|
|
|
|
243
|
23 |
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function setInstitution($val) |
|
246
|
|
|
{ |
|
247
|
|
|
$this->Company = $val; |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function setBusiness($val) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->Company = $val; |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function setOrganisation($val) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->Company = $val; |
|
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function setOrganization($val) |
|
261
|
|
|
{ |
|
262
|
|
|
$this->Company = $val; |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
15 |
|
function validate() |
|
|
|
|
|
|
266
|
|
|
{ |
|
267
|
15 |
|
$result = parent::validate(); |
|
268
|
|
|
|
|
269
|
15 |
|
foreach ($this->getRequiredFields() as $requirement) { |
|
270
|
15 |
|
if (empty($this->$requirement)) { |
|
271
|
1 |
|
$result->error("Address Model validate function - missing required field: $requirement"); |
|
272
|
1 |
|
} |
|
273
|
15 |
|
} |
|
274
|
|
|
|
|
275
|
15 |
|
return $result; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|