Passed
Pull Request — master (#1136)
by Iman
04:10
created

CRUDBooster::getTableForeignKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 2
nop 1
dl 0
loc 4
c 0
b 0
f 0
cc 3
rs 10
1
<?php
2
3
namespace crocodicstudio\crudbooster\helpers;
4
5
use crocodicstudio\crudbooster\CBCoreModule\RouteController;
6
use crocodicstudio\crudbooster\CBCoreModule\ViewHelpers;
7
use crocodicstudio\crudbooster\Modules\AuthModule\Helpers;
8
use crocodicstudio\crudbooster\Modules\LogsModule\LogsRepository;
9
use crocodicstudio\crudbooster\Modules\PrivilegeModule\GetCurrentX;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, crocodicstudio\crudbooster\helpers\GetCurrentX. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers;
11
use Session;
12
use Request;
13
use Schema;
14
use Cache;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, crocodicstudio\crudbooster\helpers\Cache. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use DB;
16
use Route;
17
use Config;
18
use Validator;
19
20
class CRUDBooster
21
{
22
    use PrivilegeHelpers, GetCurrentX, Helpers;
0 ignored issues
show
introduced by
The trait crocodicstudio\crudboost...Module\PrivilegeHelpers requires some properties which are not provided by crocodicstudio\crudbooster\helpers\CRUDBooster: $path, $is_read, $is_visible, $is_delete, $is_create, $is_edit
Loading history...
23
24
    public static function get($table, $string_conditions = null, $orderby = null, $limit = null, $skip = null)
25
    {
26
        $table = self::parseSqlTable($table);
27
        $table = $table['table'];
28
        $query = DB::table($table);
29
        if ($string_conditions) {
30
            $query->whereraw($string_conditions);
31
        }
32
        if ($orderby) {
33
            $query->orderbyraw($orderby);
34
        }
35
        if ($limit) {
36
            $query->take($limit);
37
        }
38
        if ($skip) {
39
            $query->skip($skip);
40
        }
41
42
        return $query->get();
43
    }
44
45
    public static function parseSqlTable($table)
46
    {
47
        $f = explode('.', $table);
48
49
        if (count($f) == 1) {
50
            return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')];
51
        }
52
        if (count($f) == 2) {
53
            return ["database" => $f[0], "table" => $f[1]];
54
        }
55
56
        if (count($f) == 3) {
57
            return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]];
58
        }
59
60
        return false;
61
    }
62
63
    public static function adminPath($path = null)
64
    {
65
        return url(cbAdminPath().'/'.$path);
66
    }
67
68
    public static function deleteConfirm($redirectTo)
69
    {
70
        ViewHelpers::delConfirm($redirectTo);
71
    }
72
73
    public static function getValueFilter($field)
74
    {
75
        self::getFilter($field, 'value');
76
    }
77
78
    private static function getFilter($field, $index)
79
    {
80
        $filter = request('filter_column');
81
        if ($filter[$field]) {
82
            return $filter[$field][$index];
83
        }
84
    }
85
86
    public static function getSortingFilter($field)
87
    {
88
        return self::getFilter($field, 'sorting');
89
    }
90
91
    public static function getTypeFilter($field)
92
    {
93
        return self::getFilter($field, 'type');
94
    }
95
96
    /*    public static function stringBetween($string, $start, $end)
97
        {
98
            $string = ' '.$string;
99
            $ini = strpos($string, $start);
100
            if ($ini == 0) {
101
                return '';
102
            }
103
            $ini += strlen($start);
104
            $len = strpos($string, $end, $ini) - $ini;
105
106
            return substr($string, $ini, $len);
107
        }*/
108
109
    public static function first($table, $id)
110
    {
111
        $table = self::parseSqlTable($table)['table'];
112
        if (! is_array($id)) {
113
            $pk = DbInspector::findPK($table);
114
115
            return DB::table($table)->where($pk, $id)->first();
116
        }
117
118
        $first = DB::table($table);
119
        foreach ($id as $k => $v) {
120
            $first->where($k, $v);
121
        }
122
123
        return $first->first();
124
    }
125
126
    public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true)
127
    {
128
        return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting);
129
    }
130
131
    public static function mainpath($path = null)
132
    {
133
        $controllerName = strtok(Route::currentRouteAction(), '@');
134
        // $controllerName = array_pop(explode('\\', $controllerName));
135
        $controllerName = basename($controllerName);
136
        $route_url = route($controllerName.'GetIndex');
137
138
        if (! $path) {
139
            return trim($route_url, '/');
140
        }
141
142
        if (substr($path, 0, 1) == '?') {
143
            return trim($route_url, '/').$path;
144
        }
145
146
        return $route_url.'/'.$path;
147
    }
148
149
    public static function insertTryLog($action, $name = '')
150
    {
151
        self::insertLog(trans("logging.log_try_".$action, ['name' => $name, 'module' => GetCurrentX::getCurrentModule()]));
152
    }
153
154
    public static function insertLog($description)
155
    {
156
        LogsRepository::insertLog('crudbooster: '.$description, self::myId());
157
    }
158
159
    public static function referer()
160
    {
161
        return Request::server('HTTP_REFERER');
162
    }
163
164
    public static function listCbTables()
165
    {
166
        $tables = array_map(function ($table) {
167
            return $table->TABLE_NAME;
168
        }, DbInspector::listTables());
169
170
        $filter = function ($tableName) {
171
172
            if ($tableName == config('database.migrations')) {
173
                return false;
174
            }
175
176
            if ($tableName == 'cms_users') {
177
                return true;
178
            }
179
180
            if (starts_with($tableName, 'cms_')) {
181
                return false;
182
            }
183
184
            return true;
185
        };
186
187
        return array_filter($tables, $filter);
188
    }
189
190
    public static function getUrlParameters($exception = null)
191
    {
192
        return ViewHelpers::getUrlParameters($exception);
193
    }
194
195
    /*    public static function isExistsController($table)
196
        {
197
            $ctrlName = ucwords(str_replace('_', ' ', $table));
198
            $ctrlName = str_replace(' ', '', $ctrlName).'Controller.php';
199
            $path = base_path(controllers_dir());
200
            $path2 = base_path(controllers_dir()."ControllerMaster/");
201
202
            if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) {
203
                return true;
204
            }
205
206
            return false;
207
        }*/
208
209
    public static function routeController($prefix, $controller, $namespace = null)
210
    {
211
        RouteController::routeController($prefix, $controller, $namespace);
212
    }
213
214
    /*
215
    | -------------------------------------------------------------
216
    | Alternate route for Laravel Route::controller
217
    | -------------------------------------------------------------
218
    | $prefix       = path of route
219
    | $controller   = controller name
220
    | $namespace    = namespace of controller (optional)
221
    |
222
    */
223
224
    public static function redirect($to, $message, $type = 'warning')
225
    {
226
        if (Request::ajax()) {
227
            sendAndTerminate(response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]));
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
            sendAndTerminate(response()->/** @scrutinizer ignore-call */ json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]));
Loading history...
228
        }
229
230
        sendAndTerminate(redirect($to)->with(['message' => $message, 'message_type' => $type]));
0 ignored issues
show
Bug introduced by
It seems like redirect($to)->with(arra...essage_type' => $type)) can also be of type Illuminate\Routing\Redirector; however, parameter $response of sendAndTerminate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
        sendAndTerminate(/** @scrutinizer ignore-type */ redirect($to)->with(['message' => $message, 'message_type' => $type]));
Loading history...
231
    }
232
233
    public static function componentsPath($type = '')
234
    {
235
        $componentPath = implode(DIRECTORY_SEPARATOR, ['vendor', 'crocodicstudio', 'crudbooster', 'src', 'views', 'form', 'type_components', $type]);
236
237
        return base_path($componentPath);
238
    }
239
240
    public static function PublishedComponentsPath($type = '')
241
    {
242
        $Path = implode(DIRECTORY_SEPARATOR, ['views', 'vendor', 'crudbooster', 'type_components', $type]);
243
244
        return resource_path($Path);
245
    }
246
}
247