Version: current

     1 <?php
     2 /**
     3  *
     4  *
     5  * @package cart_shipping
     6  * @author menedem
     7  */
     8 class RegionRestriction extends DataObject{
     9
    10     static $db = array(
    11         "Country" => "ShopCountry",
    12         "State" => "Varchar(255)",
    13         "City" => "Varchar(255)",
    14         "PostalCode" => "Varchar(10)"
    15     );
    16
    17     static $defaults = array(
    18         "Country" => "*",
    19         "State" => "*",
    20         "City" => "*",
    21         "PostalCode" => "*"
    22     );
    23
    24     static $default_sort = '"Country" ASC, "State" ASC, "City" ASC, "PostalCode" ASC';
    25
    26     static $summary_fields = array(
    27         'Country',
    28         'State',
    29         'City',
    30         'PostalCode'
    31     );
    32
    33
    34     /*
    35      * Specifies form field types to use in TableFields
    36      */
    37     static $table_field_types = array(
    38         'Country' => 'RestrictionRegionCountryDropdownField',
    39         'State' => 'TextField',
    40         'City' => 'TextField',
    41         'PostalCode' => 'TextField'
    42     );
    43
    44     /**
    45      * Produce a SQL filter to get matching RegionRestrictions to a given address
    46      * @param Address $address
    47      */
    48     static function address_filter(Address $address) {
    49         $restrictables = array(
    50             "Country",
    51             "State",
    52             "City",
    53             "PostalCode"
    54         );
    55         $where = array();
    56         $rr = '"RegionRestriction".';
    57         foreach($restrictables as $field){
    58             $where[] = "TRIM(LOWER($rr\"$field\")) = TRIM(LOWER('".Convert::raw2sql($address->$field)."')) OR $rr\"$field\" = '*' OR $rr\"$field\" = ''";
    59         }
    60         return "(".implode(") AND (", $where).")";
    61     }
    62
    63     static function get_table_field_types() {
    64         return self::$table_field_types;
    65     }
    66
    67     /**
    68      * Produces a sort check to make wildcards come last.
    69      * Useful because we are only interested in the wildcard,
    70      * and not sorting of other values.
    71      */
    72     static function wildcard_sort($field, $direction = "ASC"){
    73         return "CASE \"{$field}\" WHEN '*' THEN 1 ELSE 0 END $direction";
    74     }
    75
    76     function onBeforeWrite(){
    77         //prevent empty data - '*' must be used
    78         foreach(self::$defaults as $field => $value){
    79             if(empty($this->$field)){
    80                 $this->$field = $value;
    81             }
    82         }
    83         //TODO: prevent non-heirarichal entries, eg country = '*', then state = 'blah'
    84         parent::onBeforeWrite();
    85     }
    86
    87 }

[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. -