1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\AngkaMelekHuruf\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\AngkaMelekHuruf\Facades\AngkaMelekHurufFacade; |
9
|
|
|
|
10
|
|
|
/* Models */ |
11
|
|
|
use Bantenprov\AngkaMelekHuruf\Models\Bantenprov\AngkaMelekHuruf\AngkaMelekHuruf; |
12
|
|
|
|
13
|
|
|
/* Etc */ |
14
|
|
|
use Validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The AngkaMelekHurufController class. |
18
|
|
|
* |
19
|
|
|
* @package Bantenprov\AngkaMelekHuruf |
20
|
|
|
* @author bantenprov <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class AngkaMelekHurufController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Create a new controller instance. |
26
|
|
|
* |
27
|
|
|
* @return void |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
public function __construct(AngkaMelekHuruf $angka_melek_huruf) |
30
|
|
|
{ |
31
|
|
|
$this->angka_melek_huruf = $angka_melek_huruf; |
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->angka_melek_huruf->orderBy($sortCol, $sortDir); |
45
|
|
|
} else { |
46
|
|
|
$query = $this->angka_melek_huruf->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
|
|
|
$angka_melek_huruf = $this->angka_melek_huruf; |
73
|
|
|
|
74
|
|
|
$response['angka_melek_huruf'] = $angka_melek_huruf; |
|
|
|
|
75
|
|
|
$response['status'] = true; |
76
|
|
|
|
77
|
|
|
return response()->json($angka_melek_huruf); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Display the specified resource. |
82
|
|
|
* |
83
|
|
|
* @param \App\AngkaMelekHuruf $angka_melek_huruf |
|
|
|
|
84
|
|
|
* @return \Illuminate\Http\Response |
85
|
|
|
*/ |
86
|
|
|
public function store(Request $request) |
87
|
|
|
{ |
88
|
|
|
$angka_melek_huruf = $this->angka_melek_huruf; |
89
|
|
|
|
90
|
|
|
$validator = Validator::make($request->all(), [ |
91
|
|
|
'label' => 'required|max:16|unique:angka_melek_hurufs,label', |
92
|
|
|
'description' => 'max:255', |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
View Code Duplication |
if($validator->fails()){ |
|
|
|
|
96
|
|
|
$check = $angka_melek_huruf->where('label',$request->label)->whereNull('deleted_at')->count(); |
97
|
|
|
|
98
|
|
|
if ($check > 0) { |
99
|
|
|
$response['message'] = 'Failed, label ' . $request->label . ' already exists'; |
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
$angka_melek_huruf->label = $request->input('label'); |
102
|
|
|
$angka_melek_huruf->description = $request->input('description'); |
103
|
|
|
$angka_melek_huruf->save(); |
104
|
|
|
|
105
|
|
|
$response['message'] = 'success'; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$angka_melek_huruf->label = $request->input('label'); |
109
|
|
|
$angka_melek_huruf->description = $request->input('description'); |
110
|
|
|
$angka_melek_huruf->save(); |
111
|
|
|
|
112
|
|
|
$response['message'] = 'success'; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$response['status'] = true; |
116
|
|
|
|
117
|
|
|
return response()->json($response); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Store a newly created resource in storage. |
122
|
|
|
* |
123
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
124
|
|
|
* @return \Illuminate\Http\Response |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id); |
129
|
|
|
|
130
|
|
|
$response['angka_melek_huruf'] = $angka_melek_huruf; |
|
|
|
|
131
|
|
|
$response['status'] = true; |
132
|
|
|
|
133
|
|
|
return response()->json($response); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Show the form for editing the specified resource. |
138
|
|
|
* |
139
|
|
|
* @param \App\AngkaMelekHuruf $angka_melek_huruf |
|
|
|
|
140
|
|
|
* @return \Illuminate\Http\Response |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function edit($id) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id); |
145
|
|
|
|
146
|
|
|
$response['angka_melek_huruf'] = $angka_melek_huruf; |
|
|
|
|
147
|
|
|
$response['status'] = true; |
148
|
|
|
|
149
|
|
|
return response()->json($response); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Update the specified resource in storage. |
154
|
|
|
* |
155
|
|
|
* @param \Illuminate\Http\Request $request |
156
|
|
|
* @param \App\AngkaMelekHuruf $angka_melek_huruf |
|
|
|
|
157
|
|
|
* @return \Illuminate\Http\Response |
158
|
|
|
*/ |
159
|
|
|
public function update(Request $request, $id) |
160
|
|
|
{ |
161
|
|
|
$angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id); |
162
|
|
|
|
163
|
|
|
if ($request->input('old_label') == $request->input('label')) |
164
|
|
|
{ |
165
|
|
|
$validator = Validator::make($request->all(), [ |
166
|
|
|
'label' => 'required|max:16', |
167
|
|
|
'description' => 'max:255', |
168
|
|
|
]); |
169
|
|
|
} else { |
170
|
|
|
$validator = Validator::make($request->all(), [ |
171
|
|
|
'label' => 'required|max:16|unique:angka_melek_hurufs,label', |
172
|
|
|
'description' => 'max:255', |
173
|
|
|
]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
177
|
|
|
$check = $angka_melek_huruf->where('label',$request->label)->whereNull('deleted_at')->count(); |
178
|
|
|
|
179
|
|
|
if ($check > 0) { |
180
|
|
|
$response['message'] = 'Failed, label ' . $request->label . ' already exists'; |
|
|
|
|
181
|
|
|
} else { |
182
|
|
|
$angka_melek_huruf->label = $request->input('label'); |
183
|
|
|
$angka_melek_huruf->description = $request->input('description'); |
184
|
|
|
$angka_melek_huruf->save(); |
185
|
|
|
|
186
|
|
|
$response['message'] = 'success'; |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} else { |
189
|
|
|
$angka_melek_huruf->label = $request->input('label'); |
190
|
|
|
$angka_melek_huruf->description = $request->input('description'); |
191
|
|
|
$angka_melek_huruf->save(); |
192
|
|
|
|
193
|
|
|
$response['message'] = 'success'; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$response['status'] = true; |
197
|
|
|
|
198
|
|
|
return response()->json($response); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Remove the specified resource from storage. |
203
|
|
|
* |
204
|
|
|
* @param \App\AngkaMelekHuruf $angka_melek_huruf |
|
|
|
|
205
|
|
|
* @return \Illuminate\Http\Response |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id); |
210
|
|
|
|
211
|
|
|
if ($angka_melek_huruf->delete()) { |
212
|
|
|
$response['status'] = true; |
|
|
|
|
213
|
|
|
} else { |
214
|
|
|
$response['status'] = false; |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return json_encode($response); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
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.