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