1 | <?php namespace VojtaSvoboda\Reservations\Components; |
||
21 | class ReservationForm extends ComponentBase |
||
22 | { |
||
23 | const PATH_PICKADATE_COMPRESSED = '/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/'; |
||
24 | |||
25 | public function componentDetails() |
||
26 | { |
||
27 | return [ |
||
28 | 'name' => 'vojtasvoboda.reservations::lang.reservationform.name', |
||
29 | 'description' => 'vojtasvoboda.reservations::lang.reservationform.description', |
||
30 | ]; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * AJAX form submit handler. |
||
35 | */ |
||
36 | public function onSubmit() |
||
37 | { |
||
38 | // check CSRF token |
||
39 | if (Session::token() != Input::get('_token')) { |
||
40 | throw new ApplicationException(Lang::get('vojtasvoboda.reservations::lang.errors.session_expired')); |
||
41 | } |
||
42 | |||
43 | $data = Input::all(); |
||
44 | $this->getFacade()->storeReservation($data); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Fallback for non-ajax POST request. |
||
49 | */ |
||
50 | public function onRun() |
||
51 | { |
||
52 | $facade = $this->getFacade(); |
||
53 | |||
54 | $error = false; |
||
55 | if (Input::get($this->alias . '-submit')) { |
||
56 | |||
57 | // check CSRF token |
||
58 | if (Session::token() != Input::get('_token')) { |
||
59 | $error = Lang::get('vojtasvoboda.reservations::lang.errors.session_expired'); |
||
60 | } else { |
||
61 | try { |
||
62 | $data = Input::all(); |
||
63 | $facade->storeReservation($data); |
||
64 | $msg = Lang::get('vojtasvoboda.reservations::lang.reservationform.success'); |
||
65 | Flash::success($msg); |
||
66 | |||
67 | return Redirect::to($this->page->url . '#' . $this->alias, 303); |
||
68 | |||
69 | } catch(ValidationException $e) { |
||
70 | $error = $e->getMessage(); |
||
71 | |||
72 | } catch(ApplicationException $e) { |
||
73 | $error = $e->getMessage(); |
||
74 | |||
75 | } catch(Exception $e) { |
||
76 | Log::error($e->getMessage()); |
||
77 | $error = Lang::get('vojtasvoboda.reservations::lang.errors.exception'); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // inject assets |
||
83 | $this->injectAssets(); |
||
84 | |||
85 | // load booked dates and their time slots |
||
86 | $dates = $this->getReservedDates(); |
||
87 | |||
88 | // template data |
||
89 | $this->page['sent'] = Flash::check(); |
||
90 | $this->page['post'] = post(); |
||
91 | $this->page['error'] = $error; |
||
92 | $this->page['dates'] = json_encode($dates); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get reservation facade. |
||
97 | * |
||
98 | * @return ReservationsFacade |
||
99 | */ |
||
100 | protected function getFacade() |
||
104 | |||
105 | /** |
||
106 | * Get reserved dates. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | protected function getReservedDates() |
||
114 | |||
115 | /** |
||
116 | * Inject components assets. |
||
117 | */ |
||
118 | protected function injectAssets() |
||
145 | } |
||
146 |