1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anavel\Crud\View\Composers; |
4
|
|
|
|
5
|
|
|
use Anavel\Crud\Contracts\Abstractor\ModelFactory as ModelAbstractorFactory; |
6
|
|
|
use Gate; |
7
|
|
|
use Route; |
8
|
|
|
|
9
|
|
|
class SidebarComposer |
10
|
|
|
{ |
11
|
|
|
protected $modelFactory; |
12
|
|
|
|
13
|
|
|
public function __construct(ModelAbstractorFactory $modelFactory) |
14
|
|
|
{ |
15
|
|
|
$this->modelFactory = $modelFactory; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function compose($view) |
19
|
|
|
{ |
20
|
|
|
$menuItems = $this->getSidebarItems(); |
21
|
|
|
$view->with([ |
22
|
|
|
'menuItems' => $menuItems, |
23
|
|
|
'headerName' => config('anavel-crud.name'), |
24
|
|
|
]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
private function getSidebarItems() |
31
|
|
|
{ |
32
|
|
|
$modelsGroups = config('anavel-crud.modelsGroups'); |
33
|
|
|
$models = config('anavel-crud.models'); |
34
|
|
|
$menuItems = []; |
35
|
|
|
|
36
|
|
|
if (!is_null($modelsGroups)) { |
37
|
|
|
foreach ($modelsGroups as $group => $items) { |
|
|
|
|
38
|
|
|
$menuItems[$group]['isActive'] = false; |
39
|
|
|
$menuItems[$group]['name'] = transcrud($group); |
40
|
|
|
$menuItems[$group]['items'] = []; |
41
|
|
|
foreach ($items as $itemName) { |
42
|
|
|
try { |
43
|
|
|
$item = $this->getModelItem($itemName); |
44
|
|
|
$menuItems[$group]['items'][] = $item; |
45
|
|
|
if (!$menuItems[$group]['isActive'] && $item['isActive']) { |
46
|
|
|
$menuItems[$group]['isActive'] = true; |
47
|
|
|
} |
48
|
|
|
if (isset($models[$itemName])) { |
49
|
|
|
unset($models[$itemName]); |
50
|
|
|
} |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
// Remove empty groups (resulting, most probably, of different permissions) |
56
|
|
|
if (count($menuItems[$group]['items']) < 1) { |
57
|
|
|
unset($menuItems[$group]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($models as $modelName => $model) { |
63
|
|
|
try { |
64
|
|
|
$item = $this->getModelItem($modelName); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
$menuItems[$modelName]['isActive'] = $item['isActive']; |
69
|
|
|
$menuItems[$modelName]['name'] = $item['name']; |
70
|
|
|
$menuItems[$modelName]['items'][] = $item; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//Sort alphabetically de menu items |
74
|
|
|
usort($menuItems, function ($itemA, $itemB) { |
75
|
|
|
return strcmp($itemA['name'], $itemB['name']); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
return $menuItems; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $modelName |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
private function getModelItem($modelName) |
89
|
|
|
{ |
90
|
|
|
$modelAbstractor = $this->modelFactory->getByName($modelName); |
91
|
|
|
if (array_key_exists('authorize', $config = $modelAbstractor->getConfig()) && $config['authorize'] === true) { |
92
|
|
|
if (Gate::denies('adminIndex', $modelAbstractor->getInstance())) { |
93
|
|
|
throw new \Exception('Access denied'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$isActive = false; |
98
|
|
|
if (Route::current()->getParameter('model') === $modelAbstractor->getSlug()) { |
99
|
|
|
$isActive = true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$item = [ |
103
|
|
|
'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()), |
104
|
|
|
'name' => $modelAbstractor->getName(), |
105
|
|
|
'isActive' => $isActive, |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
return $item; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.