1
|
|
|
<?php |
2
|
|
|
namespace Anavel\Crud\View\Composers; |
3
|
|
|
|
4
|
|
|
use Anavel\Crud\Contracts\Abstractor\ModelFactory as ModelAbstractorFactory; |
5
|
|
|
use Route; |
6
|
|
|
use Gate; |
7
|
|
|
|
8
|
|
|
class SidebarComposer |
9
|
|
|
{ |
10
|
|
|
protected $modelFactory; |
11
|
|
|
|
12
|
|
|
public function __construct(ModelAbstractorFactory $modelFactory) |
13
|
|
|
{ |
14
|
|
|
$this->modelFactory = $modelFactory; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function compose($view) |
18
|
|
|
{ |
19
|
|
|
$menuItems = $this->getSidebarItems(); |
20
|
|
|
$view->with([ |
21
|
|
|
'menuItems' => $menuItems, |
22
|
|
|
'headerName' => config('anavel-crud.name') |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
private function getSidebarItems() |
30
|
|
|
{ |
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'] = $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
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($models as $modelName => $model) { |
59
|
|
|
try { |
60
|
|
|
$item = $this->getModelItem($modelName); |
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
$menuItems[$modelName]['isActive'] = $item['isActive']; |
65
|
|
|
$menuItems[$modelName]['name'] = $item['name']; |
66
|
|
|
$menuItems[$modelName]['items'][] = $item; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//Sort alphabetically de menu items |
70
|
|
|
usort($menuItems, function($itemA, $itemB) |
71
|
|
|
{ |
72
|
|
|
return strcmp($itemA['name'],$itemB['name']); |
73
|
|
|
}); |
74
|
|
|
return $menuItems; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $modelName |
79
|
|
|
* @return array |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
private function getModelItem($modelName) |
83
|
|
|
{ |
84
|
|
|
$modelAbstractor = $this->modelFactory->getByName($modelName); |
85
|
|
|
if (array_key_exists('authorize', $config = $modelAbstractor->getConfig()) && $config['authorize'] === true) { |
86
|
|
|
if (Gate::denies('adminIndex', $modelAbstractor->getInstance())) { |
87
|
|
|
throw new \Exception('Access denied'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$isActive = false; |
92
|
|
|
if (Route::current()->getParameter('model') === $modelAbstractor->getSlug()) { |
93
|
|
|
$isActive = true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$item = [ |
97
|
|
|
'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()), |
98
|
|
|
'name' => $modelAbstractor->getName(), |
99
|
|
|
'isActive' => $isActive |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
return $item; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.