|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule; |
|
4
|
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\controllers\CBController; |
|
6
|
|
|
use Illuminate\Support\Facades\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
use crocodicstudio\crudbooster\helpers\CRUDBooster; |
|
10
|
|
|
|
|
11
|
|
|
class AdminApiGeneratorController extends CBController |
|
12
|
|
|
{ |
|
13
|
|
|
public function cbInit() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->table = 'cms_apicustom'; |
|
16
|
|
|
$this->primaryKey = "id"; |
|
17
|
|
|
$this->title_field = "nama"; |
|
18
|
|
|
$this->buttonShow = false; |
|
19
|
|
|
$this->button_new = false; |
|
|
|
|
|
|
20
|
|
|
$this->deleteBtn = false; |
|
21
|
|
|
$this->buttonAdd = false; |
|
22
|
|
|
$this->button_import = false; |
|
23
|
|
|
$this->buttonExport = false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getIndex() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->cbLoader(); |
|
29
|
|
|
|
|
30
|
|
|
$data = []; |
|
31
|
|
|
|
|
32
|
|
|
$data['page_title'] = 'API Generator'; |
|
33
|
|
|
$data['apis'] = $this->table()->orderby('nama', 'asc')->get(); |
|
34
|
|
|
|
|
35
|
|
|
return view('CbApiGen::api_documentation', $data); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function apiDocumentation() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->cbLoader(); |
|
41
|
|
|
$data = []; |
|
42
|
|
|
|
|
43
|
|
|
$data['apis'] = $this->table()->orderby('nama', 'asc')->get(); |
|
44
|
|
|
|
|
45
|
|
|
return view('CbApiGen::api_documentation_public', $data); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getGenerator() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->cbLoader(); |
|
51
|
|
|
|
|
52
|
|
|
$data['page_title'] = 'API Generator'; |
|
|
|
|
|
|
53
|
|
|
$data['tables'] = CRUDBooster::listCbTables(); |
|
54
|
|
|
|
|
55
|
|
|
return view('CbApiGen::api_generator', $data); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getEditApi($id) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->cbLoader(); |
|
61
|
|
|
|
|
62
|
|
|
$row = $this->findRow($id)->first(); |
|
63
|
|
|
|
|
64
|
|
|
$data['row'] = $row; |
|
|
|
|
|
|
65
|
|
|
$data['parameters'] = json_encode(unserialize($row->parameters)); |
|
66
|
|
|
$data['responses'] = json_encode(unserialize($row->responses)); |
|
67
|
|
|
$data['page_title'] = 'API Generator'; |
|
68
|
|
|
|
|
69
|
|
|
$data['tables'] = CRUDBooster::listCbTables(); |
|
70
|
|
|
|
|
71
|
|
|
return view('CbApiGen::api_generator', $data); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function postSaveApiCustom() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->cbLoader(); |
|
77
|
|
|
$posts = request()->all(); |
|
78
|
|
|
|
|
79
|
|
|
$_data = []; |
|
80
|
|
|
|
|
81
|
|
|
$_data['nama'] = g('nama'); |
|
82
|
|
|
$_data['tabel'] = $posts['tabel']; |
|
83
|
|
|
$_data['aksi'] = $posts['aksi']; |
|
84
|
|
|
$_data['permalink'] = g('permalink'); |
|
85
|
|
|
$_data['method_type'] = g('method_type'); |
|
86
|
|
|
|
|
87
|
|
|
$json = $this->json(); |
|
88
|
|
|
|
|
89
|
|
|
$_data['parameters'] = serialize(array_filter($json)); |
|
90
|
|
|
|
|
91
|
|
|
$_data['sql_where'] = g('sql_where'); |
|
92
|
|
|
|
|
93
|
|
|
$json = $this->json2(); |
|
94
|
|
|
$json = array_filter($json); |
|
95
|
|
|
$_data['responses'] = serialize($json); |
|
96
|
|
|
$_data['keterangan'] = g('keterangan'); |
|
97
|
|
|
|
|
98
|
|
|
$this->saveToDB($_data); |
|
99
|
|
|
|
|
100
|
|
|
return redirect(CRUDBooster::mainpath())->with(['message' => 'Yeay, your api has been saved successfully !', 'message_type' => 'success']); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getDeleteApi($id) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->cbLoader(); |
|
106
|
|
|
$row = $this->findRow($id)->first(); |
|
107
|
|
|
$this->findRow($id)->delete(); |
|
108
|
|
|
|
|
109
|
|
|
$controllername = ucwords(str_replace('_', ' ', $row->permalink)); |
|
110
|
|
|
$controllername = str_replace(' ', '', $controllername); |
|
111
|
|
|
@unlink((controllers_dir()."Api".$controllername."Controller.php")); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
return response()->json(['status' => 1]); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
private function json() |
|
120
|
|
|
{ |
|
121
|
|
|
$params_name = g('params_name'); |
|
122
|
|
|
$params_type = g('params_type'); |
|
123
|
|
|
$params_config = g('params_config'); |
|
124
|
|
|
$params_required = g('params_required'); |
|
125
|
|
|
$params_used = g('params_used'); |
|
126
|
|
|
|
|
127
|
|
|
$json = []; |
|
128
|
|
|
for ($i = 0, $_count = count($params_name); $i <= $_count; $i++) { |
|
129
|
|
|
if (! $params_name[$i]) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
$json[] = [ |
|
133
|
|
|
'name' => $params_name[$i], |
|
134
|
|
|
'type' => $params_type[$i], |
|
135
|
|
|
'config' => $params_config[$i], |
|
136
|
|
|
'required' => $params_required[$i], |
|
137
|
|
|
'used' => $params_used[$i], |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $json; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
private function json2() |
|
148
|
|
|
{ |
|
149
|
|
|
$responses_name = g('responses_name'); |
|
150
|
|
|
$responses_type = g('responses_type'); |
|
151
|
|
|
$responses_subquery = g('responses_subquery'); |
|
152
|
|
|
$responses_used = g('responses_used'); |
|
153
|
|
|
|
|
154
|
|
|
$json = []; |
|
155
|
|
|
for ($i = 0, $_count = count($responses_name); $i <= $_count; $i++) { |
|
156
|
|
|
if (! $responses_name[$i]) { |
|
157
|
|
|
continue; |
|
158
|
|
|
} |
|
159
|
|
|
$json[] = [ |
|
160
|
|
|
'name' => $responses_name[$i], |
|
161
|
|
|
'type' => $responses_type[$i], |
|
162
|
|
|
'subquery' => $responses_subquery[$i], |
|
163
|
|
|
'used' => $responses_used[$i], |
|
164
|
|
|
]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $json; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param $a |
|
172
|
|
|
*/ |
|
173
|
|
|
private function saveToDB($a) |
|
174
|
|
|
{ |
|
175
|
|
|
if (request('id')) { |
|
176
|
|
|
return $this->findRow(g('id'))->update($a); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$controllerName = ucwords(str_replace('_', ' ', $a['permalink'])); |
|
180
|
|
|
$controllerName = str_replace(' ', '', $controllerName); |
|
181
|
|
|
$this->generateAPI($controllerName, $a['tabel'], $a['permalink'], $a['method_type']); |
|
182
|
|
|
|
|
183
|
|
|
return $this->table()->insert($a); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function generateAPI($controller_name, $table_name, $permalink, $method_type = 'post') |
|
187
|
|
|
{ |
|
188
|
|
|
$php = view('CbApiGen::api_stub', compact('controller_name', 'table_name', 'permalink', 'method_type'))->render(); |
|
189
|
|
|
file_put_contents(controllers_dir().'Api'.$controller_name.'Controller.php', $php); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|