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