Menu: Updated code to create menu HTML from array, instead of database directly.

This commit is contained in:
reliberate 2015-07-30 11:49:51 -06:00
parent 3f4a674364
commit 85c971eec8
2 changed files with 65 additions and 136 deletions

View File

@ -243,118 +243,84 @@
//create the menu //create the menu
function build_html($sql, $menu_item_level) { function build_html($menu_item_level = 0) {
$db = $this->db; $db = $this->db;
$db_menu_full = ''; $menu_html_full = '';
$menu_array = $this->menu_array();
if (!isset($_SESSION['groups'])) { if (!isset($_SESSION['groups'])) {
$_SESSION['groups'][0]['group_name'] = 'public'; $_SESSION['groups'][0]['group_name'] = 'public';
} }
if (strlen($sql) == 0) { //default sql for base of the menu foreach($menu_array as $menu_field) {
$sql = "select i.menu_item_link, l.menu_item_title as menu_language_title, i.menu_item_title, i.menu_item_protected, i.menu_item_category, i.menu_item_uuid, i.menu_item_parent_uuid "; //set the variables
$sql .= "from v_menu_items as i, v_menu_languages as l "; $menu_item_link = $menu_field['menu_item_link'];
$sql .= "where i.menu_item_uuid = l.menu_item_uuid "; $menu_item_category = $menu_field['menu_item_category'];
$sql .= "and l.menu_language = '".$_SESSION['domain']['language']['code']."' "; $menu_items = $menu_field['menu_items'];
$sql .= "and l.menu_uuid = '".$this->menu_uuid."' ";
$sql .= "and i.menu_uuid = '".$this->menu_uuid."' ";
$sql .= "and i.menu_item_parent_uuid is null ";
$sql .= "and i.menu_item_uuid in ";
$sql .= "(select menu_item_uuid from v_menu_item_groups where menu_uuid = '".$this->menu_uuid."' ";
$sql .= "and ( ";
if (!isset($_SESSION['groups'])) {
$sql .= "group_name = 'public' ";
}
else {
$x = 0;
foreach($_SESSION['groups'] as $row) {
if ($x == 0) {
$sql .= "group_name = '".$row['group_name']."' ";
}
else {
$sql .= "or group_name = '".$row['group_name']."' ";
}
$x++;
}
}
$sql .= ") ";
$sql .= "and menu_item_uuid is not null ";
$sql .= ") ";
$sql .= "order by i.menu_item_order asc ";
}
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach($result as $field) {
$menu_tags = '';
switch ($field['menu_item_category']) {
case "internal":
$menu_tags = "href='".PROJECT_PATH.$field['menu_item_link']."'";
break;
case "external":
if (substr($field['menu_item_link'], 0,1) == "/") {
$field['menu_item_link'] = PROJECT_PATH . $field['menu_item_link'];
}
$menu_tags = "href='".$field['menu_item_link']."' target='_blank'";
break;
case "email":
$menu_tags = "href='mailto:".$field['menu_item_link']."'";
break;
}
//prepare the protected menus //prepare the protected menus
if ($field['menu_item_protected'] == "true") { $menu_item_title = ($menu_field['menu_item_protected'] == "true") ? $menu_field['menu_item_title'] : $menu_field['menu_language_title'];
$menu_item_title = $field['menu_item_title'];
}
else {
$menu_item_title = $field['menu_language_title'];
}
if ($menu_item_level == "main") { //prepare the menu_tags according to the category
$db_menu = "<ul class='menu_main'>\n"; $menu_tags = '';
$db_menu .= "<li>\n"; switch ($menu_item_category) {
case "internal":
$menu_tags = "href='".PROJECT_PATH.$submenu_item_link."'";
break;
case "external":
if (substr($submenu_item_link, 0,1) == "/") {
$submenu_item_link = PROJECT_PATH.$submenu_item_link;
}
$menu_tags = "href='".$submenu_item_link."' target='_blank'";
break;
case "email":
$menu_tags = "href='mailto:".$submenu_item_link."'";
break;
}
if ($menu_item_level == 0) {
$menu_html = "<ul class='menu_main'>\n";
$menu_html .= "<li>\n";
if (!isset($_SESSION["username"])) { if (!isset($_SESSION["username"])) {
$_SESSION["username"] = ''; $_SESSION["username"] = '';
} }
if (strlen($_SESSION["username"]) == 0) { if (strlen($_SESSION["username"]) == 0) {
$db_menu .= "<a $menu_tags style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n"; $menu_html .= "<a $menu_tags style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n";
} }
else { else {
if ($field['menu_item_link'] == "/login.php" || $field['menu_item_link'] == "/users/signup.php") { if ($submenu_item_link == "/login.php" || $submenu_item_link == "/users/signup.php") {
//hide login and sign-up when the user is logged in //hide login and sign-up when the user is logged in
} }
else { else {
if (strlen($field['menu_item_link']) == 0) { if (strlen($submenu_item_link) == 0) {
$db_menu .= "<h2 align='center' style=''>".$menu_item_title."</h2>\n"; $menu_html .= "<h2 align='center' style=''>".$menu_item_title."</h2>\n";
} }
else { else {
$db_menu .= "<a ".$menu_tags." style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n"; $menu_html .= "<a ".$menu_tags." style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n";
} }
} }
} }
} }
$menu_item_level = 0; if (is_array($menu_field['menu_items']) && count($menu_field['menu_items']) > 0) {
if (strlen($field['menu_item_uuid']) > 0) { $menu_html .= $this->build_child_html($menu_item_level, $menu_field['menu_items']);
$db_menu .= $this->build_child_html($menu_item_level, $field['menu_item_uuid']);
} }
if ($menu_item_level == "main") { if ($menu_item_level == 0) {
$db_menu .= "</li>\n"; $menu_html .= "</li>\n";
$db_menu .= "</ul>\n\n"; $menu_html .= "</ul>\n\n";
} }
$db_menu_full .= $db_menu; $menu_html_full .= $menu_html;
} //end for each } //end for each
unset($prep_statement, $sql, $result); return $menu_html_full;
return $db_menu_full;
} }
//create the sub menus //create the sub menus
function build_child_html($menu_item_level, $menu_item_uuid) { function build_child_html($menu_item_level, $submenu_array) {
$db = $this->db; $db = $this->db;
$menu_item_level = $menu_item_level+1; $menu_item_level = $menu_item_level+1;
@ -363,55 +329,18 @@
$_SESSION['groups'][0]['group_name'] = 'public'; $_SESSION['groups'][0]['group_name'] = 'public';
} }
$sql = "select i.menu_item_link, l.menu_item_title as menu_language_title, i.menu_item_title, i.menu_item_protected, i.menu_item_category, i.menu_item_uuid, i.menu_item_parent_uuid "; if (count($submenu_array) > 0) {
$sql .= "from v_menu_items as i, v_menu_languages as l ";
$sql .= "where i.menu_item_uuid = l.menu_item_uuid ";
$sql .= "and l.menu_language = '".$_SESSION['domain']['language']['code']."' ";
$sql .= "and l.menu_uuid = '".$this->menu_uuid."' ";
$sql .= "and i.menu_uuid = '".$this->menu_uuid."' ";
$sql .= "and i.menu_item_parent_uuid = '$menu_item_uuid' ";
$sql .= "and i.menu_item_uuid in ";
$sql .= "(select menu_item_uuid from v_menu_item_groups where menu_uuid = '".$this->menu_uuid."' ";
$sql .= "and ( ";
if (count($_SESSION['groups']) == 0) {
$sql .= "group_name = 'public' ";
}
else {
$x = 0;
foreach($_SESSION['groups'] as $row) {
if ($x == 0) {
$sql .= "group_name = '".$row['group_name']."' ";
}
else {
$sql .= "or group_name = '".$row['group_name']."' ";
}
$x++;
}
}
$sql .= ") ";
$sql .= ") ";
$sql .= "order by l.menu_item_title, i.menu_item_order asc ";
$prep_statement_2 = $db->prepare($sql);
$prep_statement_2->execute();
$result_2 = $prep_statement_2->fetchAll(PDO::FETCH_NAMED);
if (count($result_2) > 0) {
//child menu found //child menu found
$db_menu_sub = "<ul class='menu_sub'>\n"; $submenu_html = "<ul class='menu_sub'>\n";
foreach($result_2 as $row) { foreach($submenu_array as $submenu_field) {
//set the variables //set the variables
$menu_item_link = $row['menu_item_link']; $menu_item_link = $submenu_field['menu_item_link'];
$menu_item_category = $row['menu_item_category']; $menu_item_category = $submenu_field['menu_item_category'];
$menu_item_uuid = $row['menu_item_uuid']; $menu_items = $submenu_field['menu_items'];
$menu_item_parent_uuid = $row['menu_item_parent_uuid'];
//prepare the protected menus //prepare the protected menus
if ($row['menu_item_protected'] == "true") { $menu_item_title = ($submenu_field['menu_item_protected'] == "true") ? $submenu_field['menu_item_title'] : $submenu_field['menu_language_title'];
$menu_item_title = $row['menu_item_title'];
}
else {
$menu_item_title = $row['menu_language_title'];
}
//prepare the menu_tags according to the category //prepare the menu_tags according to the category
switch ($menu_item_category) { switch ($menu_item_category) {
@ -420,7 +349,7 @@
break; break;
case "external": case "external":
if (substr($menu_item_link, 0,1) == "/") { if (substr($menu_item_link, 0,1) == "/") {
$menu_item_link = PROJECT_PATH . $menu_item_link; $menu_item_link = PROJECT_PATH.$menu_item_link;
} }
$menu_tags = "href='".$menu_item_link."' target='_blank'"; $menu_tags = "href='".$menu_item_link."' target='_blank'";
break; break;
@ -429,35 +358,35 @@
break; break;
} }
$db_menu_sub .= "<li>"; $submenu_html .= "<li>";
//get sub menu for children //get sub menu for children
if (strlen($menu_item_uuid) > 0) { if (is_array($menu_items) && count($menu_items) > 0) {
$str_child_menu = $this->build_child_html($menu_item_level, $menu_item_uuid); $str_child_menu = $this->build_child_html($menu_item_level, $menu_items);
} }
if (strlen($str_child_menu) > 1) { if (strlen($str_child_menu) > 1) {
$db_menu_sub .= "<a ".$menu_tags.">".$menu_item_title."</a>"; $submenu_html .= "<a ".$menu_tags.">".$menu_item_title."</a>";
$db_menu_sub .= $str_child_menu; $submenu_html .= $str_child_menu;
unset($str_child_menu); unset($str_child_menu);
} }
else { else {
$db_menu_sub .= "<a ".$menu_tags.">".$menu_item_title."</a>"; $submenu_html .= "<a ".$menu_tags.">".$menu_item_title."</a>";
} }
$db_menu_sub .= "</li>\n"; $submenu_html .= "</li>\n";
} }
unset($sql, $result_2); unset($submenu_array);
$db_menu_sub .="</ul>\n";
return $db_menu_sub; $submenu_html .="</ul>\n";
return $submenu_html;
} }
unset($prep_statement_2, $sql);
} }
//create the menu array //create the menu array
function menu_array($sql, $menu_item_level) { function menu_array($sql, $menu_item_level) {
$db = $this->db; $db = $this->db;
$db_menu_full = '';
if (!isset($_SESSION['groups'])) { if (!isset($_SESSION['groups'])) {
$_SESSION['groups'][0]['group_name'] = 'public'; $_SESSION['groups'][0]['group_name'] = 'public';
@ -580,7 +509,7 @@
//get sub menu for children //get sub menu for children
if (strlen($menu_item_uuid) > 0) { if (strlen($menu_item_uuid) > 0) {
$a[$x]['menu_items'] = $this->menu_child_array($menu_item_level, $menu_item_uuid); $a[$x]['menu_items'] = $this->menu_child_array($menu_item_level, $menu_item_uuid);
//$str_child_menu = //$str_child_menu =
} }
//increment the row //increment the row

View File

@ -65,7 +65,7 @@ require_once "resources/require.php";
$menu = new menu; $menu = new menu;
$menu->db = $db; $menu->db = $db;
$menu->menu_uuid = $_SESSION['domain']['menu']['uuid']; $menu->menu_uuid = $_SESSION['domain']['menu']['uuid'];
$v_menu .= $menu->build_html("", "main"); $v_menu .= $menu->build_html();
unset($menu); unset($menu);
$v_menu .= "</div>\n"; $v_menu .= "</div>\n";