Issues (93)

app/Http/Controllers/HospitalController.php (7 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Room;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Http\Request;
8
use App\Hospital;
9
use App\City;
10
11
class HospitalController extends Controller
12
{
13
    /**
14
     * Display a listing of the resource.
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function index()
19
    {
20
        $hospital = Hospital::orderBy('city_id','asc')->paginate(10);
21
        $data = [
22
            'role' => session('role'),
23
            'hospital' => $hospital
24
        ];
25
        return view('pages.hospital')->with('data',$data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.hospi...')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
26
    }
27
28
    /**
29
     * Show the form for creating a new resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function create()
34
    {
35
        $city_id = City::pluck('name', 'id');
36
        return view('pages.ext.add-hospital')->with('city_id', $city_id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.ext.a...th('city_id', $city_id) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
37
    }
38
39
    /**
40
     * Store a newly created resource in storage.
41
     *
42
     * @param Request $request
43
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
44
     * @throws \Illuminate\Validation\ValidationException
45
     */
46
    public function store(Request $request)
47
    {
48
        $this->validate($request, [
49
            'name'=>'required|min:3',
50
            'city_id'=>'required',
51
            'biography'=>'required',
52
            'address'=>'required|min:3',
53
            'medical_services'=>'required',
54
            'public_services'=>'required',
55
        ]);
56
57
        $hospital = new Hospital;
58
        $hospital->name = $request->input('name');
59
        $hospital->city_id = $request->input('city_id');
60
        $hospital->biography = $request->input('biography');
61
        $hospital->address = $request->input('address');
62
        $hospital->medical_services = $request->input('medical_services');
63
        $hospital->public_services = $request->input('public_services');
64
        $hospital->cover_images_id = 1;
65
66
        if($hospital->save()) {
67
            return redirect (route('hospital.index'))->with('success', 'Rumah sakit berhasil di tambahkan !');
68
        }
69
70
        return redirect (route('hospital.index'))->with('failed', 'Gagal menambahkan rumah sakit !');
71
    }
72
73
    /**
74
     * Display the specified resource.
75
     *
76
     * @param  int  $id
77
     * @return \Illuminate\Http\Response
78
     */
79
    public function show($id)
80
    {
81
        $hospital = Hospital::find($id);
82
        if(!$hospital) {
83
            abort(401);
84
        }
85
        $data = [
86
            'hospital' => $hospital,
87
            'rooms' => $this->getRooms($id),
88
        ];
89
90
        return view('pages.ext.view-hospital')->with('data', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.ext.v...')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
91
    }
92
93
    /**
94
     * Show the form for editing the specified resource.
95
     *
96
     * @param  int  $id
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function edit($id)
100
    {
101
        $hospital = Hospital::find($id);
102
        $city_id = City::pluck('name', 'id');
103
        $data = [
104
            'role' => session('role'),
105
            'hospital' => $hospital,
106
            'city_id' => $city_id
107
        ];
108
        return view('pages.ext.edit-hospital')->with('data', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.ext.e...')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
109
    }
110
111
    /**
112
     * Update the specified resource in storage.
113
     *
114
     * @param Request $request
115
     * @param $id
116
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
117
     * @throws \Illuminate\Validation\ValidationException
118
     */
119
    public function update(Request $request, $id)
120
    {
121
        $this->validate($request, [
122
            'name'=>'required|min:3',
123
            'city_id'=>'required',
124
            'biography'=>'required',
125
            'address'=>'required|min:3',
126
            'medical_services'=>'required',
127
            'public_services'=>'required',
128
        ]);
129
130
        $hospital = Hospital::find($id);
131
        $hospital->name = $request->input('name');
132
        $hospital->city_id = $request->input('city_id');
133
        $hospital->biography = $request->input('biography');
134
        $hospital->address = $request->input('address');
135
        $hospital->medical_services = $request->input('medical_services');
136
        $hospital->public_services = $request->input('public_services');
137
        $hospital->cover_images_id = 1;
138
139
        if($hospital->save()) {
140
            return redirect (route('hospital.index'))->with('success', 'Rumah sakit berhasil di perbaharui');
141
        }
142
143
        return redirect (route('hospital.edit', $id))->with('failed', 'Gagal memperbaharui rumah sakit !');
144
    }
145
146
    /**
147
     * Remove the specified resource from storage.
148
     *
149
     * @param  int  $id
150
     * @return \Illuminate\Http\Response
151
     */
152
    public function destroy($id)
153
    {
154
        $hospital = Hospital::find($id);
155
        $delrooms = $this->deleteRoomsAndRoomDetail($id);
156
157
        if($delrooms){
158
            if($hospital->delete()) {
159
                return redirect(route('hospital.index'))->with('success', 'Rumah sakit dihapus !');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('h...Rumah sakit dihapus !') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
160
            }
161
            return redirect(route('hospital.index'))->with('failed', 'Gagal menghapus rumah sakit !');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('h...nghapus rumah sakit !') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
162
        }
163
        return redirect(route('hospital.index'))->with('failed', 'Error ! Telah terjadi kesalahan pada server !');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('h...salahan pada server !') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
164
    }
165
166
    /**
167
     * @param $hospital_id
168
     * @return bool
169
     */
170
    private function deleteRoomsAndRoomDetail($hospital_id)
171
    {
172
        $rooms = $this->getRooms($hospital_id);
173
        $ids = $rooms->pluck('id')->toArray();
174
        if(count($ids) < 1) {
175
            return true;
176
        }
177
178
        $delroom = Room::whereIn('id', $ids)->delete();
179
        $deldetail = DB::table('room_details')
180
            ->where('hospital_id', '=', $hospital_id)
181
            ->delete();
182
183
        if($delroom && $deldetail) {
184
            return true;
185
        }
186
187
        return false;
188
    }
189
190
    /**
191
     * @param $hospital_id
192
     * @return \Illuminate\Support\Collection
193
     */
194
    private function getRooms($hospital_id)
195
    {
196
        $rooms = DB::table('rooms')
197
            ->selectRaw(DB::raw('rooms.*'))
198
            ->leftJoin('room_details', 'rooms.id', '=', 'room_details.room_id')
199
            ->where('room_details.hospital_id','=',$hospital_id)
200
            ->get();
201
202
        return $rooms;
203
    }
204
205
    public function indexUser()
206
    {
207
        $location = City::orderBy('name','asc')->pluck('name', 'id');
208
        $img = 'storage/images/hospital-icon.jpg';
209
        $data = [
210
            'img' => $img,
211
            'location' => $location
212
        ];
213
        return view ('SearchRS')->with('data',$data);
214
    }
215
216
    public function showList()
217
    {
218
        return view('LSRumahSakit');
219
    }
220
221
    public function searchHospital(Request $request)
222
    {
223
        if($request->nama == null AND $request->location != null){
224
            $hospital = Hospital::where('city_id',$request->location)->orderBy('name','asc')->paginate(5);
225
        }elseif ($request->location == null AND $request->nama != null){
226
            $hospital = Hospital::where('biography','LIKE','%'.$request->nama.'%')->orderBy('name','asc')->paginate(5);
227
        }else{
228
            $hospital = Hospital::where('biography','LIKE','%'.$request->nama.'%')->where('city_id',$request->location)->orderBy('name','asc')->paginate(5);
229
        }
230
        $location = City::orderBy('name','asc')->pluck('name','id');
231
        $data = [
232
            'hospital' => $hospital,
233
            'location' => $location
234
        ];
235
        return view ('listHospital')->with('data',$data);
236
    }
237
238
    public function searchContent($content)
239
    {
240
        $hospital = Hospital::where('biography','LIKE','%'.$content.'%')->orderBy('name','asc')->paginate(5);
241
        $location = City::orderBy('name','asc')->pluck('name','id');
242
        $data = [
243
            'hospital' => $hospital,
244
            'location' => $location
245
        ];
246
        return view ('listHospital')->with('data',$data);
247
    }
248
249
    public function viewHospital($id)
250
    {
251
        $hospital = Hospital::find($id);
252
        $data = [
253
            'hospital' => $hospital,
254
            'room' => $this->getRooms($id),
255
        ];
256
        return view ('viewhospital')->with('data',$data);
257
    }
258
}
259