Completed
Pull Request — master (#20)
by
unknown
06:45
created

ReservationForm::getCalendarSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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