|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Serverfireteam\Panel\libs; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Serverfireteam\Panel\LinkRepository; |
|
9
|
|
|
|
|
10
|
|
|
class dashboard |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Dashboard items cache |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
public static $dashboardItems; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Either retrieve the dashboard items from cache or from the config/DB if they were not yet cached |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function getItems () |
|
24
|
|
|
{ |
|
25
|
|
|
if (!self::$dashboardItems) { |
|
|
|
|
|
|
26
|
|
|
self::$dashboardItems = \App::call(self::class . '@create'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return self::$dashboardItems; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Determine whether to show the given entity type in the panel |
|
34
|
|
|
* @param $link |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
private function showLink ($link) |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$link['show_menu']) return false; |
|
40
|
|
|
|
|
41
|
|
|
$user = \Auth::guard('panel')->user(); |
|
42
|
|
|
|
|
43
|
|
|
return $user->hasRole('super') || $user->hasPermission('/' . $link['url'] . '/all'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return the array of entity types (models / links) |
|
48
|
|
|
* to show CRUD interfaces for in the panel |
|
49
|
|
|
* |
|
50
|
|
|
* @param AppHelper $appHelper |
|
51
|
|
|
* @param LinkRepository $linkRepository |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function create (AppHelper $appHelper, LinkRepository $linkRepository) |
|
56
|
|
|
{ |
|
57
|
|
|
// @TODO cache |
|
58
|
|
|
|
|
59
|
|
|
return $linkRepository->all() |
|
60
|
|
|
|
|
61
|
|
|
->filter(function ($link) { |
|
62
|
|
|
return $this->showLink($link); |
|
63
|
|
|
}) |
|
64
|
|
|
|
|
65
|
|
|
->map(function ($link) use ($appHelper) { |
|
66
|
|
|
|
|
67
|
|
|
$modelName = $link['url']; |
|
68
|
|
|
|
|
69
|
|
|
$model = $appHelper->getModel($modelName); |
|
70
|
|
|
|
|
71
|
|
|
return [ |
|
72
|
|
|
'modelName' => $modelName, |
|
73
|
|
|
'title' => $link['display'], |
|
74
|
|
|
'count' => $model::count(), |
|
75
|
|
|
'showListUrl' => 'panel/' . $modelName . '/all', |
|
76
|
|
|
'addUrl' => 'panel/' . $modelName . '/edit', |
|
77
|
|
|
]; |
|
78
|
|
|
}) |
|
79
|
|
|
|
|
80
|
|
|
->toArray(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.