|
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 DBM; |
|
|
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
class CrudController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function index() |
|
15
|
|
|
{ |
|
16
|
|
|
return view('dbm::app'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function storeOrUpdate(Request $request) |
|
20
|
|
|
{ |
|
21
|
|
|
if ($request->ajax()) { |
|
22
|
|
|
|
|
23
|
|
|
$table = $request->object; |
|
24
|
|
|
$columns = $request->fields; |
|
25
|
|
|
$permission = $request->isCrudExists ? 'update' : 'create'; |
|
26
|
|
|
|
|
27
|
|
|
if (($response = DBM::authorize('crud.' . $permission)) !== true) { |
|
28
|
|
|
return $response; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (($response = $this->makeModel($table)) !== true) { |
|
32
|
|
|
return $response; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!class_exists($table['controller'])) { |
|
36
|
|
|
\DBM::makeController($table['controller']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
try |
|
40
|
|
|
{ |
|
41
|
|
|
if ($object = $this->addOrUpdateObject($table)) { |
|
42
|
|
|
foreach ($columns as $column) { |
|
43
|
|
|
$this->addOrUpdateField($column, $object); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return response()->json([ |
|
48
|
|
|
'success' => true, |
|
49
|
|
|
'object' => $request->object, |
|
50
|
|
|
'fields' => $request->fields, |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
return $this->generateError([$e->getMessage()]); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return response()->json(['success' => false]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function makeModel($table) |
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($table['model'])) { |
|
64
|
|
|
return $this->generateError(["Model Must be provided"]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($table['makeModel'] && !class_exists($table['model'])) { |
|
68
|
|
|
\DBM::makeModel($table['model'], $table['name']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!$table['makeModel'] && !class_exists($table['model'])) { |
|
72
|
|
|
$error = "Create model {$table['model']} first or checked create model option"; |
|
73
|
|
|
return $this->generateError([$error]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function addOrUpdateObject($table) |
|
80
|
|
|
{ |
|
81
|
|
|
$object = DBM::Object()->where('name', $table['name'])->first(); |
|
82
|
|
|
$action = 'update'; |
|
83
|
|
|
if (!$object) { |
|
84
|
|
|
$object = DBM::Object(); |
|
85
|
|
|
$object->name = $table['name']; |
|
86
|
|
|
$action = 'save'; |
|
87
|
|
|
} |
|
88
|
|
|
$object->slug = Str::slug($table['slug']); |
|
89
|
|
|
$object->display_name = ucfirst($table['display_name']); |
|
90
|
|
|
$object->model = $table['model']; |
|
91
|
|
|
$object->controller = $table['controller']; |
|
92
|
|
|
$object->details = [ |
|
93
|
|
|
'findColumn' => $table['findColumn'], |
|
94
|
|
|
'searchColumn' => $table['searchColumn'], |
|
95
|
|
|
'perPage' => $table['perPage'], |
|
96
|
|
|
'orderColumn' => $table['orderColumn'], |
|
97
|
|
|
'orderDisplayColumn' => $table['orderDisplayColumn'], |
|
98
|
|
|
'orderDirection' => $table['orderDirection'], |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
if ($object->{$action}()) { |
|
102
|
|
|
return $object; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function addOrUpdateField($column, $object) |
|
109
|
|
|
{ |
|
110
|
|
|
$field = DBM::Field()->where([ |
|
111
|
|
|
'dbm_object_id' => $object->id, |
|
112
|
|
|
'name' => $column['name'], |
|
113
|
|
|
])->first(); |
|
114
|
|
|
|
|
115
|
|
|
$action = 'update'; |
|
116
|
|
|
|
|
117
|
|
|
if (!$field) { |
|
118
|
|
|
$field = DBM::Field(); |
|
119
|
|
|
$field->dbm_object_id = $object->id; |
|
120
|
|
|
$field->name = $column['name']; |
|
121
|
|
|
$action = 'save'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$field->display_name = ucfirst($column['display_name']); |
|
125
|
|
|
$field->type = $column['type']; |
|
126
|
|
|
$field->create = isset($column['create']) ? $column['create'] : false; |
|
127
|
|
|
$field->read = isset($column['read']) ? $column['read'] : false; |
|
128
|
|
|
$field->edit = isset($column['edit']) ? $column['edit'] : false; |
|
129
|
|
|
$field->delete = isset($column['delete']) ? $column['delete'] : false; |
|
130
|
|
|
$field->order = $column['order']; |
|
131
|
|
|
$field->function_name = isset($column['function_name']) ? $column['function_name'] : ""; |
|
132
|
|
|
$field->settings = json_decode($column['settings']); |
|
133
|
|
|
|
|
134
|
|
|
$field->{$action}(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function delete(Request $request) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($request->ajax()) { |
|
140
|
|
|
|
|
141
|
|
|
if (($response = DBM::authorize('crud.delete')) !== true) { |
|
142
|
|
|
return $response; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$object = DBM::Object()->where('name', $request->table)->first(); |
|
146
|
|
|
if ($object) { |
|
147
|
|
|
$object->fields()->delete(); |
|
148
|
|
|
$object->delete(); |
|
149
|
|
|
return response()->json(['success' => true]); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return response()->json(['success' => false]); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
protected function generateError($errors) |
|
157
|
|
|
{ |
|
158
|
|
|
return response()->json([ |
|
159
|
|
|
'success' => false, |
|
160
|
|
|
'errors' => $errors, |
|
161
|
|
|
], 400); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
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