1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\OrangTua\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\OrangTua\Facades\OrangTuaFacade; |
9
|
|
|
|
10
|
|
|
/* Models */ |
11
|
|
|
use Bantenprov\OrangTua\Models\Bantenprov\OrangTua\OrangTua; |
12
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
13
|
|
|
use App\User; |
14
|
|
|
|
15
|
|
|
/* Etc */ |
16
|
|
|
use Validator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The OrangTuaController class. |
20
|
|
|
* |
21
|
|
|
* @package Bantenprov\OrangTua |
22
|
|
|
* @author bantenprov <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class OrangTuaController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Create a new controller instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
protected $orang_tua; |
33
|
|
|
protected $siswa; |
34
|
|
|
protected $user; |
35
|
|
|
|
36
|
|
|
public function __construct(OrangTua $orang_tua, User $user, Siswa $siswa) |
37
|
|
|
{ |
38
|
|
|
$this->orang_tua = $orang_tua; |
39
|
|
|
$this->siswa = $siswa; |
40
|
|
|
$this->user = $user; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Display a listing of the resource. |
45
|
|
|
* |
46
|
|
|
* @return \Illuminate\Http\Response |
47
|
|
|
*/ |
48
|
|
|
public function index(Request $request) |
49
|
|
|
{ |
50
|
|
|
if ($request->has('sort')) { |
51
|
|
|
list($sortCol, $sortDir) = explode('|', $request->sort); |
52
|
|
|
|
53
|
|
|
$query = $this->orang_tua->orderBy($sortCol, $sortDir); |
54
|
|
|
} else { |
55
|
|
|
$query = $this->orang_tua->orderBy('id', 'asc'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($request->exists('filter')) { |
59
|
|
|
$query->where(function($q) use($request) { |
60
|
|
|
$value = "%{$request->filter}%"; |
61
|
|
|
$q->where('id', 'like', $value) |
62
|
|
|
->orWhere('nama_ayah', 'like', $value); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
67
|
|
|
$response = $query->with('user')->with('siswa')->paginate($perPage); |
68
|
|
|
|
69
|
|
|
return response()->json($response) |
70
|
|
|
->header('Access-Control-Allow-Origin', '*') |
71
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Show the form for creating a new resource. |
76
|
|
|
* |
77
|
|
|
* @return \Illuminate\Http\Response |
78
|
|
|
*/ |
79
|
|
|
public function create() |
80
|
|
|
{ |
81
|
|
|
$response = []; |
82
|
|
|
$siswas = $this->siswa->all(); |
83
|
|
|
$users_special = $this->user->all(); |
84
|
|
|
$users_standar = $this->user->find(\Auth::User()->id); |
85
|
|
|
$current_user = \Auth::User(); |
86
|
|
|
|
87
|
|
|
$role_check = \Auth::User()->hasRole(['superadministrator','administrator']); |
88
|
|
|
|
89
|
|
|
if($role_check){ |
90
|
|
|
$response['user_special'] = true; |
91
|
|
|
foreach($users_special as $user){ |
92
|
|
|
array_set($user, 'label', $user->name); |
93
|
|
|
} |
94
|
|
|
$response['user'] = $users_special; |
95
|
|
|
}else{ |
96
|
|
|
$response['user_special'] = false; |
97
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
98
|
|
|
$response['user'] = $users_standar; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
array_set($current_user, 'label', $current_user->name); |
102
|
|
|
|
103
|
|
|
foreach($siswas as $siswa){ |
104
|
|
|
array_set($siswa, 'label', $siswa->nama_siswa); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$response['current_user'] = $current_user; |
108
|
|
|
$response['siswa'] = $siswas; |
109
|
|
|
$response['status'] = true; |
110
|
|
|
|
111
|
|
|
return response()->json($response); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Display the specified resource. |
116
|
|
|
* |
117
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
118
|
|
|
* @return \Illuminate\Http\Response |
119
|
|
|
*/ |
120
|
|
|
public function store(Request $request) |
121
|
|
|
{ |
122
|
|
|
$orang_tua = $this->orang_tua; |
123
|
|
|
|
124
|
|
|
$validator = Validator::make($request->all(), [ |
125
|
|
|
'user_id' => 'required|unique:orangtuas,user_id', |
126
|
|
|
'nomor_un' => 'required|unique:orangtuas,nomor_un', |
127
|
|
|
'no_telp' => 'required|unique:orangtuas,no_telp', |
128
|
|
|
'nama_ayah' => 'required', |
129
|
|
|
'nama_ibu' => 'required', |
130
|
|
|
'pendidikan_ayah' => 'required', |
131
|
|
|
'kerja_ayah' => 'required', |
132
|
|
|
'pendidikan_ibu' => 'required', |
133
|
|
|
'kerja_ibu' => 'required', |
134
|
|
|
'alamat_ortu' => 'required', |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
if($validator->fails()){ |
138
|
|
|
$check = $orang_tua->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->orWhere('no_telp',$request->no_telp)->whereNull('deleted_at')->count(); |
139
|
|
|
|
140
|
|
|
if ($check > 0) { |
141
|
|
|
$response['message'] = 'Failed ! Username, Nama Siswa, Nomor Telp already exists'; |
|
|
|
|
142
|
|
View Code Duplication |
} else { |
|
|
|
|
143
|
|
|
$orang_tua->user_id = $request->input('user_id'); |
144
|
|
|
$orang_tua->nomor_un = $request->input('nomor_un'); |
145
|
|
|
$orang_tua->no_telp = $request->input('no_telp'); |
146
|
|
|
$orang_tua->nama_ayah = $request->input('nama_ayah'); |
147
|
|
|
$orang_tua->nama_ibu = $request->input('nama_ibu'); |
148
|
|
|
$orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
149
|
|
|
$orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
150
|
|
|
$orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
151
|
|
|
$orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
152
|
|
|
$orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
153
|
|
|
$orang_tua->save(); |
154
|
|
|
|
155
|
|
|
$response['message'] = 'success'; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
} else { |
|
|
|
|
159
|
|
|
$orang_tua->user_id = $request->input('user_id'); |
160
|
|
|
$orang_tua->nomor_un = $request->input('nomor_un'); |
161
|
|
|
$orang_tua->no_telp = $request->input('no_telp'); |
162
|
|
|
$orang_tua->nama_ayah = $request->input('nama_ayah'); |
163
|
|
|
$orang_tua->nama_ibu = $request->input('nama_ibu'); |
164
|
|
|
$orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
165
|
|
|
$orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
166
|
|
|
$orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
167
|
|
|
$orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
168
|
|
|
$orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
169
|
|
|
$orang_tua->save(); |
170
|
|
|
$response['message'] = 'success'; |
|
|
|
|
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$response['status'] = true; |
175
|
|
|
|
176
|
|
|
return response()->json($response); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Store a newly created resource in storage. |
181
|
|
|
* |
182
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
183
|
|
|
* @return \Illuminate\Http\Response |
184
|
|
|
*/ |
185
|
|
|
public function show($id) |
186
|
|
|
{ |
187
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
188
|
|
|
|
189
|
|
|
$response['user'] = $orang_tua->user; |
|
|
|
|
190
|
|
|
$response['siswa'] = $orang_tua->siswa; |
191
|
|
|
$response['orang_tua'] = $orang_tua; |
192
|
|
|
$response['status'] = true; |
193
|
|
|
|
194
|
|
|
return response()->json($response); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Show the form for editing the specified resource. |
199
|
|
|
* |
200
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
201
|
|
|
* @return \Illuminate\Http\Response |
202
|
|
|
*/ |
203
|
|
|
public function edit($id) |
204
|
|
|
{ |
205
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
206
|
|
|
|
207
|
|
|
array_set($orang_tua->user, 'label', $orang_tua->user->name); |
208
|
|
|
array_set($orang_tua->siswa, 'label', $orang_tua->siswa->nama_siswa); |
209
|
|
|
|
210
|
|
|
$response['user'] = $orang_tua->user; |
|
|
|
|
211
|
|
|
$response['siswa'] = $orang_tua->siswa; |
212
|
|
|
$response['orang_tua'] = $orang_tua; |
213
|
|
|
$response['status'] = true; |
214
|
|
|
|
215
|
|
|
return response()->json($response); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Update the specified resource in storage. |
220
|
|
|
* |
221
|
|
|
* @param \Illuminate\Http\Request $request |
222
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
223
|
|
|
* @return \Illuminate\Http\Response |
224
|
|
|
*/ |
225
|
|
|
public function update(Request $request, $id) |
226
|
|
|
{ |
227
|
|
|
$response = array(); |
228
|
|
|
$message = array(); |
229
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
230
|
|
|
|
231
|
|
|
$validator = Validator::make($request->all(), [ |
232
|
|
|
'user_id' => 'required|unique:orangtuas,user_id,'.$id, |
233
|
|
|
'nomor_un' => 'required|unique:orangtuas,nomor_un,'.$id, |
234
|
|
|
'no_telp' => 'required|unique:orangtuas,no_telp,'.$id, |
235
|
|
|
'nama_ayah' => 'required', |
236
|
|
|
'nama_ibu' => 'required', |
237
|
|
|
'pendidikan_ayah' => 'required', |
238
|
|
|
'kerja_ayah' => 'required', |
239
|
|
|
'pendidikan_ibu' => 'required', |
240
|
|
|
'kerja_ibu' => 'required', |
241
|
|
|
'alamat_ortu' => 'required', |
242
|
|
|
]); |
243
|
|
|
|
244
|
|
|
if ($validator->fails()) { |
245
|
|
|
|
246
|
|
|
foreach($validator->messages()->getMessages() as $key => $error){ |
247
|
|
|
foreach($error AS $error_get) { |
248
|
|
|
array_push($message, $error_get); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$check_user = $this->orang_tua->where('id','!=', $id)->where('user_id', $request->user_id); |
253
|
|
|
$check_siswa = $this->orang_tua->where('id','!=', $id)->where('nomor_un', $request->nomor_un); |
254
|
|
|
$check_no_telp = $this->orang_tua->where('id','!=', $id)->where('no_telp', $request->no_telp); |
255
|
|
|
|
256
|
|
|
if($check_user->count() > 0 || $check_siswa->count() > 0 || $check_no_telp->count() > 0){ |
257
|
|
|
$response['message'] = implode("\n",$message); |
258
|
|
View Code Duplication |
} else { |
|
|
|
|
259
|
|
|
$orang_tua->user_id = $request->input('user_id'); |
260
|
|
|
$orang_tua->nomor_un = $request->input('nomor_un'); |
261
|
|
|
$orang_tua->no_telp = $request->input('no_telp'); |
262
|
|
|
$orang_tua->nama_ayah = $request->input('nama_ayah'); |
263
|
|
|
$orang_tua->nama_ibu = $request->input('nama_ibu'); |
264
|
|
|
$orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
265
|
|
|
$orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
266
|
|
|
$orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
267
|
|
|
$orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
268
|
|
|
$orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
269
|
|
|
$orang_tua->save(); |
270
|
|
|
|
271
|
|
|
$response['message'] = 'success'; |
272
|
|
|
|
273
|
|
|
} |
274
|
|
View Code Duplication |
} else { |
|
|
|
|
275
|
|
|
$orang_tua->user_id = $request->input('user_id'); |
276
|
|
|
$orang_tua->nomor_un = $request->input('nomor_un'); |
277
|
|
|
$orang_tua->no_telp = $request->input('no_telp'); |
278
|
|
|
$orang_tua->nama_ayah = $request->input('nama_ayah'); |
279
|
|
|
$orang_tua->nama_ibu = $request->input('nama_ibu'); |
280
|
|
|
$orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
281
|
|
|
$orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
282
|
|
|
$orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
283
|
|
|
$orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
284
|
|
|
$orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
285
|
|
|
$orang_tua->save(); |
286
|
|
|
|
287
|
|
|
$response['message'] = 'success'; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$response['status'] = true; |
291
|
|
|
|
292
|
|
|
return response()->json($response); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Remove the specified resource from storage. |
297
|
|
|
* |
298
|
|
|
* @param \App\OrangTua $orang_tua |
|
|
|
|
299
|
|
|
* @return \Illuminate\Http\Response |
300
|
|
|
*/ |
301
|
|
|
public function destroy($id) |
302
|
|
|
{ |
303
|
|
|
$orang_tua = $this->orang_tua->findOrFail($id); |
304
|
|
|
|
305
|
|
|
if ($orang_tua->delete()) { |
306
|
|
|
$response['status'] = true; |
|
|
|
|
307
|
|
|
} else { |
308
|
|
|
$response['status'] = false; |
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return json_encode($response); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.