Completed
Push — master ( 3739d9...3d27ba )
by CodexShaper
05:01
created

RecordHelper::hasFunction()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace CodexShaper\DBM\Traits;
4
5
use CodexShaper\DBM\Facades\Manager as DBM;
6
use CodexShaper\DBM\Models\DBM_Collection;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Validator;
9
10
trait RecordHelper
11
{
12
    public function removeRelationshipData($field, $object, $table)
13
    {
14
        if ($field->type == 'relationship') {
15
16
            $relationship = $field->settings;
17
18
            $localModel   = $relationship->localModel;
19
            $foreignModel = $relationship->foreignModel;
20
21
            $findColumn = $object->details['findColumn'];
22
23
            $localObject = $localModel::where($findColumn, $table->{$findColumn})->first();
24
25
            if ($relationship->relationType == 'belongsToMany') {
26
27
                $pivotTable      = $relationship->pivotTable;
28
                $parentPivotKey  = $relationship->parentPivotKey;
29
                $relatedPivotKey = $relationship->relatedPivotKey;
30
31
                DBM::Object()
32
                    ->setManyToManyRelation(
33
                        $localObject,
34
                        $foreignModel,
35
                        $pivotTable,
36
                        $parentPivotKey,
37
                        $relatedPivotKey
38
                    )
39
                    ->belongs_to_many()
40
                    ->detach();
41
            } else if ($relationship->relationType == 'hasMany') {
42
43
                $foreignKey = $relationship->foreignKey;
44
                $localKey   = $relationship->localKey;
45
46
                DBM::Object()
47
                    ->setCommonRelation(
48
                        $localObject,
49
                        $foreignModel,
50
                        $foreignKey,
51
                        $localKey)
52
                    ->has_many()
53
                    ->delete();
54
            }
55
56
        }
57
    }
58
59
    public function getSettingOptions($field)
60
    {
61
        $options = $field->settings['options'];
62
        if (isset($options['controller'])) {
63
            $partials       = explode('@', $options['controller']);
64
            $controllerName = $partials[0];
65
            $methodName     = $partials[1];
66
67
            return app($controllerName)->{$methodName}();
68
        }
69
    }
70
71
    public function removeRelationshipKeyForBelongsTo($fields, $foreignKey)
72
    {
73
        $results = [];
74
75
        foreach ($fields as $key => $field) {
76
            if ($field->name == $foreignKey) {
77
                unset($fields[$key]);
78
                continue;
79
            }
80
            $results[] = $field;
81
        }
82
83
        return $results;
84
    }
85
86
    public function validation($fields, $columns, $action = "create")
87
    {
88
        $errors = [];
89
        foreach ($fields as $field) {
90
            $name = $field->name;
91
92
            if (is_object($field->settings) && property_exists($field->settings, 'validation') !== false) {
93
94
                $validationSettings = $field->settings->validation;
95
                $rules              = $this->prepareRules($columns, $action, $validationSettings);
96
                $data               = [$name => $columns->{$name}];
97
                $validator          = Validator::make($data, [$name => $rules]);
98
                if ($validator->fails()) {
99
                    foreach ($validator->errors()->all() as $error) {
100
                        $errors[] = $error;
101
                    }
102
                }
103
            }
104
        }
105
106
        return $errors;
107
    }
108
109
    public function prepareRules($columns, $action, $settings)
110
    {
111
        $rules = '';
112
113
        if (is_string($settings)) {
114
            $rules = $settings;
115
        } else if ($action == 'create' && isset($settings->create)) {
116
            $createSettings = $settings->create;
117
            $rules          = $createSettings->rules;
118
        } else if ($action == 'update' && isset($settings->update)) {
119
            $updateSettings = $settings->update;
120
            $localKey       = $updateSettings->localKey;
121
            $rules          = $updateSettings->rules . ',' . $columns->{$localKey};
122
        }
123
124
        return $rules;
125
    }
126
127
    public function getFieldType($collectionName, $fieldName)
128
    {
129
        $collection = DBM_Collection::where('name', $collectionName)->first();
130
131
        return $collection->fields()->where('name', $fieldName)->first()->type;
132
    }
133
134
    public function generateError($errors)
135
    {
136
        return response()->json([
137
            'success' => false,
138
            'errors'  => $errors,
139
        ], 400);
140
    }
141
142
    public function hasFunction($fields, $column)
143
    {
144
        foreach ($fields as $field) {
145
            if ($field->name == $column && ($field->function_name != null || $field->function_name != "")) {
146
                return $field->function_name;
147
            }
148
        }
149
150
        return false;
151
    }
152
153
    public function executeFunction($functionName, $value = null)
154
    {
155
        $signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()";
156
157
        $result = DB::raw("{$signature}");
158
159
        return $result;
160
    }
161
}
162