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\AuthModule\Helpers; |
9
|
|
|
use crocodicstudio\crudbooster\Modules\LogsModule\LogsRepository; |
10
|
|
|
use crocodicstudio\crudbooster\Modules\PrivilegeModule\GetCurrentX; |
|
|
|
|
11
|
|
|
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers; |
12
|
|
|
use Session; |
13
|
|
|
use Request; |
14
|
|
|
use Schema; |
15
|
|
|
use Cache; |
|
|
|
|
16
|
|
|
use DB; |
17
|
|
|
use Route; |
18
|
|
|
use Config; |
19
|
|
|
use Validator; |
20
|
|
|
|
21
|
|
|
class CRUDBooster |
22
|
|
|
{ |
23
|
|
|
use PrivilegeHelpers, GetCurrentX, Helpers; |
|
|
|
|
24
|
|
|
|
25
|
|
|
public static function get($table, $string_conditions = null, $orderby = null, $limit = null, $skip = null) |
26
|
|
|
{ |
27
|
|
|
$table = self::parseSqlTable($table); |
28
|
|
|
$table = $table['table']; |
29
|
|
|
$query = DB::table($table); |
30
|
|
|
if ($string_conditions) { |
31
|
|
|
$query->whereraw($string_conditions); |
32
|
|
|
} |
33
|
|
|
if ($orderby) { |
34
|
|
|
$query->orderbyraw($orderby); |
35
|
|
|
} |
36
|
|
|
if ($limit) { |
37
|
|
|
$query->take($limit); |
38
|
|
|
} |
39
|
|
|
if ($skip) { |
40
|
|
|
$query->skip($skip); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $query->get(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function parseSqlTable($table) |
47
|
|
|
{ |
48
|
|
|
$f = explode('.', $table); |
49
|
|
|
|
50
|
|
|
if (count($f) == 1) { |
51
|
|
|
return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')]; |
52
|
|
|
} |
53
|
|
|
if (count($f) == 2) { |
54
|
|
|
return ["database" => $f[0], "table" => $f[1]]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (count($f) == 3) { |
58
|
|
|
return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function adminPath($path = null) |
65
|
|
|
{ |
66
|
|
|
return url(cbAdminPath().'/'.$path); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function deleteConfirm($redirectTo) |
70
|
|
|
{ |
71
|
|
|
ViewHelpers::delConfirm($redirectTo); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function getValueFilter($field) |
75
|
|
|
{ |
76
|
|
|
self::getFilter($field, 'value'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private static function getFilter($field, $index) |
80
|
|
|
{ |
81
|
|
|
$filter = request('filter_column'); |
82
|
|
|
if ($filter[$field]) { |
83
|
|
|
return $filter[$field][$index]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function getSortingFilter($field) |
88
|
|
|
{ |
89
|
|
|
return self::getFilter($field, 'sorting'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function getTypeFilter($field) |
93
|
|
|
{ |
94
|
|
|
return self::getFilter($field, 'type'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* public static function stringBetween($string, $start, $end) |
98
|
|
|
{ |
99
|
|
|
$string = ' '.$string; |
100
|
|
|
$ini = strpos($string, $start); |
101
|
|
|
if ($ini == 0) { |
102
|
|
|
return ''; |
103
|
|
|
} |
104
|
|
|
$ini += strlen($start); |
105
|
|
|
$len = strpos($string, $end, $ini) - $ini; |
106
|
|
|
|
107
|
|
|
return substr($string, $ini, $len); |
108
|
|
|
}*/ |
109
|
|
|
|
110
|
|
|
public static function first($table, $id) |
111
|
|
|
{ |
112
|
|
|
$table = self::parseSqlTable($table)['table']; |
113
|
|
|
if (! is_array($id)) { |
114
|
|
|
$pk = self::pk($table); |
|
|
|
|
115
|
|
|
|
116
|
|
|
return DB::table($table)->where($pk, $id)->first(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$first = DB::table($table); |
120
|
|
|
foreach ($id as $k => $v) { |
121
|
|
|
$first->where($k, $v); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $first->first(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function getTableForeignKey($fieldName) |
128
|
|
|
{ |
129
|
|
|
if (substr($fieldName, 0, 3) == 'id_' || substr($fieldName, -3) == '_id') { |
130
|
|
|
return str_replace(['_id', 'id_'], '', $fieldName); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
135
|
|
|
{ |
136
|
|
|
return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting); |
137
|
|
|
} |
138
|
|
|
public static function mainpath($path = null) |
139
|
|
|
{ |
140
|
|
|
$controllerName = strtok(Route::currentRouteAction(), '@'); |
141
|
|
|
// $controllerName = array_pop(explode('\\', $controllerName)); |
142
|
|
|
$controllerName = basename($controllerName); |
143
|
|
|
$route_url = route($controllerName.'GetIndex'); |
144
|
|
|
|
145
|
|
|
if (! $path) { |
146
|
|
|
return trim($route_url, '/'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (substr($path, 0, 1) == '?') { |
150
|
|
|
return trim($route_url, '/').$path; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $route_url.'/'.$path; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public static function insertLog($description) |
157
|
|
|
{ |
158
|
|
|
LogsRepository::insertLog('crudbooster: '.$description, self::myId()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public static function insertTryLog($action, $name = '') |
162
|
|
|
{ |
163
|
|
|
self::insertLog(trans("logging.log_try_".$action, ['name' => $name, 'module' => GetCurrentX::getCurrentModule()])); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public static function referer() |
167
|
|
|
{ |
168
|
|
|
return Request::server('HTTP_REFERER'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public static function listCbTables() |
172
|
|
|
{ |
173
|
|
|
$tablesList = []; |
174
|
|
|
foreach (DbInspector::listTables() as $tableObj) { |
175
|
|
|
|
176
|
|
|
$tableName = $tableObj->TABLE_NAME; |
177
|
|
|
if ($tableName == config('database.migrations')) { |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
if (substr($tableName, 0, 4) == 'cms_' && $tableName != 'cms_users') { |
181
|
|
|
continue; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$tablesList[] = $tableName; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $tablesList; |
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])); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
sendAndTerminate(redirect($to)->with(['message' => $message, 'message_type' => $type])); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public static function componentsPath($type = '') |
234
|
|
|
{ |
235
|
|
|
$componentPath = implode(DIRECTORY_SEPARATOR, ['vendor', 'crocodicstudio', 'crudbooster', 'src', 'views', 'default', 'type_components', $type]); |
236
|
|
|
return base_path($componentPath); |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public static function PublishedComponentsPath($type = '') |
241
|
|
|
{ |
242
|
|
|
$Path = implode(DIRECTORY_SEPARATOR, ['views', 'vendor', 'crudbooster', 'type_components', $type]); |
243
|
|
|
return resource_path($Path); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
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: