-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocation.php
135 lines (104 loc) · 3.26 KB
/
Location.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php namespace Octommerce\Shipping\Models;
use Model;
use Form;
use Octommerce\Shipping\Helpers\Location as LocationHelper;
/**
* Location Model
*/
class Location extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'octommerce_shipping_locations';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = [];
/**
* @var array Relations
*/
public $hasOne = [];
public $hasMany = [];
public $belongsTo = [];
public $belongsToMany = [];
public $morphTo = [];
public $morphOne = [];
public $morphMany = [];
public $attachOne = [];
public $attachMany = [];
/**
* @var array Cache for nameList() method
*/
protected static $nameList = [];
public static function getNameList($locationCode = 'base')
{
if (isset(self::$nameList[$locationCode])) {
return self::$nameList[$locationCode];
}
$arg = self::getArgument($locationCode);
return self::$nameList[$locationCode] = self::whereRaw("code REGEXP '$arg'")
->orderBy('name')
->lists('name', 'code');
}
public function getParentAttribute()
{
$codes = explode('.', $this->code);
if (count($codes) <= 1) return null;
array_pop($codes);
return self::whereCode(implode('.', $codes))->first();
}
public function children()
{
return self::where('code', 'like', $this->code . '%');
}
public function getChildrenAttribute()
{
return $this->children()->get();
}
public static function getArgument($locationCode)
{
if ($locationCode == 'base') return '^[0-9]+$';
return "^{$locationCode}.[0-9]+$";
}
public static function formSelectProvince($name, $selectedValue = null, $options = [])
{
return Form::select($name, self::getNameList(), $selectedValue, $options);
}
public static function formSelectCity($name, $province, $selectedValue = null, $options = [])
{
return Form::select($name, self::getNameList($province), $selectedValue, $options);
}
public static function formSelectDistrict($name, $city, $selectedValue = null, $options = [])
{
return Form::select($name, self::getNameList($city), $selectedValue, $options);
}
public static function formSelectSubdistrict($name, $district, $selectedValue = null, $options = [])
{
return Form::select($name, self::getNameList($district), $selectedValue, $options);
}
public function getProvinceAttribute()
{
$arg = LocationHelper::codeByLevel($this->code, 1);
return self::where('code', $arg)->first();
}
public function getCityAttribute()
{
$arg = LocationHelper::codeByLevel($this->code, 2);
return self::where('code', $arg)->first();
}
public function getDistrictAttribute()
{
$arg = LocationHelper::codeByLevel($this->code, 3);
return self::where('code', $arg)->first();
}
public function getSubdistrictAttribute()
{
$arg = LocationHelper::codeByLevel($this->code, 4);
return self::where('code', $arg)->first();
}
}