1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\Zona\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\Zona\Facades\ZonaFacade; |
9
|
|
|
|
10
|
|
|
/* Models */ |
11
|
|
|
use Bantenprov\Zona\Models\Bantenprov\Zona\Zona; |
12
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
13
|
|
|
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah; |
14
|
|
|
use App\User; |
15
|
|
|
|
16
|
|
|
/* Etc */ |
17
|
|
|
use Validator; |
18
|
|
|
use Auth; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The ZonaController class. |
22
|
|
|
* |
23
|
|
|
* @package Bantenprov\Zona |
24
|
|
|
* @author bantenprov <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ZonaController extends Controller |
27
|
|
|
{ |
28
|
|
|
protected $zona; |
29
|
|
|
protected $siswa; |
30
|
|
|
protected $sekolah; |
31
|
|
|
protected $user; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new controller instance. |
35
|
|
|
* |
36
|
|
|
* @return void |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
$this->zona = new Zona; |
41
|
|
|
$this->siswa = new Siswa; |
42
|
|
|
$this->sekolah = new Sekolah; |
43
|
|
|
$this->user = new User; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Display a listing of the resource. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function index(Request $request) |
52
|
|
|
{ |
53
|
|
View Code Duplication |
if (request()->has('sort')) { |
|
|
|
|
54
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
55
|
|
|
|
56
|
|
|
$query = $this->zona->orderBy($sortCol, $sortDir); |
57
|
|
|
} else { |
58
|
|
|
$query = $this->zona->orderBy('id', 'asc'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($request->exists('filter')) { |
62
|
|
|
$query->where(function($q) use($request) { |
63
|
|
|
$value = "%{$request->filter}%"; |
64
|
|
|
|
65
|
|
|
$q->where('nomor_un', 'like', $value) |
66
|
|
|
->orWhere('lokasi_siswa', 'like', $value) |
67
|
|
|
->orWhere('lokasi_sekolah', 'like', $value) |
68
|
|
|
->orWhere('nilai_zona', 'like', $value); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
73
|
|
|
|
74
|
|
|
$response = $query->with(['siswa', 'sekolah', 'user'])->paginate($perPage); |
75
|
|
|
|
76
|
|
|
return response()->json($response) |
77
|
|
|
->header('Access-Control-Allow-Origin', '*') |
78
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Show the form for creating a new resource. |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Http\Response |
85
|
|
|
*/ |
86
|
|
|
public function create() |
87
|
|
|
{ |
88
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
89
|
|
|
$zona = $this->zona->getAttributes(); |
90
|
|
|
$siswas = $this->siswa->getAttributes(); |
91
|
|
|
$sekolahs = $this->sekolah->getAttributes(); |
92
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
93
|
|
|
$users_special = $this->user->all(); |
94
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
95
|
|
|
$current_user = Auth::User(); |
96
|
|
|
|
97
|
|
|
foreach($siswas as $siswa){ |
98
|
|
|
array_set($siswa, 'label', $siswa->nama_siswa); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach($sekolahs as $sekolah){ |
102
|
|
|
array_set($sekolah, 'label', $sekolah->nama); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
106
|
|
|
|
107
|
|
View Code Duplication |
if($role_check){ |
|
|
|
|
108
|
|
|
$user_special = true; |
109
|
|
|
|
110
|
|
|
foreach($users_special as $user){ |
111
|
|
|
array_set($user, 'label', $user->name); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$users = $users_special; |
115
|
|
|
}else{ |
116
|
|
|
$user_special = false; |
117
|
|
|
|
118
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
119
|
|
|
|
120
|
|
|
$users = $users_standar; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
array_set($current_user, 'label', $current_user->name); |
124
|
|
|
|
125
|
|
|
$response['zona'] = $zona; |
|
|
|
|
126
|
|
|
$response['siswas'] = $siswas; |
127
|
|
|
$response['sekolahs'] = $sekolahs; |
128
|
|
|
$response['users'] = $users; |
129
|
|
|
$response['user_special'] = $user_special; |
130
|
|
|
$response['current_user'] = $current_user; |
131
|
|
|
$response['error'] = false; |
132
|
|
|
$response['message'] = 'Success'; |
133
|
|
|
$response['status'] = true; |
134
|
|
|
|
135
|
|
|
return response()->json($response); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Store a newly created resource in storage. |
140
|
|
|
* |
141
|
|
|
* @param \Illuminate\Http\Request $request |
142
|
|
|
* @return \Illuminate\Http\Response |
143
|
|
|
*/ |
144
|
|
|
public function store(Request $request) |
145
|
|
|
{ |
146
|
|
|
$zona = $this->zona; |
147
|
|
|
|
148
|
|
|
$validator = Validator::make($request->all(), [ |
149
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,NULL,id,deleted_at,NULL", |
150
|
|
|
// 'sekolah_id' => "required|exists:{$this->sekolah->getTable()},id", |
|
|
|
|
151
|
|
|
// 'zona_siswa' => "required|exists:{$this->city->getTable()},id", |
|
|
|
|
152
|
|
|
// 'zona_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
153
|
|
|
// 'lokasi_siswa' => "required|exists:{$this->district->getTable()},id", |
|
|
|
|
154
|
|
|
// 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
155
|
|
|
// 'nilai_zona' => 'required|numeric', |
|
|
|
|
156
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
157
|
|
|
]); |
158
|
|
|
|
159
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
160
|
|
|
$error = true; |
161
|
|
|
$message = $validator->errors()->first(); |
162
|
|
|
} else { |
163
|
|
|
$nomor_un = $request->input('nomor_un'); |
164
|
|
|
$siswa = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first(); |
165
|
|
|
$zona_siswa = substr($siswa->village_id, 0, 6); |
166
|
|
|
$zona_sekolah = substr($siswa->sekolah->village_id, 0, 6); |
167
|
|
|
$lokasi_siswa = $siswa->village_id; |
168
|
|
|
$lokasi_sekolah = $siswa->sekolah->village_id; |
169
|
|
|
|
170
|
|
|
$zona->nomor_un = $nomor_un; |
171
|
|
|
$zona->sekolah_id = $siswa->sekolah->id; |
172
|
|
|
$zona->zona_siswa = $zona_siswa; |
173
|
|
|
$zona->zona_sekolah = $zona_sekolah; |
174
|
|
|
$zona->lokasi_siswa = $lokasi_siswa; |
175
|
|
|
$zona->lokasi_sekolah = $lokasi_sekolah; |
176
|
|
|
$zona->nilai_zona = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah); |
177
|
|
|
$zona->user_id = $request->input('user_id'); |
178
|
|
|
$zona->save(); |
179
|
|
|
|
180
|
|
|
$error = false; |
181
|
|
|
$message = 'Success'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$response['zona'] = $zona; |
|
|
|
|
185
|
|
|
$response['error'] = $error; |
186
|
|
|
$response['message'] = $message; |
187
|
|
|
$response['status'] = true; |
188
|
|
|
|
189
|
|
|
return response()->json($response); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Display the specified resource. |
194
|
|
|
* |
195
|
|
|
* @param \App\Zona $zona |
|
|
|
|
196
|
|
|
* @return \Illuminate\Http\Response |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id); |
201
|
|
|
|
202
|
|
|
$response['zona'] = $zona; |
|
|
|
|
203
|
|
|
$response['error'] = false; |
204
|
|
|
$response['message'] = 'Success'; |
205
|
|
|
$response['status'] = true; |
206
|
|
|
|
207
|
|
|
return response()->json($response); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Show the form for editing the specified resource. |
212
|
|
|
* |
213
|
|
|
* @param \App\Zona $zona |
|
|
|
|
214
|
|
|
* @return \Illuminate\Http\Response |
215
|
|
|
*/ |
216
|
|
|
public function edit($id) |
217
|
|
|
{ |
218
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
219
|
|
|
$zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id); |
220
|
|
|
$siswas = $this->siswa->getAttributes(); |
221
|
|
|
$sekolahs = $this->sekolah->getAttributes(); |
222
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
223
|
|
|
$users_special = $this->user->all(); |
224
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
225
|
|
|
$current_user = Auth::User(); |
226
|
|
|
|
227
|
|
|
if ($zona->siswa !== null) { |
228
|
|
|
array_set($zona->siswa, 'label', $zona->siswa->nama_siswa); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
232
|
|
|
|
233
|
|
|
if ($zona->user !== null) { |
234
|
|
|
array_set($zona->user, 'label', $zona->user->name); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
View Code Duplication |
if($role_check){ |
|
|
|
|
238
|
|
|
$user_special = true; |
239
|
|
|
|
240
|
|
|
foreach($users_special as $user){ |
241
|
|
|
array_set($user, 'label', $user->name); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$users = $users_special; |
245
|
|
|
}else{ |
246
|
|
|
$user_special = false; |
247
|
|
|
|
248
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
249
|
|
|
|
250
|
|
|
$users = $users_standar; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
array_set($current_user, 'label', $current_user->name); |
254
|
|
|
|
255
|
|
|
$response['zona'] = $zona; |
|
|
|
|
256
|
|
|
$response['siswas'] = $siswas; |
257
|
|
|
$response['sekolahs'] = $sekolahs; |
258
|
|
|
$response['users'] = $users; |
259
|
|
|
$response['user_special'] = $user_special; |
260
|
|
|
$response['current_user'] = $current_user; |
261
|
|
|
$response['error'] = false; |
262
|
|
|
$response['message'] = 'Success'; |
263
|
|
|
$response['status'] = true; |
264
|
|
|
|
265
|
|
|
return response()->json($response); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Update the specified resource in storage. |
270
|
|
|
* |
271
|
|
|
* @param \Illuminate\Http\Request $request |
272
|
|
|
* @param \App\Zona $zona |
|
|
|
|
273
|
|
|
* @return \Illuminate\Http\Response |
274
|
|
|
*/ |
275
|
|
|
public function update(Request $request, $id) |
276
|
|
|
{ |
277
|
|
|
$zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id); |
278
|
|
|
|
279
|
|
|
$validator = Validator::make($request->all(), [ |
280
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,{$id},id,deleted_at,NULL", |
281
|
|
|
// 'sekolah_id' => "required|exists:{$this->sekolah->getTable()},id", |
|
|
|
|
282
|
|
|
// 'zona_siswa' => "required|exists:{$this->city->getTable()},id", |
|
|
|
|
283
|
|
|
// 'zona_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
284
|
|
|
// 'lokasi_siswa' => "required|exists:{$this->district->getTable()},id", |
|
|
|
|
285
|
|
|
// 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
286
|
|
|
// 'nilai_zona' => 'required|numeric', |
|
|
|
|
287
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
288
|
|
|
]); |
289
|
|
|
|
290
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
291
|
|
|
$error = true; |
292
|
|
|
$message = $validator->errors()->first(); |
293
|
|
|
} else { |
294
|
|
|
$nomor_un = $request->input('nomor_un'); |
295
|
|
|
$siswa = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first(); |
296
|
|
|
$zona_siswa = substr($siswa->village_id, 0, 6); |
297
|
|
|
$zona_sekolah = substr($siswa->sekolah->village_id, 0, 6); |
298
|
|
|
$lokasi_siswa = $siswa->village_id; |
299
|
|
|
$lokasi_sekolah = $siswa->sekolah->village_id; |
300
|
|
|
|
301
|
|
|
$zona->nomor_un = $nomor_un; |
302
|
|
|
$zona->sekolah_id = $siswa->sekolah->id; |
303
|
|
|
$zona->zona_siswa = $zona_siswa; |
304
|
|
|
$zona->zona_sekolah = $zona_sekolah; |
305
|
|
|
$zona->lokasi_siswa = $lokasi_siswa; |
306
|
|
|
$zona->lokasi_sekolah = $lokasi_sekolah; |
307
|
|
|
$zona->nilai_zona = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah); |
308
|
|
|
$zona->user_id = $request->input('user_id'); |
309
|
|
|
$zona->save(); |
310
|
|
|
|
311
|
|
|
$error = false; |
312
|
|
|
$message = 'Success'; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$response['zona'] = $zona; |
|
|
|
|
316
|
|
|
$response['error'] = $error; |
317
|
|
|
$response['message'] = $message; |
318
|
|
|
$response['status'] = true; |
319
|
|
|
|
320
|
|
|
return response()->json($response); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Remove the specified resource from storage. |
325
|
|
|
* |
326
|
|
|
* @param \App\Zona $zona |
|
|
|
|
327
|
|
|
* @return \Illuminate\Http\Response |
328
|
|
|
*/ |
329
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
330
|
|
|
{ |
331
|
|
|
$zona = $this->zona->findOrFail($id); |
332
|
|
|
|
333
|
|
|
if ($zona->delete()) { |
334
|
|
|
$response['message'] = 'Success'; |
|
|
|
|
335
|
|
|
$response['success'] = true; |
336
|
|
|
$response['status'] = true; |
337
|
|
|
} else { |
338
|
|
|
$response['message'] = 'Failed'; |
|
|
|
|
339
|
|
|
$response['success'] = false; |
340
|
|
|
$response['status'] = false; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return json_encode($response); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.