Completed
Push — master ( 5af970...ffb252 )
by CodexShaper
04:49
created

ObjectController::getRelationshipDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 2
rs 10
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 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...
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Str;
11
12
class ObjectController extends Controller
13
{
14
15
    public function all(Request $request)
16
    {
17
        if ($request->ajax()) {
18
19
            if (($response = DBM::authorize('crud.browse')) !== true) {
20
                return $response;
21
            }
22
23
            $perPage = (int) $request->perPage;
24
            $query   = $request->q;
25
26
            $userPermissions = DBM::userPermissions();
27
            $tables          = Table::paginate($perPage, null, [], $query);
28
            $newTables       = $this->filterCrudTables($tables);
29
30
            return response()->json([
31
                'success'         => true,
32
                'tables'          => $newTables,
33
                'userPermissions' => $userPermissions,
34
                'pagination'      => $tables,
35
            ]);
36
        }
37
38
        return response()->json(['success' => false]);
39
    }
40
41
    public function filterCrudTables($tables)
42
    {
43
        $objects   = DBM::Object()->all();
44
        $newTables = [];
45
46
        foreach ($tables as $table) {
47
            foreach ($objects as $object) {
48
                if ($table == $object->name) {
49
                    $newTables[] = [
50
                        'name'   => $table,
51
                        'isCrud' => true,
52
                    ];
53
                    continue 2;
54
                }
55
            }
56
57
            $newTables[] = [
58
                'name'   => $table,
59
                'isCrud' => false,
60
            ];
61
        }
62
63
        return $newTables;
64
    }
65
66
    public function getObjectDetails(Request $request)
67
    {
68
        if ($request->ajax()) {
69
70
            try
71
            {
72
                if (!Table::exists($request->table)) {
73
                    throw new \Exception("Sorry! There is no table", 1);
74
                }
75
76
                $tableName  = $request->table;
77
                $details    = $this->getObject($tableName);
78
                $permission = $details['isCrudExists'] ? 'update' : 'create';
79
80
                if (($response = DBM::authorize("crud.{$permission}")) !== true) {
81
                    return $response;
82
                }
83
84
                $relationship = $this->getRelationshipDetails($tableName);
85
86
                return response()->json([
87
                    'success'              => true,
88
                    'relationship_tables'  => $relationship['tables'],
89
                    'relationship_details' => $relationship['details'],
90
                    'object'               => $details['object'],
91
                    'fields'               => $details['fields'],
92
                    'isCrudExists'         => $details['isCrudExists'],
93
                    'userPermissions'      => DBM::userPermissions(),
94
                    'driver'               => Driver::getConnectionName(),
95
                ]);
96
97
            } catch (\Exception $e) {
98
                return $this->generateError([$e->getMessage()]);
99
            }
100
        }
101
102
        return response()->json(['success' => false]);
103
    }
104
105
    public function getObject($tableName)
106
    {
107
        $isCrudExists = false;
108
        $fields       = [];
109
110
        if ($object = DBM::Object()->where('name', $tableName)->first()) {
111
            $isCrudExists = true;
112
            if (!$object->model) {
113
                $object->model = DBM::generateModelName($object->name);
114
            }
115
            $fields = $object->fields()->orderBy('order', 'ASC')->get();
116
        }
117
118
        if (!$object) {
119
            $table = Table::getTable($tableName);
120
121
            $object               = new \stdClass;
122
            $object->name         = $table['name'];
123
            $object->slug         = Str::slug($table['name']);
124
            $object->display_name = ucfirst($table['name']);
125
            $object->model        = DBM::generateModelName($table['name']);
126
            $object->controller   = '';
127
128
            $fields = $this->prepareFields($table);
129
        }
130
131
        $object->makeModel = false;
132
133
        return ['object' => $object, 'fields' => $fields, 'isCrudExists' => $isCrudExists];
134
135
    }
136
137
    public function prepareFields($table)
138
    {
139
        $fields = [];
140
        $order  = 1;
141
142
        foreach ($table['columns'] as $column) {
143
144
            $fields[] = (object) [
145
                'name'          => $column->name,
146
                'display_name'  => ucfirst($column->name),
147
                'type'          => DatabaseController::getInputType($column->type['name']),
148
                'create'        => ($column->autoincrement) ? false : true,
149
                'read'          => ($column->autoincrement) ? false : true,
150
                'edit'          => ($column->autoincrement) ? false : true,
151
                'delete'        => ($column->autoincrement) ? false : true,
152
                'function_name' => '',
153
                'order'         => $order,
154
                'settings'      => '{ }',
155
            ];
156
157
            $order++;
158
        }
159
160
        return $fields;
161
    }
162
163
    public function getRelationshipDetails($tableName)
164
    {
165
        $tables = Table::all();
166
167
        $relationshipDetails = (object) [
168
            'type'                => 'hasOne',
169
            'foreignTableDetails' => Table::getTable($tables[0]),
170
            'localTableDetails'   => Table::getTable($tableName),
171
        ];
172
173
        return [
174
            'tables'  => $tables,
175
            'details' => $relationshipDetails,
176
        ];
177
    }
178
179
    protected function generateError($errors)
180
    {
181
        return response()->json([
182
            'success' => false,
183
            'errors'  => $errors,
184
        ], 400);
185
    }
186
}
187