|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Database\Drivers\MongoDB\Type; |
|
6
|
|
|
use CodexShaper\DBM\Database\Schema\Table; |
|
7
|
|
|
use CodexShaper\DBM\Facades\Driver; |
|
8
|
|
|
use DBM; |
|
|
|
|
|
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
|
|
12
|
|
|
class CrudController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
public function index() |
|
16
|
|
|
{ |
|
17
|
|
|
return view('dbm::app'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function all(Request $request) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($request->ajax()) { |
|
23
|
|
|
|
|
24
|
|
|
if (($response = DBM::authorize('crud.browse')) !== true) { |
|
25
|
|
|
return $response; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$perPage = (int) $request->perPage; |
|
29
|
|
|
$query = $request->q; |
|
30
|
|
|
|
|
31
|
|
|
$userPermissions = DBM::userPermissions(); |
|
32
|
|
|
$tables = Table::paginate($perPage, null, [], $query); |
|
33
|
|
|
$objects = DBM::Object()->all(); |
|
34
|
|
|
|
|
35
|
|
|
$newTables = []; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($tables as $table) { |
|
38
|
|
|
foreach ($objects as $object) { |
|
39
|
|
|
if ($table == $object->name) { |
|
40
|
|
|
$newTables[] = [ |
|
41
|
|
|
'name' => $table, |
|
42
|
|
|
'isCrud' => true, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
continue 2; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$newTables[] = [ |
|
50
|
|
|
'name' => $table, |
|
51
|
|
|
'isCrud' => false, |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return response()->json([ |
|
56
|
|
|
'success' => true, |
|
57
|
|
|
'tables' => $newTables, |
|
58
|
|
|
'userPermissions' => $userPermissions, |
|
59
|
|
|
'pagination' => $tables, |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return response()->json(['success' => false]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getObjectDetails(Request $request) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($request->ajax()) { |
|
69
|
|
|
|
|
70
|
|
|
try |
|
71
|
|
|
{ |
|
72
|
|
|
if (!Table::exists($request->table)) { |
|
73
|
|
|
throw new \Exception("Sorry! There is no table", 1); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$tableName = $request->table; |
|
77
|
|
|
$relationship_tables = Table::all(); |
|
78
|
|
|
|
|
79
|
|
|
$details = $this->getObject($tableName); |
|
80
|
|
|
$permission = $details['isCrudExists'] ? 'update' : 'create'; |
|
81
|
|
|
|
|
82
|
|
|
if (($response = DBM::authorize("crud.{$permission}")) !== true) { |
|
83
|
|
|
return $response; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$relationshipDetails = (object) [ |
|
87
|
|
|
'type' => 'hasOne', |
|
88
|
|
|
'foreignTableDetails' => Table::getTable($relationship_tables[0]), |
|
89
|
|
|
'localTableDetails' => Table::getTable($tableName), |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
return response()->json([ |
|
93
|
|
|
'success' => true, |
|
94
|
|
|
'relationship_tables' => $relationship_tables, |
|
95
|
|
|
'relationship_details' => $relationshipDetails, |
|
96
|
|
|
'object' => $details['object'], |
|
97
|
|
|
'fields' => $details['fields'], |
|
98
|
|
|
'isCrudExists' => $details['isCrudExists'], |
|
99
|
|
|
'userPermissions' => DBM::userPermissions(), |
|
100
|
|
|
'driver' => Driver::getConnectionName(), |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
} catch (\Exception $e) { |
|
104
|
|
|
return $this->generateError([$e->getMessage()]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return response()->json(['success' => false]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getObject($tableName) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($object = DBM::Object()->where('name', $tableName)->first()) { |
|
114
|
|
|
$isCrudExists = true; |
|
115
|
|
|
if (!$object->model) { |
|
116
|
|
|
$object->model = DBM::generateModelName($object->name); |
|
117
|
|
|
} |
|
118
|
|
|
$fields = $object->fields()->orderBy('order', 'ASC')->get(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (!$object) { |
|
122
|
|
|
|
|
123
|
|
|
$isCrudExists = false; |
|
124
|
|
|
$table = Table::getTable($tableName); |
|
125
|
|
|
|
|
126
|
|
|
$object = new \stdClass; |
|
127
|
|
|
$object->name = $table['name']; |
|
128
|
|
|
$object->slug = Str::slug($table['name']); |
|
129
|
|
|
$object->display_name = ucfirst($table['name']); |
|
130
|
|
|
$object->model = DBM::generateModelName($table['name']); |
|
131
|
|
|
$object->controller = ''; |
|
132
|
|
|
|
|
133
|
|
|
$fields = $this->prepareFields($table); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$object->makeModel = false; |
|
137
|
|
|
|
|
138
|
|
|
return ['object' => $object, 'fields' => $fields, 'isCrudExists' => $isCrudExists]; |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function prepareFields($table) |
|
143
|
|
|
{ |
|
144
|
|
|
$fields = []; |
|
145
|
|
|
$order = 1; |
|
146
|
|
|
|
|
147
|
|
|
foreach ($table['columns'] as $column) { |
|
148
|
|
|
|
|
149
|
|
|
$fields[] = (object) [ |
|
150
|
|
|
'name' => $column->name, |
|
151
|
|
|
'display_name' => ucfirst($column->name), |
|
152
|
|
|
'type' => DatabaseController::getInputType($column->type['name']), |
|
153
|
|
|
'create' => ($column->autoincrement) ? false : true, |
|
154
|
|
|
'read' => ($column->autoincrement) ? false : true, |
|
155
|
|
|
'edit' => ($column->autoincrement) ? false : true, |
|
156
|
|
|
'delete' => ($column->autoincrement) ? false : true, |
|
157
|
|
|
'function_name' => '', |
|
158
|
|
|
'order' => $order, |
|
159
|
|
|
'settings' => '{ }', |
|
160
|
|
|
]; |
|
161
|
|
|
|
|
162
|
|
|
$order++; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $fields; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function storeOrUpdate(Request $request) |
|
169
|
|
|
{ |
|
170
|
|
|
if ($request->ajax()) { |
|
171
|
|
|
|
|
172
|
|
|
$table = $request->object; |
|
173
|
|
|
$columns = $request->fields; |
|
174
|
|
|
$permission = $request->isCrudExists ? 'update' : 'create'; |
|
175
|
|
|
|
|
176
|
|
|
if (($response = DBM::authorize('crud.' . $permission)) !== true) { |
|
177
|
|
|
return $response; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if (($response = $this->makeModel($table)) !== true) { |
|
181
|
|
|
return $response; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if (!class_exists($table['controller'])) { |
|
185
|
|
|
\DBM::makeController($table['controller']); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
try |
|
189
|
|
|
{ |
|
190
|
|
|
if ($object = $this->addOrUpdateObject($table)) { |
|
191
|
|
|
foreach ($columns as $column) { |
|
192
|
|
|
$this->addOrUpdateField($column, $object); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return response()->json(['success' => true, 'object' => $request->object, 'fields' => $request->fields]); |
|
197
|
|
|
|
|
198
|
|
|
} catch (\Exception $e) { |
|
199
|
|
|
return $this->generateError([$e->getMessage()]); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return response()->json(['success' => false]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function makeModel($table) |
|
207
|
|
|
{ |
|
208
|
|
|
if (empty($table['model'])) { |
|
209
|
|
|
return $this->generateError(["Model Must be provided"]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
if ($table['makeModel'] && !class_exists($table['model'])) { |
|
213
|
|
|
\DBM::makeModel($table['model'], $table['name']); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if (!$table['makeModel'] && !class_exists($table['model'])) { |
|
217
|
|
|
$error = "Create model {$table['model']} first or checked create model option"; |
|
218
|
|
|
return $this->generateError([$error]); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return true; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function addOrUpdateObject($table) |
|
225
|
|
|
{ |
|
226
|
|
|
$object = DBM::Object()->where('name', $table['name'])->first(); |
|
227
|
|
|
$action = 'update'; |
|
228
|
|
|
if (!$object) { |
|
229
|
|
|
$object = DBM::Object(); |
|
230
|
|
|
$object->name = $table['name']; |
|
231
|
|
|
$action = 'save'; |
|
232
|
|
|
} |
|
233
|
|
|
$object->slug = Str::slug($table['slug']); |
|
234
|
|
|
$object->display_name = ucfirst($table['display_name']); |
|
235
|
|
|
$object->model = $table['model']; |
|
236
|
|
|
$object->controller = $table['controller']; |
|
237
|
|
|
$object->details = [ |
|
238
|
|
|
'findColumn' => $table['findColumn'], |
|
239
|
|
|
'searchColumn' => $table['searchColumn'], |
|
240
|
|
|
'perPage' => $table['perPage'], |
|
241
|
|
|
'orderColumn' => $table['orderColumn'], |
|
242
|
|
|
'orderDisplayColumn' => $table['orderDisplayColumn'], |
|
243
|
|
|
'orderDirection' => $table['orderDirection'], |
|
244
|
|
|
]; |
|
245
|
|
|
|
|
246
|
|
|
if ($object->{$action}()) { |
|
247
|
|
|
return $object; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return false; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function addOrUpdateField($column, $object) |
|
254
|
|
|
{ |
|
255
|
|
|
$field = DBM::Field()->where([ |
|
256
|
|
|
'dbm_object_id' => $object->id, |
|
257
|
|
|
'name' => $column['name'], |
|
258
|
|
|
])->first(); |
|
259
|
|
|
|
|
260
|
|
|
$action = 'update'; |
|
261
|
|
|
|
|
262
|
|
|
if (!$field) { |
|
263
|
|
|
$field = DBM::Field(); |
|
264
|
|
|
$field->dbm_object_id = $object->id; |
|
265
|
|
|
$field->name = $column['name']; |
|
266
|
|
|
$action = 'save'; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
$field->display_name = ucfirst($column['display_name']); |
|
270
|
|
|
$field->type = $column['type']; |
|
271
|
|
|
$field->create = isset($column['create']) ? $column['create'] : false; |
|
272
|
|
|
$field->read = isset($column['read']) ? $column['read'] : false; |
|
273
|
|
|
$field->edit = isset($column['edit']) ? $column['edit'] : false; |
|
274
|
|
|
$field->delete = isset($column['delete']) ? $column['delete'] : false; |
|
275
|
|
|
$field->order = $column['order']; |
|
276
|
|
|
$field->function_name = isset($column['function_name']) ? $column['function_name'] : ""; |
|
277
|
|
|
$field->settings = json_decode($column['settings']); |
|
278
|
|
|
|
|
279
|
|
|
$field->{$action}(); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
public function delete(Request $request) |
|
283
|
|
|
{ |
|
284
|
|
|
if ($request->ajax()) { |
|
285
|
|
|
|
|
286
|
|
|
if (($response = DBM::authorize('crud.delete')) !== true) { |
|
287
|
|
|
return $response; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
$object = DBM::Object()->where('name', $request->table)->first(); |
|
291
|
|
|
if ($object) { |
|
292
|
|
|
$object->fields()->delete(); |
|
293
|
|
|
$object->delete(); |
|
294
|
|
|
return response()->json(['success' => true]); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
return response()->json(['success' => false]); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
protected function generateError($errors) |
|
302
|
|
|
{ |
|
303
|
|
|
return response()->json([ |
|
304
|
|
|
'success' => false, |
|
305
|
|
|
'errors' => $errors, |
|
306
|
|
|
], 400); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths