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
|
|
|
|
74
|
|
|
throw new \Exception("Sorry! There is no table", 1); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$tableName = $request->table; |
79
|
|
|
$namespace = DBM::getModelNamespace(); |
80
|
|
|
$relationship_tables = Table::all(); |
81
|
|
|
$isCrudExists = false; |
82
|
|
|
|
83
|
|
|
if ($object = DBM::Object()->where('name', $tableName)->first()) { |
84
|
|
|
|
85
|
|
|
if (($response = DBM::authorize('crud.update')) !== true) { |
86
|
|
|
return $response; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$isCrudExists = true; |
90
|
|
|
|
91
|
|
|
if (!$object->model) { |
92
|
|
|
$namespace = DBM::getModelNamespace(); |
93
|
|
|
|
94
|
|
|
$modelName = ucfirst(Str::singular($object->name)); |
95
|
|
|
|
96
|
|
|
$model = $namespace . '\\' . $modelName; |
97
|
|
|
$object->model = $model; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$fields = $object->fields()->orderBy('order', 'ASC')->get(); |
101
|
|
|
} else { |
102
|
|
|
if (($response = DBM::authorize('crud.create')) !== true) { |
103
|
|
|
return $response; |
104
|
|
|
} |
105
|
|
|
$table = Table::getTable($tableName); |
106
|
|
|
|
107
|
|
|
$modelName = ucfirst(Str::singular($table['name'])); |
108
|
|
|
$model = $namespace . '\\' . $modelName; |
109
|
|
|
|
110
|
|
|
$object = new \stdClass; |
111
|
|
|
$object->name = $table['name']; |
112
|
|
|
$object->slug = Str::slug($table['name']); |
113
|
|
|
$object->display_name = ucfirst($table['name']); |
114
|
|
|
$object->model = $model; |
115
|
|
|
$object->controller = ''; |
116
|
|
|
|
117
|
|
|
$fields = []; |
118
|
|
|
$order = 1; |
119
|
|
|
|
120
|
|
|
foreach ($table['columns'] as $column) { |
121
|
|
|
|
122
|
|
|
$fields[] = (object) [ |
123
|
|
|
'name' => $column->name, |
124
|
|
|
'display_name' => ucfirst($column->name), |
125
|
|
|
'type' => DatabaseController::getInputType($column->type['name']), |
126
|
|
|
'create' => ($column->autoincrement) ? false : true, |
127
|
|
|
'read' => ($column->autoincrement) ? false : true, |
128
|
|
|
'edit' => ($column->autoincrement) ? false : true, |
129
|
|
|
'delete' => ($column->autoincrement) ? false : true, |
130
|
|
|
'function_name' => '', |
131
|
|
|
'order' => $order, |
132
|
|
|
'settings' => '{ }', |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$order++; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$object->makeModel = false; |
140
|
|
|
|
141
|
|
|
$relationshipDetails = (object) [ |
142
|
|
|
'type' => 'hasOne', |
143
|
|
|
'foreignTableDetails' => Table::getTable($relationship_tables[0]), |
144
|
|
|
'localTableDetails' => Table::getTable($tableName), |
145
|
|
|
|
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
return response()->json([ |
150
|
|
|
'success' => false, |
151
|
|
|
'errors' => [$e->getMessage()], |
152
|
|
|
], 400); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return response()->json([ |
156
|
|
|
'success' => true, |
157
|
|
|
'relationship_tables' => $relationship_tables, |
158
|
|
|
'relationship_details' => $relationshipDetails, |
159
|
|
|
'object' => $object, |
160
|
|
|
'fields' => $fields, |
161
|
|
|
'isCrudExists' => $isCrudExists, |
162
|
|
|
'userPermissions' => DBM::userPermissions(), |
163
|
|
|
'driver' => Driver::getConnectionName(), |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return response()->json(['success' => false]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function addOrUpdateField($column, $object) |
171
|
|
|
{ |
172
|
|
|
$field = DBM::Field()->where([ |
173
|
|
|
'dbm_object_id' => $object->id, |
174
|
|
|
'name' => $column['name'], |
175
|
|
|
])->first(); |
176
|
|
|
|
177
|
|
|
$action = 'update'; |
178
|
|
|
|
179
|
|
|
if (!$field) { |
180
|
|
|
$field = DBM::Field(); |
181
|
|
|
$field->dbm_object_id = $object->id; |
182
|
|
|
$field->name = $column['name']; |
183
|
|
|
$action = 'save'; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$field->display_name = ucfirst($column['display_name']); |
187
|
|
|
$field->type = $column['type']; |
188
|
|
|
$field->create = isset($column['create']) ? $column['create'] : false; |
189
|
|
|
$field->read = isset($column['read']) ? $column['read'] : false; |
190
|
|
|
$field->edit = isset($column['edit']) ? $column['edit'] : false; |
191
|
|
|
$field->delete = isset($column['delete']) ? $column['delete'] : false; |
192
|
|
|
$field->order = $column['order']; |
193
|
|
|
$field->function_name = isset($column['function_name']) ? $column['function_name'] : ""; |
194
|
|
|
$field->settings = json_decode($column['settings']); |
195
|
|
|
|
196
|
|
|
$field->{$action}(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function addCrud($table, $columns) |
200
|
|
|
{ |
201
|
|
|
try |
202
|
|
|
{ |
203
|
|
|
$object = DBM::Object(); |
204
|
|
|
$object->name = $table['name']; |
205
|
|
|
$object->slug = Str::slug($table['slug']); |
206
|
|
|
$object->display_name = ucfirst($table['display_name']); |
207
|
|
|
$object->model = $table['model']; |
208
|
|
|
$object->controller = $table['controller']; |
209
|
|
|
$object->details = [ |
210
|
|
|
'findColumn' => $table['findColumn'], |
211
|
|
|
'searchColumn' => $table['searchColumn'], |
212
|
|
|
'perPage' => $table['perPage'], |
213
|
|
|
'orderColumn' => $table['orderColumn'], |
214
|
|
|
'orderDisplayColumn' => $table['orderDisplayColumn'], |
215
|
|
|
'orderDirection' => $table['orderDirection'], |
216
|
|
|
]; |
217
|
|
|
|
218
|
|
|
if ($object->save()) { |
219
|
|
|
|
220
|
|
|
foreach ($columns as $column) { |
221
|
|
|
|
222
|
|
|
$this->addOrUpdateField($column, $object); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return true; |
228
|
|
|
|
229
|
|
|
} catch (\Exception $e) { |
230
|
|
|
return $this->generateError([$e->getMessage()]); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function updateCrud($table, $columns) |
235
|
|
|
{ |
236
|
|
|
try |
237
|
|
|
{ |
238
|
|
|
$object = DBM::Object()->where('name', $table['name'])->first(); |
239
|
|
|
$object->slug = Str::slug($table['slug']); |
240
|
|
|
$object->display_name = ucfirst($table['display_name']); |
241
|
|
|
$object->model = $table['model']; |
242
|
|
|
$object->controller = $table['controller']; |
243
|
|
|
$object->details = [ |
244
|
|
|
'findColumn' => $table['findColumn'], |
245
|
|
|
'searchColumn' => $table['searchColumn'], |
246
|
|
|
'perPage' => $table['perPage'], |
247
|
|
|
'orderColumn' => $table['orderColumn'], |
248
|
|
|
'orderDisplayColumn' => $table['orderDisplayColumn'], |
249
|
|
|
'orderDirection' => $table['orderDirection'], |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
if ($object->update()) { |
253
|
|
|
|
254
|
|
|
foreach ($columns as $column) { |
255
|
|
|
|
256
|
|
|
$this->addOrUpdateField($column, $object); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return true; |
261
|
|
|
|
262
|
|
|
} catch (\Exception $e) { |
263
|
|
|
return $this->generateError([$e->getMessage()]); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function storeOrUpdate(Request $request) |
268
|
|
|
{ |
269
|
|
|
if ($request->ajax()) { |
270
|
|
|
|
271
|
|
|
if (($response = DBM::authorize('crud.browse')) !== true) { |
272
|
|
|
return $response; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$table = $request->object; |
276
|
|
|
$columns = $request->fields; |
277
|
|
|
$action = ($request->isCrudExists) ? 'edit' : 'add'; |
278
|
|
|
|
279
|
|
|
// return response()->json(['columns' => $columns, 'action' => $action]); |
280
|
|
|
|
281
|
|
|
if ($table['model'] == '' || $table['model'] == null) { |
282
|
|
|
return $this->generateError(["Model Must be provided"]); |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if ($table['makeModel'] == true && !class_exists($table['model'])) { |
287
|
|
|
\DBM::makeModel($table['model'], $table['name']); |
288
|
|
|
} else if ($table['makeModel'] == false && !class_exists($table['model'])) { |
289
|
|
|
return $this->generateError(["Create model '" . $table['model'] . "' first or checked create model option"]); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if (!class_exists($table['controller'])) { |
293
|
|
|
\DBM::makeController($table['controller']); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if ($action == 'add') { |
297
|
|
|
|
298
|
|
|
if (($response = DBM::authorize('crud.create')) !== true) { |
299
|
|
|
return $response; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if (($response = $this->addCrud($table, $columns)) !== true) { |
303
|
|
|
return $response; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
} else if ($action == 'edit') { |
307
|
|
|
|
308
|
|
|
if (($response = DBM::authorize('crud.update')) !== true) { |
309
|
|
|
return $response; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (($response = $this->updateCrud($table, $columns)) !== true) { |
313
|
|
|
return $response; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return response()->json([ |
318
|
|
|
'success' => true, |
319
|
|
|
'object' => $request->object, |
320
|
|
|
'fields' => $request->fields, |
321
|
|
|
]); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return response()->json(['success' => false]); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function delete(Request $request) |
328
|
|
|
{ |
329
|
|
|
if ($request->ajax()) { |
330
|
|
|
|
331
|
|
|
if (($response = DBM::authorize('crud.delete')) !== true) { |
332
|
|
|
return $response; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$object = DBM::Object()->where('name', $request->table)->first(); |
336
|
|
|
if ($object) { |
337
|
|
|
$object->fields()->delete(); |
338
|
|
|
$object->delete(); |
339
|
|
|
return response()->json(['success' => true]); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return response()->json(['success' => false]); |
344
|
|
|
} |
345
|
|
|
/* |
346
|
|
|
* RelationShip |
347
|
|
|
*/ |
348
|
|
|
public function getRelation(Request $request) |
349
|
|
|
{ |
350
|
|
|
if ($request->ajax()) { |
351
|
|
|
|
352
|
|
|
if (($response = DBM::authorize('relationship.update')) !== true) { |
353
|
|
|
return $response; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
$tableName = $request->table; |
357
|
|
|
$field = json_decode($request->field); |
358
|
|
|
|
359
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
360
|
|
|
$fields = $object->fields; |
361
|
|
|
|
362
|
|
|
$prefix = (Driver::isMongoDB()) ? "_" : ""; |
363
|
|
|
|
364
|
|
|
foreach ($fields as $fld) { |
365
|
|
|
if ($fld->id == $field->{$prefix . "id"}) { |
366
|
|
|
|
367
|
|
|
$relationship = $fld->settings; |
368
|
|
|
$localTable = $relationship['localTable']; |
369
|
|
|
$foreignTable = $relationship['foreignTable']; |
370
|
|
|
$pivotTable = $relationship['pivotTable']; |
371
|
|
|
|
372
|
|
|
$field->localFields = Table::getTable($localTable); |
373
|
|
|
$field->foreignFields = Table::getTable($foreignTable); |
374
|
|
|
$field->pivotFields = Table::getTable($pivotTable); |
375
|
|
|
$field->relationship = $relationship; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
return response()->json(['success' => true, 'field' => $field]); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return response()->json(['success' => false]); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function addRelation(Request $request) |
386
|
|
|
{ |
387
|
|
|
if ($request->ajax()) { |
388
|
|
|
|
389
|
|
|
if (($response = DBM::authorize('relationship.create')) !== true) { |
390
|
|
|
return $response; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$relationship = $request->relationship; |
394
|
|
|
|
395
|
|
|
// return response()->json(['success' => true, 'relationship' => $relationship['displayLevel']]); |
396
|
|
|
|
397
|
|
|
if (!class_exists($relationship['localModel'])) { |
398
|
|
|
|
399
|
|
|
$error = $relationship['localModel'] . " Model not found. Please create the " . $relationship['localModel'] . " model first"; |
400
|
|
|
return $this->generateError([$error]); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if (!class_exists($relationship['foreignModel'])) { |
404
|
|
|
|
405
|
|
|
$error = $relationship['foreignModel'] . " Model not found. Please create the " . $relationship['foreignModel'] . " model first"; |
406
|
|
|
return $this->generateError([$error]); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
$fieldName = strtolower(Str::singular($relationship['localTable']) . '_' . $relationship['type'] . '_' . Str::singular($relationship['foreignTable']) . '_relationship'); |
410
|
|
|
$settings = [ |
411
|
|
|
'relationType' => $relationship['type'], |
412
|
|
|
'localModel' => $relationship['localModel'], |
413
|
|
|
'localTable' => $relationship['localTable'], |
414
|
|
|
'localKey' => $relationship['localKey'], |
415
|
|
|
'foreignModel' => $relationship['foreignModel'], |
416
|
|
|
'foreignTable' => $relationship['foreignTable'], |
417
|
|
|
'foreignKey' => $relationship['foreignKey'], |
418
|
|
|
'displayLabel' => $relationship['displayLabel'], |
419
|
|
|
'pivotTable' => $relationship['pivotTable'], |
420
|
|
|
'parentPivotKey' => $relationship['parentPivotKey'], |
421
|
|
|
'relatedPivotKey' => $relationship['relatedPivotKey'], |
422
|
|
|
]; |
423
|
|
|
|
424
|
|
|
$object = DBM::Object()->where('name', $relationship['localTable'])->first(); |
425
|
|
|
$order = DBM::Field()->where('dbm_object_id', $object->id)->max('order'); |
426
|
|
|
|
427
|
|
|
$field = DBM::Field(); |
428
|
|
|
$field->dbm_object_id = $object->id; |
429
|
|
|
$field->name = $fieldName; |
430
|
|
|
$field->type = 'relationship'; |
431
|
|
|
$field->display_name = ucfirst($relationship['foreignTable']); |
432
|
|
|
$field->order = $order + 1; |
433
|
|
|
$field->settings = $settings; |
434
|
|
|
if ($field->save()) { |
435
|
|
|
return response()->json(['success' => true]); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return response()->json(['success' => false]); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function updateRelation(Request $request) |
443
|
|
|
{ |
444
|
|
|
if ($request->ajax()) { |
445
|
|
|
|
446
|
|
|
if (($response = DBM::authorize('relationship.update')) !== true) { |
447
|
|
|
return $response; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
$relationship = $request->relationship; |
451
|
|
|
$field = $request->field; |
452
|
|
|
|
453
|
|
|
// return response()->json(['relationship' => $relationship, 'field' => $field]); |
454
|
|
|
|
455
|
|
|
$settings = [ |
456
|
|
|
'relationType' => $relationship['type'], |
457
|
|
|
'localModel' => $relationship['localModel'], |
458
|
|
|
'localTable' => $relationship['localTable'], |
459
|
|
|
'localKey' => $relationship['localKey'], |
460
|
|
|
'foreignModel' => $relationship['foreignModel'], |
461
|
|
|
'foreignTable' => $relationship['foreignTable'], |
462
|
|
|
'foreignKey' => $relationship['foreignKey'], |
463
|
|
|
'displayLabel' => $relationship['displayLabel'], |
464
|
|
|
'pivotTable' => $relationship['pivotTable'], |
465
|
|
|
'parentPivotKey' => $relationship['parentPivotKey'], |
466
|
|
|
'relatedPivotKey' => $relationship['relatedPivotKey'], |
467
|
|
|
]; |
468
|
|
|
|
469
|
|
|
$field = DBM::Field()::find($field['id']); |
470
|
|
|
$field->settings = $settings; |
471
|
|
|
if ($field->update()) { |
472
|
|
|
return response()->json(['success' => true]); |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
return response()->json(['success' => false]); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
public function deleteRelation(Request $request) |
480
|
|
|
{ |
481
|
|
|
if ($request->ajax()) { |
482
|
|
|
|
483
|
|
|
if (($response = DBM::authorize('relationship.delete')) !== true) { |
484
|
|
|
return $response; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
$tableName = $request->table; |
|
|
|
|
488
|
|
|
$data = json_decode($request->field); |
489
|
|
|
|
490
|
|
|
$field = DBM::Field()::find($data->id); |
491
|
|
|
|
492
|
|
|
if ($field->delete()) { |
493
|
|
|
return response()->json(['success' => true]); |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
return response()->json(['success' => false]); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
protected function generateError($errors) |
501
|
|
|
{ |
502
|
|
|
return response()->json([ |
503
|
|
|
'success' => false, |
504
|
|
|
'errors' => $errors, |
505
|
|
|
], 400); |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|
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