-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenuManagerB.class.php
More file actions
102 lines (77 loc) · 2.65 KB
/
Copy pathMenuManagerB.class.php
File metadata and controls
102 lines (77 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* Created by PhpStorm.
* User: webform
* Date: 3/09/2018
* Time: 15:46
*/
class MenuManagerB {
// PDO connection
private $db;
// all sections in an associative array
private $datas;
// content in html for navigation
private $menu = "";
public function __construct(PDO $connect) {
// connection
$this->db = $connect;
// get values from "menu"
$this->setDatas($this->listMenu());
// create ul li arbor
$this->createMenu($this->getDatas());
}
// return all items from "menu" in an associative array
private function listMenu(): array {
$req = $this->db->query("SELECT * FROM menu;");
// not result
if (!$req->rowCount()) {
// return empty table
return [];
} else {
// return associative array
return $req->fetchAll(PDO::FETCH_ASSOC);
}
}
// recursive method
private function createMenu($array,$parent_id = 0,$parents = array()) {
foreach ($array as $element) {
$parents[] = $element['parentMenu'];
}
foreach($array as $element)
{
if($element['parentMenu']==$parent_id)
{
if(in_array($element['idMenu'],$parents))
{
$this->setMenu('<li class="dropdown-item dropdown">');
$this->setMenu('<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">'.$element['titleMenu'].' <span class="caret"></span></a> ');
}
else {
$this->setMenu('<li class="nav-item dropdown-item">');
$this->setMenu('<a href=?id="'.$element['idMenu']. '">' . $element['titleMenu'] . '</a>');
}
//if(in_array($element['idMenu'],$parents))
//{
$this->setMenu('<ul class="dropdown-menu">');
$this->setMenu($this->createMenu($array, $element['idMenu'], $parents));
$this->setMenu('</ul>');
//}
$this->setMenu('</li>');
}
}
//return $menu_html;
}
public function getDatas(): array {
return $this->datas;
}
private function setDatas($datas): void {
$this->datas = $datas;
}
public function getMenu(): string {
return $this->menu;
}
public function setMenu($menu): void {
// concatenation
$this->menu .= $menu;
}
}