1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Zahir Hayrullah |
5
|
|
|
* create date : 10/04/2020 07:00 AM |
6
|
|
|
* Last Modified Date: 10/04/2020 07:00 AM. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
10
|
|
|
use Illuminate\Contracts\View\Factory; |
11
|
|
|
use Illuminate\Http\JsonResponse; |
12
|
|
|
use Illuminate\Support\Facades\Session; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Illuminate\View\View; |
|
|
|
|
15
|
|
|
|
16
|
|
|
if (!function_exists('get_auth_user')) { |
17
|
|
|
/** |
18
|
|
|
* @param $user |
19
|
|
|
* |
20
|
|
|
* @return Authenticatable|null |
21
|
|
|
*/ |
22
|
|
|
function get_auth_user($user = null) |
23
|
|
|
{ |
24
|
|
|
$key = $user ? "auth_user_{$user->id}" : 'auth_user'; |
25
|
|
|
if (!Session::has($key)) { |
26
|
|
|
// 1-session again |
27
|
|
|
$user = $user ?? auth()->user(); |
28
|
|
|
Session::put($key, $user); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return session($key); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
/*---------------------------------- </> --------------------------------*/ |
35
|
|
|
|
36
|
|
|
if (!function_exists('user_avatar')) { |
37
|
|
|
/** |
38
|
|
|
* @param $user |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
function user_avatar($user = null) |
43
|
|
|
{ |
44
|
|
|
return optional(get_auth_user($user))->avatar; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
/*---------------------------------- </> --------------------------------*/ |
48
|
|
|
|
49
|
|
|
if (!function_exists('user_roles')) { |
50
|
|
|
/** |
51
|
|
|
* @param $user |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
function user_roles($user = null) |
56
|
|
|
{ |
57
|
|
|
return optional(get_auth_user($user))->roles; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
/*---------------------------------- </> --------------------------------*/ |
61
|
|
|
|
62
|
|
|
if (!function_exists('getArrayValidationErrors')) { |
63
|
|
|
/** |
64
|
|
|
* @param $validation |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
function getArrayValidationErrors($validation) |
69
|
|
|
{ |
70
|
|
|
$error_array = []; |
71
|
|
|
if ($validation) { |
72
|
|
|
foreach ($validation->messages()->getMessages() as $field_name => $messages) { |
73
|
|
|
$error_array[] = $messages; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $error_array; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
/*---------------------------------- </> --------------------------------*/ |
81
|
|
|
|
82
|
|
|
if (!function_exists('jsonOutput')) { |
83
|
|
|
/** |
84
|
|
|
* @param $error_array |
85
|
|
|
* @param $success_output |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
function jsonOutput($error_array, $success_output = null) |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
'error' => $error_array, |
93
|
|
|
'success' => $success_output, |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
/*---------------------------------- </> --------------------------------*/ |
98
|
|
|
|
99
|
|
|
if (!function_exists('callAPI')) { |
100
|
|
|
/** |
101
|
|
|
* @param $method |
102
|
|
|
* @param $url |
103
|
|
|
* @param $data |
104
|
|
|
* |
105
|
|
|
* @return bool|string |
106
|
|
|
*/ |
107
|
|
|
function callAPI($method, $url, $data = null) |
108
|
|
|
{ |
109
|
|
|
// $data must be json when method is post |
110
|
|
|
$curl = curl_init(); |
111
|
|
|
switch ($method) { |
112
|
|
|
case 'POST': |
113
|
|
|
$CURL_OPT = CURLOPT_POST; |
114
|
|
|
$VALUE = 1; |
115
|
|
|
break; |
116
|
|
|
case 'PUT': |
117
|
|
|
$CURL_OPT = CURLOPT_CUSTOMREQUEST; |
118
|
|
|
$VALUE = 'PUT'; |
119
|
|
|
break; |
120
|
|
|
default: |
121
|
|
|
if ($data) { |
122
|
|
|
$url = sprintf('%s?%s', $url, http_build_query($data)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$headers = get_api_headers(); |
127
|
|
|
// OPTIONS: |
128
|
|
|
if (isset($CURL_OPT) and isset($VALUE)) { |
129
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $curl, $CURL_OPT, $VALUE); |
130
|
|
|
} |
131
|
|
|
if (isset($CURL_OPT) and $data) { |
132
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $curl, $CURL_OPT, $data); |
133
|
|
|
} |
134
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $url); |
135
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_HTTPHEADER, $headers); |
136
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, 1); |
137
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
138
|
|
|
|
139
|
|
|
// EXECUTE: |
140
|
|
|
$result = curl_exec(/** @scrutinizer ignore-type */ $curl); |
141
|
|
|
// if (!$result) { |
142
|
|
|
// die('Connection Failure'); |
143
|
|
|
// } |
144
|
|
|
curl_close($curl); |
|
|
|
|
145
|
|
|
|
146
|
|
|
return $result; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
/*---------------------------------- </> --------------------------------*/ |
150
|
|
|
|
151
|
|
|
if (!function_exists('get_api_headers')) { |
152
|
|
|
function get_api_headers($headers = []) |
153
|
|
|
{ |
154
|
|
|
$headers[] = 'Content-Type: application/json'; |
155
|
|
|
if (array_key_exists('HTTP_HOST', $_SERVER)) { |
156
|
|
|
$headers[] = 'ORIGIN: '.$_SERVER['HTTP_HOST']; |
157
|
|
|
} |
158
|
|
|
if (array_key_exists('HTTP_REFERER', $_SERVER)) { |
159
|
|
|
$headers[] = 'REFERER: '.$_SERVER['HTTP_REFERER']; |
160
|
|
|
} |
161
|
|
|
if (array_key_exists('HTTP_ORIGIN', $_SERVER)) { |
162
|
|
|
$headers[] = 'ORIGIN: '.$_SERVER['HTTP_ORIGIN']; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $headers; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
/*---------------------------------- </> --------------------------------*/ |
169
|
|
|
|
170
|
|
|
if (!function_exists('delete_record')) { |
171
|
|
|
/** |
172
|
|
|
* @param $item |
173
|
|
|
* @param $permissionName |
174
|
|
|
* |
175
|
|
|
* @return JsonResponse |
176
|
|
|
*/ |
177
|
|
|
function delete_record($item, $permissionName) |
178
|
|
|
{ |
179
|
|
|
if (!is_can_delete($permissionName)) { |
180
|
|
|
return response()->json("You don't have authorize to delete this item.", 401); |
181
|
|
|
} |
182
|
|
|
// $item = ClassName($modelName)::find($id); |
183
|
|
|
if ($item) { |
184
|
|
|
$item->delete(); |
185
|
|
|
|
186
|
|
|
return response()->json('The item has been "deleted" successfully', 200); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return response()->json('The item not found', 404); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
/*---------------------------------- </> --------------------------------*/ |
193
|
|
|
|
194
|
|
|
if (!function_exists('restore_record')) { |
195
|
|
|
/** |
196
|
|
|
* @param $item |
197
|
|
|
* @param $permissionName |
198
|
|
|
* |
199
|
|
|
* @return JsonResponse |
200
|
|
|
*/ |
201
|
|
|
function restore_record($item, $permissionName) |
202
|
|
|
{ |
203
|
|
|
if (!is_can_restore($permissionName)) { |
204
|
|
|
return response()->json("You don't have authorize to restore this item.", 401); |
205
|
|
|
} |
206
|
|
|
// $item = ClassName($modelName)::withTrashed()->find($id); |
207
|
|
|
if ($item) { |
208
|
|
|
$item->restore(); |
209
|
|
|
|
210
|
|
|
return response()->json('The item has been "restored" successfully', 200); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return response()->json('The item not found', 404); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
/*---------------------------------- </> --------------------------------*/ |
217
|
|
|
|
218
|
|
|
if (!function_exists('force_delete_record')) { |
219
|
|
|
/** |
220
|
|
|
* @param $item |
221
|
|
|
* @param $permissionName |
222
|
|
|
* |
223
|
|
|
* @return JsonResponse |
224
|
|
|
*/ |
225
|
|
|
function force_delete_record($item, $permissionName) |
226
|
|
|
{ |
227
|
|
|
if (!is_can_force_delete($permissionName)) { |
228
|
|
|
return response()->json("You don't have authorize to destroy this item.", 401); |
229
|
|
|
} |
230
|
|
|
// $item = ClassName($modelName)::withTrashed()->find($id); |
231
|
|
|
if ($item) { |
232
|
|
|
$item->forceDelete(); |
233
|
|
|
|
234
|
|
|
return response()->json('The item has been "destroyed" successfully', 200); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return response()->json('The item not found', 404); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
/*---------------------------------- </> --------------------------------*/ |
241
|
|
|
|
242
|
|
|
if (!function_exists('show_record')) { |
243
|
|
|
/** |
244
|
|
|
* @param $request |
245
|
|
|
* @param $modelName |
246
|
|
|
* @param $id |
247
|
|
|
* @param $permissionName |
248
|
|
|
* @param $with |
249
|
|
|
* |
250
|
|
|
* @return Factory|JsonResponse|View|void |
251
|
|
|
*/ |
252
|
|
|
function show_record($request, $modelName, $id, $permissionName = null, $with = null) |
253
|
|
|
{ |
254
|
|
|
if (!$request->ajax()) { |
255
|
|
|
// if Request not Ajax |
256
|
|
|
if (!is_can_show($permissionName) and !is_can_show_all($permissionName)) { |
257
|
|
|
return view('backend.errors.401'); |
258
|
|
|
} |
259
|
|
|
abort(404); |
260
|
|
|
} |
261
|
|
|
if (!is_can_show($permissionName) and !is_can_show_all($permissionName)) { |
262
|
|
|
return json_not_authorize(); |
263
|
|
|
} |
264
|
|
|
if (!is_numeric($id)) { |
265
|
|
|
return json_not_found_item('Page'); |
266
|
|
|
} |
267
|
|
|
$ClassName = get_class_name($modelName); |
268
|
|
|
$item = $ClassName::with($with)->find($id); |
269
|
|
|
if (!$item) { |
270
|
|
|
return json_not_found_item(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return response()->json($item, 200); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
/*---------------------------------- </> --------------------------------*/ |
278
|
|
|
|
279
|
|
|
if (!function_exists('increment_visits')) { |
280
|
|
|
/** |
281
|
|
|
* increment visits. |
282
|
|
|
* |
283
|
|
|
* @param $row |
284
|
|
|
* @param string $key is $key_visits_slug |
285
|
|
|
*/ |
286
|
|
|
function increment_visits($row, $key = 'page') |
287
|
|
|
{ |
288
|
|
|
$key .= '_visits_'.$row->slug; |
289
|
|
|
if (!Session::has($key)) { |
290
|
|
|
$row->timestamps = false; |
291
|
|
|
$row->increment('visits'); |
292
|
|
|
Session::put($key, 1); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
/*---------------------------------- </> --------------------------------*/ |
297
|
|
|
|
298
|
|
|
if (!function_exists('json_not_found_item')) { |
299
|
|
|
/** |
300
|
|
|
* @param $item_or_page |
301
|
|
|
* @param $code |
302
|
|
|
* |
303
|
|
|
* @return JsonResponse |
304
|
|
|
*/ |
305
|
|
|
function json_not_found_item($item_or_page = null, $code = null) |
306
|
|
|
{ |
307
|
|
|
$item_or_page = $item_or_page ?? 'Item'; |
308
|
|
|
$code = $code ?? 404; |
309
|
|
|
$message = $message ?? $code.". This {$item_or_page} Not found."; |
|
|
|
|
310
|
|
|
|
311
|
|
|
return response()->json($message, $code); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
/*---------------------------------- </> --------------------------------*/ |
315
|
|
|
|
316
|
|
|
if (!function_exists('json_not_authorize')) { |
317
|
|
|
/** |
318
|
|
|
* @return JsonResponse |
319
|
|
|
*/ |
320
|
|
|
function json_not_authorize() |
321
|
|
|
{ |
322
|
|
|
return response()->json('not authorize to visit this page', 401); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
/*---------------------------------- </> --------------------------------*/ |
326
|
|
|
|
327
|
|
|
if (!function_exists('has_trash_param')) { |
328
|
|
|
/** |
329
|
|
|
* // This function to check url contains trash or not. |
330
|
|
|
* |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
|
|
function has_trash_param() |
334
|
|
|
{ |
335
|
|
|
$output = ''; |
336
|
|
|
if (strpos($_SERVER['REQUEST_URI'], 'admin/trash')) { |
337
|
|
|
$output = '/trash'; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $output; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
/*---------------------------------- </> --------------------------------*/ |
344
|
|
|
|
345
|
|
|
if (!function_exists('editorInfo')) { |
346
|
|
|
/** |
347
|
|
|
* @param $_page |
348
|
|
|
* |
349
|
|
|
* @return string |
350
|
|
|
*/ |
351
|
|
|
function editorInfo($_page) |
352
|
|
|
{ |
353
|
|
|
$output = ''; |
354
|
|
|
$creator = $_page->createdBy ? $_page->createdBy->name : ' system '; |
355
|
|
|
$editor = $_page->updatedBy ? $_page->updatedBy->name : null; |
356
|
|
|
$created_title = __('created_by', ['name' => $creator]); |
357
|
|
|
$created_title_date = __('addition_date', ['date' => $_page->created_at]); |
358
|
|
|
$modified_title = __('updated_by', ['name' => $editor]); |
359
|
|
|
$modified_title_date = __('edition_date', ['date' => $_page->updated_at]); |
360
|
|
|
|
361
|
|
|
$output .= ''; |
362
|
|
|
$output .= "<p class='user-date-info'><span data-toggle='tooltip' title='{$created_title}'> <i class='fas fa-plus-square'></i> ".hiddenSm($created_title).' </span>'; |
363
|
|
|
$output .= " - <span data-toggle='tooltip' title='{$created_title_date}'> <i class='fas fa-calendar-plus'></i> ".hiddenSm(optional($_page->created_at)->format('Y-m-d')).' </span></p>'; |
364
|
|
|
if ($editor != null) { |
365
|
|
|
$output .= "<p class='user-date-info'><span data-toggle='tooltip' title='{$modified_title}'> <i class='fas fa-edit'></i> ".hiddenSm($modified_title).' </span>'; |
366
|
|
|
$output .= " - <span data-toggle='tooltip' title='{$modified_title_date}'> <i class='fas fa-calendar'></i> ".hiddenSm(optional($_page->updated_at)->format('Y-m-d')).' </span></p>'; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
return $output; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
/*---------------------------------- </> --------------------------------*/ |
373
|
|
|
|
374
|
|
|
if (!function_exists('trashInfo')) { |
375
|
|
|
/** |
376
|
|
|
* @param $_page |
377
|
|
|
* |
378
|
|
|
* @return string |
379
|
|
|
*/ |
380
|
|
|
function trashInfo($_page) |
381
|
|
|
{ |
382
|
|
|
$output = ''; |
383
|
|
|
$deletedBy = $_page->deletedBy ? $_page->deletedBy->name : ' system '; |
384
|
|
|
$output .= "<p class='user-date-info'>"; |
385
|
|
|
$output .= " <span data-toggle='tooltip' title='تم حذفه بواسطة {$deletedBy}'> <i class='fas fa-trash'></i> ".hiddenSm('تم حذفه بواسطة :'.$deletedBy).' </span>'; |
386
|
|
|
$output .= " - <span data-toggle='tooltip' title='تاريخ الحذف {$_page->deleted_at}'> <i class='fas fa-calendar-times'></i> ".hiddenSm('بتاريخ :'.optional($_page->deleted_at)->format('Y-m-d')).' </span>'; |
387
|
|
|
$output .= '</p>'; |
388
|
|
|
|
389
|
|
|
return $output; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
/*---------------------------------- </> --------------------------------*/ |
393
|
|
|
|
394
|
|
|
if (!function_exists('hiddenSm')) { |
395
|
|
|
/** |
396
|
|
|
* @param $data |
397
|
|
|
* @param $className |
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
function hiddenSm($data, $className = null) |
402
|
|
|
{ |
403
|
|
|
$className = $className ?? 'd-none d-md-inline d-lg-inline d-xl-inline'; |
404
|
|
|
|
405
|
|
|
return "<span class='{$className}'>{$data}</span>"; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
/*---------------------------------- </> --------------------------------*/ |
409
|
|
|
|
410
|
|
|
if (!function_exists('titleLink')) { |
411
|
|
|
/** |
412
|
|
|
* @param $prefix |
413
|
|
|
* @param $row |
414
|
|
|
* @param $can_edit |
415
|
|
|
* @param string $attr |
416
|
|
|
* |
417
|
|
|
* @return string |
418
|
|
|
*/ |
419
|
|
|
function titleLink($prefix, $row, $can_edit, $attr = 'title') |
420
|
|
|
{ |
421
|
|
|
$output = '<p>'; |
422
|
|
|
foreach (langSymbols() as $symbol) { |
|
|
|
|
423
|
|
|
if (isset($row->{langAttr($attr, $symbol)})) { |
424
|
|
|
$_title = $row->{langAttr($attr, $symbol)}; |
425
|
|
|
$str_title = Str::limit($_title, 50); |
426
|
|
|
$output .= "<span data-toggle='tooltip' title='{$_title}' >{$str_title}</span><br/>"; |
427
|
|
|
if ($can_edit) { |
428
|
|
|
$output .= "<a href='/{$prefix}/{$row->id}/edit' data-toggle='tooltip' title='{$_title}' >{$str_title}</a><br/>"; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return "{$output}</p>"; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
/*---------------------------------- </> --------------------------------*/ |
437
|
|
|
|
438
|
|
|
if (!function_exists('slugLink')) { |
439
|
|
|
/** |
440
|
|
|
* @param $row |
441
|
|
|
* @param string $prefix |
442
|
|
|
* @param null $url |
|
|
|
|
443
|
|
|
* |
444
|
|
|
* @return string |
445
|
|
|
*/ |
446
|
|
|
function slugLink($row, $prefix = '', $url = null) |
447
|
|
|
{ |
448
|
|
|
if (!$url) { |
|
|
|
|
449
|
|
|
$url = url($prefix.'/'.$row->slug); |
450
|
|
|
} |
451
|
|
|
// $fullLink=url($row->slug); |
452
|
|
|
// $output = "<a href='$url' target='_blank' data-toggle='tooltip' title='{$fullLink}'>$row->slug</a>"; |
453
|
|
|
return "<a href='{$url}' target='_blank' data-toggle='tooltip' title='زيارة الرابط: {$row->slug}'>{$row->slug}</a>"; |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
/*---------------------------------- </> --------------------------------*/ |
457
|
|
|
|
458
|
|
|
if (!function_exists('actionLinks')) { |
459
|
|
|
/** |
460
|
|
|
* @param $row |
461
|
|
|
* @param $prefix |
462
|
|
|
* @param $user_can_edit |
463
|
|
|
* @param $user_can_delete |
464
|
|
|
* |
465
|
|
|
* @return string |
466
|
|
|
*/ |
467
|
|
|
function actionLinks($row, $prefix, $user_can_edit, $user_can_delete) |
468
|
|
|
{ |
469
|
|
|
// if ($modelName == null) { |
470
|
|
|
// $modelName = str_replace('admin/', '', $prefix); |
471
|
|
|
// } |
472
|
|
|
if (auth()->check()) { |
473
|
|
|
$output = ''; |
474
|
|
|
$trans_edit = __('edit'); |
475
|
|
|
if ($prefix == null and $user_can_edit) { |
476
|
|
|
$output = "<a href='javascript:void(0)' class='btn btn-primary w-100 btn-xs edit' id='{$row->id}'><i class='fas fa-pencil-alt'></i> {$trans_edit} </a>"; |
477
|
|
|
} else { |
478
|
|
|
if ($user_can_edit) { |
479
|
|
|
$output = "<a href='/{$prefix}/{$row->id}/edit' class='btn btn-primary w-100 btn-xs edit' id='{$row->id}'><i class='fas fa-pencil-alt'></i> {$trans_edit} </a>"; |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
if ($user_can_delete) { |
483
|
|
|
$trans_delete = __('delete'); |
484
|
|
|
$output .= "<button class='btn btn-danger w-100 btn-xs delete' id='{$row->id}'><i class='fas fa-trash'></i> {$trans_delete} </button>"; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
return $output; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
/*---------------------------------- </> --------------------------------*/ |
492
|
|
|
|
493
|
|
|
if (!function_exists('trashActionLinks')) { |
494
|
|
|
/** |
495
|
|
|
* @param $row |
496
|
|
|
* @param $user_can_restore |
497
|
|
|
* @param $user_can_force_delete |
498
|
|
|
* |
499
|
|
|
* @return string |
500
|
|
|
*/ |
501
|
|
|
function trashActionLinks($row, $user_can_restore, $user_can_force_delete) |
502
|
|
|
{ |
503
|
|
|
$output = ''; |
504
|
|
|
if (auth()->check()) { |
505
|
|
|
if ($user_can_restore) { |
506
|
|
|
$restore = __('restore'); |
507
|
|
|
$output = "<a href='javascript:void(0)' class='btn btn-info w-100 btn-xs btn-restore' id='{$row->id}'><i class='fas fa-trash-restore'></i> {$restore} </a>"; |
508
|
|
|
} |
509
|
|
|
if ($user_can_force_delete) { |
510
|
|
|
$force_delete = __('force_delete'); |
511
|
|
|
$output .= "<button class='btn bg-dark w-100 btn-xs btn-force-delete' id='{$row->id}'><i class='fas fa-fire-alt'></i> {$force_delete} </button>"; |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
return $output; |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
/*---------------------------------- </> --------------------------------*/ |
519
|
|
|
|
520
|
|
|
if (!function_exists('addTableButton')) { |
521
|
|
|
function addTableButton($modelName, $title = null, $href = null, $name = null, $id = null, $icon = null) |
522
|
|
|
{ |
523
|
|
|
if (!is_can_create($modelName)) { |
524
|
|
|
return ''; |
525
|
|
|
} |
526
|
|
|
if (has_trash_param()) { |
527
|
|
|
return ''; |
528
|
|
|
} |
529
|
|
|
$name = $name ?? 'add'; |
530
|
|
|
$id = $id ?? 'add-btn'; |
531
|
|
|
$icon = $icon ?? 'fa-plus'; |
532
|
|
|
if (!$href) { |
533
|
|
|
$output = "<button class='btn btn-tool {$id}' name='{$name}' id='{$id}'> |
534
|
|
|
<i class='fas fa-fw {$icon} text-success'></i> |
535
|
|
|
{$title} |
536
|
|
|
</button> |
537
|
|
|
|
538
|
|
|
"; |
539
|
|
|
} else { |
540
|
|
|
$output = "<a href='{$href}' class='btn btn-tool {$id}' id='{$id}'> |
541
|
|
|
<i class='fas {$icon} text-success'></i> |
542
|
|
|
{$title} |
543
|
|
|
</a>"; |
544
|
|
|
} |
545
|
|
|
// <button type="button" class="btn btn-tool" data-card-widget="collapse"> |
546
|
|
|
// <i class="fas fa-minus"></i> |
547
|
|
|
// </button> |
548
|
|
|
// <button type="button" class="btn btn-tool" data-card-widget="remove"> |
549
|
|
|
// <i class="fas fa-times"></i> |
550
|
|
|
// </button> |
551
|
|
|
return $output; |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
/*---------------------------------- </> --------------------------------*/ |
555
|
|
|
|
556
|
|
|
if (!function_exists('addTrashButton')) { |
557
|
|
|
function addTrashButton($permissionName, $href = null, $params = null) |
558
|
|
|
{ |
559
|
|
|
$request_has_trashed = has_trash_param() ? false : true; |
560
|
|
|
if ($request_has_trashed) { |
561
|
|
|
if (!is_can_restore($permissionName) and !is_can_force_delete($permissionName)) { |
562
|
|
|
return ''; |
563
|
|
|
} |
564
|
|
|
$href = url("/admin/trash/{$href}"); |
565
|
|
|
if ($params) { |
566
|
|
|
$href .= $params; |
567
|
|
|
} |
568
|
|
|
$title = $title ?? __('button.deleted_records'); |
|
|
|
|
569
|
|
|
$id = $id ?? 'trash_data'; |
|
|
|
|
570
|
|
|
$icon = $icon ?? 'fa-trash-alt'; |
|
|
|
|
571
|
|
|
} else { |
572
|
|
|
if (!is_can_show($permissionName) and !is_can_show_all($permissionName)) { |
573
|
|
|
return ''; |
574
|
|
|
} |
575
|
|
|
$href = url("/admin/{$href}"); |
576
|
|
|
if ($params) { |
577
|
|
|
$href .= $params; |
578
|
|
|
} |
579
|
|
|
$title = $title ?? __('button.active_records'); |
580
|
|
|
$id = $id ?? 'all_data'; |
581
|
|
|
$icon = $icon ?? 'fa-list'; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
return "<a href='{$href}' class='btn btn-sm btn-danger float-right text-capitalize' id='{$id}'> <i class='fas {$icon}'></i> {$title} </a>"; |
585
|
|
|
} |
586
|
|
|
} |
587
|
|
|
/*---------------------------------- </> --------------------------------*/ |
588
|
|
|
|
589
|
|
|
if (!function_exists('activeRecordButton')) { |
590
|
|
|
/** |
591
|
|
|
* @param $permissionName |
592
|
|
|
* @param $href |
593
|
|
|
* @param $params |
594
|
|
|
* @param $className |
595
|
|
|
* |
596
|
|
|
* @return string |
597
|
|
|
*/ |
598
|
|
|
function activeRecordButton($permissionName, $href = null, $params = null, $className = null) |
599
|
|
|
{ |
600
|
|
|
if (request()->has('active') and (request()->get('active') === 'false')) { |
601
|
|
|
$href = url("/admin/{$href}"); |
602
|
|
|
$title = $title ?? __('active_records'); |
|
|
|
|
603
|
|
|
$className = $className ?? 'btn-outline-info'; |
604
|
|
|
$icon = 'fa-fw fa-check-circle'; |
605
|
|
|
} else { |
606
|
|
|
$href = url("/admin/{$href}?active=false"); |
607
|
|
|
$title = $title ?? __('inactive_records'); |
608
|
|
|
$className = $className ?? 'btn-warning'; |
609
|
|
|
$icon = 'fa-fw fa-times-circle'; |
610
|
|
|
} |
611
|
|
|
if (!is_can_show($permissionName) and !is_can_show_all($permissionName)) { |
612
|
|
|
return ''; |
613
|
|
|
} |
614
|
|
|
if ($params) { |
615
|
|
|
$href .= $params; |
616
|
|
|
} |
617
|
|
|
$id = $id ?? 'all_data'; |
|
|
|
|
618
|
|
|
$icon = $icon ?? 'fa-fw fa-check-circle'; |
619
|
|
|
|
620
|
|
|
return "<div class=''> <a href='{$href}' class='btn {$className} mx-2' id='{$id}'> <i class='fas {$icon}'></i> {$title} </a></div>"; |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
/*---------------------------------- </> --------------------------------*/ |
624
|
|
|
|
625
|
|
|
if (!function_exists('activeButton')) { |
626
|
|
|
function activeButton($item = null) |
627
|
|
|
{ |
628
|
|
|
if ($item) { |
629
|
|
|
$checked = old('active', $item->active) != 1 ? '' : 'checked'; |
630
|
|
|
} else { |
631
|
|
|
$checked = 'checked'; |
632
|
|
|
} |
633
|
|
|
$active = __('active').' <i class="fas fa-check"></i>'; |
|
|
|
|
634
|
|
|
$inactive = __('inactive').' <i class="fas fa-times"> </i>'; |
|
|
|
|
635
|
|
|
|
636
|
|
|
return "<div class='checkbox '> |
637
|
|
|
<!-- edit active button --> |
638
|
|
|
<input type='checkbox' class='text-center align-middle' name='active' id='_active' |
639
|
|
|
data-toggle='toggle' |
640
|
|
|
data-on='{$active}' data-onstyle='success' data-off='{$inactive}' data-offstyle='danger' data-width='125' value='1' {$checked}> |
641
|
|
|
<!-- edit active button --> |
642
|
|
|
</div>"; |
643
|
|
|
} |
644
|
|
|
} |
645
|
|
|
/*---------------------------------- </> --------------------------------*/ |
646
|
|
|
|
647
|
|
|
if (!function_exists('copyBtn')) { |
648
|
|
|
/** |
649
|
|
|
* @param $shorten_link |
650
|
|
|
* @param $className |
651
|
|
|
* |
652
|
|
|
* @return string |
653
|
|
|
*/ |
654
|
|
|
function copyBtn($shorten_link, $className = null) |
655
|
|
|
{ |
656
|
|
|
$className = $className ?? 'float-right'; |
657
|
|
|
|
658
|
|
|
return " <button class='btn border btn-light btn-clipboard {$className}' data-toggle='tooltip' data-clipboard-text='{$shorten_link}' title='".__('copy')."'> |
|
|
|
|
659
|
|
|
<img src='".asset('img/clippy.svg')." ' width='17px' alt='".__('copy_to_clipboard')."'></button>"; |
|
|
|
|
660
|
|
|
} |
661
|
|
|
} |
662
|
|
|
/*---------------------------------- </> --------------------------------*/ |
663
|
|
|
|
664
|
|
|
if (!function_exists('backButton')) { |
665
|
|
|
/** |
666
|
|
|
* @return string |
667
|
|
|
*/ |
668
|
|
|
function backButton() |
669
|
|
|
{ |
670
|
|
|
$url = url()->previous(); |
671
|
|
|
$title = __('back'); |
672
|
|
|
|
673
|
|
|
return "<a href='$url' class=' btn btn-default float-right'> <i class='fas fa-fw fa-chevron-circle-left'></i> $title </a>"; |
674
|
|
|
} |
675
|
|
|
} |
676
|
|
|
/*---------------------------------- </> --------------------------------*/ |
677
|
|
|
|
678
|
|
|
if (!function_exists('ClassName')) { |
679
|
|
|
function ClassName($modelName) |
680
|
|
|
{ |
681
|
|
|
// check name if content \\ dont add App as prefix model |
682
|
|
|
if (strpos($modelName, '\\') === false) { |
683
|
|
|
$modelName = "\App\\{$modelName}"; |
684
|
|
|
} |
685
|
|
|
// if(!(class_exists($modelName))) { |
686
|
|
|
// return "$modelName model Not Found."; |
687
|
|
|
// } |
688
|
|
|
return $modelName; |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
/*---------------------------------- </> --------------------------------*/ |
692
|
|
|
|
693
|
|
|
if (!function_exists('viewOrError')) { |
694
|
|
|
function viewOrError($permissionName, $viewName, $type) |
695
|
|
|
{ |
696
|
|
|
if (!is_can_create($permissionName)) { |
697
|
|
|
return view('backend.errors.401'); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
return view($viewName.$type); |
701
|
|
|
} |
702
|
|
|
} |
703
|
|
|
/*---------------------------------- </> --------------------------------*/ |
704
|
|
|
|
705
|
|
|
if (!function_exists('getActionColumn')) { |
706
|
|
|
function getActionColumn($datatable, $can_edit, $can_delete, $can_restore, $can_force_delete, $trash) |
707
|
|
|
{ |
708
|
|
|
$datatable->addColumn('action', function ($row) use ($can_edit, $can_delete, $can_restore, $can_force_delete, $trash) { |
709
|
|
|
if ($trash) { |
710
|
|
|
return trashActionLinks($row, $can_restore, $can_force_delete); |
711
|
|
|
} else { |
712
|
|
|
return actionLinks($row, $row->adminPrefix, $can_edit, $can_delete); |
713
|
|
|
} |
714
|
|
|
}); |
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
/*---------------------------------- </> --------------------------------*/ |
718
|
|
|
|
719
|
|
|
if (!function_exists('list_of_error_codes')) { |
720
|
|
|
function list_of_error_codes() |
721
|
|
|
{ |
722
|
|
|
return config('record_errors.codes'); |
723
|
|
|
// return [ |
724
|
|
|
// ['code' => 401, 'icon' => 'fas fa-fas fa-fw fa-exclamation', 'bg-color' => 'bg-warning', 'color' => 'warning'], |
725
|
|
|
// ['code' => 403, 'icon' => 'fas fa-fas fa-fw fa-exclamation-circle', 'bg-color' => 'bg-blue', 'color' => 'blue'], |
726
|
|
|
// ['code' => 404, 'icon' => 'fas fa-fas fa-fw fa-exclamation-triangle', 'bg-color' => 'bg-danger', 'color' => 'danger'], |
727
|
|
|
// ['code' => 419, 'icon' => 'fas fa-fas fa-fw fa-exclamation-circle', 'bg-color' => 'bg-secondary', 'color' => 'secondary'], |
728
|
|
|
// ['code' => 429, 'icon' => 'fas fa-fas fa-fw fa-exclamation-circle', 'bg-color' => 'bg-dark', 'color' => 'dark'], |
729
|
|
|
// ['code' => 500, 'icon' => 'fas fa-fas fa-fw fa-exclamation-triangle', 'bg-color' => 'bg-danger', 'color' => 'danger'], |
730
|
|
|
// ['code' => 503, 'icon' => 'fas fa-fas fa-fw fa-exclamation', 'bg-color' => 'bg-info', 'color' => 'info'], |
731
|
|
|
// ]; |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
/*---------------------------------- </> --------------------------------*/ |
735
|
|
|
|
736
|
|
|
if (!function_exists('list_of_menu_error_items')) { |
737
|
|
|
function list_of_menu_error_items() |
738
|
|
|
{ |
739
|
|
|
$items = []; |
740
|
|
|
foreach (list_of_error_codes() as $erra) { |
741
|
|
|
$items[] = [ |
742
|
|
|
'text' => "{$erra['code']} Error Records ", |
743
|
|
|
'url' => "errors-management/records/{$erra['code']}", |
744
|
|
|
'icon_color' => "{$erra['color']}", |
745
|
|
|
'icon' => "{$erra['icon']}", |
746
|
|
|
// 'can' => 'show-error-records' |
747
|
|
|
]; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
return $items; |
751
|
|
|
} |
752
|
|
|
} |
753
|
|
|
/*---------------------------------- </> --------------------------------*/ |
754
|
|
|
|
755
|
|
|
if (!function_exists('displayVisitsCount')) { |
756
|
|
|
function displayVisitsCount($value) |
757
|
|
|
{ |
758
|
|
|
if ($value > 1000000) { |
759
|
|
|
$number = $value / 1000000; |
760
|
|
|
|
761
|
|
|
return $newVal = number_format($number, 2).'M'; |
|
|
|
|
762
|
|
|
} else { |
763
|
|
|
if ($value > 1000) { |
764
|
|
|
$number = $value / 1000; |
765
|
|
|
|
766
|
|
|
return $newVal = number_format($number, 2).'K'; |
767
|
|
|
} |
768
|
|
|
} |
769
|
|
|
//if you want 2 decimal digits |
770
|
|
|
} |
771
|
|
|
} |
772
|
|
|
/*---------------------------------- </> --------------------------------*/ |
773
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: