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