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