Passed
Push — master ( b35c98...707c53 )
by Vojta
05:27
created

Reservations::getFacade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace VojtaSvoboda\Reservations\Controllers;
2
3
use App;
4
use Backend\Classes\Controller;
5
use BackendMenu;
6
use Flash;
7
use Lang;
8
use VojtaSvoboda\Reservations\Facades\ReservationsFacade;
9
use VojtaSvoboda\Reservations\Models\Reservation;
10
use VojtaSvoboda\Reservations\Models\Status;
11
12
class Reservations extends Controller
13
{
14
    public $implement = [
15
        'Backend\Behaviors\ListController',
16
        'Backend\Behaviors\FormController',
17
        'Backend\Behaviors\ImportExportController',
18
    ];
19
20
    public $listConfig = 'config_list.yaml';
21
    public $formConfig = 'config_form.yaml';
22
    public $importExportConfig = 'config_import_export.yaml';
23
24
    private $stateColors;
25
    private $stateNames;
26
27
    public $requiredPermissions = [
28
        'vojtasvoboda.reservations.reservations',
29
    ];
30
31
    public function __construct()
32
    {
33
        parent::__construct();
34
35
        $action = $this->action == 'export' ? 'export' : 'reservations';
36
        BackendMenu::setContext('VojtaSvoboda.Reservations', 'reservations', $action);
37
    }
38
39
    /**
40
     * Extend controller listing.
41
     */
42
    public function index()
43
    {
44
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/bulk-actions.js');
45
        $this->asExtension('ListController')->index();
46
    }
47
48
    /**
49
     * Override displaying reservation listing.
50
     *
51
     * @param Reservation $record
52
     * @param string $columnName
53
     *
54
     * @return string
55
     */
56
    public function listOverrideColumnValue($record, $columnName)
57
    {
58
        $statusStyle = "display:inline-block;width:13px;height:14px;position:relative;top:2px;margin-right:4px;color:#fff;font-size:11px;padding-left:3px;";
59
60
        if ($columnName == 'status_id') {
61
            $color = $this->getStateColor($record->status_id);
62
            $name = $this->getStateName($record->status_id);
63
64
            return '<div><span style="' . $statusStyle . 'background-color:' . $color . '"></span> ' . $name . '</div>';
65
        }
66
67
        if ($columnName == 'returning') {
68
            return $this->isReturning($record->email) ? '<i class=" icon-star"></i>' : '';
69
        }
70
    }
71
72
    /**
73
     * Bulk change state and bulk delete.
74
     *
75
     * @return mixed
76
     */
77
    public function index_onBulkAction()
78
    {
79
        if (($bulkAction = post('action')) && ($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
80
            if ($bulkAction == 'delete') {
81
                $this->getFacade()->bulkDelete($checkedIds);
82
            } else {
83
                $this->getFacade()->bulkStateChange($checkedIds, $bulkAction);
84
            }
85
86
            $msg = Lang::get('vojtasvoboda.reservations::lang.reservations.change_status_success');
87
            Flash::success($msg);
88
        }
89
90
        return $this->listRefresh();
91
    }
92
93
    /**
94
     * Return if User is returning.
95
     *
96
     * @param string $email Users email.
97
     *
98
     * @return bool
99
     */
100
    private function isReturning($email)
101
    {
102
        return $this->getFacade()->isUserReturning($email);
103
    }
104
105
    /**
106
     * Get facade providing all plugin logic.
107
     *
108
     * @return ReservationsFacade
109
     */
110
    private function getFacade()
111
    {
112
        return App::make(ReservationsFacade::class);
113
    }
114
115
    /**
116
     * Get color by state ident.
117
     *
118
     * @param string $ident
119
     *
120
     * @return string|null
121
     */
122 View Code Duplication
    private function getStateColor($ident)
123
    {
124
        if ($this->stateColors === null) {
125
            $this->stateColors = Status::lists('color', 'ident');
126
        }
127
128
        return isset($this->stateColors[$ident]) ? $this->stateColors[$ident] : null;
129
    }
130
131
    /**
132
     * Get name by state ident.
133
     *
134
     * @param string $ident
135
     *
136
     * @return string|null
137
     */
138 View Code Duplication
    private function getStateName($ident)
139
    {
140
        if ($this->stateNames === null) {
141
            $this->stateNames = Status::lists('name', 'ident');
142
        }
143
144
        return isset($this->stateNames[$ident]) ? $this->stateNames[$ident] : null;
145
    }
146
}
147