1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
4
|
|
|
|
5
|
|
|
use Cache; |
6
|
|
|
use crocodicstudio\crudbooster\exceptions\CBValidationException; |
7
|
|
|
use DB; |
8
|
|
|
use Illuminate\Validation\ValidationException; |
9
|
|
|
use Image; |
10
|
|
|
use Request; |
11
|
|
|
use Route; |
12
|
|
|
use Schema; |
13
|
|
|
use Session; |
14
|
|
|
use Storage; |
15
|
|
|
use Validator; |
16
|
|
|
|
17
|
|
|
class CB |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function getRoleByName($roleName) { |
21
|
|
|
return $this->find("cb_roles",['name'=>$roleName]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function fcm() { |
25
|
|
|
return new FCM(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function sidebar() { |
29
|
|
|
return new SidebarMenus(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function session() { |
33
|
|
|
return new UserSession(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getDeveloperUrl($path = null) { |
37
|
|
|
$path = ($path)?"/".trim($path,"/"):null; |
38
|
|
|
return url(cbConfig("DEV_PATH")).$path; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getProfileUrl() { |
42
|
|
|
return $this->getAdminUrl()."/profile"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getLogoutUrl() { |
46
|
|
|
return $this->getAdminUrl()."/logout"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getLoginUrl() { |
50
|
|
|
return $this->getAdminUrl("login"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getAdminUrl($path = null) { |
54
|
|
|
$path = ($path)?"/".trim($path,"/"):null; |
55
|
|
|
return url(cbConfig("ADMIN_PATH")).$path; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getAppName() { |
59
|
|
|
return env("APP_NAME","CRUDBOOSTER"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function uploadBase64($value) |
63
|
|
|
{ |
64
|
|
|
$fileData = base64_decode($value); |
65
|
|
|
$mime_type = finfo_buffer(finfo_open(), $fileData, FILEINFO_MIME_TYPE); |
66
|
|
|
if($mime_type) { |
67
|
|
|
if($mime_type = explode('/', $mime_type)) { |
68
|
|
|
$ext = $mime_type[1]; |
69
|
|
|
if($ext) { |
70
|
|
|
$filePath = 'uploads/'.date('Y-m'); |
71
|
|
|
Storage::makeDirectory($filePath); |
72
|
|
|
$filename = sha1(strRandom(5)).'.'.$ext; |
73
|
|
|
if (Storage::put($filePath.'/'.$filename, $fileData)) { |
74
|
|
|
self::resizeImage($filePath.'/'.$filename); |
|
|
|
|
75
|
|
|
return $filePath.'/'.$filename; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function uploadFile($name, $encrypt = true, $resize_width = 1024, $resize_height = null) |
84
|
|
|
{ |
85
|
|
|
if (request()->hasFile($name)) { |
86
|
|
|
|
87
|
|
|
$file = request()->file($name); |
88
|
|
|
$ext = $file->getClientOriginalExtension(); |
89
|
|
|
if(in_array($ext,cbConfig("UPLOAD_FILE_EXTENSION_ALLOWED"))) { |
90
|
|
|
$filename = slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)); |
91
|
|
|
$file_path = 'uploads/'.date('Y-m'); |
92
|
|
|
|
93
|
|
|
//Create Directory Monthly |
94
|
|
|
Storage::makeDirectory($file_path); |
95
|
|
|
|
96
|
|
|
if ($encrypt == true) { |
97
|
|
|
$filename = sha1(strRandom(5)).'.'.$ext; |
98
|
|
|
} else { |
99
|
|
|
$filename = slug($filename, '_').'.'.$ext; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (Storage::putFileAs($file_path, $file, $filename)) { |
103
|
|
|
if($resize_width || $resize_height) { |
104
|
|
|
$this->resizeImage($file_path.'/'.$filename, $resize_width, $resize_height); |
105
|
|
|
} |
106
|
|
|
return $file_path.'/'.$filename; |
107
|
|
|
} else { |
108
|
|
|
throw new \Exception("Something went wrong, file can't upload!"); |
109
|
|
|
} |
110
|
|
|
}else{ |
111
|
|
|
throw new \Exception("The file format is not allowed!"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} else { |
115
|
|
|
throw new \Exception("There is no file send to server!"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function resizeImage($fullFilePath, $resize_width = null, $resize_height = null, $qty = 100, $thumbQty = 75) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$images_ext = cbConfig("UPLOAD_IMAGE_EXTENSION_ALLOWED"); |
122
|
|
|
|
123
|
|
|
$filename = basename($fullFilePath); |
124
|
|
|
$file_path = trim(str_replace($filename, '', $fullFilePath), '/'); |
125
|
|
|
$ext = pathinfo($fullFilePath, PATHINFO_EXTENSION); |
126
|
|
|
|
127
|
|
|
if (in_array(strtolower($ext), $images_ext)) { |
128
|
|
|
|
129
|
|
|
if ($resize_width && $resize_height) { |
130
|
|
|
$img = Image::make(storage_path('app/'.$file_path.'/'.$filename)); |
131
|
|
|
$img->fit($resize_width, $resize_height); |
132
|
|
|
$img->save(storage_path('app/'.$file_path.'/'.$filename), $qty); |
133
|
|
|
} elseif ($resize_width && ! $resize_height) { |
134
|
|
|
$img = Image::make(storage_path('app/'.$file_path.'/'.$filename)); |
135
|
|
|
$img->resize($resize_width, null, function ($constraint) { |
136
|
|
|
$constraint->aspectRatio(); |
137
|
|
|
}); |
138
|
|
|
$img->save(storage_path('app/'.$file_path.'/'.$filename), $qty); |
139
|
|
|
} elseif (! $resize_width && $resize_height) { |
140
|
|
|
$img = Image::make(storage_path('app/'.$file_path.'/'.$filename)); |
141
|
|
|
$img->resize(null, $resize_height, function ($constraint) { |
142
|
|
|
$constraint->aspectRatio(); |
143
|
|
|
}); |
144
|
|
|
$img->save(storage_path('app/'.$file_path.'/'.$filename), $qty); |
145
|
|
|
} else { |
146
|
|
|
$img = Image::make(storage_path('app/'.$file_path.'/'.$filename)); |
147
|
|
|
if ($img->width() > 1300) { |
148
|
|
|
$img->resize(1300, null, function ($constraint) { |
149
|
|
|
$constraint->aspectRatio(); |
150
|
|
|
}); |
151
|
|
|
} |
152
|
|
|
$img->save(storage_path('app/'.$file_path.'/'.$filename), $qty); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function update($table, $id, $data) |
158
|
|
|
{ |
159
|
|
|
DB::table($table) |
160
|
|
|
->where($this->pk($table), $id) |
161
|
|
|
->update($data); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function updateCompact($table, $id, $params) { |
165
|
|
|
$data = []; |
166
|
|
|
foreach ($params as $param) { |
167
|
|
|
$data[$param] = request($param); |
168
|
|
|
} |
169
|
|
|
$this->update($table, $id, $data); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function find($table, $id) |
173
|
|
|
{ |
174
|
|
|
if (is_array($id)) { |
175
|
|
|
$first = DB::table($table); |
176
|
|
|
foreach ($id as $k => $v) { |
177
|
|
|
$first->where($k, $v); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $first->first(); |
181
|
|
|
} else { |
182
|
|
|
$pk = $this->pk($table); |
183
|
|
|
|
184
|
|
|
return DB::table($table)->where($pk, $id)->first(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function listAllTable() |
189
|
|
|
{ |
190
|
|
|
return DB::connection()->getDoctrineSchemaManager()->listTableNames(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function findAll($table, $condition_array = []) |
194
|
|
|
{ |
195
|
|
|
return DB::table($table)->where($condition_array)->get(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function redirectBack($message, $type = 'warning') |
199
|
|
|
{ |
200
|
|
|
if (request()->ajax()) { |
201
|
|
|
return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $_SERVER['HTTP_REFERER']]); |
202
|
|
|
} else { |
203
|
|
|
return redirect()->back()->withInput() |
204
|
|
|
->with(['message'=> $message, 'message_type'=> $type]); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function redirect($to, $message, $type = 'warning') |
209
|
|
|
{ |
210
|
|
|
if (Request::ajax()) { |
211
|
|
|
return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]); |
212
|
|
|
} else { |
213
|
|
|
return redirect($to)->with(['message' => $message, 'message_type' => $type]); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
public function getCurrentMethod() |
219
|
|
|
{ |
220
|
|
|
$action = str_replace("App\Http\Controllers", "", Route::currentRouteAction()); |
221
|
|
|
$atloc = strpos($action, '@') + 1; |
222
|
|
|
$method = substr($action, $atloc); |
223
|
|
|
|
224
|
|
|
return $method; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function stringBetween($string, $start, $end) |
228
|
|
|
{ |
229
|
|
|
$string = ' '.$string; |
230
|
|
|
$ini = strpos($string, $start); |
231
|
|
|
if ($ini == 0) { |
232
|
|
|
return ''; |
233
|
|
|
} |
234
|
|
|
$ini += strlen($start); |
235
|
|
|
$len = strpos($string, $end, $ini) - $ini; |
236
|
|
|
|
237
|
|
|
return substr($string, $ini, $len); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param array $rules |
242
|
|
|
* @param array $messages |
243
|
|
|
* @throws CBValidationException |
244
|
|
|
*/ |
245
|
|
|
public function validation($rules = [], $messages = []) |
246
|
|
|
{ |
247
|
|
|
$input_arr = request()->all(); |
248
|
|
|
|
249
|
|
|
foreach ($rules as $a => $b) { |
250
|
|
|
if (is_int($a)) { |
251
|
|
|
$rules[$b] = 'required'; |
252
|
|
|
} else { |
253
|
|
|
$rules[$a] = $b; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$validator = Validator::make($input_arr, $rules, $messages); |
258
|
|
|
if ($validator->fails()) { |
259
|
|
|
$message = $validator->errors()->all(); |
260
|
|
|
throw new CBValidationException(implode("; ",$message)); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function pk($table) |
265
|
|
|
{ |
266
|
|
|
return $this->findPrimaryKey($table); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function findPrimaryKey($table) |
270
|
|
|
{ |
271
|
|
|
$pk = DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey(); |
272
|
|
|
if(!$pk) { |
273
|
|
|
return null; |
274
|
|
|
} |
275
|
|
|
return $pk->getColumns()[0]; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
279
|
|
|
{ |
280
|
|
|
$params = Request::all(); |
281
|
|
|
$mainpath = trim(self::mainpath(), '/'); |
|
|
|
|
282
|
|
|
$key = sanitizeXSS($key); |
283
|
|
|
$type = sanitizeXSS($type); |
284
|
|
|
$value = sanitizeXSS($value); |
285
|
|
|
|
286
|
|
|
if ($params['filter_column'] && $singleSorting) { |
287
|
|
|
foreach ($params['filter_column'] as $k => $filter) { |
288
|
|
|
foreach ($filter as $t => $val) { |
289
|
|
|
if ($t == 'sorting') { |
290
|
|
|
unset($params['filter_column'][$k]['sorting']); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$params['filter_column'][$key][$type] = $value; |
297
|
|
|
|
298
|
|
|
if (isset($params)) { |
299
|
|
|
return $mainpath.'?'.http_build_query($params); |
300
|
|
|
} else { |
301
|
|
|
return $mainpath.'?filter_column['.$key.']['.$type.']='.$value; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
|
306
|
|
|
public function getUrlParameters($exception = null) |
307
|
|
|
{ |
308
|
|
|
$get = request()->all(); |
309
|
|
|
$inputhtml = ''; |
310
|
|
|
|
311
|
|
|
if ($get) { |
|
|
|
|
312
|
|
|
if (is_array($exception)) { |
313
|
|
|
foreach ($exception as $e) { |
314
|
|
|
unset($get[$e]); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
$string_parameters = http_build_query($get); |
318
|
|
|
$string_parameters_array = explode('&', $string_parameters); |
319
|
|
|
foreach ($string_parameters_array as $s) { |
320
|
|
|
$part = explode('=', $s); |
321
|
|
|
if(isset($part[0]) && isset($part[1])) { |
322
|
|
|
$name = htmlspecialchars(urldecode($part[0])); |
323
|
|
|
$name = strip_tags($name); |
324
|
|
|
$value = htmlspecialchars(urldecode($part[1])); |
325
|
|
|
$value = strip_tags($value); |
326
|
|
|
if ($name) { |
327
|
|
|
$inputhtml .= "<input type='hidden' name='$name' value='$value'/>\n"; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $inputhtml; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
|
337
|
|
|
public function routeGet($prefix, $controller) { |
338
|
|
|
$alias = str_replace("@"," ", $controller); |
339
|
|
|
$alias = ucwords($alias); |
340
|
|
|
$alias = str_replace(" ","",$alias); |
341
|
|
|
Route::get($prefix, ['uses' => $controller, 'as' => $alias]); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
public function routePost($prefix, $controller) { |
345
|
|
|
$alias = str_replace("@"," ", $controller); |
346
|
|
|
$alias = ucwords($alias); |
347
|
|
|
$alias = str_replace(" ","",$alias); |
348
|
|
|
Route::post($prefix, ['uses' => $controller, 'as' => $alias]); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function routeGroupBackend(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
352
|
|
|
Route::group([ |
353
|
|
|
'middleware' => ['web', \crocodicstudio\crudbooster\middlewares\CBBackend::class], |
354
|
|
|
'prefix' => cbConfig('ADMIN_PATH'), |
355
|
|
|
'namespace' => $namespace, |
356
|
|
|
], $callback); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/* |
360
|
|
|
| -------------------------------------------------------------------------------------------------------------- |
361
|
|
|
| Alternate route for Laravel Route::controller |
362
|
|
|
| -------------------------------------------------------------------------------------------------------------- |
363
|
|
|
| $prefix = path of route |
364
|
|
|
| $controller = controller name |
365
|
|
|
| $namespace = namespace of controller (optional) |
366
|
|
|
| |
367
|
|
|
*/ |
368
|
|
|
public function routeController($prefix, $controller, $namespace = null) |
369
|
|
|
{ |
370
|
|
|
|
371
|
|
|
$prefix = trim($prefix, '/').'/'; |
372
|
|
|
|
373
|
|
|
$namespace = ($namespace) ?: 'App\Http\Controllers'; |
374
|
|
|
|
375
|
|
|
try { |
376
|
|
|
Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller.'GetIndex']); |
377
|
|
|
|
378
|
|
|
$controller_class = new \ReflectionClass($namespace.'\\'.$controller); |
379
|
|
|
$controller_methods = $controller_class->getMethods(\ReflectionMethod::IS_PUBLIC); |
380
|
|
|
$wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}'; |
381
|
|
|
foreach ($controller_methods as $method) { |
382
|
|
|
|
383
|
|
|
if ($method->class != 'Illuminate\Routing\Controller' && $method->name != 'getIndex') { |
384
|
|
|
if (substr($method->name, 0, 3) == 'get') { |
385
|
|
|
$method_name = substr($method->name, 3); |
386
|
|
|
$slug = array_filter(preg_split('/(?=[A-Z])/', $method_name)); |
|
|
|
|
387
|
|
|
$slug = strtolower(implode('-', $slug)); |
388
|
|
|
$slug = ($slug == 'index') ? '' : $slug; |
389
|
|
|
Route::get($prefix.$slug.$wildcards, ['uses' => $controller.'@'.$method->name, 'as' => $controller.'Get'.$method_name]); |
390
|
|
|
} elseif (substr($method->name, 0, 4) == 'post') { |
391
|
|
|
$method_name = substr($method->name, 4); |
392
|
|
|
$slug = array_filter(preg_split('/(?=[A-Z])/', $method_name)); |
393
|
|
|
Route::post($prefix.strtolower(implode('-', $slug)).$wildcards, [ |
394
|
|
|
'uses' => $controller.'@'.$method->name, |
395
|
|
|
'as' => $controller.'Post'.$method_name, |
396
|
|
|
]); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
} catch (\Exception $e) { |
|
|
|
|
401
|
|
|
|
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|