Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Menu::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Utils {
4
5
	use Modules\Entitizer, Template;
6
7
	class Menu {
8
9
		private $menu = [0 => ['children' => []]];
10
11
		# Parse item
12
13
		private function parseItem(int $id) {
14
15
			if ([] !== $this->menu[$id]['children']) {
16
17
				$item = View::get('Blocks\Utils\Menu\Container');
18
19
				$item->text = $this->menu[$id]['entity']->text;
20
21
				$item->children = ($children = Template::group());
22
23
				foreach ($this->menu[$id]['children'] as $child) $children->add($this->parseItem($child));
24
25
			} else {
26
27
				$item = View::get('Blocks\Utils\Menu\Item');
28
29
				$item->target = (($this->menu[$id]['entity']->target === TARGET_BLANK) ? '_blank' : '_self');
30
31
				$item->link = $this->menu[$id]['entity']->link;
32
33
				$item->text = $this->menu[$id]['entity']->text;
34
			}
35
36
			# ------------------------
37
38
			return $item;
39
		}
40
41
		# Constructor
42
43
		public function __construct() {
44
45
			$menu = Entitizer::treeview(TABLE_MENU)->subtree(0, ['active' => true]);
46
47
			if (false !== $menu) $this->menu = $menu;
48
		}
49
50
		# Get block
51
52
		public function block() {
53
54
			$menu = Template::group();
55
56
			foreach ($this->menu[0]['children'] as $id) $menu->add($this->parseItem($id));
57
58
			# ------------------------
59
60
			return $menu;
61
		}
62
	}
63
}
64