1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
| --------------------------------------------------------------------------------------------------------------- |
4
|
|
|
| Main Helper of CRUDBooster |
5
|
|
|
| Do not edit or modify this helper unless your modification will be replace if any update from CRUDBooster. |
6
|
|
|
| |
7
|
|
|
| Homepage : http://crudbooster.com |
8
|
|
|
| --------------------------------------------------------------------------------------------------------------- |
9
|
|
|
| |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
if(!function_exists('rglob')) { |
13
|
|
|
function rglob($pattern, $flags = 0) { |
14
|
|
|
$files = glob($pattern, $flags); |
15
|
|
|
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { |
16
|
|
|
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags)); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
return $files; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if(!function_exists('convertPHPToMomentFormat')) { |
23
|
|
|
function convertPHPToMomentFormat($format) |
24
|
|
|
{ |
25
|
|
|
$replacements = [ |
26
|
|
|
'd' => 'DD', |
27
|
|
|
'D' => 'ddd', |
28
|
|
|
'j' => 'D', |
29
|
|
|
'l' => 'dddd', |
30
|
|
|
'N' => 'E', |
31
|
|
|
'S' => 'o', |
32
|
|
|
'w' => 'e', |
33
|
|
|
'z' => 'DDD', |
34
|
|
|
'W' => 'W', |
35
|
|
|
'F' => 'MMMM', |
36
|
|
|
'm' => 'MM', |
37
|
|
|
'M' => 'MMM', |
38
|
|
|
'n' => 'M', |
39
|
|
|
't' => '', // no equivalent |
40
|
|
|
'L' => '', // no equivalent |
41
|
|
|
'o' => 'YYYY', |
42
|
|
|
'Y' => 'YYYY', |
43
|
|
|
'y' => 'YY', |
44
|
|
|
'a' => 'a', |
45
|
|
|
'A' => 'A', |
46
|
|
|
'B' => '', // no equivalent |
47
|
|
|
'g' => 'h', |
48
|
|
|
'G' => 'H', |
49
|
|
|
'h' => 'hh', |
50
|
|
|
'H' => 'HH', |
51
|
|
|
'i' => 'mm', |
52
|
|
|
's' => 'ss', |
53
|
|
|
'u' => 'SSS', |
54
|
|
|
'e' => 'zz', // deprecated since version 1.6.0 of moment.js |
55
|
|
|
'I' => '', // no equivalent |
56
|
|
|
'O' => '', // no equivalent |
57
|
|
|
'P' => '', // no equivalent |
58
|
|
|
'T' => '', // no equivalent |
59
|
|
|
'Z' => '', // no equivalent |
60
|
|
|
'c' => '', // no equivalent |
61
|
|
|
'r' => '', // no equivalent |
62
|
|
|
'U' => 'X', |
63
|
|
|
]; |
64
|
|
|
$momentFormat = strtr($format, $replacements); |
65
|
|
|
return $momentFormat; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if(!function_exists('slug')) { |
70
|
|
|
function slug($string, $separator = '-') { |
71
|
|
|
return \Illuminate\Support\Str::slug($string, $separator); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if(!function_exists('columnSingleton')) { |
76
|
|
|
/** |
77
|
|
|
* @return \crocodicstudio\crudbooster\controllers\scaffolding\singletons\ColumnSingleton |
78
|
|
|
*/ |
79
|
|
|
function columnSingleton() { |
80
|
|
|
return app('ColumnSingleton'); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if(!function_exists('cbHook')) |
85
|
|
|
{ |
86
|
|
|
/** |
87
|
|
|
* @return crocodicstudio\crudbooster\hooks\CBHook |
88
|
|
|
*/ |
89
|
|
|
function cbHook() |
90
|
|
|
{ |
91
|
|
|
$className = "\App\Http\CBHook"; |
92
|
|
|
return new $className(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if(!function_exists('getTypeHook')) |
97
|
|
|
{ |
98
|
|
|
/** |
99
|
|
|
* @param string $type |
100
|
|
|
* @return crocodicstudio\crudbooster\types\TypesHookInterface |
101
|
|
|
*/ |
102
|
|
|
function getTypeHook($type) { |
103
|
|
|
$className = '\crocodicstudio\crudbooster\types\\'.$type.'\Hook'; |
104
|
|
|
$typeClass = new $className(); |
105
|
|
|
return $typeClass; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if(!function_exists('getPrimaryKey')) |
110
|
|
|
{ |
111
|
|
|
function getPrimaryKey($table_name) |
112
|
|
|
{ |
113
|
|
|
return cb()->pk($table_name); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if(!function_exists('cb')) |
118
|
|
|
{ |
119
|
|
|
function cb() |
120
|
|
|
{ |
121
|
|
|
return new \crocodicstudio\crudbooster\helpers\CB(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if(!function_exists('cbAsset')) { |
126
|
|
|
function cbAsset($path) { |
127
|
|
|
return asset("cb_asset/".$path); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if(!function_exists("cbConfig")) { |
132
|
|
|
function cbConfig($name) { |
133
|
|
|
return config("crudbooster.".$name); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if(!function_exists("strRandom")) { |
138
|
|
|
function strRandom($length = 5) { |
139
|
|
|
return \Illuminate\Support\Str::random($length); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if(!function_exists('module')) { |
144
|
|
|
function module() |
145
|
|
|
{ |
146
|
|
|
$module = new \crocodicstudio\crudbooster\helpers\Module(); |
147
|
|
|
return $module; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if(!function_exists('getAdminLoginURL')) { |
152
|
|
|
function getAdminLoginURL() |
153
|
|
|
{ |
154
|
|
|
return url(config('crudbooster.ADMIN_LOGIN_PATH')); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if(!function_exists('dummyPhoto')) { |
159
|
|
|
function dummyPhoto() |
160
|
|
|
{ |
161
|
|
|
return config('crudbooster.DUMMY_PHOTO'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if(!function_exists('assetThumbnail')) { |
166
|
|
|
function assetThumbnail($path) { |
167
|
|
|
$path = str_replace('uploads/','uploads_thumbnail/',$path); |
168
|
|
|
return asset($path); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if(!function_exists('assetResize')) { |
173
|
|
|
function assetResize($path,$width,$height=null,$quality=70) { |
174
|
|
|
$basename = basename($path); |
175
|
|
|
$pathWithoutName = str_replace($basename, '', $path); |
176
|
|
|
$newLocation = $pathWithoutName.'/w_'.$width.'_h_'.$height.'_'.$basename; |
177
|
|
|
if(Storage::exists($newLocation)) { |
178
|
|
|
return asset($newLocation); |
179
|
|
|
}else{ |
180
|
|
|
$img = Image::make(storage_path($path))->fit($width,$height); |
181
|
|
|
$img->save(storage_path($newLocation),$quality); |
182
|
|
|
return asset($newLocation); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if(!function_exists('extract_unit')) { |
188
|
|
|
/* |
189
|
|
|
Credits: Bit Repository |
190
|
|
|
URL: http://www.bitrepository.com/extract-content-between-two-delimiters-with-php.html |
191
|
|
|
*/ |
192
|
|
|
function extract_unit($string, $start, $end) |
193
|
|
|
{ |
194
|
|
|
$pos = stripos($string, $start); |
195
|
|
|
$str = substr($string, $pos); |
196
|
|
|
$str_two = substr($str, strlen($start)); |
197
|
|
|
$second_pos = stripos($str_two, $end); |
198
|
|
|
$str_three = substr($str_two, 0, $second_pos); |
199
|
|
|
$unit = trim($str_three); // remove whitespaces |
200
|
|
|
return $unit; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
if(!function_exists('now')) { |
206
|
|
|
function now() { |
207
|
|
|
return date('Y-m-d H:i:s'); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/* |
212
|
|
|
| -------------------------------------------------------------------------------------------------------------- |
213
|
|
|
| Get data from input post/get more simply |
214
|
|
|
| -------------------------------------------------------------------------------------------------------------- |
215
|
|
|
| $name = name of input |
216
|
|
|
| |
217
|
|
|
*/ |
218
|
|
|
|
219
|
|
|
if(!function_exists('putSetting')) { |
220
|
|
|
function putSetting($key, $value) |
221
|
|
|
{ |
222
|
|
|
if(file_exists(storage_path('.cbconfig'))) { |
223
|
|
|
$settings = file_get_contents(storage_path('.cbconfig')); |
224
|
|
|
$settings = unserialize($settings); |
225
|
|
|
}else{ |
226
|
|
|
$settings = []; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$settings[$key] = $value; |
230
|
|
|
|
231
|
|
|
$settings = serialize($settings); |
232
|
|
|
file_put_contents(storage_path('.cbconfig'), $settings); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if(!function_exists('getSetting')) { |
237
|
|
|
function getSetting($key, $default = null) |
238
|
|
|
{ |
239
|
|
|
if(file_exists(storage_path('.cbconfig'))) { |
240
|
|
|
$settings = file_get_contents(storage_path('.cbconfig')); |
241
|
|
|
$settings = unserialize($settings); |
242
|
|
|
}else{ |
243
|
|
|
$settings = []; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if(isset($settings[$key])) { |
247
|
|
|
return $settings[$key]?:$default; |
248
|
|
|
}else{ |
249
|
|
|
return $default; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if(!function_exists('timeAgo')) { |
255
|
|
|
function timeAgo($datetime_to, $datetime_from = null, $full = false) |
256
|
|
|
{ |
257
|
|
|
$datetime_from = ($datetime_from) ?: date('Y-m-d H:i:s'); |
258
|
|
|
$now = new \DateTime; |
259
|
|
|
if ($datetime_from != '') { |
260
|
|
|
$now = new \DateTime($datetime_from); |
261
|
|
|
} |
262
|
|
|
$ago = new \DateTime($datetime_to); |
263
|
|
|
$diff = $now->diff($ago); |
264
|
|
|
|
265
|
|
|
$diff->w = floor($diff->d / 7); |
|
|
|
|
266
|
|
|
$diff->d -= $diff->w * 7; |
267
|
|
|
|
268
|
|
|
$string = [ |
269
|
|
|
'y' => 'year', |
270
|
|
|
'm' => 'month', |
271
|
|
|
'w' => 'week', |
272
|
|
|
'd' => 'day', |
273
|
|
|
'h' => 'hour', |
274
|
|
|
'i' => 'minute', |
275
|
|
|
's' => 'second', |
276
|
|
|
]; |
277
|
|
|
foreach ($string as $k => &$v) { |
278
|
|
|
if ($diff->$k) { |
279
|
|
|
$v = $diff->$k.' '.$v.($diff->$k > 1 ? 's' : ''); |
280
|
|
|
} else { |
281
|
|
|
unset($string[$k]); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if (! $full) { |
286
|
|
|
$string = array_slice($string, 0, 1); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $string ? implode(', ', $string).' ' : 'just now'; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if(!function_exists("array_map_r")) { |
294
|
|
|
function array_map_r( $func, $arr ) |
295
|
|
|
{ |
296
|
|
|
$newArr = array(); |
297
|
|
|
|
298
|
|
|
foreach( $arr as $key => $value ) |
299
|
|
|
{ |
300
|
|
|
$key = $func($key); |
301
|
|
|
$newArr[ $key ] = ( is_array( $value ) ? array_map_r( $func, $value ) : ( is_array($func) ? call_user_func_array($func, $value) : $func( $value ) ) ); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $newArr; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if(!function_exists("sanitizeXSS")) |
309
|
|
|
{ |
310
|
|
|
function sanitizeXSS($value) |
311
|
|
|
{ |
312
|
|
|
$value = filter_var($value, FILTER_SANITIZE_STRING); |
313
|
|
|
$value = strip_tags($value); |
314
|
|
|
$value = htmlspecialchars($value); |
315
|
|
|
$value = preg_replace('~\r\n?~', "\n", $value); |
316
|
|
|
return $value; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if(!function_exists("requestAll")) { |
321
|
|
|
function requestAll() { |
322
|
|
|
$all = array_map_r("sanitizeXSS", request()->all()); |
323
|
|
|
return $all; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
|
329
|
|
|
if(!function_exists('getURLFormat')) { |
330
|
|
|
function getURLFormat($name) { |
331
|
|
|
$url = request($name); |
332
|
|
|
if(filter_var($url, FILTER_VALIDATE_URL)) { |
333
|
|
|
return $url; |
334
|
|
|
}else{ |
335
|
|
|
return request()->url(); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
if(!function_exists('g')) { |
341
|
|
|
function g($name, $safe = true) { |
342
|
|
|
if($safe == true) { |
343
|
|
|
$response = request($name); |
344
|
|
|
if(is_string($response)) { |
345
|
|
|
$response = sanitizeXSS($response); |
346
|
|
|
}elseif (is_array($response)) { |
347
|
|
|
array_walk_recursive($response, function(&$response) { |
348
|
|
|
$response = sanitizeXSS($response); |
349
|
|
|
}); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return $response; |
353
|
|
|
}else{ |
354
|
|
|
return Request::get($name); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
if(!function_exists('min_var_export')) { |
360
|
|
|
function min_var_export($input) { |
361
|
|
|
if(is_array($input)) { |
362
|
|
|
$buffer = []; |
363
|
|
|
foreach($input as $key => $value) |
364
|
|
|
$buffer[] = var_export($key, true)."=>".min_var_export($value); |
365
|
|
|
return "[".implode(",",$buffer)."]"; |
366
|
|
|
} else |
367
|
|
|
return var_export($input, true); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if(!function_exists('rrmdir')) { |
372
|
|
|
/* |
373
|
|
|
* http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir |
374
|
|
|
*/ |
375
|
|
|
function rrmdir($dir) { |
376
|
|
|
if (is_dir($dir)) { |
377
|
|
|
$objects = scandir($dir); |
378
|
|
|
foreach ($objects as $object) { |
379
|
|
|
if ($object != "." && $object != "..") { |
380
|
|
|
if (is_dir($dir."/".$object)) |
381
|
|
|
rrmdir($dir."/".$object); |
382
|
|
|
else |
383
|
|
|
unlink($dir."/".$object); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
rmdir($dir); |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
|