Completed
Pull Request — master (#20)
by Vojta
08:55
created

ReservationForm::getCalendarSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 13
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 Config;
16
use VojtaSvoboda\Reservations\Models\Settings;
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
    protected $pickerLang = [
28
        'cs' => 'cs_CZ',
29
        'es' => 'es_ES',
30
        'ru' => 'ru_RU',
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 reservation facade.
107
     *
108
     * @return ReservationsFacade
109
     */
110
	protected function getFacade()
111
    {
112
        return App::make(ReservationsFacade::class);
113
    }
114
115
    /**
116
     * Get reserved dates.
117
     *
118
     * @return array
119
     */
120
    protected function getReservedDates()
121
    {
122
        return $this->getFacade()->getReservedDates();
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    protected function getCalendarSetting()
129
    {
130
        $dateFormat = Settings::get('formats_date', Config::get('vojtasvoboda.reservations::config.formats.date', 'd/m/Y'));
131
        $timeFormat = Settings::get('formats_time', Config::get('vojtasvoboda.reservations::config.formats.time', 'H:i'));
132
        $reservationInterval = Settings::get('reservation_interval', Config::get('vojtasvoboda.reservations::config.reservation.interval', 15));
133
        $defaultWorkingDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
134
135
        return [
136
            'formats_date' => $dateFormat,
137
            'formats_time' => $timeFormat,
138
            'reservation_interval' => (int) $reservationInterval,
139
            'first_weekday' => (int) Settings::get('first_weekday', false),
140
            'work_time_from' => Settings::get('work_time_from', '10:00'),
141
            'work_time_to' => Settings::get('work_time_to', '18:00'),
142
            'work_days' => Settings::get('work_days', $defaultWorkingDays),
143
        ];
144
    }
145
146
    /**
147
     * Inject components assets.
148
     */
149
    protected function injectAssets()
150
    {
151
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.css');
152
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.date.css');
153
        $this->addCss(self::PATH_PICKADATE_COMPRESSED.'themes/classic.time.css');
154
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.js');
155
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.date.js');
156
        $this->addJs(self::PATH_PICKADATE_COMPRESSED.'picker.time.js');
157
158
        $locale = Lang::getLocale();
159
        $translation = isset($this->pickerLang[$locale]) ? $this->pickerLang[$locale] : null;
160
        if ($translation !== null) {
161
            $this->addJs(self::PATH_PICKADATE_COMPRESSED.'translations/'.$translation.'.js');
162
        }
163
164
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js');
165
    }
166
}
167