Passed
Push — master ( db1581...a5af1f )
by Ferry
04:30
created

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