|
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
|
|
|
$value = $values; |
|
74
|
|
|
if (!Driver::isMongoDB()) { |
|
75
|
|
|
$value = is_array($values) ? json_encode($values) : $values; |
|
76
|
|
|
} |
|
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
|
|
|
try { |
|
354
|
|
|
|
|
355
|
|
|
$table = DBM::model($model, $tableName)->where($key, $columns->{$key})->first(); |
|
356
|
|
|
|
|
357
|
|
|
if ($table) { |
|
358
|
|
|
|
|
359
|
|
|
foreach ($fields as $field) { |
|
360
|
|
|
|
|
361
|
|
|
$field = json_decode($field); |
|
362
|
|
|
|
|
363
|
|
|
$this->removeRelationshipData($field); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
if ($table->delete()) { |
|
367
|
|
|
return response()->json(['success' => true]); |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
} catch (\Exception $e) { |
|
372
|
|
|
return $this->generateError([$e->getMessage()]); |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
return response()->json(['success' => false]); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
public function removeRelationshipData($field) |
|
380
|
|
|
{ |
|
381
|
|
|
if ($field->type == 'relationship') { |
|
382
|
|
|
|
|
383
|
|
|
$relationship = $field->settings; |
|
384
|
|
|
|
|
385
|
|
|
$localModel = $relationship->localModel; |
|
386
|
|
|
$foreignModel = $relationship->foreignModel; |
|
387
|
|
|
|
|
388
|
|
|
$findColumn = $object->details['findColumn']; |
|
|
|
|
|
|
389
|
|
|
|
|
390
|
|
|
$localObject = $localModel::where($findColumn, $table->{$findColumn})->first(); |
|
|
|
|
|
|
391
|
|
|
|
|
392
|
|
|
if ($relationship->relationType == 'belongsToMany') { |
|
393
|
|
|
|
|
394
|
|
|
$pivotTable = $relationship->pivotTable; |
|
395
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
|
396
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
|
397
|
|
|
|
|
398
|
|
|
DBM::Object() |
|
399
|
|
|
->setManyToManyRelation( |
|
400
|
|
|
$localObject, |
|
401
|
|
|
$foreignModel, |
|
402
|
|
|
$pivotTable, |
|
403
|
|
|
$parentPivotKey, |
|
404
|
|
|
$relatedPivotKey |
|
405
|
|
|
) |
|
406
|
|
|
->belongs_to_many() |
|
407
|
|
|
->detach(); |
|
408
|
|
|
} else if ($relationship->relationType == 'hasMany') { |
|
409
|
|
|
|
|
410
|
|
|
$foreignKey = $relationship->foreignKey; |
|
411
|
|
|
$localKey = $relationship->localKey; |
|
412
|
|
|
|
|
413
|
|
|
DBM::Object() |
|
414
|
|
|
->setCommonRelation( |
|
415
|
|
|
$localObject, |
|
416
|
|
|
$foreignModel, |
|
417
|
|
|
$foreignKey, |
|
418
|
|
|
$localKey) |
|
419
|
|
|
->has_many() |
|
420
|
|
|
->delete(); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
protected function getSettingOptions($field) |
|
427
|
|
|
{ |
|
428
|
|
|
$options = $field->settings['options']; |
|
429
|
|
|
if (isset($options['controller'])) { |
|
430
|
|
|
$partials = explode('@', $options['controller']); |
|
431
|
|
|
$controllerName = $partials[0]; |
|
432
|
|
|
$methodName = $partials[1]; |
|
433
|
|
|
|
|
434
|
|
|
return app($controllerName)->{$methodName}(); |
|
435
|
|
|
} |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
public function getTableDetails(Request $request) |
|
439
|
|
|
{ |
|
440
|
|
|
if ($request->ajax()) { |
|
441
|
|
|
|
|
442
|
|
|
if (($response = DBM::authorize('record.browse')) !== true) { |
|
443
|
|
|
return $response; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
$tableName = $request->table; |
|
447
|
|
|
$object = DBM::Object()->where('name', $tableName)->first(); |
|
448
|
|
|
|
|
449
|
|
|
if (!$object) { |
|
450
|
|
|
return response()->json([ |
|
451
|
|
|
'success' => false, |
|
452
|
|
|
'errors' => ["There is no Object details"], |
|
453
|
|
|
], 400); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
$createFields = $object->createFields(); |
|
457
|
|
|
$browseFields = $object->readFields(); |
|
458
|
|
|
$editFields = $object->editFields(); |
|
459
|
|
|
$deleteFields = $object->deleteFields(); |
|
460
|
|
|
$fields = $object->allFields(); |
|
461
|
|
|
$foreignTableData = []; |
|
462
|
|
|
|
|
463
|
|
|
// return response()->json(['createFields' => auth()->user()]); |
|
464
|
|
|
|
|
465
|
|
|
foreach ($createFields as $key => $field) { |
|
466
|
|
|
|
|
467
|
|
|
if ($field->type == 'relationship') { |
|
468
|
|
|
|
|
469
|
|
|
$relationship = $field->settings; |
|
470
|
|
|
// $relationshipType = $relationship['relationType']; |
|
471
|
|
|
// $localModel = $relationship['localModel']; |
|
472
|
|
|
// $localKey = $relationship['localKey']; |
|
473
|
|
|
$foreignModel = $relationship['foreignModel']; |
|
474
|
|
|
$foreignKey = $relationship['foreignKey']; |
|
475
|
|
|
// $relationshipType = $relationship['relationType']; |
|
476
|
|
|
|
|
477
|
|
|
// if ($relationshipType == 'belongsTo') { |
|
478
|
|
|
$createFields = $this->removeRelationshipKeyForBelongsTo($createFields, $foreignKey); |
|
479
|
|
|
$field->foreignTableData = $foreignModel::all(); |
|
480
|
|
|
$field->relationship = $relationship; |
|
481
|
|
|
// } else { |
|
482
|
|
|
// unset($fields[$key]); |
|
483
|
|
|
// } |
|
484
|
|
|
} else { |
|
485
|
|
|
if (isset($field->settings['options'])) { |
|
486
|
|
|
$options = $this->getSettingOptions($field); |
|
487
|
|
|
if (is_array($options)) { |
|
488
|
|
|
$createFields[$key]->options = $options; |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
} |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
foreach ($editFields as $key => $field) { |
|
496
|
|
|
|
|
497
|
|
|
if ($field->type == 'relationship') { |
|
498
|
|
|
|
|
499
|
|
|
$relationship = $field->settings; |
|
500
|
|
|
// $relationshipType = $relationship['relationType']; |
|
501
|
|
|
// $localModel = $relationship['localModel']; |
|
502
|
|
|
// $localKey = $relationship['localKey']; |
|
503
|
|
|
$foreignModel = $relationship['foreignModel']; |
|
504
|
|
|
$foreignKey = $relationship['foreignKey']; |
|
505
|
|
|
// $relationshipType = $relationship['relationType']; |
|
506
|
|
|
|
|
507
|
|
|
// if ($relationshipType == 'belongsTo') { |
|
508
|
|
|
$editFields = $this->removeRelationshipKeyForBelongsTo($editFields, $foreignKey); |
|
509
|
|
|
$field->foreignTableData = $foreignModel::all(); |
|
510
|
|
|
$field->relationship = $relationship; |
|
511
|
|
|
// } else { |
|
512
|
|
|
// unset($fields[$key]); |
|
513
|
|
|
// } |
|
514
|
|
|
continue; |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
if (isset($field->settings['options'])) { |
|
518
|
|
|
$options = $this->getSettingOptions($field); |
|
519
|
|
|
if (is_array($options)) { |
|
520
|
|
|
$editFields[$key]->options = $options; |
|
521
|
|
|
} |
|
522
|
|
|
} |
|
523
|
|
|
} |
|
524
|
|
|
$model = $object->model; |
|
525
|
|
|
|
|
526
|
|
|
if (!class_exists($model)) { |
|
527
|
|
|
return $this->generateError(["Model not found. Please create model first"]); |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
$perPage = (int) $request->perPage; |
|
531
|
|
|
$query = $request->q; |
|
532
|
|
|
|
|
533
|
|
|
$searchColumn = $object->details['searchColumn']; |
|
534
|
|
|
|
|
535
|
|
|
$records = DBM::model($model, $tableName)->paginate($perPage); |
|
536
|
|
|
|
|
537
|
|
|
// return response()->json(['success' => $perPage]); |
|
538
|
|
|
|
|
539
|
|
|
if (!empty($query) && !empty($searchColumn)) { |
|
540
|
|
|
$records = DBM::model($model, $tableName) |
|
541
|
|
|
->where($searchColumn, 'LIKE', '%' . $query . '%') |
|
542
|
|
|
->paginate($perPage); |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
foreach ($records as $item => $record) { |
|
546
|
|
|
|
|
547
|
|
|
foreach ($browseFields as $field) { |
|
548
|
|
|
|
|
549
|
|
|
if ($field->type == 'relationship') { |
|
550
|
|
|
|
|
551
|
|
|
$relationship = $field->settings; |
|
552
|
|
|
|
|
553
|
|
|
$findColumn = $object->details['findColumn']; |
|
554
|
|
|
|
|
555
|
|
|
$localModel = $relationship['localModel']; |
|
556
|
|
|
$localKey = $relationship['localKey']; |
|
557
|
|
|
$foreignModel = $relationship['foreignModel']; |
|
558
|
|
|
$foreignKey = $relationship['foreignKey']; |
|
559
|
|
|
$relationshipType = $relationship['relationType']; |
|
560
|
|
|
$displayLabel = $relationship['displayLabel']; |
|
561
|
|
|
|
|
562
|
|
|
if ($relationshipType == 'belongsTo') { |
|
563
|
|
|
|
|
564
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
|
565
|
|
|
|
|
566
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->belongs_to; |
|
567
|
|
|
|
|
568
|
|
|
$record->{$field->name} = $datas; |
|
569
|
|
|
$field->displayLabel = $displayLabel; |
|
570
|
|
|
$field->localKey = $localKey; |
|
571
|
|
|
$field->foreignKey = $foreignKey; |
|
572
|
|
|
$field->relationshipType = $relationshipType; |
|
573
|
|
|
|
|
574
|
|
|
} else if ($relationshipType == 'hasMany') { |
|
575
|
|
|
|
|
576
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
|
577
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->has_many; |
|
578
|
|
|
|
|
579
|
|
|
$record->{$field->name} = $datas; |
|
580
|
|
|
$field->displayLabel = $displayLabel; |
|
581
|
|
|
$field->localKey = $localKey; |
|
582
|
|
|
$field->foreignKey = $foreignKey; |
|
583
|
|
|
$field->relationshipType = $relationshipType; |
|
584
|
|
|
|
|
585
|
|
|
} else if ($relationshipType == 'belongsToMany') { |
|
586
|
|
|
|
|
587
|
|
|
$pivotTable = $relationship['pivotTable']; |
|
588
|
|
|
$parentPivotKey = $relationship['parentPivotKey']; |
|
589
|
|
|
$relatedPivotKey = $relationship['relatedPivotKey']; |
|
590
|
|
|
|
|
591
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
|
592
|
|
|
|
|
593
|
|
|
$datas = DBM::Object()->setManyToManyRelation($localObject, $foreignModel, $pivotTable, $parentPivotKey, $relatedPivotKey)->belongs_to_many; |
|
594
|
|
|
|
|
595
|
|
|
$record->{$field->name} = $datas; |
|
596
|
|
|
$field->displayLabel = $displayLabel; |
|
597
|
|
|
$field->localKey = $localKey; |
|
598
|
|
|
$field->foreignKey = $foreignKey; |
|
599
|
|
|
$field->relationshipType = $relationshipType; |
|
600
|
|
|
} |
|
601
|
|
|
} |
|
602
|
|
|
} |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
$newRecords = []; |
|
606
|
|
|
$newRecord = new \stdClass; |
|
607
|
|
|
|
|
608
|
|
|
foreach ($records as $item => $record) { |
|
609
|
|
|
|
|
610
|
|
|
foreach ($fields as $key => &$field) { |
|
611
|
|
|
|
|
612
|
|
|
if (isset($record->{$field->name})) { |
|
613
|
|
|
|
|
614
|
|
|
$record->{$field->name} = is_json($record->{$field->name}) ? json_decode($record->{$field->name}, true) : $record->{$field->name}; |
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
if ($request->findValue && $record->{$object->details['findColumn']} == $request->findValue) { |
|
619
|
|
|
$newRecord = $record; |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
|
|
$newRecords[] = $record; |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
$objectDetails = $object->details; |
|
626
|
|
|
$objectDetails['perPage'] = $perPage; |
|
627
|
|
|
|
|
628
|
|
|
return response()->json([ |
|
629
|
|
|
'success' => true, |
|
630
|
|
|
'object' => $object, |
|
631
|
|
|
'objectDetails' => $objectDetails, |
|
632
|
|
|
'createFields' => $createFields, |
|
633
|
|
|
'browseFields' => $browseFields, |
|
634
|
|
|
'editFields' => $editFields, |
|
635
|
|
|
'deleteFields' => $deleteFields, |
|
636
|
|
|
'records' => $newRecords, |
|
637
|
|
|
'record' => $newRecord, |
|
638
|
|
|
'foreignTableData' => $foreignTableData, |
|
639
|
|
|
'userPermissions' => DBM::userPermissions(), |
|
640
|
|
|
'pagination' => $records, |
|
641
|
|
|
]); |
|
642
|
|
|
} |
|
643
|
|
|
|
|
644
|
|
|
return response()->json(['success' => false]); |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
public function removeRelationshipKeyForBelongsTo($fields, $foreignKey) |
|
648
|
|
|
{ |
|
649
|
|
|
$results = []; |
|
650
|
|
|
|
|
651
|
|
|
foreach ($fields as $key => $field) { |
|
652
|
|
|
if ($field->name == $foreignKey) { |
|
653
|
|
|
unset($fields[$key]); |
|
654
|
|
|
continue; |
|
655
|
|
|
} |
|
656
|
|
|
$results[] = $field; |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
|
|
return $results; |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
protected function validation($fields, $columns, $action = "create") |
|
663
|
|
|
{ |
|
664
|
|
|
$errors = []; |
|
665
|
|
|
foreach ($fields as $field) { |
|
666
|
|
|
$name = $field->name; |
|
667
|
|
|
|
|
668
|
|
|
if (is_object($field->settings) && property_exists($field->settings, 'validation') !== false) { |
|
669
|
|
|
$validationSettings = $field->settings->validation; |
|
670
|
|
|
|
|
671
|
|
|
if ($action == 'create' && isset($validationSettings->create)) { |
|
672
|
|
|
$createSettings = $validationSettings->create; |
|
673
|
|
|
$rules = $createSettings->rules; |
|
674
|
|
|
} else if ($action == 'update' && isset($validationSettings->update)) { |
|
675
|
|
|
$updateSettings = $validationSettings->update; |
|
676
|
|
|
$localKey = $updateSettings->localKey; |
|
677
|
|
|
$rules = $updateSettings->rules . ',' . $columns->{$localKey}; |
|
678
|
|
|
} else if (isset($field->settings->validation) && is_string($validationSettings)) { |
|
679
|
|
|
$rules = $validationSettings; |
|
680
|
|
|
} else { |
|
681
|
|
|
$rules = ''; |
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
$data = [$name => $columns->{$name}]; |
|
685
|
|
|
$validator = Validator::make($data, [ |
|
686
|
|
|
$name => $rules, |
|
687
|
|
|
]); |
|
688
|
|
|
|
|
689
|
|
|
if ($validator->fails()) { |
|
690
|
|
|
|
|
691
|
|
|
foreach ($validator->errors()->all() as $error) { |
|
692
|
|
|
$errors[] = $error; |
|
693
|
|
|
} |
|
694
|
|
|
} |
|
695
|
|
|
} |
|
696
|
|
|
} |
|
697
|
|
|
|
|
698
|
|
|
return $errors; |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
protected function getFieldType($collectionName, $fieldName) |
|
702
|
|
|
{ |
|
703
|
|
|
$collection = DBM_Collection::where('name', $collectionName)->first(); |
|
704
|
|
|
|
|
705
|
|
|
return $collection->fields()->where('name', $fieldName)->first()->type; |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
protected function generateError($errors) |
|
709
|
|
|
{ |
|
710
|
|
|
return response()->json([ |
|
711
|
|
|
'success' => false, |
|
712
|
|
|
'errors' => $errors, |
|
713
|
|
|
], 400); |
|
714
|
|
|
} |
|
715
|
|
|
|
|
716
|
|
|
protected function hasFunction($fields, $column) |
|
717
|
|
|
{ |
|
718
|
|
|
foreach ($fields as $field) { |
|
719
|
|
|
if ($field->name == $column && ($field->function_name != null || $field->function_name != "")) { |
|
720
|
|
|
return $field->function_name; |
|
721
|
|
|
} |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
return false; |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
protected function executeFunction($functionName, $value = null) |
|
728
|
|
|
{ |
|
729
|
|
|
$signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()"; |
|
730
|
|
|
|
|
731
|
|
|
$result = DB::raw("{$signature}"); |
|
732
|
|
|
|
|
733
|
|
|
return $result; |
|
734
|
|
|
} |
|
735
|
|
|
} |
|
736
|
|
|
|
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