Test Failed
Push — master ( 9287ab...2f3843 )
by Georgi
03:56
created

NavMenu::addItems()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
nc 1
nop 2
dl 0
loc 32
rs 9.0111
c 1
b 0
f 0
1
<?php 
2
3
namespace Epesi\Core\Layout\Seeds;
4
5
use Illuminate\Support\Collection;
6
use atk4\ui\Menu as BaseMenu;
7
use Epesi\Core\Layout\Integration\Joints\NavMenuJoint;
8
9
class NavMenu extends BaseMenu
10
{
11
    public $ui = 'inverted nav menu';
12
    
13
	public function init()
14
	{		
15
		parent::init();
16
		
17
		$this->addHeader($this->app->title);
18
		
19
		$items = collect();
20
		foreach(NavMenuJoint::collect() as $joint) {
21
			$items = $items->merge($joint->items());
22
		}
23
24
		$this->addItems($this, $items);
25
		
26
// 		$this->js(true)->find('.toggle-group .header')->click(new jsFunction(['e'], [new jsExpression('$(e.target).next(".menu").slideToggle()')]))->click();
27
		
28
// 		$this->app->addStyle('
29
// 			.toggle-group .header {
30
// 				cursor: pointer;
31
// 			}
32
// 		');
33
	}
34
	
35
	public static function addItems($menu, Collection $items)
36
	{
37
		$items->sort(function ($entry1, $entry2) {
38
			$weight1 = $entry1['weight']?? 10;
39
			$weight2 = $entry2['weight']?? 10;
40
			
41
			return $weight1 <=> $weight2;
42
		})->map(function($entry, $caption) use ($menu) {
43
			if (! ($entry['access'] ?? true)) return;
44
			
45
			if (!is_array($entry)) {
46
				$entry = ['action' => $entry];
47
			}
48
			
49
			$entry['item'] = $entry['item'] ?? $caption;
50
			
51
			if (is_array($entry['item'])) {
52
				$entry['item'] = [$caption] + $entry['item'];
53
			}
54
			
55
			if ($subitems = $entry['menu'] ?? []) {
56
				$submenu = $menu->addMenu($entry['item']);
57
				
58
				self::addItems($submenu, collect($subitems));
59
			}
60
			elseif ($subitems = $entry['group'] ?? []) {
61
			    $subgroup = $menu->addGroup($entry['item']);
62
63
				self::addItems($subgroup, collect($subitems));
64
			}
65
			else {
66
			    $menu->addItem($entry['item'], $entry['action'] ?? '');
67
			}
68
		});
69
	}
70
}