1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Database\Schema\Table; |
6
|
|
|
use CodexShaper\DBM\Facades\Driver; |
7
|
|
|
use CodexShaper\DBM\Traits\RecordTrait; |
8
|
|
|
use DBM; |
|
|
|
|
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class RecordController extends Controller |
12
|
|
|
{ |
13
|
|
|
use RecordTrait; |
14
|
|
|
|
15
|
|
|
public function index() |
16
|
|
|
{ |
17
|
|
|
return view('dbm::app'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function store(Request $request) |
21
|
|
|
{ |
22
|
|
|
if ($request->ajax()) { |
23
|
|
|
|
24
|
|
|
if (($response = DBM::authorize('record.create')) !== true) { |
25
|
|
|
return $response; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$tableName = $request->table; |
29
|
|
|
$columns = json_decode($request->columns); |
30
|
|
|
$fields = json_decode($request->fields); |
31
|
|
|
|
32
|
|
|
$errors = $this->validation($fields, $columns); |
33
|
|
|
|
34
|
|
|
if (count($errors) > 0) { |
35
|
|
|
return $this->generateError($errors); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
39
|
|
|
|
40
|
|
|
if (!class_exists($object->model)) { |
41
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
|
46
|
|
|
$table = $this->processStoreData($request, $object); |
47
|
|
|
|
48
|
|
|
if ($table->save()) { |
49
|
|
|
$this->storeRelationshipData($fields, $columns, $object, $table); |
50
|
|
|
return response()->json(['success' => true]); |
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 processStoreData($request, $object) |
62
|
|
|
{ |
63
|
|
|
$originalColumns = Table::getColumnsName($request->table); |
64
|
|
|
$columns = json_decode($request->columns); |
65
|
|
|
$fields = json_decode($request->fields); |
66
|
|
|
$table = DBM::model($object->model, $request->table); |
67
|
|
|
|
68
|
|
|
foreach ($columns as $column => $value) { |
69
|
|
|
if (in_array($column, $originalColumns)) { |
70
|
|
|
|
71
|
|
|
if ($request->hasFile($column)) { |
72
|
|
|
$value = $this->saveFiles($request, $column, $request->table); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!Driver::isMongoDB()) { |
76
|
|
|
if ($functionName = $this->hasFunction($fields, $column)) { |
77
|
|
|
$value = $this->executeFunction($functionName, $value); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$table->{$column} = $this->prepareStoreField($value, $request->table, $column); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $table; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function update(Request $request) |
89
|
|
|
{ |
90
|
|
|
if ($request->ajax()) { |
91
|
|
|
|
92
|
|
|
if (($response = DBM::authorize('record.update')) !== true) { |
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$tableName = $request->table; |
97
|
|
|
$columns = json_decode($request->columns); |
98
|
|
|
$fields = json_decode($request->fields); |
99
|
|
|
|
100
|
|
|
$errors = $this->validation($fields, $columns, 'update'); |
101
|
|
|
|
102
|
|
|
if (count($errors) > 0) { |
103
|
|
|
return $this->generateError($errors); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
107
|
|
|
|
108
|
|
|
if (!class_exists($object->model)) { |
109
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$table = $this->processUpdateData($request, $object); |
114
|
|
|
if ($table->update()) { |
115
|
|
|
$this->updateRelationshipData($fields, $columns, $object, $table); |
116
|
|
|
return response()->json(['success' => true]); |
117
|
|
|
} |
118
|
|
|
return response()->json(['success' => true]); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
return $this->generateError([$e->getMessage()]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return response()->json(['success' => false]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function processUpdateData($request, $object) |
128
|
|
|
{ |
129
|
|
|
$originalColumns = Table::getColumnsName($request->table); |
130
|
|
|
$columns = json_decode($request->columns); |
131
|
|
|
$fields = json_decode($request->fields); |
132
|
|
|
$key = $object->details['findColumn']; |
133
|
|
|
|
134
|
|
|
$table = DBM::model($object->model, $request->table)->where($key, $columns->{$key})->first(); |
135
|
|
|
|
136
|
|
|
foreach ($columns as $column => $value) { |
137
|
|
|
|
138
|
|
|
if (in_array($column, $originalColumns)) { |
139
|
|
|
|
140
|
|
|
if ($request->hasFile($column)) { |
141
|
|
|
$value = $this->saveFiles($request, $column, $request->table); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($value !== null && $value !== "") { |
145
|
|
|
|
146
|
|
|
if (!Driver::isMongoDB()) { |
147
|
|
|
if ($functionName = $this->hasFunction($fields, $column)) { |
148
|
|
|
$value = $this->executeFunction($functionName, $value); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$table->{$column} = $this->prepareStoreField($value, $request->table, $column); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $table; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function delete(Request $request) |
162
|
|
|
{ |
163
|
|
|
if ($request->ajax()) { |
164
|
|
|
|
165
|
|
|
if (($response = DBM::authorize('record.delete')) !== true) { |
166
|
|
|
return $response; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$tableName = $request->table; |
170
|
|
|
// $originalColumns = Table::getColumnsName($tableName); |
171
|
|
|
$columns = json_decode($request->columns); |
172
|
|
|
$fields = $request->fields; |
173
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
174
|
|
|
$model = $object->model; |
175
|
|
|
$details = $object->details; |
176
|
|
|
$key = $details['findColumn']; |
177
|
|
|
|
178
|
|
|
if (!class_exists($model)) { |
179
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
try { |
183
|
|
|
$table = DBM::model($model, $tableName)->where($key, $columns->{$key})->first(); |
184
|
|
|
if ($table) { |
185
|
|
|
// Remove Relationship data |
186
|
|
|
foreach ($fields as $field) { |
187
|
|
|
$field = json_decode($field); |
188
|
|
|
$this->removeRelationshipData($field, $object, $table); |
189
|
|
|
} |
190
|
|
|
// Check Table deleted successfully |
191
|
|
|
if ($table->delete()) { |
192
|
|
|
return response()->json(['success' => true]); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} catch (\Exception $e) { |
197
|
|
|
return $this->generateError([$e->getMessage()]); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return response()->json(['success' => false]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getTableDetails(Request $request) |
205
|
|
|
{ |
206
|
|
|
if ($request->ajax()) { |
207
|
|
|
|
208
|
|
|
if (($response = DBM::authorize('record.browse')) !== true) { |
209
|
|
|
return $response; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$tableName = $request->table; |
213
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
214
|
|
|
|
215
|
|
|
if (!$object) { |
216
|
|
|
return response()->json([ |
217
|
|
|
'success' => false, |
218
|
|
|
'errors' => ["There is no Object details"], |
219
|
|
|
], 400); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$createFields = $object->createFields(); |
223
|
|
|
$browseFields = $object->readFields(); |
224
|
|
|
$editFields = $object->editFields(); |
225
|
|
|
$deleteFields = $object->deleteFields(); |
226
|
|
|
$fields = $object->allFields(); |
227
|
|
|
$foreignTableData = []; |
228
|
|
|
|
229
|
|
|
$createFields = $this->prepareRecordFields($createFields); |
230
|
|
|
$editFields = $this->prepareRecordFields($editFields); |
231
|
|
|
|
232
|
|
|
$model = $object->model; |
233
|
|
|
|
234
|
|
|
if (!class_exists($model)) { |
235
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$perPage = (int) $request->perPage; |
239
|
|
|
$query = $request->q; |
240
|
|
|
$searchColumn = $object->details['searchColumn']; |
241
|
|
|
$records = DBM::model($model, $tableName)->paginate($perPage); |
242
|
|
|
|
243
|
|
|
if (!empty($query) && !empty($searchColumn)) { |
244
|
|
|
$records = DBM::model($model, $tableName) |
245
|
|
|
->where($searchColumn, 'LIKE', '%' . $query . '%') |
246
|
|
|
->paginate($perPage); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$records = $this->prepareRelationshipData($records, $browseFields, $object); |
250
|
|
|
$recordsDetail = $this->prepareJsonFieldData($records, $fields, $object, $request->findValue); |
251
|
|
|
|
252
|
|
|
$objectDetails = $object->details; |
253
|
|
|
$objectDetails['perPage'] = $perPage; |
254
|
|
|
|
255
|
|
|
return response()->json([ |
256
|
|
|
'success' => true, |
257
|
|
|
'object' => $object, |
258
|
|
|
'objectDetails' => $objectDetails, |
259
|
|
|
'createFields' => $createFields, |
260
|
|
|
'browseFields' => $browseFields, |
261
|
|
|
'editFields' => $editFields, |
262
|
|
|
'deleteFields' => $deleteFields, |
263
|
|
|
'records' => $recordsDetail['records'], |
264
|
|
|
'record' => $recordsDetail['record'], |
265
|
|
|
'foreignTableData' => $foreignTableData, |
266
|
|
|
'userPermissions' => DBM::userPermissions(), |
267
|
|
|
'pagination' => $records, |
268
|
|
|
]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return response()->json(['success' => false]); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
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