Passed
Push — master ( b05bb8...3739d9 )
by CodexShaper
05:15
created

RecordController   F

Complexity

Total Complexity 114

Size/Duplication

Total Lines 721
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 356
c 5
b 0
f 0
dl 0
loc 721
ccs 0
cts 478
cp 0
rs 2
wmc 114

14 Methods

Rating   Name   Duplication   Size   Complexity  
F store() 0 167 23
A index() 0 3 1
F update() 0 137 25
A getSettingOptions() 0 9 2
A removeRelationshipKeyForBelongsTo() 0 13 3
B delete() 0 46 8
F getTableDetails() 0 207 27
A generateError() 0 6 1
A prepareRules() 0 16 6
A validation() 0 21 6
A hasFunction() 0 9 5
A getFieldType() 0 5 1
A executeFunction() 0 7 2
A removeRelationshipData() 0 42 4

How to fix   Complexity   

Complex Class

Complex classes like RecordController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RecordController, and based on these observations, apply Extract Interface, too.

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;
0 ignored issues
show
Bug introduced by
The type DBM was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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, $object, $table);
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, $object, $table)
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
670
                $validationSettings = $field->settings->validation;
671
                $rules              = $this->prepareRules($columns, $action, $validationSettings);
672
                $data               = [$name => $columns->{$name}];
673
                $validator          = Validator::make($data, [$name => $rules]);
674
                if ($validator->fails()) {
675
                    foreach ($validator->errors()->all() as $error) {
676
                        $errors[] = $error;
677
                    }
678
                }
679
            }
680
        }
681
682
        return $errors;
683
    }
684
685
    protected function prepareRules($columns, $action, $settings)
686
    {
687
        $rules = '';
688
689
        if (is_string($settings)) {
690
            $rules = $settings;
691
        } else if ($action == 'create' && isset($settings->create)) {
692
            $createSettings = $settings->create;
693
            $rules          = $createSettings->rules;
694
        } else if ($action == 'update' && isset($settings->update)) {
695
            $updateSettings = $settings->update;
696
            $localKey       = $updateSettings->localKey;
697
            $rules          = $updateSettings->rules . ',' . $columns->{$localKey};
698
        }
699
700
        return $rules;
701
    }
702
703
    protected function getFieldType($collectionName, $fieldName)
704
    {
705
        $collection = DBM_Collection::where('name', $collectionName)->first();
706
707
        return $collection->fields()->where('name', $fieldName)->first()->type;
708
    }
709
710
    protected function generateError($errors)
711
    {
712
        return response()->json([
713
            'success' => false,
714
            'errors'  => $errors,
715
        ], 400);
716
    }
717
718
    protected function hasFunction($fields, $column)
719
    {
720
        foreach ($fields as $field) {
721
            if ($field->name == $column && ($field->function_name != null || $field->function_name != "")) {
722
                return $field->function_name;
723
            }
724
        }
725
726
        return false;
727
    }
728
729
    protected function executeFunction($functionName, $value = null)
730
    {
731
        $signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()";
732
733
        $result = DB::raw("{$signature}");
734
735
        return $result;
736
    }
737
}
738