Version: current

      1 <?php
      2 /**
      3  * Address model using a generic format for storing international addresses.
      4  *
      5  * Typical Address Hierarcy:
      6  *  Continent
      7  *  Country
      8  *  State / Province / Territory (Island?)
      9  *  District / Suburb / County / City
     10  *      Code / Zip (may cross over the above)
     11  *  Street / Road - name + type: eg Gandalf Cresent
     12  *  (Premises/Building/Unit/Suite)
     13  *      (Floor/Level/Side/Wing)
     14  *  Number / Entrance / Room
     15  *  Person(s), Company, Department
     16  *
     17  * Collection of international address formats:
     18  * @see http://bitboost.com/ref/international-address-formats.html
     19  * xAL address standard:
     20  * @see https://www.oasis-open.org/committees/ciq/ciq.html#6
     21  * Universal Postal Union addressing standards:
     22  * @see http://www.upu.int/nc/en/activities/addressing/standards.html
     23  */
     24 class Address extends DataObject{
     25
     26     static $db = array(
     27         'Country' => 'ShopCountry',  //level1: Country = ISO 2-character country code
     28         'PostalCode' => 'Varchar(20)',
     29         'State' => 'Varchar(100)', //level2: Locality, Administrative Area, State, Province, Territory, Island
     30         'City' => 'Varchar(100)', //level3: Dependent Locality, City, Suburb, County, District
     31         'Address' => 'Varchar(255)', //Number + type of thoroughfare/street. P.O. box
     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      * Tub-titles for address fields that describe what they are for
     48      * @var boolean
     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      * @todo: customise format and labels, based on passed locale
     60      * @param unknown_type $nameprefix
     61      */
     62     function getFormFields($nameprefix = ""){
     63         $countries = SiteConfig::current_site_config()->getCountriesList();
     64         //$countryfield = (count($countries)) ? new DropdownField($nameprefix."Country", _t('Address.COUNTRY','Country'),$countries) : new ReadonlyField($nameprefix."Country",_t('Address.COUNTRY','Country'));
     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      * Get an array of fields that must be populated in a form.
     99      * Required fields can be customised via self::$required_fields
    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      * Produces a map for loading/saving form fields.
    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      * Convert address to a single string.
    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      * Add alias setters for fields which are synonymous
    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. -