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