-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddress.php
120 lines (104 loc) · 2.62 KB
/
Address.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
<?php namespace Octommerce\Shipping\Models;
use Model;
/**
* Address Model
*/
class Address extends Model
{
use \October\Rain\Database\Traits\Validation;
/**
* @var string The database table used by the model.
*/
public $table = 'octommerce_shipping_addresses';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = [
'user_id',
'location_code',
'address_name',
'name',
'phone',
'street',
'postcode',
'latitude',
'longitude'
];
/**
* Validation rules
*/
public $rules = [
'address_name' => ['required', 'min:3', 'regex:/^[a-z A-Z]+$/'],
'name' => ['min:3', 'regex:/^[a-z A-Z]+$/'],
'street' => 'required|min:20|string',
'phone' => ['regex:/^(?:\+?62[^0]|0[^0])[0-9]{9,10}$/'],
'location_code' => 'required',
];
/**
* @var array The array of custom error messages.
*/
public $customMessages = [
'location_code.required' => 'The subdistrict field is required.'
];
/**
* @var array Relations
*/
public $hasOne = [];
public $hasMany = [];
public $belongsTo = [
'location' => [
'Octommerce\Shipping\Models\Location',
'key' => 'location_code',
'otherKey' => 'code'
],
'user' => [
'RainLab\User\Models\User'
]
];
public $belongsToMany = [];
public $morphTo = [];
public $morphOne = [];
public $morphMany = [];
public $attachOne = [];
public $attachMany = [];
public function beforeCreate()
{
if ( ! \Auth::getUser()->addresses()->primary()->count()) {
$this->is_primary = true;
}
}
public function afterDelete()
{
//TODO: If it's primary, set the replacement
}
public function setNameAttribute($value)
{
$this->attributes['name'] = !empty($value) ? $value : \Auth::getUser()->name;
}
public function setPhoneAttribute($value)
{
$this->attributes['phone'] = !empty($value) ? $value : \Auth::getUser()->phone;
}
/**
* Set the address to primary
*/
public function setPrimary()
{
$this->user->addresses()->update(['is_primary' => false]);
$this->is_primary = true;
$this->save();
}
/**
* Add scope to filter primary address
*
* @param Builder $query
*/
public function scopePrimary($query)
{
$query->whereIsPrimary(true);
}
}