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 Illuminate\Support\Facades\DB; |
9
|
|
|
use Bantenprov\Zona\Facades\ZonaFacade; |
10
|
|
|
|
11
|
|
|
/* Models */ |
12
|
|
|
use Bantenprov\Zona\Models\Bantenprov\Zona\Zona; |
13
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
14
|
|
|
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah; |
15
|
|
|
use App\User; |
16
|
|
|
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai; |
17
|
|
|
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah; |
18
|
|
|
|
19
|
|
|
/* Etc */ |
20
|
|
|
use Validator; |
21
|
|
|
use Auth; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The ZonaController class. |
25
|
|
|
* |
26
|
|
|
* @package Bantenprov\Zona |
27
|
|
|
* @author bantenprov <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class ZonaController extends Controller |
30
|
|
|
{ |
31
|
|
|
protected $zona; |
32
|
|
|
protected $siswa; |
33
|
|
|
protected $sekolah; |
34
|
|
|
protected $user; |
35
|
|
|
protected $nilai; |
36
|
|
|
protected $admin_sekolah; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new controller instance. |
40
|
|
|
* |
41
|
|
|
* @return void |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->zona = new Zona; |
46
|
|
|
$this->siswa = new Siswa; |
47
|
|
|
$this->sekolah = new Sekolah; |
48
|
|
|
$this->user = new User; |
49
|
|
|
$this->nilai = new Nilai; |
50
|
|
|
$this->admin_sekolah = new AdminSekolah; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Display a listing of the resource. |
55
|
|
|
* |
56
|
|
|
* @return \Illuminate\Http\Response |
57
|
|
|
*/ |
58
|
|
|
public function index(Request $request) |
59
|
|
|
{ |
60
|
|
|
$admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first(); |
61
|
|
|
|
62
|
|
|
if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){ |
63
|
|
|
$response = []; |
64
|
|
|
return response()->json($response) |
65
|
|
|
->header('Access-Control-Allow-Origin', '*') |
66
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (request()->has('sort')) { |
70
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
71
|
|
|
|
72
|
|
|
if($this->checkRole(['superadministrator'])){ |
73
|
|
|
$query = $this->zona->orderBy($sortCol, $sortDir); |
74
|
|
|
}else{ |
75
|
|
|
$query = $this->zona->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy($sortCol, $sortDir); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
if($this->checkRole(['superadministrator'])){ |
79
|
|
|
$query = $this->zona->orderBy('id', 'asc'); |
80
|
|
|
}else{ |
81
|
|
|
$query = $this->zona->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy('id', 'asc'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($request->exists('filter')) { |
86
|
|
|
if($this->checkRole(['superadministrator'])){ |
87
|
|
View Code Duplication |
$query->where(function($q) use($request) { |
|
|
|
|
88
|
|
|
$value = "%{$request->filter}%"; |
89
|
|
|
|
90
|
|
|
$q->where('sekolah_id', 'like', $value) |
91
|
|
|
->orWhere('admin_sekolah_id', 'like', $value); |
92
|
|
|
}); |
93
|
|
|
}else{ |
94
|
|
View Code Duplication |
$query->where(function($q) use($request, $admin_sekolah) { |
|
|
|
|
95
|
|
|
$value = "%{$request->filter}%"; |
96
|
|
|
|
97
|
|
|
$q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
104
|
|
|
|
105
|
|
|
$response = $query->with(['siswa', 'sekolah', 'user'])->paginate($perPage); |
106
|
|
|
|
107
|
|
|
return response()->json($response) |
108
|
|
|
->header('Access-Control-Allow-Origin', '*') |
109
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Display a listing of the resource. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Http\Response |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function get() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$zonas = $this->zona->with(['siswa', 'sekolah', 'user'])->get(); |
120
|
|
|
|
121
|
|
|
$response['zonas'] = $zonas; |
|
|
|
|
122
|
|
|
$response['error'] = false; |
123
|
|
|
$response['message'] = 'Success'; |
124
|
|
|
$response['status'] = true; |
125
|
|
|
|
126
|
|
|
return response()->json($response); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Show the form for creating a new resource. |
131
|
|
|
* |
132
|
|
|
* @return \Illuminate\Http\Response |
133
|
|
|
*/ |
134
|
|
|
public function create() |
135
|
|
|
{ |
136
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
137
|
|
|
$zona = $this->zona->getAttributes(); |
138
|
|
|
//$siswas = $this->siswa->getAttributes(); |
|
|
|
|
139
|
|
|
$sekolahs = $this->sekolah->getAttributes(); |
140
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
141
|
|
|
$users_special = $this->user->all(); |
142
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
143
|
|
|
$current_user = Auth::User(); |
144
|
|
|
|
145
|
|
|
$admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first(); |
146
|
|
|
|
147
|
|
|
if($this->checkRole(['superadministrator'])){ |
148
|
|
|
$siswas = $this->siswa->all(); |
149
|
|
|
}else{ |
150
|
|
|
$sekolah_id = $admin_sekolah->sekolah_id; |
151
|
|
|
$siswas = $this->siswa->where('sekolah_id', $sekolah_id)->get(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
foreach ($siswas as $siswa) { |
155
|
|
|
array_set($siswa, 'label', $siswa->nomor_un.' - '.$siswa->nama_siswa); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach ($sekolahs as $sekolah) { |
159
|
|
|
array_set($sekolah, 'label', $sekolah->nama); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
163
|
|
|
|
164
|
|
View Code Duplication |
if ($role_check) { |
|
|
|
|
165
|
|
|
$user_special = true; |
166
|
|
|
|
167
|
|
|
foreach ($users_special as $user) { |
168
|
|
|
array_set($user, 'label', $user->name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$users = $users_special; |
172
|
|
|
} else { |
173
|
|
|
$user_special = false; |
174
|
|
|
|
175
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
176
|
|
|
|
177
|
|
|
$users = $users_standar; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
array_set($current_user, 'label', $current_user->name); |
181
|
|
|
|
182
|
|
|
$response['zona'] = $zona; |
|
|
|
|
183
|
|
|
$response['siswa'] = $siswas; |
184
|
|
|
$response['sekolahs'] = $sekolahs; |
185
|
|
|
$response['users'] = $users; |
186
|
|
|
$response['user_special'] = $user_special; |
187
|
|
|
$response['current_user'] = $current_user; |
188
|
|
|
$response['error'] = false; |
189
|
|
|
$response['message'] = 'Success'; |
190
|
|
|
$response['status'] = true; |
191
|
|
|
|
192
|
|
|
return response()->json($response); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Store a newly created resource in storage. |
197
|
|
|
* |
198
|
|
|
* @param \Illuminate\Http\Request $request |
199
|
|
|
* @return \Illuminate\Http\Response |
200
|
|
|
*/ |
201
|
|
|
public function store(Request $request) |
202
|
|
|
{ |
203
|
|
|
$zona = $this->zona; |
204
|
|
|
|
205
|
|
|
$validator = Validator::make($request->all(), [ |
206
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,NULL,id,deleted_at,NULL", |
207
|
|
|
// 'sekolah_id' => "required|exists:{$this->sekolah->getTable()},id", |
|
|
|
|
208
|
|
|
// 'zona_siswa' => "required|exists:{$this->city->getTable()},id", |
|
|
|
|
209
|
|
|
// 'zona_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
210
|
|
|
// 'lokasi_siswa' => "required|exists:{$this->district->getTable()},id", |
|
|
|
|
211
|
|
|
// 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
212
|
|
|
// 'nilai' => 'required|numeric', |
|
|
|
|
213
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
214
|
|
|
]); |
215
|
|
|
|
216
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
217
|
|
|
$error = true; |
218
|
|
|
$message = $validator->errors()->first(); |
219
|
|
|
} else { |
220
|
|
|
$nomor_un = $request->input('nomor_un'); |
221
|
|
|
$siswa = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first(); |
222
|
|
|
$zona_siswa = substr($siswa->village_id, 0, 6); |
223
|
|
|
$zona_sekolah = substr($siswa->sekolah->village_id, 0, 6); |
224
|
|
|
$lokasi_siswa = $siswa->village_id; |
225
|
|
|
$lokasi_sekolah = $siswa->sekolah->village_id; |
226
|
|
|
|
227
|
|
|
$zona->nomor_un = $nomor_un; |
228
|
|
|
$zona->sekolah_id = $siswa->sekolah->id; |
229
|
|
|
$zona->zona_siswa = $zona_siswa; |
230
|
|
|
$zona->zona_sekolah = $zona_sekolah; |
231
|
|
|
$zona->lokasi_siswa = $lokasi_siswa; |
232
|
|
|
$zona->lokasi_sekolah = $lokasi_sekolah; |
233
|
|
|
$zona->nilai = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah); |
234
|
|
|
$zona->user_id = $request->input('user_id'); |
235
|
|
|
|
236
|
|
|
$nilai = $this->nilai->updateOrCreate( |
237
|
|
|
[ |
238
|
|
|
'nomor_un' => $zona->nomor_un, |
239
|
|
|
], |
240
|
|
|
[ |
241
|
|
|
'nomor_un' => $zona->nomor_un, |
242
|
|
|
'zona' => $zona->nilai, |
243
|
|
|
'total' => null, |
244
|
|
|
'user_id' => $zona->user_id, |
245
|
|
|
] |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
DB::beginTransaction(); |
249
|
|
|
|
250
|
|
|
if ($zona->save() && $nilai->save()) |
251
|
|
|
{ |
252
|
|
|
DB::commit(); |
253
|
|
|
|
254
|
|
|
$error = false; |
255
|
|
|
$message = 'Success'; |
256
|
|
|
} else { |
257
|
|
|
DB::rollBack(); |
258
|
|
|
|
259
|
|
|
$error = true; |
260
|
|
|
$message = 'Failed'; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$response['zona'] = $zona; |
|
|
|
|
265
|
|
|
$response['error'] = $error; |
266
|
|
|
$response['message'] = $message; |
267
|
|
|
$response['status'] = true; |
268
|
|
|
|
269
|
|
|
return response()->json($response); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Display the specified resource. |
274
|
|
|
* |
275
|
|
|
* @param \App\Zona $zona |
|
|
|
|
276
|
|
|
* @return \Illuminate\Http\Response |
277
|
|
|
*/ |
278
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
$zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id); |
281
|
|
|
|
282
|
|
|
$response['zona'] = $zona; |
|
|
|
|
283
|
|
|
$response['error'] = false; |
284
|
|
|
$response['message'] = 'Success'; |
285
|
|
|
$response['status'] = true; |
286
|
|
|
|
287
|
|
|
return response()->json($response); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Show the form for editing the specified resource. |
292
|
|
|
* |
293
|
|
|
* @param \App\Zona $zona |
|
|
|
|
294
|
|
|
* @return \Illuminate\Http\Response |
295
|
|
|
*/ |
296
|
|
|
public function edit($id) |
297
|
|
|
{ |
298
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
299
|
|
|
$zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id); |
300
|
|
|
$siswas = $this->siswa->getAttributes(); |
301
|
|
|
$sekolahs = $this->sekolah->getAttributes(); |
302
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
303
|
|
|
$users_special = $this->user->all(); |
304
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
305
|
|
|
$current_user = Auth::User(); |
306
|
|
|
|
307
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
308
|
|
|
|
309
|
|
|
if ($zona->user !== null) { |
310
|
|
|
array_set($zona->user, 'label', $zona->user->name); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
View Code Duplication |
if ($role_check) { |
|
|
|
|
314
|
|
|
$user_special = true; |
315
|
|
|
|
316
|
|
|
foreach ($users_special as $user) { |
317
|
|
|
array_set($user, 'label', $user->name); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$users = $users_special; |
321
|
|
|
} else { |
322
|
|
|
$user_special = false; |
323
|
|
|
|
324
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
325
|
|
|
|
326
|
|
|
$users = $users_standar; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
array_set($current_user, 'label', $current_user->name); |
330
|
|
|
|
331
|
|
|
$response['zona'] = $zona; |
|
|
|
|
332
|
|
|
$response['siswas'] = $siswas; |
333
|
|
|
$response['sekolahs'] = $sekolahs; |
334
|
|
|
$response['users'] = $users; |
335
|
|
|
$response['user_special'] = $user_special; |
336
|
|
|
$response['current_user'] = $current_user; |
337
|
|
|
$response['error'] = false; |
338
|
|
|
$response['message'] = 'Success'; |
339
|
|
|
$response['status'] = true; |
340
|
|
|
|
341
|
|
|
return response()->json($response); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Update the specified resource in storage. |
346
|
|
|
* |
347
|
|
|
* @param \Illuminate\Http\Request $request |
348
|
|
|
* @param \App\Zona $zona |
|
|
|
|
349
|
|
|
* @return \Illuminate\Http\Response |
350
|
|
|
*/ |
351
|
|
|
public function update(Request $request, $id) |
352
|
|
|
{ |
353
|
|
|
$zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id); |
354
|
|
|
|
355
|
|
|
$validator = Validator::make($request->all(), [ |
356
|
|
|
// 'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,{$id},id,deleted_at,NULL", |
|
|
|
|
357
|
|
|
// 'sekolah_id' => "required|exists:{$this->sekolah->getTable()},id", |
|
|
|
|
358
|
|
|
// 'zona_siswa' => "required|exists:{$this->city->getTable()},id", |
|
|
|
|
359
|
|
|
// 'zona_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
360
|
|
|
// 'lokasi_siswa' => "required|exists:{$this->district->getTable()},id", |
|
|
|
|
361
|
|
|
// 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id", |
|
|
|
|
362
|
|
|
// 'nilai' => 'required|numeric', |
|
|
|
|
363
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
364
|
|
|
]); |
365
|
|
|
|
366
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
367
|
|
|
$error = true; |
368
|
|
|
$message = $validator->errors()->first(); |
369
|
|
|
} else { |
370
|
|
|
$nomor_un = $zona->nomor_un; // $request->input('nomor_un'); |
|
|
|
|
371
|
|
|
$siswa = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first(); |
372
|
|
|
$zona_siswa = substr($siswa->village_id, 0, 6); |
373
|
|
|
$zona_sekolah = substr($siswa->sekolah->village_id, 0, 6); |
374
|
|
|
$lokasi_siswa = $siswa->village_id; |
375
|
|
|
$lokasi_sekolah = $siswa->sekolah->village_id; |
376
|
|
|
|
377
|
|
|
$zona->nomor_un = $nomor_un; |
378
|
|
|
$zona->sekolah_id = $siswa->sekolah->id; |
379
|
|
|
$zona->zona_siswa = $zona_siswa; |
380
|
|
|
$zona->zona_sekolah = $zona_sekolah; |
381
|
|
|
$zona->lokasi_siswa = $lokasi_siswa; |
382
|
|
|
$zona->lokasi_sekolah = $lokasi_sekolah; |
383
|
|
|
$zona->nilai = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah); |
384
|
|
|
$zona->user_id = $request->input('user_id'); |
385
|
|
|
|
386
|
|
|
$nilai = $this->nilai->updateOrCreate( |
387
|
|
|
[ |
388
|
|
|
'nomor_un' => $zona->nomor_un, |
389
|
|
|
], |
390
|
|
|
[ |
391
|
|
|
'nomor_un' => $zona->nomor_un, |
392
|
|
|
'zona' => $zona->nilai, |
393
|
|
|
'total' => null, |
394
|
|
|
'user_id' => $zona->user_id, |
395
|
|
|
] |
396
|
|
|
); |
397
|
|
|
|
398
|
|
|
DB::beginTransaction(); |
399
|
|
|
|
400
|
|
|
if ($zona->save() && $nilai->save()) |
401
|
|
|
{ |
402
|
|
|
DB::commit(); |
403
|
|
|
|
404
|
|
|
$error = false; |
405
|
|
|
$message = 'Success'; |
406
|
|
|
} else { |
407
|
|
|
DB::rollBack(); |
408
|
|
|
|
409
|
|
|
$error = true; |
410
|
|
|
$message = 'Failed'; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
$response['zona'] = $zona; |
|
|
|
|
415
|
|
|
$response['error'] = $error; |
416
|
|
|
$response['message'] = $message; |
417
|
|
|
$response['status'] = true; |
418
|
|
|
|
419
|
|
|
return response()->json($response); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Remove the specified resource from storage. |
424
|
|
|
* |
425
|
|
|
* @param \App\Zona $zona |
|
|
|
|
426
|
|
|
* @return \Illuminate\Http\Response |
427
|
|
|
*/ |
428
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
429
|
|
|
{ |
430
|
|
|
$zona = $this->zona->findOrFail($id); |
431
|
|
|
|
432
|
|
|
if ($zona->delete()) { |
433
|
|
|
$response['message'] = 'Success'; |
|
|
|
|
434
|
|
|
$response['success'] = true; |
435
|
|
|
$response['status'] = true; |
436
|
|
|
} else { |
437
|
|
|
$response['message'] = 'Failed'; |
|
|
|
|
438
|
|
|
$response['success'] = false; |
439
|
|
|
$response['status'] = false; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return json_encode($response); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
protected function checkRole($role = array()) |
446
|
|
|
{ |
447
|
|
|
return Auth::user()->hasRole($role); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
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.