1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Components; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Cms\Classes\ComponentBase; |
5
|
|
|
use Exception; |
6
|
|
|
use Flash; |
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use Input; |
9
|
|
|
use Lang; |
10
|
|
|
use October\Rain\Exception\ApplicationException; |
11
|
|
|
use October\Rain\Exception\ValidationException; |
12
|
|
|
use Redirect; |
13
|
|
|
use Session; |
14
|
|
|
use VojtaSvoboda\Reservations\Facades\ReservationsFacade; |
15
|
|
|
use VojtaSvoboda\Reservations\Models\Settings; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Reservation Form component. |
19
|
|
|
* |
20
|
|
|
* @package VojtaSvoboda\Reservations\Components |
21
|
|
|
*/ |
22
|
|
|
class ReservationForm extends ComponentBase |
23
|
|
|
{ |
24
|
|
|
const PATH_PICKADATE_COMPRESSED = '/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/'; |
25
|
|
|
|
26
|
|
|
public function componentDetails() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'name' => 'vojtasvoboda.reservations::lang.reservationform.name', |
30
|
|
|
'description' => 'vojtasvoboda.reservations::lang.reservationform.description', |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* AJAX form submit handler. |
36
|
|
|
*/ |
37
|
|
|
public function onSubmit() |
38
|
|
|
{ |
39
|
|
|
// check CSRF token |
40
|
|
|
if (Session::token() != Input::get('_token')) { |
41
|
|
|
throw new ApplicationException(Lang::get('vojtasvoboda.reservations::lang.errors.session_expired')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$data = Input::all(); |
45
|
|
|
$this->getFacade()->storeReservation($data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Fallback for non-ajax POST request. |
50
|
|
|
*/ |
51
|
|
|
public function onRun() |
52
|
|
|
{ |
53
|
|
|
$facade = $this->getFacade(); |
54
|
|
|
|
55
|
|
|
$error = false; |
56
|
|
|
if (Input::get($this->alias . '-submit')) { |
57
|
|
|
|
58
|
|
|
// check CSRF token |
59
|
|
|
if (Session::token() != Input::get('_token')) { |
60
|
|
|
$error = Lang::get('vojtasvoboda.reservations::lang.errors.session_expired'); |
61
|
|
|
} else { |
62
|
|
|
try { |
63
|
|
|
$data = Input::all(); |
64
|
|
|
$facade->storeReservation($data); |
65
|
|
|
$msg = Lang::get('vojtasvoboda.reservations::lang.reservationform.success'); |
66
|
|
|
Flash::success($msg); |
67
|
|
|
|
68
|
|
|
return Redirect::to($this->page->url . '#' . $this->alias, 303); |
69
|
|
|
|
70
|
|
|
} catch(ValidationException $e) { |
71
|
|
|
$error = $e->getMessage(); |
72
|
|
|
|
73
|
|
|
} catch(ApplicationException $e) { |
74
|
|
|
$error = $e->getMessage(); |
75
|
|
|
|
76
|
|
|
} catch(Exception $e) { |
77
|
|
|
Log::error($e->getMessage()); |
78
|
|
|
$error = Lang::get('vojtasvoboda.reservations::lang.errors.exception'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// inject assets |
84
|
|
|
$this->injectAssets(); |
85
|
|
|
|
86
|
|
|
// load booked dates and their time slots |
87
|
|
|
$dates = $this->getReservedDates(); |
88
|
|
|
|
89
|
|
|
// template data |
90
|
|
|
$this->page['sent'] = Flash::check(); |
91
|
|
|
$this->page['post'] = post(); |
92
|
|
|
$this->page['error'] = $error; |
93
|
|
|
$this->page['dates'] = json_encode($dates); |
94
|
|
|
$this->page['settings'] = [ |
95
|
|
|
'formats_date' => Settings::get('formats_date', 'd/m/Y'), |
96
|
|
|
'formats_time' => Settings::get('formats_time', 'H:i'), |
97
|
|
|
'reservation_interval' => Settings::get('reservation_interval', 15), |
98
|
|
|
'first_weekday' => Settings::get('first_weekday', false), |
99
|
|
|
'work_time_from' => Settings::get('work_time_from', '10:00'), |
100
|
|
|
'work_time_to' => Settings::get('work_time_to', '18:00'), |
101
|
|
|
'work_days' => Settings::get('work_days', ['monday','tuesday','wednesday','thursday','friday']), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get reservation facade. |
107
|
|
|
* |
108
|
|
|
* @return ReservationsFacade |
109
|
|
|
*/ |
110
|
|
|
protected function getFacade() |
111
|
|
|
{ |
112
|
|
|
return App::make(ReservationsFacade::class); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get reserved dates. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function getReservedDates() |
121
|
|
|
{ |
122
|
|
|
return $this->getFacade()->getReservedDates(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Inject components assets. |
127
|
|
|
*/ |
128
|
|
|
protected function injectAssets() |
129
|
|
|
{ |
130
|
|
|
$this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.css'); |
131
|
|
|
$this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.date.css'); |
132
|
|
|
$this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.time.css'); |
133
|
|
|
$this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.js'); |
134
|
|
|
$this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.date.js'); |
135
|
|
|
$this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.time.js'); |
136
|
|
|
|
137
|
|
|
switch (Lang::getLocale()) { |
138
|
|
|
case 'cs': |
139
|
|
|
$pickerTranslate = 'cs_CZ'; |
140
|
|
|
break; |
141
|
|
|
case 'es': |
142
|
|
|
$pickerTranslate = 'es_ES'; |
143
|
|
|
break; |
144
|
|
|
case 'ru': |
145
|
|
|
$pickerTranslate = 'ru_RU'; |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (isset($pickerTranslate)) { |
150
|
|
|
$this->addJs(self::PATH_PICKADATE_COMPRESSED.'translations/'.$pickerTranslate.'.js'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|