Passed
Push — master ( 5ca280...347b45 )
by Georgi
02:45
created

RightMenu::addTool()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php 
2
3
namespace Epesi\Base\Layout\Seeds;
4
5
use atk4\ui\Menu as BaseMenu;
6
use atk4\ui\FormField\Input;
7
use Illuminate\Support\Facades\URL;
8
use Epesi\Base\Layout\Integration\Joints\UserMenuJoint;
9
use Illuminate\Support\Facades\Auth;
10
11
class RightMenu extends BaseMenu
12
{
13
	protected $entries;
14
	protected $tools;
15
	
16
	protected $userMenu;
17
	
18
	protected $joint = UserMenuJoint::class;
19
	
20
	public function init()
21
	{		
22
		parent::init();
23
24
		$this->entries = collect();
25
		
26
		$this->tools = collect();
27
		
28
		$this->addItem()->add(new Input([
29
				'placeholder' => 'Search',
30
				'icon' => 'search'
31
		]))->addClass('transparent');
32
		
33
		// $messageMenu = $this->menuRight->addMenu(['', 'icon' => 'envelope outline']);
34
		
35
		$this->addItem([
36
				'icon' => 'th'
37
		], new LaunchPad($this))->setAttr([
0 ignored issues
show
Bug introduced by
new Epesi\Base\Layout\Seeds\LaunchPad($this) of type Epesi\Base\Layout\Seeds\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

37
		], /** @scrutinizer ignore-type */ new LaunchPad($this))->setAttr([
Loading history...
38
				'title' => __('Launchpad'),
39
		]);
40
		
41
		foreach(UserMenuJoint::collect() as $joint) {
42
			foreach ($joint->tools()?: [] as $tool) {
43
				$this->addTool($tool);
44
			}
45
46
			foreach ($joint->entries()?: [] as $entry) {
47
				$this->addEntry($entry);
48
			}			
49
		}
50
51
		$this->userMenu = $this->addMenu([
52
				Auth::user()->name . ' as Team A',
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...
53
				'icon' => 'user'
54
		]);
55
		
56
		$this->addEntry(['Perspective', 'icon' => 'users']);
57
		$this->addEntry(['My Contact', 'icon' => 'contact']);
58
		$this->addEntry(['My Company', 'icon' => 'users']);
59
		
60
		$this->addEntry([
61
				'item' => ['Logout', 'icon' => 'sign out', 'attr' => ['onclick' => "event.preventDefault();$('#logout-form').submit();"]],
62
				'action' => url('logout'), 
63
				'group' => '10000:user',
64
				'callback' => function ($item){
65
					$logoutForm = $item->add(['View', 'attr' => ['method' => 'POST', 'action' => URL::to('logout')]])->setElement('form')->addStyle(['display' => 'none']);
66
					$logoutForm->id = 'logout-form';
67
					$logoutForm->add(['View', 'attr' => ['type' => 'hidden', 'name' => '_token', 'value' => csrf_token()]])->setElement('input');
68
				},
69
		]);
70
	}
71
72
	public function addEntry($entry)
73
	{
74
		$entry = collect(array_merge(['item' => $entry, 'group' => '00500:general', 'weight' => 10], $entry));
75
		
76
		if (! $entry->get('item')) return;
77
		
78
		$this->entries->add($entry);
79
	}
80
	
81
	public function addTool($tool)
0 ignored issues
show
Unused Code introduced by
The parameter $tool is not used and could be removed. ( Ignorable by Annotation )

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

81
	public function addTool(/** @scrutinizer ignore-unused */ $tool)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
	{
83
		
84
	}
85
	
86
	public function renderView()
87
	{
88
		$this->addRegisteredEntries();
89
		
90
		parent::renderView();
91
	}
92
	
93
	protected function addRegisteredEntries()
94
	{
95
		$empty = true;
96
		foreach ($this->entries->groupBy('group')->sortKeys() as $group) {
97
			if (!$empty) $this->userMenu->addDivider();
98
		
99
			foreach ($group->sortBy('weight') as $entry) {
100
				$empty = false;
101
				
102
				$item = $this->userMenu->addItem($entry['item'], $entry->get('action'));
103
				
104
				if (! $callback = $entry->get('callback')) continue;
105
					
106
				$callback($item);
107
			}
108
		}
109
	}
110
}