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->orderBy($sortCol, $sortDir); |
55
|
|
|
} else { |
56
|
|
|
$query = $this->pendaftaran->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
|
|
|
foreach($response as $kegiatan){ |
71
|
|
|
array_set($response->data, 'kegiatan', $kegiatan->kegiatan->label); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach($response as $user){ |
75
|
|
|
array_set($response->data, 'user', $user->user->name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return response()->json($response) |
79
|
|
|
->header('Access-Control-Allow-Origin', '*') |
80
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Show the form for creating a new resource. |
85
|
|
|
* |
86
|
|
|
* @return \Illuminate\Http\Response |
87
|
|
|
*/ |
88
|
|
|
public function create() |
89
|
|
|
{ |
90
|
|
|
$kegiatan = $this->kegiatanModel->all(); |
91
|
|
|
$users_special = $this->user->all(); |
92
|
|
|
$users_standar = $this->user->find(\Auth::User()->id); |
93
|
|
|
|
94
|
|
|
$role_check = \Auth::User()->hasRole(['superadministrator','administrator']); |
95
|
|
|
|
96
|
|
|
if($role_check){ |
97
|
|
|
$response['user_special'] = true; |
|
|
|
|
98
|
|
|
foreach($users_special as $user){ |
99
|
|
|
array_set($user, 'label', $user->name); |
100
|
|
|
} |
101
|
|
|
$response['user'] = $users_special; |
102
|
|
|
}else{ |
103
|
|
|
$response['user_special'] = false; |
|
|
|
|
104
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
105
|
|
|
$response['user'] = $users_standar; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$response['kegiatan'] = $kegiatan; |
109
|
|
|
$response['status'] = true; |
110
|
|
|
|
111
|
|
|
return response()->json($response); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Display the specified resource. |
116
|
|
|
* |
117
|
|
|
* @param \App\Pendaftaran $pendaftaran |
|
|
|
|
118
|
|
|
* @return \Illuminate\Http\Response |
119
|
|
|
*/ |
120
|
|
|
public function store(Request $request) |
121
|
|
|
{ |
122
|
|
|
$pendaftaran = $this->pendaftaran; |
123
|
|
|
|
124
|
|
|
$validator = Validator::make($request->all(), [ |
125
|
|
|
'kegiatan_id' => 'required', |
126
|
|
|
'user_id' => 'required', |
127
|
|
|
'label' => 'required|max:16|unique:pendaftarans,label', |
128
|
|
|
'description' => 'max:255', |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
if($validator->fails()){ |
132
|
|
|
$check = $pendaftaran->where('label',$request->label)->whereNull('deleted_at')->count(); |
133
|
|
|
|
134
|
|
|
if ($check > 0) { |
135
|
|
|
$response['message'] = 'Failed, label ' . $request->label . ' already exists'; |
|
|
|
|
136
|
|
|
} else { |
137
|
|
|
// $pendaftaran->kegiatan_id = $request->input('kegiatan_id'); |
|
|
|
|
138
|
|
|
// $pendaftaran->user_id = $request->input('user_id'); |
|
|
|
|
139
|
|
|
// $pendaftaran->label = $request->input('label'); |
|
|
|
|
140
|
|
|
// $pendaftaran->description = $request->input('description'); |
|
|
|
|
141
|
|
|
$this->insertWithWorkflow($pendaftaran, $request->all()); |
142
|
|
|
//$pendaftaran->save(); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$response['message'] = 'success'; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
// $pendaftaran->kegiatan_id = $request->input('kegiatan_id'); |
|
|
|
|
148
|
|
|
// $pendaftaran->user_id = $request->input('user_id'); |
|
|
|
|
149
|
|
|
// $pendaftaran->label = $request->input('label'); |
|
|
|
|
150
|
|
|
// $pendaftaran->description = $request->input('description'); |
|
|
|
|
151
|
|
|
$this->insertWithWorkflow($pendaftaran, $request->all()); |
152
|
|
|
//$pendaftaran->save(); |
|
|
|
|
153
|
|
|
$response['message'] = 'success'; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
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 show($id) |
168
|
|
|
{ |
169
|
|
|
$pendaftaran = $this->pendaftaran->findOrFail($id); |
170
|
|
|
|
171
|
|
|
$response['pendaftaran'] = $pendaftaran; |
|
|
|
|
172
|
|
|
$response['kegiatan'] = $pendaftaran->kegiatan; |
173
|
|
|
$response['user'] = $pendaftaran->user; |
174
|
|
|
$response['status'] = true; |
175
|
|
|
|
176
|
|
|
return response()->json($response); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Show the form for editing the specified resource. |
181
|
|
|
* |
182
|
|
|
* @param \App\Pendaftaran $pendaftaran |
|
|
|
|
183
|
|
|
* @return \Illuminate\Http\Response |
184
|
|
|
*/ |
185
|
|
|
public function edit($id) |
186
|
|
|
{ |
187
|
|
|
$pendaftaran = $this->pendaftaran->findOrFail($id); |
188
|
|
|
|
189
|
|
|
array_set($pendaftaran->user, 'label', $pendaftaran->user->name); |
190
|
|
|
|
191
|
|
|
$response['pendaftaran'] = $pendaftaran; |
|
|
|
|
192
|
|
|
$response['kegiatan'] = $pendaftaran->kegiatan; |
193
|
|
|
$response['user'] = $pendaftaran->user; |
194
|
|
|
$response['status'] = true; |
195
|
|
|
|
196
|
|
|
return response()->json($response); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Update the specified resource in storage. |
201
|
|
|
* |
202
|
|
|
* @param \Illuminate\Http\Request $request |
203
|
|
|
* @param \App\Pendaftaran $pendaftaran |
|
|
|
|
204
|
|
|
* @return \Illuminate\Http\Response |
205
|
|
|
*/ |
206
|
|
|
public function update(Request $request, $id) |
207
|
|
|
{ |
208
|
|
|
$pendaftaran = $this->pendaftaran->findOrFail($id); |
209
|
|
|
|
210
|
|
|
if ($request->input('old_label') == $request->input('label')) |
211
|
|
|
{ |
212
|
|
|
$validator = Validator::make($request->all(), [ |
213
|
|
|
'label' => 'required|max:16', |
214
|
|
|
'description' => 'max:255', |
215
|
|
|
'kegiatan_id' => 'required', |
216
|
|
|
'user_id' => 'required', |
217
|
|
|
]); |
218
|
|
|
} else { |
219
|
|
|
$validator = Validator::make($request->all(), [ |
220
|
|
|
'label' => 'required|max:16|unique:pendaftarans,label', |
221
|
|
|
'description' => 'max:255', |
222
|
|
|
'kegiatan_id' => 'required', |
223
|
|
|
'user_id' => 'required', |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ($validator->fails()) { |
228
|
|
|
$check = $pendaftaran->where('label',$request->label)->whereNull('deleted_at')->count(); |
229
|
|
|
|
230
|
|
|
if ($check > 0) { |
231
|
|
|
$response['message'] = 'Failed, label ' . $request->label . ' already exists'; |
|
|
|
|
232
|
|
View Code Duplication |
} else { |
|
|
|
|
233
|
|
|
// $pendaftaran->label = $request->input('label'); |
|
|
|
|
234
|
|
|
// $pendaftaran->description = $request->input('description'); |
|
|
|
|
235
|
|
|
// $pendaftaran->kegiatan_id = $request->input('kegiatan_id'); |
|
|
|
|
236
|
|
|
// $pendaftaran->user_id = $request->input('user_id'); |
|
|
|
|
237
|
|
|
// $pendaftaran->save(); |
|
|
|
|
238
|
|
|
$update = $this->updateWithWorkflow($pendaftaran, $id, $request->all()); |
239
|
|
|
|
240
|
|
|
// $response['message'] = 'success'; |
|
|
|
|
241
|
|
|
$response['message'] = $update['message']; |
|
|
|
|
242
|
|
|
} |
243
|
|
View Code Duplication |
} 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
|
|
|
$update = $this->updateWithWorkflow($pendaftaran, $id, $request->all()); |
250
|
|
|
|
251
|
|
|
// $response['message'] = 'success'; |
|
|
|
|
252
|
|
|
$response['message'] = $update['message']; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$response['status'] = true; |
256
|
|
|
|
257
|
|
|
return response()->json($response); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Remove the specified resource from storage. |
262
|
|
|
* |
263
|
|
|
* @param \App\Pendaftaran $pendaftaran |
|
|
|
|
264
|
|
|
* @return \Illuminate\Http\Response |
265
|
|
|
*/ |
266
|
|
|
public function destroy($id) |
267
|
|
|
{ |
268
|
|
|
$pendaftaran = $this->pendaftaran->findOrFail($id); |
269
|
|
|
|
270
|
|
|
if ($pendaftaran->delete()) { |
271
|
|
|
$response['status'] = true; |
|
|
|
|
272
|
|
|
} else { |
273
|
|
|
$response['status'] = false; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return json_encode($response); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|