Completed
Push — master ( 3c5480...50295b )
by Adam
06:57
created

ReservationController::getSearchFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 43
ccs 0
cts 23
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Interfaces\ManageTableInterface;
6
// TODO
7
use App\Http\Requests\GuestRequest;
8
use App\Http\Requests\ReservationSearchRequest;
9
use App\Models\Guest;
10
use App\Models\Reservation;
11
use App\Services\GuestTableService;
12
use App\Services\ReservationTableService;
13
use Illuminate\Database\Eloquent\ModelNotFoundException;
14
15
class ReservationController extends Controller implements ManageTableInterface
16
{
17
    protected $reservationTableService;
18
19 2
    public function __construct(ReservationTableService $reservationTableService)
20
    {
21 2
        $this->reservationTableService = $reservationTableService;
22 2
    }
23
24 2
    public function index()
25
    {
26 2
        $title = trans('general.reservations');
27
28 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
29 2
            ->with('guest:id,first_name,last_name')
30 2
            ->with('room:id,number')
31 2
            ->paginate($this->getItemsPerPage());
32
33 2
        if ($dataset->isEmpty()) {
34 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
35
        }
36
37
        $viewData = [
38 2
            'columns'       => $this->reservationTableService->getColumns(),
39 2
            'dataset'       => $dataset,
40 2
            'routeName'     => $this->reservationTableService->getRouteName(),
41 2
            'title'         => $title,
42
            // TODO
43 2
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
44
        ];
45
46 2
        return view('list', $viewData);
47
    }
48
49
    public function chooseGuest(GuestTableService $guestTableService)
50
    {
51
        $title = trans('general.choose_guest');
52
53
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
54
            ->paginate($this->getItemsPerPage());
55
56
        if ($dataset->isEmpty()) {
57
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
58
        }
59
60
        $viewData = [
61
            'columns'         => $guestTableService->getColumns(),
62
            'dataset'         => $dataset,
63
            'routeName'       => $guestTableService->getRouteName(),
64
            'title'           => $title,
65
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_rooms',
66
        ];
67
68
        return view('list', $viewData);
69
    }
70
71
    public function searchRooms($guestId)
72
    {
73
        $dataset = new Reservation();
74
        $dataset->guest_id = $guestId;
75
        $title = trans('navigation.reservation_search_rooms');
76
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_choose_room', $guestId);
77
78
        $viewData = [
79
            'dataset'     => $dataset,
80
            'fields'      => $this->getSearchFields(),
81
            'title'       => $title,
82
            'submitRoute' => $submitRoute
83
        ];
84
85
        return view('addedit', $viewData);
86
    }
87
88
    public function reservationSearch(ReservationSearchRequest $request, $guestId = null)
89
    {
90
        try {
91
            $object = Guest::findOrFail($guestId);
0 ignored issues
show
Unused Code introduced by
The assignment to $object is dead and can be removed.
Loading history...
92
        } catch (ModelNotFoundException $e) {
93
            return $this->returnBack([
94
                'message'     => trans('general.object_not_found'),
95
                'alert-class' => 'alert-danger',
96
            ]);
97
        }
98
99
        dd($request->all());
100
101
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
102
            ->with([
103
                'message'     => trans('general.saved'),
104
                'alert-class' => 'alert-success',
105
            ]);
106
    }
107
108
    // TODO
109
    public function store(GuestRequest $request, $objectId = null)
110
    {
111
        if ($objectId === null) {
112
            $object = new Reservation();
113
        } else {
114
            try {
115
                $object = Reservation::findOrFail($objectId);
116
            } catch (ModelNotFoundException $e) {
117
                return $this->returnBack([
118
                    'message'     => trans('general.object_not_found'),
119
                    'alert-class' => 'alert-danger',
120
                ]);
121
            }
122
        }
123
124
        $object->fill($request->all());
125
        $object->save();
126
127
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
128
            ->with([
129
                'message'     => trans('general.saved'),
130
                'alert-class' => 'alert-success',
131
            ]);
132
    }
133
134
    public function delete($objectId)
135
    {
136
        Reservation::destroy($objectId);
137
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
138
139
        return response()->json($data);
140
    }
141
142
    // TODO
143 1
    public function showAddEditForm($objectId = null)
144
    {
145 1
        if ($objectId === null) {
146
            $dataset = new Reservation();
147
            $title = trans('navigation.add_reservation');
148
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
149
        } else {
150
            try {
151 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
152 1
                ->with('guest:id,first_name,last_name')
153 1
                ->with('room:id,number')
154 1
                ->findOrFail($objectId);
155 1
            } catch (ModelNotFoundException $e) {
156 1
                return $this->returnBack([
157 1
                    'message'     => trans('general.object_not_found'),
158 1
                    'alert-class' => 'alert-danger',
159
                ]);
160
            }
161
162
            $title = trans('navigation.edit_reservation');
163
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
164
        }
165
166
        $viewData = [
167
            'dataset'     => $dataset,
168
            'fields'      => $this->getFields(),
169
            'title'       => $title,
170
            'submitRoute' => $submitRoute,
171
            'routeName'   => $this->reservationTableService->getRouteName(),
172
        ];
173
174
        return view('addedit', $viewData);
175
    }
176
177
    // TODO
178
    public function getSearchFields()
179
    {
180
        return [
181
            [
182
                'id'    => 'guest',
183
                'title' => trans('general.guest'),
184
                'value' => function (Reservation $data) {
185
                    return $data->guest->full_name;
186
                },
187
                'optional' => [
188
                    'disabled' => 'disabled',
189
                ],
190
            ],
191
            [
192
                'id'    => 'date_start',
193
                'title' => trans('general.reservation_date_of_stay_start'),
194
                'value' => function (Reservation $data) {
195
                    return $data->date_start;
196
                },
197
                'type'     => 'date',
198
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
199
                    'required' => 'required',
200
                ],*/
201
            ],
202
            [
203
                'id'    => 'date_end',
204
                'title' => trans('general.reservation_date_of_stay_start_end'),
205
                'value' => function (Reservation $data) {
206
                    return $data->date_end;
207
                },
208
                //'type'     => 'date',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
210
                    'required' => 'required',
211
                ],*/
212
            ],
213
            [
214
                'id'    => 'people',
215
                'title' => trans('general.number_of_people'),
216
                'value' => function () {
217
                    // TODO
218
                    return 1;
219
                },
220
                'type'     => 'number',
221
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
222
                    'required' => 'required',
223
                ],*/
224
            ],
225
        ];
226
    }
227
228
    // TODO
229
    public function getFields()
230
    {
231
        return [
232
            [
233
                'id'    => 'first_name',
234
                'title' => trans('general.first_name'),
235
                'value' => function (Reservation $data) {
236
                    return $data->first_name;
0 ignored issues
show
Bug introduced by
The property first_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
237
                },
238
                'optional' => [
239
                    'required' => 'required',
240
                ],
241
            ],
242
            [
243
                'id'    => 'last_name',
244
                'title' => trans('general.last_name'),
245
                'value' => function (Reservation $data) {
246
                    return $data->last_name;
0 ignored issues
show
Bug introduced by
The property last_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
247
                },
248
                'optional' => [
249
                    'required' => 'required',
250
                ],
251
            ],
252
            [
253
                'id'    => 'address',
254
                'title' => trans('general.address'),
255
                'value' => function (Reservation $data) {
256
                    return $data->address;
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
257
                },
258
                'optional' => [
259
                    'required' => 'required',
260
                ],
261
            ],
262
            [
263
                'id'    => 'zip_code',
264
                'title' => trans('general.zip_code'),
265
                'value' => function (Reservation $data) {
266
                    return $data->zip_code;
0 ignored issues
show
Bug introduced by
The property zip_code does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
267
                },
268
                'optional' => [
269
                    'required'    => 'required',
270
                    'placeholder' => '00-000',
271
                ],
272
            ],
273
            [
274
                'id'    => 'place',
275
                'title' => trans('general.place'),
276
                'value' => function (Reservation $data) {
277
                    return $data->place;
0 ignored issues
show
Bug introduced by
The property place does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
278
                },
279
                'optional' => [
280
                    'required' => 'required',
281
                ],
282
            ],
283
            [
284
                'id'    => 'PESEL',
285
                'title' => trans('general.PESEL'),
286
                'value' => function (Reservation $data) {
287
                    return $data->PESEL;
0 ignored issues
show
Bug introduced by
The property PESEL does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
288
                },
289
                'optional' => [
290
                    'required'    => 'required',
291
                    'placeholder' => '12345654321',
292
                ],
293
            ],
294
            [
295
                'id'    => 'contact',
296
                'title' => trans('general.contact'),
297
                'value' => function (Reservation $data) {
298
                    return $data->contact;
0 ignored issues
show
Bug introduced by
The property contact does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
299
                },
300
                'type'     => 'textarea',
301
                'optional' => [
302
                    'placeholder' => trans('general.contact_placeholder'),
303
                ],
304
            ],
305
        ];
306
    }
307
}
308