-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathhelpers.php
560 lines (493 loc) · 15.4 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/*
* Global helpers file with misc functions.
*/
if (! function_exists('app_name')) {
/**
* Helper to grab the application name.
*
* @return mixed
*/
function app_name()
{
return config('app.name');
}
}
/*
* Global helpers file with misc functions.
*/
if (! function_exists('app_url')) {
/**
* Helper to grab the application name.
*
* @return mixed
*/
function app_url()
{
return config('app.url');
}
}
/*
* Global helpers file with misc functions.
*/
if (! function_exists('user_registration')) {
/**
* Helper to grab the application name.
*
* @return mixed
*/
function user_registration()
{
$user_registration = config('app.user_registration');
if (env('USER_REGISTRATION') === true) {
$user_registration = true;
}
return $user_registration;
}
}
/*
*
* label_case
*
* ------------------------------------------------------------------------
*/
if (! function_exists('label_case')) {
/**
* Prepare the Column Name for Lables.
*/
function label_case($text)
{
$order = ['_', '-'];
$replace = ' ';
$new_text = trim(\Illuminate\Support\Str::title(str_replace('"', '', $text)));
$new_text = trim(\Illuminate\Support\Str::title(str_replace($order, $replace, $text)));
return preg_replace('!\s+!', ' ', $new_text);
}
}
/*
*
* show_column_value
*
* ------------------------------------------------------------------------
*/
if (! function_exists('show_column_value')) {
/**
* Generates the function comment for the given function.
*
* @param string $valueObject Model Object
* @param string $column Column Name
* @param string $return_format Return Type
* @param mixed $valueObject The value object.
* @param mixed $column The column.
* @param string $return_format The return format. Default is empty string.
* @return string Raw/Formatted Column Value
* @return mixed The column value or formatted value.
*/
function show_column_value($valueObject, $column, $return_format = '')
{
$column_name = $column->name;
$column_type = $column->type;
$value = $valueObject->$column_name;
if (! $value) {
return $value;
}
if ($return_format === 'raw') {
return $value;
}
if (($column_type === 'date') && $value !== '') {
$datetime = \Carbon\Carbon::parse($value);
return $datetime->isoFormat('LL');
}
if (($column_type === 'datetime' || $column_type === 'timestamp') && $value !== '') {
$datetime = \Carbon\Carbon::parse($value);
return $datetime->isoFormat('LLLL');
}
if ($column_type === 'json') {
$return_text = json_encode($value);
} elseif ($column_type !== 'json' && \Illuminate\Support\Str::endsWith(strtolower($value), ['png', 'jpg', 'jpeg', 'gif', 'svg'])) {
$img_path = asset($value);
$return_text = '<figure class="figure">
<a href="'.$img_path.'" data-lightbox="image-set" data-title="Path: '.$value.'">
<img src="'.$img_path.'" style="max-width:200px;" class="figure-img img-fluid rounded img-thumbnail" alt="">
</a>
<figcaption class="figure-caption">Path: '.$value.'</figcaption>
</figure>';
} else {
$return_text = $value;
}
return $return_text;
}
}
/*
*
* field_required
* Show a * if field is required
*
* ------------------------------------------------------------------------
*/
if (! function_exists('field_required')) {
/**
* Prepare the Column Name for Lables.
*/
function field_required($required)
{
$return_text = '';
if ($required !== '') {
$return_text = ' <span class="text-danger text-red-500">*</span>';
}
return $return_text;
}
}
/**
* Get or Set the Settings Values.
*/
if (! function_exists('setting')) {
function setting($key, $default = null)
{
if (is_null($key)) {
return new App\Models\Setting;
}
if (is_array($key)) {
return App\Models\Setting::set($key[0], $key[1]);
}
$value = App\Models\Setting::get($key);
return is_null($value) ? value($default) : $value;
}
}
/*
* Show Human readable file size
*/
if (! function_exists('humanFilesize')) {
function humanFilesize($size, $precision = 2)
{
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$step = 1024;
$i = 0;
while ($size / $step > 0.9) {
$size /= $step;
$i++;
}
return round($size, $precision).$units[$i];
}
}
/*
*
* Encode Id to a Hashids / Sqids
*
* ------------------------------------------------------------------------
*/
if (! function_exists('encode_id')) {
/**
* Encode Id to a Hashids / Sqids.
*/
function encode_id($id)
{
$sqids = new Sqids\Sqids(alphabet: 'abcdefghijklmnopqrstuvwxyz123456789');
return $sqids->encode([$id]);
}
}
/*
*
* Decode Id from Hashids / Sqids
*
* ------------------------------------------------------------------------
*/
if (! function_exists('decode_id')) {
/**
* Decode Id from Hashids / Sqids.
*/
function decode_id($hashid)
{
$sqids = new Sqids\Sqids(alphabet: 'abcdefghijklmnopqrstuvwxyz123456789');
$id = $sqids->decode($hashid);
if (count($id)) {
return $id[0];
}
abort(404);
}
}
/*
*
* Prepare a Slug for a given string
* Laravel default str_slug does not work for Unicode
*
* ------------------------------------------------------------------------
*/
if (! function_exists('slug_format')) {
/**
* Format a string to Slug.
*/
function slug_format($string)
{
$base_string = $string;
$string = preg_replace('/\s+/u', '-', trim($string));
$string = str_replace('/', '-', $string);
$string = str_replace('\\', '-', $string);
$string = strtolower($string);
return substr($string, 0, 190);
}
}
/*
*
* icon
* A short and easy way to show icon fornts
* Default value will be check icon from FontAwesome (https://fontawesome.com)
*
* ------------------------------------------------------------------------
*/
if (! function_exists('icon')) {
/**
* Format a string to Slug.
*/
function icon($string = 'fa-regular fa-circle-check')
{
return "<i class='".$string."'></i> ";
}
}
/*
*
* logUserAccess
* Get current user's `name` and `id` and
* log as debug data. Additional text can be added too.
*
* ------------------------------------------------------------------------
*/
if (! function_exists('logUserAccess')) {
/**
* Format a string to Slug.
*/
function logUserAccess($text = '')
{
$auth_text = '';
if (\Auth::check()) {
$auth_text = 'User:'.\Auth::user()->name.' (ID:'.\Auth::user()->id.')';
}
\Log::debug(label_case($text)." | {$auth_text}");
}
}
/*
*
* bn2enNumber
* Convert a Bengali number to English
*
* ------------------------------------------------------------------------
*/
if (! function_exists('bn2enNumber')) {
/**
* Prepare the Column Name for Lables.
*/
function bn2enNumber($number)
{
$search_array = ['১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'];
$replace_array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
return str_replace($search_array, $replace_array, $number);
}
}
/*
*
* bn2enNumber
* Convert a English number to Bengali
*
* ------------------------------------------------------------------------
*/
if (! function_exists('en2bnNumber')) {
/**
* Prepare the Column Name for Lables.
*/
function en2bnNumber($number)
{
$search_array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
$replace_array = ['১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'];
return str_replace($search_array, $replace_array, $number);
}
}
/*
*
* bn2enNumber
* Convert a English number to Bengali
*
* ------------------------------------------------------------------------
*/
if (! function_exists('en2bnDate')) {
/**
* Convert a English number to Bengali.
*/
function en2bnDate($date)
{
// Convert numbers
$search_array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
$replace_array = ['১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'];
$bn_date = str_replace($search_array, $replace_array, $date);
// Convert Short Week Day Names
$search_array = ['Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu'];
$replace_array = ['শুক্র', 'শনি', 'রবি', 'সোম', 'মঙ্গল', 'বুধ', 'বৃহঃ'];
$bn_date = str_replace($search_array, $replace_array, $bn_date);
// Convert Month Names
$search_array = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$replace_array = ['জানুয়ারী', 'ফেব্রুয়ারী', 'মার্চ', 'এপ্রিল', 'মে', 'জুন', 'জুলাই', 'আগষ্ট', 'সেপ্টেম্বর', 'অক্টোবর', 'নভেম্বর', 'ডিসেম্বর'];
$bn_date = str_replace($search_array, $replace_array, $bn_date);
// Convert Short Month Names
$search_array = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$replace_array = ['জানুয়ারী', 'ফেব্রুয়ারী', 'মার্চ', 'এপ্রিল', 'মে', 'জুন', 'জুলাই', 'আগষ্ট', 'সেপ্টেম্বর', 'অক্টোবর', 'নভেম্বর', 'ডিসেম্বর'];
$bn_date = str_replace($search_array, $replace_array, $bn_date);
// Convert AM-PM
$search_array = ['am', 'pm', 'AM', 'PM'];
$replace_array = ['পূর্বাহ্ন', 'অপরাহ্ন', 'পূর্বাহ্ন', 'অপরাহ্ন'];
return str_replace($search_array, $replace_array, $bn_date);
}
}
/*
*
* banglaDate
* Get the Date of Bengali Calendar from the Gregorian Calendar
* By default is will return the Today's Date
*
* ------------------------------------------------------------------------
*/
if (! function_exists('banglaDate')) {
function banglaDate($date_input = '')
{
if ($date_input === '') {
$date_input = date('Y-m-d');
}
$date_input = strtotime($date_input);
$en_day = intval(date('j', $date_input));
$en_month = intval(date('n', $date_input));
$en_year = intval(date('Y', $date_input));
$bn_month_days = [30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 29, 30];
$bn_month_middate = [13, 12, 14, 13, 14, 14, 15, 15, 15, 16, 14, 14];
$bn_months = ['পৌষ', 'মাঘ', 'ফাল্গুন', 'চৈত্র', 'বৈশাখ', 'জ্যৈষ্ঠ', 'আষাঢ়', 'শ্রাবণ', 'ভাদ্র', 'আশ্বিন', 'কার্তিক', 'অগ্রহায়ণ'];
// Day & Month
if ($en_day <= $bn_month_middate[$en_month - 1]) {
$bn_day = $en_day + $bn_month_days[$en_month - 1] - $bn_month_middate[$en_month - 1];
$bn_month = $bn_months[$en_month - 1];
// Leap Year
if (($en_year % 400 === 0 || ($en_year % 100 !== 0 && $en_year % 4 === 0)) && $en_month === 3) {
$bn_day += 1;
}
} else {
$bn_day = $en_day - $bn_month_middate[$en_month - 1];
$bn_month = $bn_months[$en_month % 12];
}
// Year
$bn_year = $en_year - 593;
if (($en_year < 4) || (($en_year === 4) && (($en_day < 14) || ($en_day === 14)))) {
$bn_year -= 1;
}
$return_bn_date = $bn_day.' '.$bn_month.' '.$bn_year;
return en2bnNumber($return_bn_date);
}
}
/*
*
* Decode Id to a Hashids\Hashids
*
* ------------------------------------------------------------------------
*/
if (! function_exists('generate_rgb_code')) {
/**
* Prepare the Column Name for Lables.
*/
function generate_rgb_code($opacity = '0.9')
{
$str = '';
for ($i = 1; $i <= 3; $i++) {
$num = mt_rand(0, 255);
$str .= "{$num},";
}
$str .= "{$opacity},";
return substr($str, 0, -1);
}
}
/*
*
* Return Date with weekday
*
* ------------------------------------------------------------------------
*/
if (! function_exists('date_today')) {
/**
* Return Date with weekday.
*
* Carbon Locale will be considered here
* Example:
* শুক্রবার, ২৪ জুলাই ২০২০
* Friday, July 24, 2020
*/
function date_today()
{
return \Carbon\Carbon::now()->isoFormat('dddd, LL');
}
}
if (! function_exists('language_direction')) {
/**
* return direction of languages.
*
* @return string
*/
function language_direction($language = null)
{
if (empty($language)) {
$language = app()->getLocale();
}
$language = strtolower(substr($language, 0, 2));
$rtlLanguages = [
'ar', // 'العربية', Arabic
'arc', // 'ܐܪܡܝܐ', Aramaic
'bcc', // 'بلوچی مکرانی', Southern Balochi
'bqi', // 'بختياري', Bakthiari
'ckb', // 'Soranî / کوردی', Sorani Kurdish
'dv', // 'ދިވެހިބަސް', Dhivehi
'fa', // 'فارسی', Persian
'glk', // 'گیلکی', Gilaki
'he', // 'עברית', Hebrew
'lrc', // - 'لوری', Northern Luri
'mzn', // 'مازِرونی', Mazanderani
'pnb', // 'پنجابی', Western Punjabi
'ps', // 'پښتو', Pashto
'sd', // 'سنڌي', Sindhi
'ug', // 'Uyghurche / ئۇيغۇرچە', Uyghur
'ur', // 'اردو', Urdu
'yi', // 'ייִדיש', Yiddish
];
if (in_array($language, $rtlLanguages)) {
return 'rtl';
}
return 'ltr';
}
}
/*
* Application Demo Mode check
*/
if (! function_exists('demo_mode')) {
/**
* Helper to grab the application name.
*
* @return mixed
*/
function demo_mode()
{
$return_string = false;
if (env('DEMO_MODE') === true) {
$return_string = true;
}
return $return_string;
}
}
/*
* Split Name to First Name and Last Name
*/
if (! function_exists('split_name')) {
/**
* Split Name to First Name and Last Name.
*
* @return mixed
*/
function split_name($name)
{
$name = trim($name);
$last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
$first_name = trim(preg_replace('#'.preg_quote($last_name, '#').'#', '', $name));
return [$first_name, $last_name];
}
}