1 | <?php namespace VojtaSvoboda\Reservations\Models; |
||
19 | class Reservation extends Model |
||
20 | { |
||
21 | use SoftDeleteTrait; |
||
22 | |||
23 | use ValidationTrait; |
||
24 | |||
25 | /** @var string $table The database table used by the model */ |
||
26 | public $table = 'vojtasvoboda_reservations_reservations'; |
||
27 | |||
28 | /** @var array Rules */ |
||
29 | public $rules = [ |
||
30 | 'date' => 'required|date', |
||
31 | 'locale' => 'max:20', |
||
32 | 'email' => 'required|email', |
||
33 | 'name' => 'required|max:300', |
||
34 | 'street' => 'required|max:300', |
||
35 | 'town' => 'max:300', |
||
36 | 'zip' => 'numeric', |
||
37 | 'phone' => 'required|max:300', |
||
38 | 'message' => 'required|max:3000', |
||
39 | ]; |
||
40 | |||
41 | public $fillable = [ |
||
42 | 'status', 'date', 'locale', 'email', 'name', 'lastname', |
||
43 | 'street', 'town', 'zip', 'phone', 'message', |
||
44 | ]; |
||
45 | |||
46 | public $dates = ['date', 'created_at', 'updated_at', 'deleted_at']; |
||
47 | |||
48 | public $belongsTo = [ |
||
49 | 'status' => 'VojtaSvoboda\Reservations\Models\Status', |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | 8 | * Before create reservation. |
|
54 | */ |
||
55 | 8 | public function beforeCreate() |
|
56 | 8 | { |
|
57 | $this->hash = $this->getUniqueHash(); |
||
58 | 8 | $this->number = $this->getUniqueNumber(); |
|
59 | |||
60 | 8 | $this->locale = App::getLocale(); |
|
61 | 8 | ||
62 | 8 | $this->ip = Request::server('REMOTE_ADDR'); |
|
63 | $this->ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR'); |
||
64 | 8 | $this->user_agent = Request::server('HTTP_USER_AGENT'); |
|
65 | 6 | ||
66 | if ($this->status === null) { |
||
67 | 8 | $this->status = $this->getDefaultStatus(); |
|
68 | } |
||
69 | } |
||
70 | |||
71 | public function beforeSave() |
||
75 | |||
76 | 7 | /** |
|
77 | * Check reservation date availability. |
||
78 | 7 | */ |
|
79 | public function validateAvailability() |
||
85 | |||
86 | /** |
||
87 | * Scope for getting non cancelled reservations. |
||
88 | * |
||
89 | * @param $query |
||
90 | * |
||
91 | * @return mixed |
||
92 | 7 | */ |
|
93 | public function scopeNotCancelled($query) |
||
101 | |||
102 | /** |
||
103 | * Set machine scope |
||
104 | * |
||
105 | * @param $query |
||
106 | 7 | * |
|
107 | * @return mixed |
||
108 | */ |
||
109 | 7 | public function scopeMachine($query) |
|
117 | |||
118 | /** |
||
119 | * Get default reservation status. |
||
120 | * |
||
121 | * @return Status |
||
122 | */ |
||
123 | 6 | public function getDefaultStatus() |
|
124 | { |
||
125 | 6 | $statusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received'); |
|
126 | |||
127 | 6 | return Status::where('ident', $statusIdent)->first(); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Generate unique hash for each reservation. |
||
132 | * |
||
133 | * @return string|null |
||
134 | */ |
||
135 | 9 | public function getUniqueHash() |
|
136 | { |
||
137 | 9 | $length = Config::get('vojtasvoboda.reservations::config.hash', 32); |
|
138 | 9 | if ($length == 0) { |
|
139 | return null; |
||
140 | } |
||
141 | |||
142 | 9 | return substr(md5('reservations-' . Str::random($length)), 0, $length); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Generate unique number for each reservation. With this hash you can reference |
||
147 | * concrete reservation instead of using internal Reservation ID. |
||
148 | * |
||
149 | * @return string|null |
||
150 | */ |
||
151 | 9 | public function getUniqueNumber() |
|
152 | { |
||
153 | // init |
||
154 | 9 | $min = Config::get('vojtasvoboda.reservations::config.number.min', 123456); |
|
155 | 9 | $max = Config::get('vojtasvoboda.reservations::config.number.max', 999999); |
|
156 | 9 | if ($min == 0 || $max == 0) { |
|
157 | return null; |
||
158 | } |
||
159 | |||
160 | // generate random number |
||
161 | 9 | $count = 0; |
|
162 | do { |
||
163 | 9 | $number = mt_rand($min, $max); |
|
164 | |||
165 | 9 | } while ((self::where('number', $number)->count() > 0) && (++$count < 1000)); |
|
166 | |||
167 | 9 | return $count >= 1000 ? null : $number; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Get Reservations facade. |
||
172 | * |
||
173 | * @return ReservationsFacade |
||
174 | */ |
||
175 | private function getFacade() |
||
179 | } |
||
180 |