Passed
Push — master ( 38a665...decd12 )
by Ferry
05:33
created

cbLang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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("cbLang")) {
13
    /**
14
     * @param string $key
15
     * @param array $replace
16
     * @return array|\Illuminate\Contracts\Translation\Translator|null|string
17
     */
18
    function cbLang($key, $replace = [])
19
    {
20
        return trans("cb::cb.".$key, $replace);
21
    }
22
}
23
24
if(!function_exists('rglob')) {
25
    function rglob($pattern, $flags = 0) {
26
        $files = glob($pattern, $flags);
27
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
28
            $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
0 ignored issues
show
Bug introduced by
It seems like rglob($dir . '/' . basename($pattern), $flags) can also be of type false; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            $files = array_merge($files, /** @scrutinizer ignore-type */ rglob($dir.'/'.basename($pattern), $flags));
Loading history...
Bug introduced by
It seems like $files can also be of type false; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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