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