Completed
Pull Request — master (#16)
by
unknown
06:36
created

ReservationForm::injectAssets()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 5
eloc 20
nc 8
nop 0
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
16
/**
17
 * Reservation Form component.
18
 *
19
 * @package VojtaSvoboda\Reservations\Components
20
 */
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()
101
    {
102
        return App::make(ReservationsFacade::class);
103
    }
104
105
    /**
106
     * Get reserved dates.
107
     *
108
     * @return array
109
     */
110
    protected function getReservedDates()
111
    {
112
        return $this->getFacade()->getReservedDates();
113
    }
114
115
    /**
116
     * Inject components assets.
117
     */
118
    protected function injectAssets()
119
    {
120
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.css');
121
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.date.css');
122
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.time.css');
123
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.js');
124
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.date.js');
125
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.time.js');
126
127
        switch (Lang::getLocale()) {
128
            case 'cs':
129
                $pickerTranslate = 'cs_CZ';
130
                break;
131
            case 'es':
132
                $pickerTranslate = 'es_ES';
133
                break;
134
            case 'ru':
135
                $pickerTranslate = 'ru_RU';
136
                break;
137
        }
138
139
        if (isset($pickerTranslate)) {
140
            $this->addJs(self::PATH_PICKADATE_COMPRESSED.'translations/'.$pickerTranslate.'.js');
141
        }
142
143
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js');
144
    }
145
}
146