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