Test Failed
Push — master ( 794710...4acb1e )
by Georgi
03:38
created

RightMenu::renderView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php 
2
3
namespace Epesi\Core\Layout\View;
4
5
use atk4\ui\Menu as BaseMenu;
6
use atk4\ui\FormField\Input;
7
use Epesi\Core\Layout\Integration\Joints\UserMenuJoint;
8
use Illuminate\Support\Facades\Auth;
9
10
class RightMenu extends BaseMenu
11
{
12
	protected $entries;
13
	protected $tools;
14
	
15
	protected $userMenu;
16
	
17
	protected $userMenuLabel;
18
	
19
	public function init()
20
	{		
21
		parent::init();
22
23
		$this->entries = collect();
24
		
25
		$this->tools = collect();
26
		
27
		// credits
28
		$this->addItem(__(':epesi powered version :version', ['epesi' => config('epesi.ui.credit.title', 'EPESI'), 'version' => $this->app->version]))->setStyle('font-size', '80%')->link(config('epesi.ui.credit.link'));
29
		
30
		// global search
31
		$this->addItem()->add(new Input([
32
				'placeholder' => 'Search',
33
				'icon' => 'search'
34
		]))->addClass('transparent');
35
		
36
		// $messageMenu = $this->menuRight->addMenu(['', 'icon' => 'envelope outline']);
37
		
38
		$this->addItem([
39
				'icon' => 'th'
40
		], new LaunchPad($this));
0 ignored issues
show
Bug introduced by
new Epesi\Core\Layout\View\LaunchPad($this) of type Epesi\Core\Layout\View\LaunchPad is incompatible with the type array|string expected by parameter $action of atk4\ui\Menu::addItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
		], /** @scrutinizer ignore-type */ new LaunchPad($this));
Loading history...
41
		
42
		foreach(UserMenuJoint::collect() as $joint) {
43
			foreach ($joint->tools()?: [] as $tool) {
44
				$this->addTool($tool);
45
			}
46
47
			foreach ($joint->entries()?: [] as $entry) {
48
				$this->addEntry($entry);
49
			}			
50
		}
51
52
		$this->userMenu = $this->addMenu([
53
				$this->getUserMenuLabel(),
54
				'icon' => 'user'
55
		]);
56
		
57
		$this->addEntry(['Perspective', 'icon' => 'users']);
58
		$this->addEntry(['My Contact', 'icon' => 'contact']);
59
		$this->addEntry(['My Company', 'icon' => 'users']);
60
		
61
		$this->addEntry([
62
				'item' => ['Logout', 'icon' => 'sign out', 'attr' => ['onclick' => "event.preventDefault();$('#logout-form').submit();"]],
63
				'action' => url('logout'), 
64
				'group' => '10000:user',
65
				'callback' => function ($item){
66
					$logoutForm = $item->add(['View', 'attr' => ['method' => 'POST', 'action' => url('logout')]])->setElement('form')->addStyle(['display' => 'none']);
67
					$logoutForm->id = 'logout-form';
68
					$logoutForm->add(['View', 'attr' => ['type' => 'hidden', 'name' => '_token', 'value' => csrf_token()]])->setElement('input');
69
				},
70
		]);
71
	}
72
73
	public function setUserMenuLabel($label)
74
	{
75
		$this->userMenuLabel = $label;
76
		
77
		return $this;
78
	}
79
	
80
	public function getUserMenuLabel()
81
	{
82
		return $this->userMenuLabel?: Auth::user()->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83
	}
84
	
85
	public function addEntry($entry)
86
	{
87
		$entry = collect(array_merge(['item' => $entry, 'group' => '00500:general', 'weight' => 10], $entry));
88
		
89
		if (! $entry->get('item')) return;
90
		
91
		$this->entries->add($entry);
92
	}
93
	
94
	public function addTool($tool)
95
	{
96
		
97
	}
98
	
99
	public function renderView()
100
	{
101
		$this->addRegisteredEntries();
102
		
103
		parent::renderView();
104
	}
105
	
106
	protected function addRegisteredEntries()
107
	{
108
		$empty = true;
109
		foreach ($this->entries->groupBy('group')->sortKeys() as $group) {
110
			if (!$empty) $this->userMenu->addDivider();
111
		
112
			foreach ($group->sortBy('weight') as $entry) {
113
				$empty = false;
114
				
115
				$item = $this->userMenu->addItem($entry['item'], $entry->get('action'));
116
				
117
				if (! $callback = $entry->get('callback')) continue;
118
					
119
				$callback($item);
120
			}
121
		}
122
	}
123
}