1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use crocodicstudio\crudbooster\exceptions\CBValidationException; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Illuminate\Support\Facades\Config; |
10
|
|
|
use Illuminate\Validation\ValidationException; |
11
|
|
|
use Intervention\Image\Facades\Image; |
12
|
|
|
use Request; |
13
|
|
|
use Route; |
14
|
|
|
use Illuminate\Support\Facades\Schema; |
15
|
|
|
use Illuminate\Support\Facades\Session; |
16
|
|
|
use Illuminate\Support\Facades\Storage; |
17
|
|
|
use Validator; |
18
|
|
|
|
19
|
|
|
class CB |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
public function htmlHelper() { |
23
|
|
|
return (new HTMLHelper()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getRoleByName($roleName) { |
27
|
|
|
return $this->find("cb_roles",['name'=>$roleName]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function fcm() { |
31
|
|
|
return new FCM(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function sidebar() { |
35
|
|
|
return new SidebarMenus(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function session() { |
39
|
|
|
return new UserSession(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getDeveloperPath($path = null) { |
43
|
|
|
$path = ($path)?"/".trim($path,"/"):null; |
44
|
|
|
return "developer/".getSetting("developer_path").$path; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getDeveloperUrl($path = null) { |
48
|
|
|
return url($this->getDeveloperPath($path)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getProfileUrl() { |
52
|
|
|
return $this->getAdminUrl()."/profile"; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getLogoutUrl() { |
56
|
|
|
return $this->getAdminUrl()."/logout"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getLoginUrl() { |
60
|
|
|
return $this->getAdminUrl("login"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getAdminPath() { |
64
|
|
|
return getSetting("ADMIN_PATH","admin"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getAdminUrl($path = null) { |
68
|
|
|
$path = ($path)?"/".trim($path,"/"):null; |
69
|
|
|
return url($this->getAdminPath()).$path; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getAppName() { |
73
|
|
|
return getSetting("APP_NAME", env("APP_NAME","CRUDBOOSTER")); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $filename |
78
|
|
|
* @param $extension |
79
|
|
|
* @param $file |
80
|
|
|
* @param null $resize_width |
|
|
|
|
81
|
|
|
* @param null $resize_height |
|
|
|
|
82
|
|
|
* @return string |
83
|
|
|
* @throws \Exception |
84
|
|
|
*/ |
85
|
|
|
private function uploadFileProcess($filename, $extension, $file, $encrypt = true, $resize_width = null, $resize_height = null) |
86
|
|
|
{ |
87
|
|
|
if(in_array($extension,cbConfig("UPLOAD_FILE_EXTENSION_ALLOWED"))) { |
88
|
|
|
$file_path = cbConfig("UPLOAD_PATH_FORMAT"); |
89
|
|
|
$file_path = str_replace("{Y}",date('Y'), $file_path); |
90
|
|
|
$file_path = str_replace("{m}", date('m'), $file_path); |
91
|
|
|
$file_path = str_replace("{d}", date("d"), $file_path); |
92
|
|
|
|
93
|
|
|
//Create Directory Base On Template |
94
|
|
|
Storage::makeDirectory($file_path); |
95
|
|
|
Storage::put($file_path."/index.html"," ","public"); |
96
|
|
|
Storage::put($file_path."/.gitignore","!.gitignore","public"); |
97
|
|
|
|
98
|
|
|
if ($encrypt == true) { |
99
|
|
|
$filename = md5(strRandom(5)).'.'.$extension; |
100
|
|
|
} else { |
101
|
|
|
$filename = slug($filename, '_').'.'.$extension; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if($resize_width || $resize_height) { |
|
|
|
|
105
|
|
|
$this->resizeImage($file, $file_path.'/'.$filename, $resize_width, $resize_height); |
106
|
|
|
return $file_path.'/'.$filename; |
107
|
|
|
}else{ |
108
|
|
|
if (Storage::putFileAs($file_path, $file, $filename, 'public')) { |
109
|
|
|
return $file_path.'/'.$filename; |
110
|
|
|
} else { |
111
|
|
|
throw new \Exception("Something went wrong, file can't upload!"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
}else{ |
115
|
|
|
throw new \Exception("The file format is not allowed!"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $base64_value |
121
|
|
|
* @param bool $encrypt |
122
|
|
|
* @param null $resize_width |
|
|
|
|
123
|
|
|
* @param null $resize_height |
|
|
|
|
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
|
|
public function uploadBase64($filename, $base64_value, $encrypt = true, $resize_width = null, $resize_height = null) |
127
|
|
|
{ |
128
|
|
|
$fileData = base64_decode($base64_value); |
129
|
|
|
$mime_type = finfo_buffer(finfo_open(), $fileData, FILEINFO_MIME_TYPE); |
130
|
|
|
if($mime_type) { |
131
|
|
|
if($mime_type = explode('/', $mime_type)) { |
132
|
|
|
$ext = $mime_type[1]; |
133
|
|
|
if($filename && $ext) { |
134
|
|
|
return $this->uploadFileProcess($filename, $ext, $fileData, $encrypt, $resize_width, $resize_height); |
135
|
|
|
} |
136
|
|
|
}else { |
137
|
|
|
throw new \Exception("Mime type not found"); |
138
|
|
|
} |
139
|
|
|
}else{ |
140
|
|
|
throw new \Exception("Mime type not found"); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $name |
146
|
|
|
* @param bool $encrypt |
147
|
|
|
* @param int $resize_width |
148
|
|
|
* @param null $resize_height |
|
|
|
|
149
|
|
|
* @return string |
150
|
|
|
* @throws \Exception |
151
|
|
|
*/ |
152
|
|
|
public function uploadFile($name, $encrypt = true, $resize_width = null, $resize_height = null) |
153
|
|
|
{ |
154
|
|
|
if (request()->hasFile($name)) { |
155
|
|
|
$file = request()->file($name); |
156
|
|
|
$filename = $file->getClientOriginalName(); |
157
|
|
|
$ext = strtolower($file->getClientOriginalExtension()); |
158
|
|
|
|
159
|
|
|
if($filename && $ext) { |
160
|
|
|
return $this->uploadFileProcess($filename, $ext, $file, $encrypt, $resize_width, $resize_height); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} else { |
164
|
|
|
throw new \Exception("There is no file send to server!"); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $file |
170
|
|
|
* @param $fullFilePath |
171
|
|
|
* @param null $resize_width |
|
|
|
|
172
|
|
|
* @param null $resize_height |
|
|
|
|
173
|
|
|
* @param int $qty |
174
|
|
|
* @param int $thumbQty |
175
|
|
|
* @throws \Exception |
176
|
|
|
*/ |
177
|
|
|
public function resizeImage($file, $fullFilePath, $resize_width = null, $resize_height = null, $qty = 100, $thumbQty = 75) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$images_ext = cbConfig("UPLOAD_IMAGE_EXTENSION_ALLOWED"); |
180
|
|
|
|
181
|
|
|
$filename = basename($fullFilePath); |
182
|
|
|
$file_path = trim(str_replace($filename, '', $fullFilePath), '/'); |
|
|
|
|
183
|
|
|
$ext = pathinfo($fullFilePath, PATHINFO_EXTENSION); |
184
|
|
|
|
185
|
|
|
if (in_array(strtolower($ext), $images_ext)) { |
186
|
|
|
|
187
|
|
|
// Upload file |
188
|
|
|
$img = Image::make($file); |
189
|
|
|
$img->encode($ext, $qty); |
190
|
|
|
|
191
|
|
|
if ($resize_width && $resize_height) { |
|
|
|
|
192
|
|
|
$img->fit($resize_width, $resize_height); |
193
|
|
|
|
194
|
|
|
} elseif ($resize_width && ! $resize_height) { |
|
|
|
|
195
|
|
|
|
196
|
|
|
$img->resize($resize_width, null, function ($constraint) { |
197
|
|
|
$constraint->aspectRatio(); |
198
|
|
|
}); |
199
|
|
|
|
200
|
|
|
} elseif (! $resize_width && $resize_height) { |
|
|
|
|
201
|
|
|
|
202
|
|
|
$img->resize(null, $resize_height, function ($constraint) { |
203
|
|
|
$constraint->aspectRatio(); |
204
|
|
|
}); |
205
|
|
|
|
206
|
|
|
} else { |
207
|
|
|
|
208
|
|
|
if ($img->width() > cbConfig("DEFAULT_IMAGE_MAX_WIDTH_RES")) { |
209
|
|
|
$img->resize(cbConfig("DEFAULT_IMAGE_MAX_WIDTH_RES"), null, function ($constraint) { |
210
|
|
|
$constraint->aspectRatio(); |
211
|
|
|
}); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
Storage::put($fullFilePath, $img, 'public'); |
216
|
|
|
}else{ |
217
|
|
|
throw new \Exception("The file format is not allowed!"); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param $table |
223
|
|
|
* @param $id |
224
|
|
|
* @param $data |
225
|
|
|
*/ |
226
|
|
|
public function update($table, $id, $data) |
227
|
|
|
{ |
228
|
|
|
DB::table($table) |
229
|
|
|
->where($this->pk($table), $id) |
230
|
|
|
->update($data); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param $table |
235
|
|
|
* @param $id |
236
|
|
|
* @param $params |
237
|
|
|
*/ |
238
|
|
|
public function updateCompact($table, $id, $params) { |
239
|
|
|
$data = []; |
240
|
|
|
foreach ($params as $param) { |
241
|
|
|
$data[$param] = request($param); |
242
|
|
|
} |
243
|
|
|
$this->update($table, $id, $data); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param $table |
248
|
|
|
* @param $id |
249
|
|
|
*/ |
250
|
|
|
public function delete($table, $id) |
251
|
|
|
{ |
252
|
|
|
DB::table($table)->where($this->pk($table), $id)->delete(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param $table |
257
|
|
|
* @param $id |
258
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|mixed|null|object |
259
|
|
|
*/ |
260
|
|
|
public function find($table, $id) |
261
|
|
|
{ |
262
|
|
|
if (is_array($id)) { |
263
|
|
|
$idHash = md5("find".$table.serialize($id)); |
264
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
265
|
|
|
|
266
|
|
|
$first = DB::table($table); |
267
|
|
|
foreach ($id as $k => $v) { |
268
|
|
|
$first->where($k, $v); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$data = $first->first(); |
272
|
|
|
miscellanousSingleton()->setData($idHash,$data); |
273
|
|
|
return $data; |
274
|
|
|
} else { |
275
|
|
|
$idHash = md5("find".$table.$id); |
276
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
277
|
|
|
|
278
|
|
|
$pk = $this->pk($table); |
279
|
|
|
$data = DB::table($table)->where($pk, $id)->first(); |
280
|
|
|
miscellanousSingleton()->setData($idHash,$data); |
281
|
|
|
return $data; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param $table |
287
|
|
|
* @param callable|string|null $conditionOrCallback |
288
|
|
|
* @return \Illuminate\Support\Collection|mixed |
289
|
|
|
*/ |
290
|
|
|
public function findAll($table, $conditionOrCallback = null) |
291
|
|
|
{ |
292
|
|
|
$data = []; |
293
|
|
|
$idHash = null; |
294
|
|
|
|
295
|
|
|
if(is_array($conditionOrCallback)) { |
296
|
|
|
$idHash = md5("findAll".$table.serialize($conditionOrCallback)); |
297
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
298
|
|
|
|
299
|
|
|
$data = DB::table($table)->where($conditionOrCallback)->get(); |
300
|
|
|
} elseif (is_callable($conditionOrCallback)) { |
301
|
|
|
$idHash = "findAll".$table.spl_object_hash($conditionOrCallback); |
|
|
|
|
302
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
303
|
|
|
|
304
|
|
|
$data = DB::table($table); |
305
|
|
|
$data = call_user_func($conditionOrCallback, $data); |
306
|
|
|
$data = $data->get(); |
307
|
|
|
} else { |
308
|
|
|
$idHash = md5("findAll".$table.$conditionOrCallback); |
|
|
|
|
309
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
310
|
|
|
|
311
|
|
|
$data = DB::table($table); |
312
|
|
|
if($conditionOrCallback) { |
313
|
|
|
$data = $data->whereRaw($conditionOrCallback); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
$data = $data->get(); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if($idHash && $data) { |
319
|
|
|
miscellanousSingleton()->setData($idHash, $data); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return $data; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function listAllTable() |
326
|
|
|
{ |
327
|
|
|
$idHash = md5("listAllTable"); |
328
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
329
|
|
|
$data = DB::connection()->getDoctrineSchemaManager()->listTableNames(); |
330
|
|
|
miscellanousSingleton()->setData($idHash, $data); |
331
|
|
|
return $data; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function listAllColumns($table) |
335
|
|
|
{ |
336
|
|
|
$idHash = md5("listAllColumns".$table); |
337
|
|
|
if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
338
|
|
|
$data = Schema::getColumnListing($table); |
339
|
|
|
miscellanousSingleton()->setData($idHash, $data); |
340
|
|
|
return $data; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function redirectBack($message, $type = 'warning') |
344
|
|
|
{ |
345
|
|
|
if (request()->ajax()) { |
346
|
|
|
return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $_SERVER['HTTP_REFERER']]); |
347
|
|
|
} else { |
348
|
|
|
return redirect()->back()->withInput() |
349
|
|
|
->with(['message'=> $message, 'message_type'=> $type]); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function redirect($to, $message, $type = 'warning') |
354
|
|
|
{ |
355
|
|
|
if (Request::ajax()) { |
356
|
|
|
return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]); |
357
|
|
|
} else { |
358
|
|
|
return redirect($to)->with(['message' => $message, 'message_type' => $type]); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
public function getCurrentMethod() |
364
|
|
|
{ |
365
|
|
|
$action = str_replace("App\Http\Controllers", "", Route::currentRouteAction()); |
366
|
|
|
$atloc = strpos($action, '@') + 1; |
367
|
|
|
$method = substr($action, $atloc); |
368
|
|
|
|
369
|
|
|
return $method; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function stringBetween($string, $start, $end) |
373
|
|
|
{ |
374
|
|
|
$string = ' '.$string; |
375
|
|
|
$ini = strpos($string, $start); |
376
|
|
|
if ($ini == 0) { |
377
|
|
|
return ''; |
378
|
|
|
} |
379
|
|
|
$ini += strlen($start); |
380
|
|
|
$len = strpos($string, $end, $ini) - $ini; |
381
|
|
|
|
382
|
|
|
return substr($string, $ini, $len); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @param array $rules |
387
|
|
|
* @param array $messages |
388
|
|
|
* @throws CBValidationException |
389
|
|
|
*/ |
390
|
|
|
public function validation($rules = [], $messages = []) |
391
|
|
|
{ |
392
|
|
|
$input_arr = request()->all(); |
393
|
|
|
|
394
|
|
|
foreach ($rules as $a => $b) { |
395
|
|
|
if (is_int($a)) { |
396
|
|
|
$rules[$b] = 'required'; |
397
|
|
|
} else { |
398
|
|
|
$rules[$a] = $b; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
$validator = Validator::make($input_arr, $rules, $messages); |
403
|
|
|
if ($validator->fails()) { |
404
|
|
|
$message = $validator->errors()->all(); |
405
|
|
|
throw new CBValidationException(implode("; ",$message)); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
public function pk($table) |
410
|
|
|
{ |
411
|
|
|
return $this->findPrimaryKey($table); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
public function findPrimaryKey($table) |
415
|
|
|
{ |
416
|
|
|
$pk = DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey(); |
417
|
|
|
if(!$pk) { |
418
|
|
|
return null; |
419
|
|
|
} |
420
|
|
|
return $pk->getColumns()[0]; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
public function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
424
|
|
|
{ |
425
|
|
|
$params = Request::all(); |
426
|
|
|
$mainpath = trim(self::mainpath(), '/'); |
|
|
|
|
427
|
|
|
$key = sanitizeXSS($key); |
428
|
|
|
$type = sanitizeXSS($type); |
429
|
|
|
$value = sanitizeXSS($value); |
430
|
|
|
|
431
|
|
|
if ($params['filter_column'] && $singleSorting) { |
432
|
|
|
foreach ($params['filter_column'] as $k => $filter) { |
433
|
|
|
foreach ($filter as $t => $val) { |
434
|
|
|
if ($t == 'sorting') { |
435
|
|
|
unset($params['filter_column'][$k]['sorting']); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
$params['filter_column'][$key][$type] = $value; |
442
|
|
|
|
443
|
|
|
if (isset($params)) { |
444
|
|
|
return $mainpath.'?'.http_build_query($params); |
445
|
|
|
} else { |
446
|
|
|
return $mainpath.'?filter_column['.$key.']['.$type.']='.$value; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
|
451
|
|
|
public function getUrlParameters($exception = null) |
452
|
|
|
{ |
453
|
|
|
$get = request()->all(); |
454
|
|
|
$inputhtml = ''; |
455
|
|
|
|
456
|
|
|
if ($get) { |
|
|
|
|
457
|
|
|
if (is_array($exception)) { |
458
|
|
|
foreach ($exception as $e) { |
459
|
|
|
unset($get[$e]); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
$string_parameters = http_build_query($get); |
463
|
|
|
$string_parameters_array = explode('&', $string_parameters); |
464
|
|
|
foreach ($string_parameters_array as $s) { |
465
|
|
|
$part = explode('=', $s); |
466
|
|
|
if(isset($part[0]) && isset($part[1])) { |
467
|
|
|
$name = htmlspecialchars(urldecode($part[0])); |
468
|
|
|
$name = strip_tags($name); |
469
|
|
|
$value = htmlspecialchars(urldecode($part[1])); |
470
|
|
|
$value = strip_tags($value); |
471
|
|
|
if ($name) { |
472
|
|
|
$inputhtml .= "<input type='hidden' name='$name' value='$value'/>\n"; |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return $inputhtml; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
|
482
|
|
|
public function routeGet($prefix, $controller) { |
483
|
|
|
$alias = str_replace("@"," ", $controller); |
484
|
|
|
$alias = ucwords($alias); |
485
|
|
|
$alias = str_replace(" ","",$alias); |
486
|
|
|
Route::get($prefix, ['uses' => $controller, 'as' => $alias]); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
public function routePost($prefix, $controller) { |
490
|
|
|
$alias = str_replace("@"," ", $controller); |
491
|
|
|
$alias = ucwords($alias); |
492
|
|
|
$alias = str_replace(" ","",$alias); |
493
|
|
|
Route::post($prefix, ['uses' => $controller, 'as' => $alias]); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
public function routeGroupBackend(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
497
|
|
|
Route::group([ |
498
|
|
|
'middleware' => ['web', \crocodicstudio\crudbooster\middlewares\CBBackend::class], |
499
|
|
|
'prefix' => cb()->getAdminPath(), |
500
|
|
|
'namespace' => $namespace, |
501
|
|
|
], $callback); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
public function routeGroupDeveloper(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
505
|
|
|
Route::group([ |
506
|
|
|
'middleware' => ['web', \crocodicstudio\crudbooster\middlewares\CBDeveloper::class], |
507
|
|
|
'prefix' => "developer/".getSetting('developer_path'), |
508
|
|
|
'namespace' => $namespace, |
509
|
|
|
], $callback); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/* |
513
|
|
|
| -------------------------------------------------------------------------------------------------------------- |
514
|
|
|
| Alternate route for Laravel Route::controller |
515
|
|
|
| -------------------------------------------------------------------------------------------------------------- |
516
|
|
|
| $prefix = path of route |
517
|
|
|
| $controller = controller name |
518
|
|
|
| |
519
|
|
|
*/ |
520
|
|
|
public function routeController($prefix, $controller) |
521
|
|
|
{ |
522
|
|
|
|
523
|
|
|
$prefix = trim($prefix, '/').'/'; |
524
|
|
|
|
525
|
|
|
if(substr($controller,0,1) != "\\") { |
526
|
|
|
$controller = "\App\Http\Controllers\\".$controller; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
$exp = explode("\\", $controller); |
530
|
|
|
$controller_name = end($exp); |
531
|
|
|
|
532
|
|
|
try { |
533
|
|
|
Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller_name.'GetIndex']); |
534
|
|
|
|
535
|
|
|
$controller_class = new \ReflectionClass($controller); |
536
|
|
|
$controller_methods = $controller_class->getMethods(\ReflectionMethod::IS_PUBLIC); |
537
|
|
|
$wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}'; |
538
|
|
|
foreach ($controller_methods as $method) { |
539
|
|
|
|
540
|
|
|
if ($method->class != 'Illuminate\Routing\Controller' && $method->name != 'getIndex') { |
541
|
|
|
if (substr($method->name, 0, 3) == 'get') { |
542
|
|
|
$method_name = substr($method->name, 3); |
543
|
|
|
$slug = array_filter(preg_split('/(?=[A-Z])/', $method_name)); |
|
|
|
|
544
|
|
|
$slug = strtolower(implode('-', $slug)); |
545
|
|
|
$slug = ($slug == 'index') ? '' : $slug; |
546
|
|
|
Route::get($prefix.$slug.$wildcards, ['uses' => $controller.'@'.$method->name, 'as' => $controller_name.'Get'.$method_name]); |
547
|
|
|
} elseif (substr($method->name, 0, 4) == 'post') { |
548
|
|
|
$method_name = substr($method->name, 4); |
549
|
|
|
$slug = array_filter(preg_split('/(?=[A-Z])/', $method_name)); |
550
|
|
|
Route::post($prefix.strtolower(implode('-', $slug)).$wildcards, [ |
551
|
|
|
'uses' => $controller.'@'.$method->name, |
552
|
|
|
'as' => $controller_name.'Post'.$method_name, |
553
|
|
|
]); |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
} catch (\Exception $e) { |
|
|
|
|
558
|
|
|
|
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
|