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 CodexShaper\DBM\Models\DBM_Collection; |
9
|
|
|
use DBM; |
|
|
|
|
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Support\Facades\DB; |
12
|
|
|
use Illuminate\Support\Facades\Validator; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
class RecordController extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function index() |
19
|
|
|
{ |
20
|
|
|
return view('dbm::app'); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function store(Request $request) |
24
|
|
|
{ |
25
|
|
|
if ($request->ajax()) { |
26
|
|
|
|
27
|
|
|
if (($response = DBM::authorize('record.create')) !== true) { |
28
|
|
|
return $response; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$tableName = $request->table; |
32
|
|
|
$originalColumns = Table::getColumnsName($tableName); |
33
|
|
|
$columns = json_decode($request->columns); |
34
|
|
|
$fields = json_decode($request->fields); |
35
|
|
|
|
36
|
|
|
// return response()->json(['columns' => $columns]); |
37
|
|
|
|
38
|
|
|
$errors = $this->validation($fields, $columns); |
39
|
|
|
|
40
|
|
|
if (count($errors) > 0) { |
41
|
|
|
return $this->generateError($errors); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
45
|
|
|
$model = $object->model; |
46
|
|
|
|
47
|
|
|
if (!class_exists($model)) { |
48
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
|
53
|
|
|
$table = DBM::model($model, $tableName); |
54
|
|
|
|
55
|
|
|
foreach ($columns as $column => $value) { |
56
|
|
|
// $column = $field->name; |
57
|
|
|
// $value = $request->{$column}; |
58
|
|
|
if (in_array($column, $originalColumns)) { |
59
|
|
|
|
60
|
|
|
if ($request->hasFile($column)) { |
61
|
|
|
$files = $request->file($column); |
62
|
|
|
$values = []; |
63
|
|
|
foreach ($files as $file) { |
64
|
|
|
$fileName = Str::random(config('dbm.filesystem.random_length')) . '.' . $file->getClientOriginalExtension(); |
|
|
|
|
65
|
|
|
$path = 'public/dbm/' . $tableName; |
66
|
|
|
$file->storeAs($path, $fileName); |
67
|
|
|
$values[] = $fileName; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// $value = json_encode($values); |
71
|
|
|
|
72
|
|
|
if (count($values) > 1) { |
73
|
|
|
if (!Driver::isMongoDB()) { |
|
|
|
|
74
|
|
|
$value = is_array($values) ? json_encode($values) : $values; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
$value = $values; |
77
|
|
|
} else if (count($values) == 1) { |
78
|
|
|
$value = $values[0]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!Driver::isMongoDB()) { |
83
|
|
|
if ($functionName = $this->hasFunction($fields, $column)) { |
84
|
|
|
$value = $this->executeFunction($functionName, $value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// if ($value != "" || !empty($value)) { |
89
|
|
|
|
90
|
|
|
$table->{$column} = is_array($value) ? json_encode($value) : $value; |
91
|
|
|
|
92
|
|
|
if (Driver::isMongoDB()) { |
93
|
|
|
|
94
|
|
|
$fieldType = $this->getFieldType($tableName, $column); |
95
|
|
|
|
96
|
|
|
if (!in_array($fieldType, Type::getTypes())) { |
97
|
|
|
$this->generateError([$fieldType . " type not supported."]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$table->{$column} = Type::$fieldType($value); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
// } |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($table->save()) { |
109
|
|
|
|
110
|
|
|
foreach ($fields as $field) { |
111
|
|
|
|
112
|
|
|
if (isset($field->relationship) && $field->relationship->relationType == "belongsToMany") { |
113
|
|
|
|
114
|
|
|
$relationship = $field->relationship; |
115
|
|
|
|
116
|
|
|
$localModel = $relationship->localModel; |
117
|
|
|
$localTable = $relationship->localTable; |
118
|
|
|
$foreignModel = $relationship->foreignModel; |
119
|
|
|
$pivotTable = $relationship->pivotTable; |
120
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
121
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
122
|
|
|
|
123
|
|
|
$findColumn = $object->details['findColumn']; |
124
|
|
|
|
125
|
|
|
$localObject = DBM::model($localModel, $localTable)::where($findColumn, $table->{$findColumn})->first(); |
126
|
|
|
|
127
|
|
|
DBM::Object() |
128
|
|
|
->setManyToManyRelation( |
129
|
|
|
$localObject, |
130
|
|
|
$foreignModel, |
131
|
|
|
$pivotTable, |
132
|
|
|
$parentPivotKey, |
133
|
|
|
$relatedPivotKey |
134
|
|
|
) |
135
|
|
|
->belongs_to_many() |
136
|
|
|
->attach($columns->{$relatedPivotKey}); |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// else if (isset($field['relationship']) && $field['relationship']['relationType'] == "hasMany") { |
141
|
|
|
|
142
|
|
|
// $relationship = $field['relationship']; |
143
|
|
|
|
144
|
|
|
// $localModel = $relationship['localModel']; |
145
|
|
|
// $findColumn = $object->details['findColumn']; |
146
|
|
|
// $foreignModel = $relationship['foreignModel']; |
147
|
|
|
// $foreignKey = $relationship['foreignKey']; |
148
|
|
|
// $localKey = $relationship['localKey']; |
149
|
|
|
|
150
|
|
|
// $localObject = $localModel::where($findColumn, $table->{$findColumn})->first(); |
151
|
|
|
|
152
|
|
|
// $items = []; |
153
|
|
|
|
154
|
|
|
// foreach ($columns[$foreignKey] as $key => $value) { |
155
|
|
|
// // return response()->json(['columns' => $columns['menu_id'], 'fields' => $value, 'model' => $foreignModel]); |
156
|
|
|
// $originalColumns = Table::getColumnsName($relationship['foreignTable']); |
157
|
|
|
// $foreignTableFields = DBM::Object()->where('name', $relationship['foreignTable'])->first()->fields()->where('create', 1)->get(); |
158
|
|
|
|
159
|
|
|
// $oldItem = $foreignModel::find($value); |
160
|
|
|
// $item = new $foreignModel; |
161
|
|
|
|
162
|
|
|
// foreach ($foreignTableFields as $foreignTableField) { |
163
|
|
|
// if (in_array($foreignTableField->name, $originalColumns)) { |
164
|
|
|
// if ($foreignTableField->name != $foreignKey) { |
165
|
|
|
// $item->{$foreignTableField->name} = $oldItem->{$foreignTableField->name}; |
166
|
|
|
// } |
167
|
|
|
// } |
168
|
|
|
// } |
169
|
|
|
// DBM::Object() |
170
|
|
|
// ->setCommonRelation( |
171
|
|
|
// $localObject, |
172
|
|
|
// $foreignModel, |
173
|
|
|
// $foreignKey, |
174
|
|
|
// $localKey) |
175
|
|
|
// ->has_many() |
176
|
|
|
// ->save($item); |
177
|
|
|
// } |
178
|
|
|
|
179
|
|
|
// } |
180
|
|
|
} |
181
|
|
|
return response()->json(['success' => true, 'object' => $object, 'table' => $table]); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} catch (\Exception $e) { |
185
|
|
|
return $this->generateError([$e->getMessage()]); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return response()->json(['success' => false]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function update(Request $request) |
193
|
|
|
{ |
194
|
|
|
if ($request->ajax()) { |
195
|
|
|
|
196
|
|
|
if (($response = DBM::authorize('record.update')) !== true) { |
197
|
|
|
return $response; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$tableName = $request->table; |
201
|
|
|
$originalColumns = Table::getColumnsName($tableName); |
202
|
|
|
$columns = json_decode($request->columns); |
203
|
|
|
$fields = json_decode($request->fields); |
204
|
|
|
|
205
|
|
|
// return response()->json(['columns' => $columns->multiple_dropdown]); |
206
|
|
|
|
207
|
|
|
$errors = $this->validation($fields, $columns, 'update'); |
208
|
|
|
|
209
|
|
|
if (count($errors) > 0) { |
210
|
|
|
return $this->generateError($errors); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
214
|
|
|
$model = $object->model; |
215
|
|
|
$details = $object->details; |
216
|
|
|
$key = $details['findColumn']; |
217
|
|
|
|
218
|
|
|
if (!class_exists($model)) { |
219
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
try { |
223
|
|
|
|
224
|
|
|
$table = DBM::model($model, $tableName)->where($key, $columns->{$key})->first(); |
225
|
|
|
|
226
|
|
|
// return response()->json(['details' => $table, 'key' => $key]); |
227
|
|
|
|
228
|
|
|
foreach ($columns as $column => $value) { |
229
|
|
|
// $column = $field->name; |
230
|
|
|
// $value = $request->{$column}; |
231
|
|
|
|
232
|
|
|
if (in_array($column, $originalColumns)) { |
233
|
|
|
|
234
|
|
|
if ($request->hasFile($column)) { |
235
|
|
|
$files = $request->file($column); |
236
|
|
|
$values = []; |
237
|
|
|
foreach ($files as $file) { |
238
|
|
|
$fileName = Str::random(config('dbm.filesystem.random_length')) . '.' . $file->getClientOriginalExtension(); |
|
|
|
|
239
|
|
|
$path = 'public/dbm/' . $tableName; |
240
|
|
|
$file->storeAs($path, $fileName); |
241
|
|
|
$values[] = $fileName; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// $value = json_encode($values); |
245
|
|
|
|
246
|
|
|
if (count($values) > 1) { |
247
|
|
|
$value = $values; |
248
|
|
|
if (!Driver::isMongoDB()) { |
249
|
|
|
$value = is_array($values) ? json_encode($values) : $value; |
250
|
|
|
} |
251
|
|
|
} else if (count($values) == 1) { |
252
|
|
|
$value = $values[0]; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ($value !== null && $value !== "") { |
257
|
|
|
|
258
|
|
|
if (!Driver::isMongoDB()) { |
259
|
|
|
if ($functionName = $this->hasFunction($fields, $column)) { |
260
|
|
|
|
261
|
|
|
$value = $this->executeFunction($functionName, $value); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$table->{$column} = is_array($value) ? json_encode($value) : $value; |
266
|
|
|
|
267
|
|
|
if (Driver::isMongoDB()) { |
268
|
|
|
|
269
|
|
|
$fieldType = $this->getFieldType($tableName, $column); |
270
|
|
|
|
271
|
|
|
if (!in_array($fieldType, Type::getTypes())) { |
272
|
|
|
$this->generateError([$fieldType . " type not supported."]); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$table->{$column} = Type::$fieldType($value); |
276
|
|
|
|
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ($table->update()) { |
283
|
|
|
|
284
|
|
|
foreach ($fields as $field) { |
285
|
|
|
|
286
|
|
|
// return response()->json(['fields' => $fields]); |
287
|
|
|
|
288
|
|
|
if (isset($field->relationship)) { |
289
|
|
|
|
290
|
|
|
$relationship = $field->relationship; |
291
|
|
|
|
292
|
|
|
$localModel = $relationship->localModel; |
293
|
|
|
$localTable = $relationship->localTable; |
294
|
|
|
$foreignModel = $relationship->foreignModel; |
295
|
|
|
|
296
|
|
|
if ($field->relationship->relationType == "belongsToMany") { |
297
|
|
|
$pivotTable = $relationship->pivotTable; |
298
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
299
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
300
|
|
|
|
301
|
|
|
$findColumn = $object->details['findColumn']; |
302
|
|
|
|
303
|
|
|
$localObject = DBM::model($localModel, $localTable)->where($findColumn, $table->{$findColumn})->first(); |
304
|
|
|
|
305
|
|
|
DBM::Object() |
306
|
|
|
->setManyToManyRelation( |
307
|
|
|
$localObject, |
308
|
|
|
$foreignModel, |
309
|
|
|
$pivotTable, |
310
|
|
|
$parentPivotKey, |
311
|
|
|
$relatedPivotKey |
312
|
|
|
) |
313
|
|
|
->belongs_to_many() |
314
|
|
|
->sync($columns->{$relatedPivotKey}); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return response()->json(['success' => true]); |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
} catch (\Exception $e) { |
324
|
|
|
return $this->generateError([$e->getMessage()]); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return response()->json(['success' => false]); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function delete(Request $request) |
332
|
|
|
{ |
333
|
|
|
if ($request->ajax()) { |
334
|
|
|
|
335
|
|
|
if (($response = DBM::authorize('record.delete')) !== true) { |
336
|
|
|
return $response; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$tableName = $request->table; |
340
|
|
|
$originalColumns = Table::getColumnsName($tableName); |
|
|
|
|
341
|
|
|
$columns = json_decode($request->columns); |
342
|
|
|
$fields = $request->fields; |
343
|
|
|
|
344
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
345
|
|
|
$model = $object->model; |
346
|
|
|
$details = $object->details; |
347
|
|
|
$key = $details['findColumn']; |
348
|
|
|
|
349
|
|
|
if (!class_exists($model)) { |
350
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
351
|
|
|
|
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
try { |
355
|
|
|
|
356
|
|
|
$table = DBM::model($model, $tableName)->where($key, $columns->{$key})->first(); |
357
|
|
|
|
358
|
|
|
if ($table) { |
359
|
|
|
|
360
|
|
|
foreach ($fields as $field) { |
361
|
|
|
|
362
|
|
|
$field = json_decode($field); |
363
|
|
|
|
364
|
|
|
if ($field->type == 'relationship') { |
365
|
|
|
|
366
|
|
|
$relationship = $field->settings; |
367
|
|
|
|
368
|
|
|
$localModel = $relationship->localModel; |
369
|
|
|
$foreignModel = $relationship->foreignModel; |
370
|
|
|
|
371
|
|
|
$findColumn = $object->details['findColumn']; |
372
|
|
|
|
373
|
|
|
$localObject = $localModel::where($findColumn, $table->{$findColumn})->first(); |
374
|
|
|
|
375
|
|
|
if ($relationship->relationType == 'belongsToMany') { |
376
|
|
|
|
377
|
|
|
$pivotTable = $relationship->pivotTable; |
378
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
379
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
380
|
|
|
|
381
|
|
|
DBM::Object() |
382
|
|
|
->setManyToManyRelation( |
383
|
|
|
$localObject, |
384
|
|
|
$foreignModel, |
385
|
|
|
$pivotTable, |
386
|
|
|
$parentPivotKey, |
387
|
|
|
$relatedPivotKey |
388
|
|
|
) |
389
|
|
|
->belongs_to_many() |
390
|
|
|
->detach(); |
391
|
|
|
} else if ($relationship->relationType == 'hasMany') { |
392
|
|
|
|
393
|
|
|
$foreignKey = $relationship->foreignKey; |
394
|
|
|
$localKey = $relationship->localKey; |
395
|
|
|
|
396
|
|
|
DBM::Object() |
397
|
|
|
->setCommonRelation( |
398
|
|
|
$localObject, |
399
|
|
|
$foreignModel, |
400
|
|
|
$foreignKey, |
401
|
|
|
$localKey) |
402
|
|
|
->has_many() |
403
|
|
|
->delete(); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
if ($table->delete()) { |
410
|
|
|
return response()->json(['success' => true]); |
|
|
|
|
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
} catch (\Exception $e) { |
415
|
|
|
return $this->generateError([$e->getMessage()]); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
return response()->json(['success' => false]); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
protected function getSettingOptions($field) |
423
|
|
|
{ |
424
|
|
|
$options = $field->settings['options']; |
425
|
|
|
if (isset($options['controller'])) { |
426
|
|
|
$partials = explode('@', $options['controller']); |
427
|
|
|
$controllerName = $partials[0]; |
428
|
|
|
$methodName = $partials[1]; |
429
|
|
|
|
430
|
|
|
return app($controllerName)->{$methodName}(); |
|
|
|
|
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
public function getTableDetails(Request $request) |
435
|
|
|
{ |
436
|
|
|
if ($request->ajax()) { |
437
|
|
|
|
438
|
|
|
if (($response = DBM::authorize('record.browse')) !== true) { |
439
|
|
|
return $response; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
$tableName = $request->table; |
443
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
444
|
|
|
|
445
|
|
|
if (!$object) { |
446
|
|
|
return response()->json([ |
|
|
|
|
447
|
|
|
'success' => false, |
448
|
|
|
'errors' => ["There is no Object details"], |
449
|
|
|
], 400); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$createFields = $object->createFields(); |
453
|
|
|
$browseFields = $object->readFields(); |
454
|
|
|
$editFields = $object->editFields(); |
455
|
|
|
$deleteFields = $object->deleteFields(); |
456
|
|
|
$fields = $object->allFields(); |
457
|
|
|
$foreignTableData = []; |
458
|
|
|
|
459
|
|
|
// return response()->json(['createFields' => auth()->user()]); |
460
|
|
|
|
461
|
|
|
foreach ($createFields as $key => $field) { |
462
|
|
|
|
463
|
|
|
if ($field->type == 'relationship') { |
464
|
|
|
|
465
|
|
|
$relationship = $field->settings; |
466
|
|
|
$relationshipType = $relationship['relationType']; |
|
|
|
|
467
|
|
|
$localModel = $relationship['localModel']; |
|
|
|
|
468
|
|
|
$localKey = $relationship['localKey']; |
|
|
|
|
469
|
|
|
$foreignModel = $relationship['foreignModel']; |
470
|
|
|
$foreignKey = $relationship['foreignKey']; |
471
|
|
|
$relationshipType = $relationship['relationType']; |
472
|
|
|
|
473
|
|
|
// if ($relationshipType == 'belongsTo') { |
474
|
|
|
$createFields = $this->removeRelationshipKeyForBelongsTo($createFields, $foreignKey); |
475
|
|
|
$field->foreignTableData = $foreignModel::all(); |
476
|
|
|
$field->relationship = $relationship; |
477
|
|
|
// } else { |
478
|
|
|
// unset($fields[$key]); |
479
|
|
|
// } |
480
|
|
|
} else { |
481
|
|
|
if (isset($field->settings['options'])) { |
482
|
|
|
$options = $this->getSettingOptions($field); |
483
|
|
|
if (is_array($options)) { |
484
|
|
|
$createFields[$key]->options = $options; |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
foreach ($editFields as $key => $field) { |
492
|
|
|
|
493
|
|
|
if ($field->type == 'relationship') { |
494
|
|
|
|
495
|
|
|
$relationship = $field->settings; |
496
|
|
|
$relationshipType = $relationship['relationType']; |
497
|
|
|
$localModel = $relationship['localModel']; |
498
|
|
|
$localKey = $relationship['localKey']; |
499
|
|
|
$foreignModel = $relationship['foreignModel']; |
500
|
|
|
$foreignKey = $relationship['foreignKey']; |
501
|
|
|
$relationshipType = $relationship['relationType']; |
502
|
|
|
|
503
|
|
|
// if ($relationshipType == 'belongsTo') { |
504
|
|
|
$editFields = $this->removeRelationshipKeyForBelongsTo($editFields, $foreignKey); |
505
|
|
|
$field->foreignTableData = $foreignModel::all(); |
506
|
|
|
$field->relationship = $relationship; |
507
|
|
|
// } else { |
508
|
|
|
// unset($fields[$key]); |
509
|
|
|
// } |
510
|
|
|
continue; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
if (isset($field->settings['options'])) { |
514
|
|
|
$options = $this->getSettingOptions($field); |
515
|
|
|
if (is_array($options)) { |
516
|
|
|
$editFields[$key]->options = $options; |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
$model = $object->model; |
521
|
|
|
|
522
|
|
|
if (!class_exists($model)) { |
523
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
$perPage = (int) $request->perPage; |
527
|
|
|
$query = $request->q; |
528
|
|
|
|
529
|
|
|
$searchColumn = $object->details['searchColumn']; |
530
|
|
|
|
531
|
|
|
$records = DBM::model($model, $tableName)->paginate($perPage); |
532
|
|
|
|
533
|
|
|
// return response()->json(['success' => $perPage]); |
534
|
|
|
|
535
|
|
|
if (!empty($query) && !empty($searchColumn)) { |
536
|
|
|
$records = DBM::model($model, $tableName) |
537
|
|
|
->where($searchColumn, 'LIKE', '%' . $query . '%') |
538
|
|
|
->paginate($perPage); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
foreach ($records as $item => $record) { |
542
|
|
|
|
543
|
|
|
foreach ($browseFields as $field) { |
544
|
|
|
|
545
|
|
|
if ($field->type == 'relationship') { |
546
|
|
|
|
547
|
|
|
$relationship = $field->settings; |
548
|
|
|
|
549
|
|
|
$findColumn = $object->details['findColumn']; |
550
|
|
|
|
551
|
|
|
$localModel = $relationship['localModel']; |
552
|
|
|
$localKey = $relationship['localKey']; |
553
|
|
|
$foreignModel = $relationship['foreignModel']; |
554
|
|
|
$foreignKey = $relationship['foreignKey']; |
555
|
|
|
$relationshipType = $relationship['relationType']; |
556
|
|
|
$displayLabel = $relationship['displayLabel']; |
557
|
|
|
|
558
|
|
|
if ($relationshipType == 'belongsTo') { |
559
|
|
|
|
560
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
561
|
|
|
|
562
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->belongs_to; |
563
|
|
|
|
564
|
|
|
$record->{$field->name} = $datas; |
565
|
|
|
$field->displayLabel = $displayLabel; |
566
|
|
|
$field->localKey = $localKey; |
567
|
|
|
$field->foreignKey = $foreignKey; |
568
|
|
|
$field->relationshipType = $relationshipType; |
569
|
|
|
|
570
|
|
|
} else if ($relationshipType == 'hasMany') { |
571
|
|
|
|
572
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
573
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->has_many; |
574
|
|
|
|
575
|
|
|
$record->{$field->name} = $datas; |
576
|
|
|
$field->displayLabel = $displayLabel; |
577
|
|
|
$field->localKey = $localKey; |
578
|
|
|
$field->foreignKey = $foreignKey; |
579
|
|
|
$field->relationshipType = $relationshipType; |
580
|
|
|
|
581
|
|
|
} else if ($relationshipType == 'belongsToMany') { |
582
|
|
|
|
583
|
|
|
$pivotTable = $relationship['pivotTable']; |
584
|
|
|
$parentPivotKey = $relationship['parentPivotKey']; |
585
|
|
|
$relatedPivotKey = $relationship['relatedPivotKey']; |
586
|
|
|
|
587
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
588
|
|
|
|
589
|
|
|
$datas = DBM::Object()->setManyToManyRelation($localObject, $foreignModel, $pivotTable, $parentPivotKey, $relatedPivotKey)->belongs_to_many; |
590
|
|
|
|
591
|
|
|
$record->{$field->name} = $datas; |
592
|
|
|
$field->displayLabel = $displayLabel; |
593
|
|
|
$field->localKey = $localKey; |
594
|
|
|
$field->foreignKey = $foreignKey; |
595
|
|
|
$field->relationshipType = $relationshipType; |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
$newRecords = []; |
602
|
|
|
$newRecord = new \stdClass; |
603
|
|
|
|
604
|
|
|
foreach ($records as $item => $record) { |
605
|
|
|
|
606
|
|
|
foreach ($fields as $key => &$field) { |
607
|
|
|
|
608
|
|
|
if (isset($record->{$field->name})) { |
609
|
|
|
|
610
|
|
|
$record->{$field->name} = is_json($record->{$field->name}) ? json_decode($record->{$field->name}, true) : $record->{$field->name}; |
611
|
|
|
} |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
if ($request->findValue && $record->{$object->details['findColumn']} == $request->findValue) { |
615
|
|
|
$newRecord = $record; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
$newRecords[] = $record; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
$objectDetails = $object->details; |
622
|
|
|
$objectDetails['perPage'] = $perPage; |
623
|
|
|
|
624
|
|
|
return response()->json([ |
625
|
|
|
'success' => true, |
626
|
|
|
'object' => $object, |
627
|
|
|
'objectDetails' => $objectDetails, |
628
|
|
|
'createFields' => $createFields, |
629
|
|
|
'browseFields' => $browseFields, |
630
|
|
|
'editFields' => $editFields, |
631
|
|
|
'deleteFields' => $deleteFields, |
632
|
|
|
'records' => $newRecords, |
633
|
|
|
'record' => $newRecord, |
634
|
|
|
'foreignTableData' => $foreignTableData, |
635
|
|
|
'userPermissions' => DBM::userPermissions(), |
636
|
|
|
'pagination' => $records, |
637
|
|
|
]); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
return response()->json(['success' => false]); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
public function removeRelationshipKeyForBelongsTo($fields, $foreignKey) |
644
|
|
|
{ |
645
|
|
|
$results = []; |
646
|
|
|
|
647
|
|
|
foreach ($fields as $key => $field) { |
648
|
|
|
if ($field->name == $foreignKey) { |
649
|
|
|
unset($fields[$key]); |
650
|
|
|
continue; |
651
|
|
|
} |
652
|
|
|
$results[] = $field; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
return $results; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
protected function validation($fields, $columns, $action = "create") |
659
|
|
|
{ |
660
|
|
|
$errors = []; |
661
|
|
|
foreach ($fields as $field) { |
662
|
|
|
$name = $field->name; |
663
|
|
|
|
664
|
|
|
if ((is_array($field->settings) || is_object($field->settings)) && array_key_exists('validation', $field->settings) !== false) { |
|
|
|
|
665
|
|
|
$validationSettings = $field->settings->validation; |
666
|
|
|
|
667
|
|
|
if ($action == 'create' && isset($validationSettings->create)) { |
668
|
|
|
$createSettings = $validationSettings->create; |
669
|
|
|
$rules = $createSettings->rules; |
670
|
|
|
} else if ($action == 'update' && isset($validationSettings->update)) { |
671
|
|
|
$updateSettings = $validationSettings->update; |
672
|
|
|
$localKey = $updateSettings->localKey; |
673
|
|
|
$rules = $updateSettings->rules . ',' . $columns->{$localKey}; |
674
|
|
|
} else if (isset($field->settings->validation) && is_string($validationSettings)) { |
675
|
|
|
$rules = $validationSettings; |
676
|
|
|
} else { |
677
|
|
|
$rules = ''; |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
$data = [$name => $columns->{$name}]; |
681
|
|
|
$validator = Validator::make($data, [ |
682
|
|
|
$name => $rules, |
683
|
|
|
]); |
684
|
|
|
|
685
|
|
|
if ($validator->fails()) { |
686
|
|
|
|
687
|
|
|
foreach ($validator->errors()->all() as $error) { |
688
|
|
|
$errors[] = $error; |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
// foreach ($fields as $field) { |
695
|
|
|
|
696
|
|
|
// if ($field->required) { |
697
|
|
|
// $name = $field->name; |
698
|
|
|
// if ($field->type == 'relationship') { |
699
|
|
|
// $relationship = $field->relationship; |
700
|
|
|
// if ($relationship->relationType == 'belongsToMany') { |
701
|
|
|
// $name = $relationship->relatedPivotKey; |
702
|
|
|
// } else { |
703
|
|
|
// $name = $relationship->foreignKey; |
704
|
|
|
// } |
705
|
|
|
|
706
|
|
|
// if (!isset($columns->{$name}) || empty($columns->{$name})) { |
707
|
|
|
// $errors[] = "{$name} Required"; |
708
|
|
|
// } |
709
|
|
|
|
710
|
|
|
// continue; |
711
|
|
|
// } |
712
|
|
|
|
713
|
|
|
// if (!isset($columns->{$name}) || $columns->{$name} == "") { |
714
|
|
|
// $errors[] = "{$name} Required"; |
715
|
|
|
// } |
716
|
|
|
// } |
717
|
|
|
// } |
718
|
|
|
|
719
|
|
|
return $errors; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
protected function getFieldType($collectionName, $fieldName) |
723
|
|
|
{ |
724
|
|
|
$collection = DBM_Collection::where('name', $collectionName)->first(); |
725
|
|
|
|
726
|
|
|
return $collection->fields()->where('name', $fieldName)->first()->type; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
protected function generateError($errors) |
730
|
|
|
{ |
731
|
|
|
return response()->json([ |
|
|
|
|
732
|
|
|
'success' => false, |
733
|
|
|
'errors' => $errors, |
734
|
|
|
], 400); |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
protected function hasFunction($fields, $column) |
738
|
|
|
{ |
739
|
|
|
foreach ($fields as $field) { |
740
|
|
|
if ($field->name == $column && ($field->function_name != null || $field->function_name != "")) { |
741
|
|
|
return $field->function_name; |
742
|
|
|
} |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
return false; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
protected function executeFunction($functionName, $value = null) |
749
|
|
|
{ |
750
|
|
|
$signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()"; |
751
|
|
|
|
752
|
|
|
$result = DB::raw("{$signature}"); |
753
|
|
|
|
754
|
|
|
return $result; |
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
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