1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
4
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Facades\CbRouter; |
6
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\ViewHelpers; |
7
|
|
|
use crocodicstudio\crudbooster\Modules\LogsModule\LogsRepository; |
8
|
|
|
use crocodicstudio\crudbooster\Modules\PrivilegeModule\GetCurrentX; |
|
|
|
|
9
|
|
|
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers; |
10
|
|
|
use Session; |
11
|
|
|
use Request; |
12
|
|
|
use Schema; |
13
|
|
|
use Cache; |
14
|
|
|
use DB; |
15
|
|
|
use Route; |
16
|
|
|
use Config; |
17
|
|
|
use Validator; |
18
|
|
|
|
19
|
|
|
class CRUDBooster |
20
|
|
|
{ |
21
|
|
|
use PrivilegeHelpers, GetCurrentX; |
|
|
|
|
22
|
|
|
|
23
|
|
|
public static function parseSqlTable($table) |
24
|
|
|
{ |
25
|
|
|
$f = explode('.', $table); |
26
|
|
|
|
27
|
|
|
if (count($f) == 1) { |
28
|
|
|
return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')]; |
29
|
|
|
} |
30
|
|
|
if (count($f) == 2) { |
31
|
|
|
return ["database" => $f[0], "table" => $f[1]]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (count($f) == 3) { |
35
|
|
|
return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function adminPath($path = null) |
42
|
|
|
{ |
43
|
|
|
return url(cbAdminPath().'/'.$path); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function deleteConfirm($redirectTo) |
47
|
|
|
{ |
48
|
|
|
return view('_admin_template.deleteConfirmModal', ['redirectTo'=> $redirectTo])->render(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function getValueFilter($field) |
52
|
|
|
{ |
53
|
|
|
return self::getFilter($field, 'value'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private static function getFilter($field, $index) |
57
|
|
|
{ |
58
|
|
|
$filter = request('filter_column'); |
59
|
|
|
if ($filter[$field]) { |
60
|
|
|
return $filter[$field][$index]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function getSortingFilter($field) |
65
|
|
|
{ |
66
|
|
|
return self::getFilter($field, 'sorting'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function getTypeFilter($field) |
70
|
|
|
{ |
71
|
|
|
return self::getFilter($field, 'type'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function first($table, $id) |
75
|
|
|
{ |
76
|
|
|
$table = self::parseSqlTable($table)['table']; |
77
|
|
|
|
78
|
|
|
if (! is_array($id)) { |
79
|
|
|
$id = [DbInspector::findPK($table) => $id]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return DB::table($table)->where($id)->first(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
86
|
|
|
{ |
87
|
|
|
return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function mainpath($path = null) |
91
|
|
|
{ |
92
|
|
|
$controllerName = strtok(Route::currentRouteAction(), '@'); |
93
|
|
|
// $controllerName = array_pop(explode('\\', $controllerName)); |
94
|
|
|
$controllerName = basename($controllerName); |
95
|
|
|
$routeUrl = route($controllerName.'GetIndex'); |
96
|
|
|
|
97
|
|
|
if (! $path) { |
98
|
|
|
return trim($routeUrl, '/'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (substr($path, 0, 1) == '?') { |
102
|
|
|
return trim($routeUrl, '/').$path; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $routeUrl.'/'.$path; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public static function listCbTables() |
109
|
|
|
{ |
110
|
|
|
$tables = DbInspector::listTables(); |
111
|
|
|
|
112
|
|
|
$filter = function ($tableName) { |
113
|
|
|
|
114
|
|
|
if ($tableName == config('database.migrations')) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($tableName == 'cms_users') { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (starts_with($tableName, 'cms_')) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
return array_filter($tables, $filter); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public static function getUrlParameters($exception = null) |
133
|
|
|
{ |
134
|
|
|
return ViewHelpers::getUrlParameters($exception); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/* public static function isExistsController($table) |
138
|
|
|
{ |
139
|
|
|
$ctrlName = ucwords(str_replace('_', ' ', $table)); |
140
|
|
|
$ctrlName = str_replace(' ', '', $ctrlName).'Controller.php'; |
141
|
|
|
$path = base_path(controllers_dir()); |
142
|
|
|
$path2 = base_path(controllers_dir()."ControllerMaster/"); |
143
|
|
|
|
144
|
|
|
if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) { |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return false; |
149
|
|
|
}*/ |
150
|
|
|
|
151
|
|
|
public static function routeController(string $prefix, string $controller, $namespace = '') |
152
|
|
|
{ |
153
|
|
|
CbRouter::routeController($prefix, $controller, $namespace); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/* |
157
|
|
|
| ------------------------------------------------------------- |
158
|
|
|
| Alternate route for Laravel Route::controller |
159
|
|
|
| ------------------------------------------------------------- |
160
|
|
|
| $prefix = path of route |
161
|
|
|
| $controller = controller name |
162
|
|
|
| $namespace = namespace of controller (optional) |
163
|
|
|
| |
164
|
|
|
*/ |
165
|
|
|
|
166
|
|
|
public static function redirect($to, $message, $type = 'warning') |
167
|
|
|
{ |
168
|
|
|
if (Request::ajax()) { |
169
|
|
|
sendAndTerminate(response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to])); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
sendAndTerminate(redirect($to)->with(['message' => $message, 'message_type' => $type])); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public static function allowOnlySuperAdmin() |
176
|
|
|
{ |
177
|
|
|
event('cb.unauthorizedTryToSuperAdminArea', [cbUser(), request()->fullUrlWithQuery()]); |
|
|
|
|
178
|
|
|
if (self::isSuperadmin()) { |
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
self::denyAccess(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: