Completed
Pull Request — master (#18)
by
unknown
06:35
created

ReservationForm::getFacade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\Facades\ReservationsFacade;
15
use VojtaSvoboda\Reservations\Models\Settings;
16
use Config;
17
18
/**
19
 * Reservation Form component.
20
 *
21
 * @package VojtaSvoboda\Reservations\Components
22
 */
23
class ReservationForm extends ComponentBase
24
{
25
    const PATH_PICKADATE_COMPRESSED = '/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/';
26
27
    public function componentDetails()
28
	{
29
		return [
30
			'name' => 'vojtasvoboda.reservations::lang.reservationform.name',
31
			'description' => 'vojtasvoboda.reservations::lang.reservationform.description',
32
		];
33
	}
34
35
    /**
36
     * AJAX form submit handler.
37
     */
38
    public function onSubmit()
39
    {
40
        // check CSRF token
41
        if (Session::token() != Input::get('_token')) {
42
            throw new ApplicationException(Lang::get('vojtasvoboda.reservations::lang.errors.session_expired'));
43
        }
44
45
        $data = Input::all();
46
        $this->getFacade()->storeReservation($data);
47
    }
48
49
    /**
50
     * Fallback for non-ajax POST request.
51
     */
52
	public function onRun()
53
	{
54
        $facade = $this->getFacade();
55
56
		$error = false;
57
		if (Input::get($this->alias . '-submit')) {
58
59
            // check CSRF token
60
            if (Session::token() != Input::get('_token')) {
61
                $error = Lang::get('vojtasvoboda.reservations::lang.errors.session_expired');
62
            } else {
63
                try {
64
                    $data = Input::all();
65
                    $facade->storeReservation($data);
66
                    $msg = Lang::get('vojtasvoboda.reservations::lang.reservationform.success');
67
                    Flash::success($msg);
68
69
                    return Redirect::to($this->page->url . '#' . $this->alias, 303);
70
71
                } catch(ValidationException $e) {
72
                    $error = $e->getMessage();
73
74
                } catch(ApplicationException $e) {
75
                    $error = $e->getMessage();
76
77
                } catch(Exception $e) {
78
                    Log::error($e->getMessage());
79
                    $error = Lang::get('vojtasvoboda.reservations::lang.errors.exception');
80
                }
81
            }
82
		}
83
84
		// inject assets
85
        $this->injectAssets();
86
87
		// load booked dates and their time slots
88
        $dates = $this->getReservedDates();
89
90
        // template data
91
		$this->page['sent'] = Flash::check();
92
		$this->page['post'] = post();
93
		$this->page['error'] = $error;
94
        $this->page['dates'] = json_encode($dates);
95
        $this->page['settings'] = [
96
            'formats_date'         => Settings::get(
97
                'formats_date',
98
                Config::get('vojtasvoboda.reservations::config.formats.date', 'd/m/Y')
99
            ),
100
            'formats_time'         => Settings::get(
101
                'formats_time',
102
                Config::get('vojtasvoboda.reservations::config.formats.time', 'H:i')
103
            ),
104
            'reservation_interval' => Settings::get(
105
                'reservation_interval',
106
                Config::get('vojtasvoboda.reservations::config.reservation.interval', 15)
107
            ),
108
            'first_weekday'        => (int)Settings::get('first_weekday', false),
109
            'work_time_from'       => Settings::get('work_time_from', '10:00'),
110
            'work_time_to'         => Settings::get('work_time_to', '18:00'),
111
            'work_days'            => Settings::get('work_days', ['monday','tuesday','wednesday','thursday','friday']),
112
        ];
113
	}
114
115
    /**
116
     * Get reservation facade.
117
     *
118
     * @return ReservationsFacade
119
     */
120
	protected function getFacade()
121
    {
122
        return App::make(ReservationsFacade::class);
123
    }
124
125
    /**
126
     * Get reserved dates.
127
     *
128
     * @return array
129
     */
130
    protected function getReservedDates()
131
    {
132
        return $this->getFacade()->getReservedDates();
133
    }
134
135
    /**
136
     * Inject components assets.
137
     */
138
    protected function injectAssets()
139
    {
140
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.css');
141
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.date.css');
142
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.time.css');
143
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.js');
144
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.date.js');
145
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.time.js');
146
147
        switch (Lang::getLocale()) {
148
            case 'cs':
149
                $pickerTranslate = 'cs_CZ';
150
                break;
151
            case 'es':
152
                $pickerTranslate = 'es_ES';
153
                break;
154
            case 'ru':
155
                $pickerTranslate = 'ru_RU';
156
                break;
157
        }
158
159
        if (isset($pickerTranslate)) {
160
            $this->addJs(self::PATH_PICKADATE_COMPRESSED.'translations/'.$pickerTranslate.'.js');
161
        }
162
163
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js');
164
    }
165
}
166