1 | <?php |
||
2 | |||
3 | namespace AlexBowers\MultipleDashboard; |
||
4 | |||
5 | use Laravel\Nova\Nova; |
||
6 | use Illuminate\Http\Request; |
||
7 | use Laravel\Nova\Http\Requests\NovaRequest; |
||
8 | use Illuminate\Support\Str; |
||
9 | use Symfony\Component\Finder\Finder; |
||
10 | |||
11 | class DashboardNova extends Nova |
||
12 | { |
||
13 | /** |
||
14 | * The registered dashboard names. |
||
15 | */ |
||
16 | public static $dashboards = []; |
||
17 | |||
18 | /** |
||
19 | * The registered cards for the default dashboard. |
||
20 | */ |
||
21 | public static $default_dashboard_cards = []; |
||
22 | |||
23 | /** |
||
24 | * Copy the default dashboard cards so we can |
||
25 | * return which cards are in use on the |
||
26 | * main dashboard. |
||
27 | */ |
||
28 | public static function copyDefaultDashboardCards() |
||
29 | { |
||
30 | static::$default_dashboard_cards = static::$cards; |
||
31 | |||
32 | return new static; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Get the dashboards registered with Nova. |
||
37 | * |
||
38 | * @param \Illuminate\Http\Request $request |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public static function availableDashboards(Request $request) |
||
42 | { |
||
43 | return collect(static::$dashboards) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
44 | ->filter |
||
45 | ->authorize($request) |
||
46 | ->all(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Register one or more dashboards |
||
51 | * |
||
52 | * @param Dashboard ...$dashboards |
||
53 | */ |
||
54 | public static function registerDashboards(Dashboard ...$dashboards) |
||
55 | { |
||
56 | static::dashboards($dashboards); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Register the dashboards from a specific directory |
||
61 | */ |
||
62 | public static function dashboardsIn($directory) |
||
63 | { |
||
64 | $namespace = app()->getNamespace(); |
||
65 | |||
66 | $dashboards = []; |
||
67 | |||
68 | foreach ((new Finder)->in($directory)->files() as $dashboard) { |
||
69 | $dashboard = $namespace.str_replace( |
||
70 | ['/', '.php'], |
||
71 | ['\\', ''], |
||
72 | Str::after($dashboard->getPathname(), app_path().DIRECTORY_SEPARATOR) |
||
73 | ); |
||
74 | |||
75 | if (is_subclass_of($dashboard, Dashboard::class) && !(new \ReflectionClass($dashboard))->isAbstract()) { |
||
76 | $dashboards[] = $dashboard; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | static::dashboards( |
||
81 | collect($dashboards)->transform(function ($dashboard) { |
||
82 | if ($dashboard instanceof Dashboard) { |
||
83 | return $dashboard; |
||
84 | } |
||
85 | |||
86 | return app()->make($dashboard); |
||
87 | })->sortBy('label')->sortBy('order')->all() |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Load the cards in use in each dashboard |
||
93 | */ |
||
94 | public static function cardsInDashboards() |
||
95 | { |
||
96 | $dashboards = static::$dashboards; |
||
97 | |||
98 | foreach ($dashboards as $dashboard) { |
||
99 | DashboardNova::cards($dashboard->cards()); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Register the given dashboards. |
||
105 | * |
||
106 | * @param array $dashboards |
||
107 | * @return static |
||
108 | */ |
||
109 | public static function dashboards(array $dashboards) |
||
110 | { |
||
111 | static::$dashboards = array_merge(static::$dashboards, $dashboards); |
||
112 | |||
113 | return new static; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Get the available dashboard cards for the given request. |
||
118 | * |
||
119 | * @param \Laravel\Nova\Http\Requests\NovaRequest $request |
||
120 | * @return \Illuminate\Support\Collection |
||
121 | */ |
||
122 | public static function allAvailableDashboardCards(NovaRequest $request) |
||
123 | { |
||
124 | return collect(static::$dashboards) |
||
125 | ->filter |
||
126 | ->authorize($request) |
||
127 | ->flatMap(function ($dashboard) { |
||
128 | return $dashboard->cards(); |
||
129 | })->merge(static::$cards) |
||
130 | ->unique() |
||
131 | ->filter |
||
132 | ->authorize($request) |
||
133 | ->values(); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get the available dashboard cards for the given request. |
||
138 | * |
||
139 | * @param string $dashboard |
||
140 | * @param \Laravel\Nova\Http\Requests\NovaRequest $request |
||
141 | * @return \Illuminate\Support\Collection |
||
142 | */ |
||
143 | public static function availableDashboardCardsForDashboard($dashboard, NovaRequest $request) |
||
144 | { |
||
145 | return collect(static::$dashboards)->filter->authorize($request)->filter(function ($board) use ($dashboard) { |
||
146 | return $board->uriKey() === $dashboard; |
||
147 | })->flatMap(function ($dashboard) { |
||
148 | return $dashboard->cards(); |
||
149 | })->filter->authorize($request)->values(); |
||
150 | } |
||
151 | } |
||
152 |