1 <?php
2 3 4 5 6 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 36
37 static $table_field_types = array(
38 'Country' => 'RestrictionRegionCountryDropdownField',
39 'State' => 'TextField',
40 'City' => 'TextField',
41 'PostalCode' => 'TextField'
42 );
43
44 45 46 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 69 70 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
78 foreach(self::$defaults as $field => $value){
79 if(empty($this->$field)){
80 $this->$field = $value;
81 }
82 }
83
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.
-