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