1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 class Address extends DataObject{
25
26 static $db = array(
27 'Country' => 'ShopCountry',
28 'PostalCode' => 'Varchar(20)',
29 'State' => 'Varchar(100)',
30 'City' => 'Varchar(100)',
31 'Address' => 'Varchar(255)',
32
33 );
34
35 static $has_one = array(
36 'Member' => 'Member'
37 );
38
39 static $required_fields = array(
40 'Address',
41 'City',
42 'State',
43 'Country'
44 );
45
46 47 48 49
50 static $show_form_hints = false;
51
52 function getCMSFields() {
53 $fields = parent::getCMSFields();
54 $fields->replaceField('Country', new RestrictionRegionCountryDropdownField('Country', $this->fieldLabel('Country'), null, $this->Country));
55 return $fields;
56 }
57
58 59 60 61
62 function getFormFields($nameprefix = ""){
63 $countries = SiteConfig::current_site_config()->getCountriesList();
64
65 if (count($countries) == 0) {
66 $countryfield = new ReadonlyField($nameprefix."Country", $this->fieldLabel('Country'));
67 }
68 else if (count($countries) == 1) {
69 $countryfield = new HiddenField($nameprefix."Country", $this->fieldLabel('Country'), array_shift(array_keys($countries)));
70 }
71 else {
72 $countryfield = new DropdownField($nameprefix."Country", $this->fieldLabel('Country'), $countries, 'RU');
73 $countryfield->setHasEmptyDefault(true);
74 $countryfield->setAutocomplete('country-name');
75 }
76
77 $fields = new FieldSet(
78 $countryfield,
79 $postalcodefield = new TextField($nameprefix.'PostalCode', $this->fieldLabel('PostalCode')),
80 $statefield = new TextField($nameprefix.'State', $this->fieldLabel('State')),
81 $cityfield = new TextField($nameprefix.'City', $this->fieldLabel('City')),
82 $addressfield = new TextField($nameprefix.'Address', $this->fieldLabel('Address'))
83 );
84 $postalcodefield->setAutocomplete('postal-code');
85 $statefield->setAutocomplete('region');
86 $cityfield->setAutocomplete('city');
87 $addressfield->setAutocomplete('street-address');
88 if(self::$show_form_hints){
89 $addressfield->setRightTitle(_t("Address.ADDRESSHINT","street / thoroughfare number, name, and type or P.O. Box"));
90 $cityfield->setRightTitle(_t("Address.CITYHINT","or suburb, county, district"));
91 $statefield->setRightTitle(_t("Address.STATEHINT","or province, territory, island"));
92 }
93 $this->extend('updateFormFields',$fields, $nameprefix);
94 return $fields;
95 }
96
97 98 99 100
101 function getRequiredFields($nameprefix = ""){
102 $fields = array();
103 foreach(self::$required_fields as $field){
104 $fields[] = $nameprefix.$field;
105 }
106 $this->extend('updateRequiredFields',$fields, $nameprefix);
107 return $fields;
108 }
109
110 111 112
113 function getFieldMap($prefix = ''){
114 $map = $this->getFormFields()->saveableFields();
115 foreach($map as $key => $value){
116 $map[$prefix.$key] = $key;
117 unset($map[$key]);
118 }
119 return $map;
120 }
121
122 123 124
125 function FullAddress($separator = ', '){
126 $fields = array(
127 'Country' => DBField::Create('ShopCountry', $this->Country)->Nice(),
128 'PostalCode' => $this->PostalCode,
129 'State' => $this->State,
130 'City' => $this->City,
131 'Address' => $this->Address,
132 );
133 $this->extend('updateFullAddress', $fields);
134 return implode($separator, array_filter($fields));
135 }
136
137 function getTitle() {
138 return $this->FullAddress();
139 }
140
141 function forTemplate(){
142 return $this->renderWith('Address');
143 }
144
145 146 147
148 function setProvince($val){$this->State = $val;}
149 function setTerritory($val){$this->State = $val;}
150 function setIsland($val){$this->State = $val;}
151 function setStreet($val){$this->Address = $val;}
152
153 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-