Completed
Push — master ( d6a5af...93636f )
by Iman
16s
created

CRUDBooster::getTableColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace crocodicstudio\crudbooster\helpers;
4
5
use crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo;
6
use crocodicstudio\crudbooster\CBCoreModule\RouteController;
7
use crocodicstudio\crudbooster\CBCoreModule\ViewHelpers;
8
use crocodicstudio\crudbooster\Modules\LogsModule\LogsRepository;
9
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers;
10
use Session;
11
use Request;
12
use Schema;
13
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...
14
use DB;
15
use Route;
16
use Config;
17
use Validator;
18
19
class CRUDBooster
20
{
21
    use PrivilegeHelpers;
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...
22
23
    public static function get($table, $string_conditions = null, $orderby = null, $limit = null, $skip = null)
24
    {
25
        $table = self::parseSqlTable($table);
26
        $table = $table['table'];
27
        $query = DB::table($table);
28
        if ($string_conditions) {
29
            $query->whereraw($string_conditions);
30
        }
31
        if ($orderby) {
32
            $query->orderbyraw($orderby);
33
        }
34
        if ($limit) {
35
            $query->take($limit);
36
        }
37
        if ($skip) {
38
            $query->skip($skip);
39
        }
40
41
        return $query->get();
42
    }
43
44
    public static function parseSqlTable($table)
45
    {
46
        $f = explode('.', $table);
47
48
        if (count($f) == 1) {
49
            return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')];
50
        }
51
        if (count($f) == 2) {
52
            return ["database" => $f[0], "table" => $f[1]];
53
        }
54
55
        if (count($f) == 3) {
56
            return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]];
57
        }
58
59
        return false;
60
    }
61
62
    public static function me()
63
    {
64
        return CbUsersRepo::find(session('admin_id'));
65
    }
66
67
    public static function myName()
68
    {
69
        return session('admin_name');
70
    }
71
72
    public static function myPhoto()
73
    {
74
        return session('admin_photo');
75
    }
76
77
    public static function isLocked()
78
    {
79
        return session('admin_lock');
80
    }
81
82
    public static function getCurrentModule()
83
    {
84
        return GetCurrentX::getCurrentModule();
85
    }
86
87
    public static function getCurrentMenuId()
88
    {
89
        return GetCurrentX::getCurrentMenuId();
90
    }
91
92
    public static function adminPath($path = null)
93
    {
94
        return url(cbAdminPath().'/'.$path);
95
    }
96
97
    public static function deleteConfirm($redirectTo)
98
    {
99
        ViewHelpers::delConfirm($redirectTo);
100
    }
101
102
    public static function getCurrentId()
103
    {
104
        return GetCurrentX::getCurrentId();
105
    }
106
107
    public static function getCurrentMethod()
108
    {
109
        return GetCurrentX::getCurrentMethod();
110
    }
111
112
    public static function getValueFilter($field)
113
    {
114
        self::getFilter($field, 'value');
115
    }
116
117
    private static function getFilter($field, $index)
118
    {
119
        $filter = request('filter_column');
120
        if ($filter[$field]) {
121
            return $filter[$field][$index];
122
        }
123
    }
124
125
    public static function getSortingFilter($field)
126
    {
127
        return self::getFilter($field, 'sorting');
128
    }
129
130
    public static function getTypeFilter($field)
131
    {
132
        return self::getFilter($field, 'type');
133
    }
134
135
/*    public static function stringBetween($string, $start, $end)
136
    {
137
        $string = ' '.$string;
138
        $ini = strpos($string, $start);
139
        if ($ini == 0) {
140
            return '';
141
        }
142
        $ini += strlen($start);
143
        $len = strpos($string, $end, $ini) - $ini;
144
145
        return substr($string, $ini, $len);
146
    }*/
147
148
    public static function first($table, $id)
149
    {
150
        $table = self::parseSqlTable($table)['table'];
151
        if (! is_array($id)) {
152
            $pk = self::pk($table);
0 ignored issues
show
Bug introduced by
The method pk() does not exist on crocodicstudio\crudbooster\helpers\CRUDBooster. ( Ignorable by Annotation )

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

152
            /** @scrutinizer ignore-call */ 
153
            $pk = self::pk($table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
154
            return DB::table($table)->where($pk, $id)->first();
155
        }
156
157
        $first = DB::table($table);
158
        foreach ($id as $k => $v) {
159
            $first->where($k, $v);
160
        }
161
162
        return $first->first();
163
    }
164
165
    public static function getTableForeignKey($fieldName)
166
    {
167
        if (substr($fieldName, 0, 3) == 'id_' || substr($fieldName, -3) == '_id') {
168
            return str_replace(['_id', 'id_'], '', $fieldName);
169
        }
170
    }
171
172
    public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true)
173
    {
174
        return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting);
175
    }
176
    public static function mainpath($path = null)
177
    {
178
        $controllerName = strtok(Route::currentRouteAction(), '@');
179
        // $controllerName = array_pop(explode('\\', $controllerName));
180
        $controllerName = basename($controllerName);
181
        $route_url = route($controllerName.'GetIndex');
182
183
        if (! $path) {
184
            return trim($route_url, '/');
185
        }
186
187
        if (substr($path, 0, 1) == '?') {
188
            return trim($route_url, '/').$path;
189
        }
190
191
        return $route_url.'/'.$path;
192
    }
193
194
    public static function insertLog($description)
195
    {
196
        LogsRepository::insertLog('crudbooster: '.$description, self::myId());
197
    }
198
199
    public static function insertTryLog($action, $name = '')
200
    {
201
        self::insertLog(trans("logging.log_try_".$action, ['name' => $name, 'module' => self::getCurrentModule()]));
202
    }
203
204
    public static function myId()
205
    {
206
        return session('admin_id');
207
    }
208
209
    public static function referer()
210
    {
211
        return Request::server('HTTP_REFERER');
212
    }
213
214
    public static function listCbTables()
215
    {
216
        $tablesList = [];
217
        foreach (DbInspector::listTables() as $tableObj) {
218
219
            $tableName = $tableObj->TABLE_NAME;
220
            if ($tableName == config('database.migrations')) {
221
                continue;
222
            }
223
            if (substr($tableName, 0, 4) == 'cms_' && $tableName != 'cms_users') {
224
                continue;
225
            }
226
227
            $tablesList[] = $tableName;
228
        }
229
230
        return $tablesList;
231
    }
232
233
    public static function getUrlParameters($exception = null)
234
    {
235
        return ViewHelpers::getUrlParameters($exception);
236
    }
237
238
/*    public static function isExistsController($table)
239
    {
240
        $ctrlName = ucwords(str_replace('_', ' ', $table));
241
        $ctrlName = str_replace(' ', '', $ctrlName).'Controller.php';
242
        $path = base_path(controllers_dir());
243
        $path2 = base_path(controllers_dir()."ControllerMaster/");
244
245
        if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) {
246
            return true;
247
        }
248
249
        return false;
250
    }*/
251
252
    public static function routeController($prefix, $controller, $namespace = null)
253
    {
254
        RouteController::routeController($prefix, $controller, $namespace);
255
    }
256
257
    /*
258
    | -------------------------------------------------------------
259
    | Alternate route for Laravel Route::controller
260
    | -------------------------------------------------------------
261
    | $prefix       = path of route
262
    | $controller   = controller name
263
    | $namespace    = namespace of controller (optional)
264
    |
265
    */
266
267
    public static function denyAccess()
268
    {
269
        static::redirect(static::adminPath(), cbTrans('denied_access'));
270
    }
271
272
    public static function redirect($to, $message, $type = 'warning')
273
    {
274
        if (Request::ajax()) {
275
            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

275
            sendAndTerminate(response()->/** @scrutinizer ignore-call */ json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]));
Loading history...
276
        }
277
278
        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

278
        sendAndTerminate(/** @scrutinizer ignore-type */ redirect($to)->with(['message' => $message, 'message_type' => $type]));
Loading history...
279
    }
280
281
    public static function componentsPath($type = '')
282
    {
283
        $componentPath = implode(DIRECTORY_SEPARATOR, ['vendor', 'crocodicstudio', 'crudbooster', 'src', 'views', 'default', 'type_components', $type]);
284
        return base_path($componentPath);
285
286
    }
287
288
    public static function PublishedComponentsPath($type = '')
289
    {
290
        $Path = implode(DIRECTORY_SEPARATOR, ['views', 'vendor', 'crudbooster', 'type_components', $type]);
291
        return resource_path($Path);
292
    }
293
}
294