1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\ProgramKeahlian\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\ProgramKeahlian\Facades\ProgramKeahlianFacade; |
9
|
|
|
use App\User; |
10
|
|
|
|
11
|
|
|
/* Models */ |
12
|
|
|
use Bantenprov\ProgramKeahlian\Models\Bantenprov\ProgramKeahlian\ProgramKeahlian; |
13
|
|
|
|
14
|
|
|
/* Etc */ |
15
|
|
|
use Validator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The ProgramKeahlianController class. |
19
|
|
|
* |
20
|
|
|
* @package Bantenprov\ProgramKeahlian |
21
|
|
|
* @author bantenprov <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ProgramKeahlianController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Create a new controller instance. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
protected $user; |
31
|
|
|
public function __construct(ProgramKeahlian $program_keahlian, User $user) |
32
|
|
|
{ |
33
|
|
|
$this->program_keahlian = $program_keahlian; |
34
|
|
|
$this->user = $user; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Display a listing of the resource. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Http\Response |
41
|
|
|
*/ |
42
|
|
|
public function index(Request $request) |
43
|
|
|
{ |
44
|
|
|
if ($request->has('sort')) { |
45
|
|
|
list($sortCol, $sortDir) = explode('|', $request->sort); |
46
|
|
|
|
47
|
|
|
$query = $this->program_keahlian->with('user')->orderBy($sortCol, $sortDir); |
48
|
|
|
} else { |
49
|
|
|
$query = $this->program_keahlian->with('user')->orderBy('id', 'asc'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($request->exists('filter')) { |
53
|
|
View Code Duplication |
$query->where(function($q) use($request) { |
|
|
|
|
54
|
|
|
$value = "%{$request->filter}%"; |
55
|
|
|
$q->where('label', 'like', $value) |
56
|
|
|
->orWhere('keterangan', 'like', $value); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$perPage = $request->has('per_page') ? (int) $request->per_page : null; |
62
|
|
|
$response = $query->paginate($perPage); |
63
|
|
|
|
64
|
|
|
return response()->json($response) |
65
|
|
|
->header('Access-Control-Allow-Origin', '*') |
66
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Show the form for creating a new resource. |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Http\Response |
73
|
|
|
*/ |
74
|
|
|
public function create() |
75
|
|
|
{ |
76
|
|
|
$response = []; |
77
|
|
|
|
78
|
|
|
$users_special = $this->user->all(); |
79
|
|
|
$users_standar = $this->user->find(\Auth::User()->id); |
80
|
|
|
$current_user = \Auth::User(); |
81
|
|
|
|
82
|
|
|
$role_check = \Auth::User()->hasRole(['superadministrator','administrator']); |
83
|
|
|
|
84
|
|
|
if($role_check){ |
85
|
|
|
$response['user_special'] = true; |
86
|
|
|
foreach($users_special as $user){ |
87
|
|
|
array_set($user, 'label', $user->name); |
88
|
|
|
} |
89
|
|
|
$response['user'] = $users_special; |
90
|
|
|
}else{ |
91
|
|
|
$response['user_special'] = false; |
92
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
93
|
|
|
$response['user'] = $users_standar; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
array_set($current_user, 'label', $current_user->name); |
97
|
|
|
|
98
|
|
|
$response['current_user'] = $current_user; |
99
|
|
|
$response['status'] = true; |
100
|
|
|
|
101
|
|
|
return response()->json($response); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Display the specified resource. |
106
|
|
|
* |
107
|
|
|
* @param \App\ProgramKeahlian $program_keahlian |
|
|
|
|
108
|
|
|
* @return \Illuminate\Http\Response |
109
|
|
|
*/ |
110
|
|
|
public function store(Request $request) |
111
|
|
|
{ |
112
|
|
|
$program_keahlian = $this->program_keahlian; |
113
|
|
|
|
114
|
|
|
$validator = Validator::make($request->all(), [ |
115
|
|
|
'label' => 'required', |
116
|
|
|
'keterangan' => 'required', |
117
|
|
|
'user_id' => 'required|unique:sekolahs,user_id', |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
View Code Duplication |
if($validator->fails()){ |
|
|
|
|
121
|
|
|
$check = $program_keahlian->where('user_id',$request->user_id)->whereNull('deleted_at')->count(); |
122
|
|
|
|
123
|
|
|
if ($check > 0) { |
124
|
|
|
$response['message'] = 'Failed' . $request->user_id . ' already exists'; |
|
|
|
|
125
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
$program_keahlian->label = $request->input('label'); |
128
|
|
|
$program_keahlian->keterangan = $request->input('keterangan'); |
129
|
|
|
$program_keahlian->user_id = $request->input('user_id'); |
130
|
|
|
$program_keahlian->save(); |
131
|
|
|
|
132
|
|
|
$response['message'] = 'success'; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$program_keahlian->label = $request->input('label'); |
136
|
|
|
$program_keahlian->keterangan = $request->input('keterangan'); |
137
|
|
|
$program_keahlian->user_id = $request->input('user_id'); |
138
|
|
|
$program_keahlian->save(); |
139
|
|
|
|
140
|
|
|
$response['message'] = 'success'; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$response['status'] = true; |
144
|
|
|
|
145
|
|
|
return response()->json($response); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Store a newly created resource in storage. |
150
|
|
|
* |
151
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
152
|
|
|
* @return \Illuminate\Http\Response |
153
|
|
|
*/ |
154
|
|
|
public function show($id) |
155
|
|
|
{ |
156
|
|
|
$program_keahlian = $this->program_keahlian->findOrFail($id); |
157
|
|
|
|
158
|
|
|
array_set($program_keahlian, 'user', $program_keahlian->user->name); |
159
|
|
|
|
160
|
|
|
$response['program_keahlian'] = $program_keahlian; |
|
|
|
|
161
|
|
|
$response['status'] = true; |
162
|
|
|
|
163
|
|
|
return response()->json($response); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Show the form for editing the specified resource. |
168
|
|
|
* |
169
|
|
|
* @param \App\ProgramKeahlian $program_keahlian |
|
|
|
|
170
|
|
|
* @return \Illuminate\Http\Response |
171
|
|
|
*/ |
172
|
|
|
public function edit($id) |
173
|
|
|
{ |
174
|
|
|
$program_keahlian = $this->program_keahlian->findOrFail($id); |
175
|
|
|
|
176
|
|
|
array_set($program_keahlian->user, 'label', $program_keahlian->user->name); |
177
|
|
|
|
178
|
|
|
$response['program_keahlian'] = $program_keahlian; |
|
|
|
|
179
|
|
|
$response['user'] = $program_keahlian->user; |
180
|
|
|
$response['status'] = true; |
181
|
|
|
|
182
|
|
|
return response()->json($response); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Update the specified resource in storage. |
187
|
|
|
* |
188
|
|
|
* @param \Illuminate\Http\Request $request |
189
|
|
|
* @param \App\ProgramKeahlian $program_keahlian |
|
|
|
|
190
|
|
|
* @return \Illuminate\Http\Response |
191
|
|
|
*/ |
192
|
|
|
public function update(Request $request, $id) |
193
|
|
|
{ |
194
|
|
|
$program_keahlian = $this->program_keahlian->findOrFail($id); |
195
|
|
|
|
196
|
|
|
if ($request->input('old_user_id') == $request->input('user_id')) |
197
|
|
|
{ |
198
|
|
|
$validator = Validator::make($request->all(), [ |
199
|
|
|
'label' => 'required', |
200
|
|
|
'user_id' => 'required', |
201
|
|
|
'keterangan' => 'required', |
202
|
|
|
|
203
|
|
|
]); |
204
|
|
|
} else { |
205
|
|
|
$validator = Validator::make($request->all(), [ |
206
|
|
|
'label' => 'required', |
207
|
|
|
'keterangan' => 'required', |
208
|
|
|
'user_id' => 'required|unique:program_keahlians,user_id', |
209
|
|
|
]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
213
|
|
|
$check = $program_keahlian->where('user_id',$request->user_id)->whereNull('deleted_at')->count(); |
214
|
|
|
|
215
|
|
|
if ($check > 0) { |
216
|
|
|
$response['message'] = 'Failed,Username ' . $request->user_id . ' already exists'; |
|
|
|
|
217
|
|
|
} else { |
218
|
|
|
$program_keahlian->label = $request->input('label'); |
219
|
|
|
$program_keahlian->user_id = $request->input('user_id'); |
220
|
|
|
$program_keahlian->keterangan = $request->input('keterangan'); |
221
|
|
|
$program_keahlian->save(); |
222
|
|
|
|
223
|
|
|
$response['message'] = 'success'; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
} else { |
226
|
|
|
$program_keahlian->label = $request->input('label'); |
227
|
|
|
$program_keahlian->user_id = $request->input('user_id'); |
228
|
|
|
$program_keahlian->keterangan = $request->input('keterangan'); |
229
|
|
|
$program_keahlian->save(); |
230
|
|
|
|
231
|
|
|
$response['message'] = 'success'; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$response['status'] = true; |
235
|
|
|
|
236
|
|
|
return response()->json($response); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Remove the specified resource from storage. |
241
|
|
|
* |
242
|
|
|
* @param \App\ProgramKeahlian $program_keahlian |
|
|
|
|
243
|
|
|
* @return \Illuminate\Http\Response |
244
|
|
|
*/ |
245
|
|
|
public function destroy($id) |
246
|
|
|
{ |
247
|
|
|
$program_keahlian = $this->program_keahlian->findOrFail($id); |
248
|
|
|
|
249
|
|
|
if ($program_keahlian->delete()) { |
250
|
|
|
$response['status'] = true; |
|
|
|
|
251
|
|
|
} else { |
252
|
|
|
$response['status'] = false; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return json_encode($response); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
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.