1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\RoomRequest; |
6
|
|
|
use App\Models\Room; |
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
8
|
|
|
|
9
|
|
|
class RoomController extends Controller |
10
|
|
|
{ |
11
|
4 |
|
private function getRouteName() |
12
|
|
|
{ |
13
|
4 |
|
return 'room'; |
14
|
|
|
} |
15
|
|
|
|
16
|
4 |
|
public function index() |
17
|
|
|
{ |
18
|
4 |
|
$title = trans('general.rooms'); |
19
|
|
|
|
20
|
4 |
|
$dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment') |
21
|
4 |
|
->paginate($this->getItemsPerPage()); |
22
|
|
|
|
23
|
|
|
$viewData = [ |
24
|
4 |
|
'columns' => $this->getColumns(), |
25
|
4 |
|
'dataset' => $dataset, |
26
|
4 |
|
'routeName' => $this->getRouteName(), |
27
|
4 |
|
'title' => $title, |
28
|
|
|
]; |
29
|
|
|
|
30
|
4 |
|
return view('list', $viewData); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function store(RoomRequest $request, $id = null) |
34
|
|
|
{ |
35
|
1 |
|
if ($id === null) { |
36
|
|
|
$object = new Room(); |
37
|
|
|
} else { |
38
|
|
|
try { |
39
|
1 |
|
$object = Room::findOrFail($id); |
40
|
|
|
} catch (ModelNotFoundException $e) { |
41
|
|
|
return Controller::returnBack([ |
42
|
|
|
'message' => trans('general.object_not_found'), |
43
|
|
|
'alert-class' => 'alert-danger', |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$object->fill($request->all()); |
49
|
1 |
|
$object->save(); |
50
|
|
|
|
51
|
1 |
|
return redirect()->route($this->getRouteName().'.index') |
52
|
1 |
|
->with([ |
53
|
1 |
|
'message' => trans('general.saved'), |
54
|
1 |
|
'alert-class' => 'alert-success', |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function delete($id) |
59
|
|
|
{ |
60
|
1 |
|
Room::destroy($id); |
61
|
1 |
|
$data = ['class' => 'alert-success', 'message' => trans('general.deleted')]; |
62
|
|
|
|
63
|
1 |
|
return response()->json($data); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function showAddEditForm($id = null) |
67
|
|
|
{ |
68
|
2 |
|
if ($id === null) { |
69
|
|
|
$dataset = new Room(); |
70
|
|
|
$title = trans('general.add'); |
71
|
|
|
$submitRoute = route($this->getRouteName().'.postadd'); |
72
|
|
|
} else { |
73
|
|
|
try { |
74
|
2 |
|
$dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')->findOrFail($id); |
75
|
1 |
|
} catch (ModelNotFoundException $e) { |
76
|
1 |
|
return Controller::returnBack([ |
77
|
1 |
|
'message' => trans('general.object_not_found'), |
78
|
1 |
|
'alert-class' => 'alert-danger', |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$title = trans('general.edit'); |
83
|
1 |
|
$submitRoute = route($this->getRouteName().'.postedit', $id); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$title .= ' '.mb_strtolower(trans('general.room')); |
87
|
|
|
|
88
|
|
|
$viewData = [ |
89
|
1 |
|
'dataset' => $dataset, |
90
|
1 |
|
'fields' => $this->getFields(), |
91
|
1 |
|
'title' => $title, |
92
|
1 |
|
'submitRoute' => $submitRoute, |
93
|
1 |
|
'routeName' => $this->getRouteName(), |
94
|
|
|
]; |
95
|
|
|
|
96
|
1 |
|
return view('addedit', $viewData); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
private function getFields() |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
|
|
[ |
103
|
1 |
|
'id' => 'number', |
104
|
1 |
|
'title' => trans('general.number'), |
105
|
|
|
'value' => function (Room $data) { |
106
|
1 |
|
return $data->number; |
107
|
1 |
|
}, |
108
|
|
|
'optional' => [ |
109
|
|
|
'required' => 'required', |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
[ |
113
|
1 |
|
'id' => 'floor', |
114
|
1 |
|
'title' => trans('general.floor'), |
115
|
|
|
'value' => function (Room $data) { |
116
|
1 |
|
return $data->floor; |
117
|
1 |
|
}, |
118
|
1 |
|
'type' => 'number', |
119
|
|
|
'optional' => [ |
120
|
|
|
'required' => 'required', |
121
|
|
|
], |
122
|
|
|
], |
123
|
|
|
[ |
124
|
1 |
|
'id' => 'capacity', |
125
|
1 |
|
'title' => trans('general.capacity'), |
126
|
|
|
'value' => function (Room $data) { |
127
|
1 |
|
return $data->capacity; |
128
|
1 |
|
}, |
129
|
1 |
|
'type' => 'number', |
130
|
|
|
'optional' => [ |
131
|
|
|
'required' => 'required', |
132
|
|
|
], |
133
|
|
|
], |
134
|
|
|
[ |
135
|
1 |
|
'id' => 'price', |
136
|
1 |
|
'title' => trans('general.price'), |
137
|
|
|
'value' => function (Room $data) { |
138
|
1 |
|
return $data->price; |
139
|
1 |
|
}, |
140
|
1 |
|
'type' => 'number', |
141
|
|
|
'optional' => [ |
142
|
|
|
'step' => '0.01', |
143
|
|
|
'placeholder' => '0.00', |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
[ |
147
|
1 |
|
'id' => 'comment', |
148
|
1 |
|
'title' => trans('general.comment'), |
149
|
|
|
'value' => function (Room $data) { |
150
|
1 |
|
return $data->comment; |
151
|
1 |
|
}, |
152
|
1 |
|
'type' => 'textarea', |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
private function getColumns() |
158
|
|
|
{ |
159
|
|
|
$dataset = [ |
160
|
|
|
[ |
161
|
4 |
|
'title' => trans('general.number'), |
162
|
|
|
'value' => function (Room $data) { |
163
|
2 |
|
return $data->number; |
164
|
4 |
|
}, |
165
|
|
|
], |
166
|
|
|
[ |
167
|
4 |
|
'title' => trans('general.floor'), |
168
|
|
|
'value' => function (Room $data) { |
169
|
2 |
|
return $data->floor; |
170
|
4 |
|
}, |
171
|
|
|
], |
172
|
|
|
[ |
173
|
4 |
|
'title' => trans('general.capacity'), |
174
|
|
|
'value' => function (Room $data) { |
175
|
2 |
|
return $data->capacity; |
176
|
4 |
|
}, |
177
|
|
|
], |
178
|
|
|
[ |
179
|
4 |
|
'title' => trans('general.price'), |
180
|
|
|
'value' => function (Room $data) { |
181
|
2 |
|
return $data->price; |
182
|
4 |
|
}, |
183
|
|
|
], |
184
|
|
|
[ |
185
|
4 |
|
'title' => trans('general.comment'), |
186
|
4 |
|
'value' => function (Room $data) { |
187
|
2 |
|
return $data->comment; |
188
|
4 |
|
}, |
189
|
|
|
], |
190
|
|
|
]; |
191
|
|
|
|
192
|
4 |
|
return $dataset; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|