1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
4
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\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 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
|
|
|
return ViewHelpers::delConfirm($redirectTo); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function getValueFilter($field) |
74
|
|
|
{ |
75
|
|
|
return 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 first($table, $id) |
97
|
|
|
{ |
98
|
|
|
$table = self::parseSqlTable($table)['table']; |
99
|
|
|
if (! is_array($id)) { |
100
|
|
|
$pk = DbInspector::findPK($table); |
101
|
|
|
|
102
|
|
|
return DB::table($table)->where($pk, $id)->first(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$first = DB::table($table); |
106
|
|
|
foreach ($id as $k => $v) { |
107
|
|
|
$first->where($k, $v); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $first->first(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
114
|
|
|
{ |
115
|
|
|
return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function mainpath($path = null) |
119
|
|
|
{ |
120
|
|
|
$controllerName = strtok(Route::currentRouteAction(), '@'); |
121
|
|
|
// $controllerName = array_pop(explode('\\', $controllerName)); |
122
|
|
|
$controllerName = basename($controllerName); |
123
|
|
|
$routeUrl = route($controllerName.'GetIndex'); |
124
|
|
|
|
125
|
|
|
if (! $path) { |
126
|
|
|
return trim($routeUrl, '/'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (substr($path, 0, 1) == '?') { |
130
|
|
|
return trim($routeUrl, '/').$path; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $routeUrl.'/'.$path; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function insertTryLog($action, $name = '') |
137
|
|
|
{ |
138
|
|
|
self::insertLog(trans("logging.log_try_".$action, ['name' => $name, 'module' => GetCurrentX::getCurrentModule()])); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public static function insertLog($description) |
142
|
|
|
{ |
143
|
|
|
LogsRepository::insertLog('crudbooster: '.$description, self::myId()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public static function listCbTables() |
147
|
|
|
{ |
148
|
|
|
$tables = DbInspector::listTables(); |
149
|
|
|
|
150
|
|
|
$filter = function ($tableName) { |
151
|
|
|
|
152
|
|
|
if ($tableName == config('database.migrations')) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($tableName == 'cms_users') { |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (starts_with($tableName, 'cms_')) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
}; |
166
|
|
|
|
167
|
|
|
return array_filter($tables, $filter); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public static function getUrlParameters($exception = null) |
171
|
|
|
{ |
172
|
|
|
return ViewHelpers::getUrlParameters($exception); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/* public static function isExistsController($table) |
176
|
|
|
{ |
177
|
|
|
$ctrlName = ucwords(str_replace('_', ' ', $table)); |
178
|
|
|
$ctrlName = str_replace(' ', '', $ctrlName).'Controller.php'; |
179
|
|
|
$path = base_path(controllers_dir()); |
180
|
|
|
$path2 = base_path(controllers_dir()."ControllerMaster/"); |
181
|
|
|
|
182
|
|
|
if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) { |
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return false; |
187
|
|
|
}*/ |
188
|
|
|
|
189
|
|
|
public static function routeController(string $prefix, string $controller, $namespace = null) |
190
|
|
|
{ |
191
|
|
|
CbRouter::routeController($prefix, $controller, $namespace); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/* |
195
|
|
|
| ------------------------------------------------------------- |
196
|
|
|
| Alternate route for Laravel Route::controller |
197
|
|
|
| ------------------------------------------------------------- |
198
|
|
|
| $prefix = path of route |
199
|
|
|
| $controller = controller name |
200
|
|
|
| $namespace = namespace of controller (optional) |
201
|
|
|
| |
202
|
|
|
*/ |
203
|
|
|
|
204
|
|
|
public static function redirect($to, $message, $type = 'warning') |
205
|
|
|
{ |
206
|
|
|
if (Request::ajax()) { |
207
|
|
|
sendAndTerminate(response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to])); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
sendAndTerminate(redirect($to)->with(['message' => $message, 'message_type' => $type])); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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: