ReservationForm::onSubmit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
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\Classes\Variables;
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
    const PATH_PICKADATE_COMPRESSED = '/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/';
25
26
    protected $pickerLang = [
27
        'cs' => 'cs_CZ',
28
        'es' => 'es_ES',
29
        'ru' => 'ru_RU',
30
        'fr' => 'fr_FR',
31
    ];
32
33
    public function componentDetails()
34
	{
35
		return [
36
			'name' => 'vojtasvoboda.reservations::lang.reservationform.name',
37
			'description' => 'vojtasvoboda.reservations::lang.reservationform.description',
38
		];
39
	}
40
41
    /**
42
     * AJAX form submit handler.
43
     */
44
    public function onSubmit()
45
    {
46
        // check CSRF token
47
        if (Session::token() !== Input::get('_token')) {
48
            throw new ApplicationException(Lang::get('vojtasvoboda.reservations::lang.errors.session_expired'));
49
        }
50
51
        $data = Input::all();
52
        $this->getFacade()->storeReservation($data);
53
    }
54
55
    /**
56
     * Fallback for non-ajax POST request.
57
     */
58
	public function onRun()
59
	{
60
        $facade = $this->getFacade();
61
62
		$error = false;
63
		if (Input::get($this->alias . '-submit')) {
64
65
            // check CSRF token
66
            if (Session::token() !== Input::get('_token')) {
67
                $error = Lang::get('vojtasvoboda.reservations::lang.errors.session_expired');
68
69
            } else {
70
                try {
71
                    $data = Input::all();
72
                    $facade->storeReservation($data);
73
                    $msg = Lang::get('vojtasvoboda.reservations::lang.reservationform.success');
74
                    Flash::success($msg);
75
76
                    return Redirect::to($this->page->url . '#' . $this->alias, 303);
77
78
                } catch(ValidationException $e) {
79
                    $error = $e->getMessage();
80
81
                } catch(ApplicationException $e) {
82
                    $error = $e->getMessage();
83
84
                } catch(Exception $e) {
85
                    Log::error($e->getMessage());
86
                    $error = Lang::get('vojtasvoboda.reservations::lang.errors.exception');
87
                }
88
            }
89
		}
90
91
		// inject assets
92
        $this->injectAssets();
93
94
		// load booked dates and their time slots
95
        $dates = $this->getReservedDates();
96
97
        // template data
98
		$this->page['sent'] = Flash::check();
99
		$this->page['post'] = post();
100
		$this->page['error'] = $error;
101
        $this->page['dates'] = json_encode($dates);
102
        $this->page['settings'] = $this->getCalendarSetting();
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
     * @return array
117
     */
118
    protected function getCalendarSetting()
119
    {
120
        return [
121
            'formats_date' => Variables::getDateFormat(),
122
            'formats_time' => Variables::getTimeFormat(),
123
            'reservation_interval' => Variables::getReservationInterval(),
124
            'first_weekday' => Variables::getFirstWeekday(),
125
            'work_time_from' => Variables::getWorkTimeFrom(),
126
            'work_time_to' => Variables::getWorkTimeTo(),
127
            'work_days' => Variables::getWorkingDays(),
128
        ];
129
    }
130
131
    /**
132
     * Inject components assets.
133
     */
134
    protected function injectAssets()
135
    {
136
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.css');
137
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.date.css');
138
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.time.css');
139
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.js');
140
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.date.js');
141
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.time.js');
142
143
        $locale = Lang::getLocale();
144
        $translation = isset($this->pickerLang[$locale]) ? $this->pickerLang[$locale] : null;
145
        if ($translation !== null) {
146
            $this->addJs(self::PATH_PICKADATE_COMPRESSED.'translations/'.$translation.'.js');
147
        }
148
149
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/convert.js');
150
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js');
151
    }
152
153
    /**
154
     * @return ReservationsFacade
155
     */
156
    protected function getFacade()
157
    {
158
        return App::make(ReservationsFacade::class);
159
    }
160
}
161