1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Models; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Config; |
6
|
|
|
use Model; |
7
|
|
|
use October\Rain\Database\Traits\SoftDelete as SoftDeleteTrait; |
8
|
|
|
use October\Rain\Database\Traits\Validation as ValidationTrait; |
9
|
|
|
use Request; |
10
|
|
|
use Str; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Reservation class. |
14
|
|
|
* |
15
|
|
|
* @package VojtaSvoboda\Reservations\Models |
16
|
|
|
*/ |
17
|
|
|
class Reservation extends Model |
18
|
|
|
{ |
19
|
|
|
use SoftDeleteTrait; |
20
|
|
|
|
21
|
|
|
use ValidationTrait; |
22
|
|
|
|
23
|
|
|
/** @var string $table The database table used by the model */ |
24
|
|
|
public $table = 'vojtasvoboda_reservations_reservations'; |
25
|
|
|
|
26
|
|
|
/** @var array Rules */ |
27
|
|
|
public $rules = [ |
28
|
|
|
'date' => 'required|date', |
29
|
|
|
'locale' => 'max:20', |
30
|
|
|
'email' => 'required|email', |
31
|
|
|
'name' => 'required|max:300', |
32
|
|
|
'street' => 'required|max:300', |
33
|
|
|
'town' => 'max:300', |
34
|
|
|
'zip' => 'numeric', |
35
|
|
|
'phone' => 'required|max:300', |
36
|
|
|
'message' => 'required|max:3000', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public $fillable = [ |
40
|
|
|
'status', 'date', 'locale', 'email', 'name', 'lastname', |
41
|
|
|
'street', 'town', 'zip', 'phone', 'message', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
public $dates = ['created_at', 'updated_at', 'deleted_at']; |
45
|
|
|
|
46
|
|
|
public $belongsTo = [ |
47
|
|
|
'status' => 'VojtaSvoboda\Reservations\Models\Status', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Before create reservation. |
52
|
|
|
*/ |
53
|
8 |
|
public function beforeCreate() |
54
|
|
|
{ |
55
|
8 |
|
$this->hash = $this->getUniqueHash(); |
56
|
8 |
|
$this->number = $this->getUniqueNumber(); |
57
|
|
|
|
58
|
8 |
|
$this->locale = App::getLocale(); |
59
|
|
|
|
60
|
8 |
|
$this->ip = Request::server('REMOTE_ADDR'); |
61
|
8 |
|
$this->ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR'); |
62
|
8 |
|
$this->user_agent = Request::server('HTTP_USER_AGENT'); |
63
|
|
|
|
64
|
8 |
|
if ($this->status === null) { |
65
|
6 |
|
$this->status = $this->getDefaultStatus(); |
66
|
|
|
} |
67
|
8 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Scope for getting non cancelled reservations. |
71
|
|
|
* |
72
|
|
|
* @param $query |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
7 |
|
public function scopeNotCancelled($query) |
77
|
|
|
{ |
78
|
7 |
|
$cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']); |
79
|
|
|
|
80
|
7 |
|
return $query->whereHas('status', function($query) use ($cancelledStatuses) { |
81
|
7 |
|
$query->whereNotIn('ident', $cancelledStatuses); |
82
|
7 |
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set machine scope |
87
|
|
|
* |
88
|
|
|
* @param $query |
89
|
|
|
* |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
7 |
|
public function scopeMachine($query) |
93
|
|
|
{ |
94
|
7 |
|
$ip = Request::server('REMOTE_ADDR'); |
95
|
7 |
|
$ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR'); |
96
|
7 |
|
$user_agent = Request::server('HTTP_USER_AGENT'); |
97
|
|
|
|
98
|
7 |
|
return $query->whereIp($ip)->whereIpForwarded($ip_forwarded)->whereUserAgent($user_agent); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get default reservation status. |
103
|
|
|
* |
104
|
|
|
* @return Status |
105
|
|
|
*/ |
106
|
7 |
|
public function getDefaultStatus() |
107
|
|
|
{ |
108
|
|
|
$statusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received'); |
109
|
7 |
|
|
110
|
7 |
|
return Status::where('ident', $statusIdent)->first(); |
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
/** |
114
|
|
|
* Generate unique hash for each reservation. |
115
|
7 |
|
* |
116
|
|
|
* @return string|null |
117
|
|
|
*/ |
118
|
|
|
public function getUniqueHash() |
119
|
|
|
{ |
120
|
|
|
$length = Config::get('vojtasvoboda.reservations::config.hash', 32); |
121
|
|
|
if ($length == 0) { |
122
|
|
|
return null; |
123
|
6 |
|
} |
124
|
|
|
|
125
|
6 |
|
return substr(md5('reservations-' . Str::random($length)), 0, $length); |
126
|
|
|
} |
127
|
6 |
|
|
128
|
|
|
/** |
129
|
|
|
* Generate unique number for each reservation. With this hash you can reference |
130
|
|
|
* concrete reservation instead of using internal Reservation ID. |
131
|
|
|
* |
132
|
|
|
* @return string|null |
133
|
|
|
*/ |
134
|
|
|
public function getUniqueNumber() |
135
|
9 |
|
{ |
136
|
|
|
// init |
137
|
9 |
|
$min = Config::get('vojtasvoboda.reservations::config.number.min', 123456); |
138
|
9 |
|
$max = Config::get('vojtasvoboda.reservations::config.number.max', 999999); |
139
|
|
|
if ($min == 0 || $max == 0) { |
140
|
|
|
return null; |
141
|
|
|
} |
142
|
9 |
|
|
143
|
|
|
// generate random number |
144
|
|
|
$count = 0; |
145
|
|
|
do { |
146
|
|
|
$number = mt_rand($min, $max); |
147
|
|
|
|
148
|
|
|
} while ((self::where('number', $number)->count() > 0) && (++$count < 1000)); |
149
|
|
|
|
150
|
|
|
return $count >= 1000 ? null : $number; |
151
|
9 |
|
} |
152
|
|
|
} |
153
|
|
|
|