|
1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Components; |
|
2
|
|
|
|
|
3
|
|
|
use App; |
|
4
|
|
|
use Cms\Classes\ComponentBase; |
|
5
|
|
|
use Config; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Flash; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
use Input; |
|
10
|
|
|
use Lang; |
|
11
|
|
|
use October\Rain\Exception\ApplicationException; |
|
12
|
|
|
use October\Rain\Exception\ValidationException; |
|
13
|
|
|
use Redirect; |
|
14
|
|
|
use Session; |
|
15
|
|
|
use VojtaSvoboda\Reservations\Facades\ReservationsFacade; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Reservation Form component. |
|
19
|
|
|
* |
|
20
|
|
|
* @package VojtaSvoboda\Reservations\Components |
|
21
|
|
|
*/ |
|
22
|
|
|
class ReservationForm extends ComponentBase |
|
23
|
|
|
{ |
|
24
|
|
|
public function componentDetails() |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
'name' => 'vojtasvoboda.reservations::lang.reservationform.name', |
|
28
|
|
|
'description' => 'vojtasvoboda.reservations::lang.reservationform.description', |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* AJAX form submit handler. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function onSubmit() |
|
36
|
|
|
{ |
|
37
|
|
|
// check CSRF token |
|
38
|
|
|
if (Session::token() != Input::get('_token')) { |
|
39
|
|
|
throw new ApplicationException('Form session expired! Please refresh the page.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @var ReservationsFacade $facade */ |
|
43
|
|
|
$facade = App::make('vojtasvoboda.reservations.facade'); |
|
44
|
|
|
$data = Input::all(); |
|
45
|
|
|
$facade->storeReservation($data); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Fallback for non-ajax POST request. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function onRun() |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
/** @var ReservationsFacade $facade */ |
|
54
|
|
|
$facade = App::make('vojtasvoboda.reservations.facade'); |
|
55
|
|
|
|
|
56
|
|
|
$error = false; |
|
57
|
|
|
if (Input::get('submit')) { |
|
58
|
|
|
|
|
59
|
|
|
// check CSRF token |
|
60
|
|
|
if (Session::token() != Input::get('_token')) { |
|
61
|
|
|
$error = 'Form session expired! Please refresh the page.'; |
|
62
|
|
|
|
|
63
|
|
|
} else { |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
$data = Input::all(); |
|
67
|
|
|
$facade->storeReservation($data); |
|
68
|
|
|
$msg = Lang::get('vojtasvoboda.reservations::lang.reservationform.success'); |
|
69
|
|
|
Flash::success($msg); |
|
70
|
|
|
|
|
71
|
|
|
return Redirect::to($this->page->url . '#' . $this->alias, 303); |
|
72
|
|
|
|
|
73
|
|
|
} catch(ValidationException $e) { |
|
|
|
|
|
|
74
|
|
|
$error = $e->getMessage(); |
|
75
|
|
|
|
|
76
|
|
|
} catch(ApplicationException $e) { |
|
|
|
|
|
|
77
|
|
|
$error = $e->getMessage(); |
|
78
|
|
|
|
|
79
|
|
|
} catch(Exception $e) { |
|
80
|
|
|
Log::error($e->getMessage()); |
|
81
|
|
|
$error = 'We\'re sorry, but something went wrong and the page cannot be displayed.'; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// inject assets |
|
87
|
|
|
$this->injectAssets(); |
|
88
|
|
|
|
|
89
|
|
|
// load booked dates |
|
90
|
|
|
$dates = $facade->getReservedDates(); |
|
91
|
|
|
|
|
92
|
|
|
// template data |
|
93
|
|
|
$this->page['sent'] = Flash::check(); |
|
94
|
|
|
$this->page['post'] = $_POST; |
|
95
|
|
|
$this->page['error'] = $error; |
|
96
|
|
|
$this->page['dates'] = json_encode($dates); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Inject components assets. |
|
101
|
|
|
*/ |
|
102
|
|
|
private function injectAssets() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->addCss('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/themes/classic.css'); |
|
105
|
|
|
$this->addCss('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/themes/classic.date.css'); |
|
106
|
|
|
$this->addCss('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/themes/classic.time.css'); |
|
107
|
|
|
$this->addJs('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/picker.js'); |
|
108
|
|
|
$this->addJs('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/picker.date.js'); |
|
109
|
|
|
$this->addJs('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/picker.time.js'); |
|
110
|
|
|
$this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: