1
|
|
|
<?php |
|
|
|
|
2
|
|
|
jRequire("../Query/Query.php"); |
3
|
|
|
class Menu extends Query { |
|
|
|
|
4
|
|
|
public function __construct() { |
5
|
|
|
parent::__construct(); |
6
|
|
|
} |
7
|
|
|
public function init() { |
8
|
|
|
$menu = $this->queryFetch("SELECT * FROM menu WHERE flag_active = 1 ORDER BY `order`"); |
9
|
|
|
$temp = []; |
10
|
|
|
foreach ($menu as $i) { |
11
|
|
|
if($i["fk_menu"] == 0) { |
12
|
|
|
$pk_menu = $i["pk_menu"]; |
13
|
|
|
array_push($temp, array("label" => $i["label"], "link" => $i["link"], "submenu" => [], "relative" => false)); |
14
|
|
|
$submenu = $this->queryFetch("SELECT * FROM menu WHERE fk_menu = $pk_menu ORDER BY `order`"); |
15
|
|
|
if($submenu) |
16
|
|
|
foreach ($submenu as $j) |
17
|
|
|
array_push( $temp[count($temp)-1]["submenu"], array("label" => $j["label"], "link" => $j["link"], "submenu" => [], "relative" => false) ); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
$this->tags["menu"] = $temp; |
21
|
|
|
return $temp; |
22
|
|
|
} |
23
|
|
|
public function draw() { |
24
|
|
|
$temp = ""; |
25
|
|
|
$host = $this->parameters["app"]->server["HTTP_HOST"]; |
26
|
|
|
$uri = $this->parameters["app"]->server["REQUEST_URI"]; |
27
|
|
|
$actualLink = "http://$host$uri"; |
28
|
|
|
$relative = $this->parameters["app"]->server["RELATIVE"]; |
29
|
|
|
foreach ($this->tags["menu"] as $i) { |
30
|
|
|
$prePath = ""; |
31
|
|
|
if($i["relative"]) |
32
|
|
|
$prePath = $relative; |
33
|
|
|
$active = $this->isSubString($actualLink, array_merge(array_column($i["submenu"], 'link'), array($i["link"]))) ? "active" : ""; |
34
|
|
|
if( is_array($i["submenu"]) && count($i["submenu"])<1) |
35
|
|
|
$temp .= "<li class='$active'><a href='$prePath$i[link]'>$i[label]</a></li>"; |
36
|
|
|
else { |
37
|
|
|
$temp .= "<li class='dropdown $active'>"; |
38
|
|
|
$temp .= |
39
|
|
|
'<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'. |
40
|
|
|
$i["label"]. |
41
|
|
|
'<span class="caret"></span>'. |
42
|
|
|
'</a>'. |
43
|
|
|
'<ul class="dropdown-menu">'; |
44
|
|
|
foreach ($i["submenu"] as $j) { |
45
|
|
|
$prePath = ""; |
46
|
|
|
if($j["relative"]) |
47
|
|
|
$prePath = $relative; |
48
|
|
|
$temp .= "<li><a href='$prePath$j[link]'>$j[label]</a></li>"; |
49
|
|
|
} |
50
|
|
|
$temp .= "</ul></li>"; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $temp; |
54
|
|
|
} |
55
|
|
|
protected function isSubString( $_string, $_list) { |
56
|
|
|
$success = false; |
57
|
|
|
foreach ($_list as $i) |
58
|
|
|
if(strpos($_string,$i) !== false) |
59
|
|
|
$success = true; |
60
|
|
|
return $success; |
61
|
|
|
} |
62
|
|
|
public function loginWithUser() { |
|
|
|
|
63
|
|
|
$this->init(); |
64
|
|
|
$temp = []; |
65
|
|
|
if(!isset($_SESSION["username"])) |
66
|
|
|
$_SESSION["username"] ="guest"; |
67
|
|
|
$user = $_SESSION["username"]; |
68
|
|
|
$blackList = $this->queryFetch( |
69
|
|
|
"SELECT user.*,user_section.* |
70
|
|
|
FROM user |
71
|
|
|
INNER JOIN user_x_section |
72
|
|
|
ON user.pk_user = user_x_section.fk_user |
73
|
|
|
INNER JOIN user_section |
74
|
|
|
ON user_section.pk_user_section = user_x_section.fk_user_section |
75
|
|
|
WHERE user.username = '$user'" |
76
|
|
|
); |
77
|
|
|
foreach ($this->tags["menu"] as $i) { |
|
|
|
|
78
|
|
|
$success = true; |
79
|
|
|
$k = $i["label"]; |
80
|
|
|
foreach ($blackList as $j) |
81
|
|
|
if( $j["section"] == $k) |
82
|
|
|
$success = false; |
83
|
|
|
if($success) |
84
|
|
|
array_push($temp,$i); |
85
|
|
|
} |
86
|
|
|
$this->tags["menu"] = $temp; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
?> |
|
|
|
|
90
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.