public function menu_tree($menu_id, $maxdepth = false)
{
static $passed_ids;
static $passed_actives;
static $main_menu_id;
if (!is_array($passed_actives)) {
$passed_actives = array();
}
if (!is_array($passed_ids)) {
$passed_ids = array();
}
$menu_params = '';
if (is_string($menu_id)) {
$menu_params = parse_params($menu_id);
if (is_array($menu_params)) {
extract($menu_params);
}
} elseif (is_array($menu_id)) {
$menu_params = $menu_id;
extract($menu_id);
}
if (is_array($menu_id)) {
$menu_params = $menu_id;
extract($menu_id);
}
$cache_group = 'menus/global';
$function_cache_id = false;
$args = func_get_args();
foreach ($args as $k => $v) {
$function_cache_id = $function_cache_id . serialize($k) . serialize($v);
}
$function_cache_id = __FUNCTION__ . crc32($function_cache_id . site_url());
if (defined('PAGE_ID')) {
$function_cache_id = $function_cache_id . PAGE_ID;
}
if (defined('CATEGORY_ID')) {
$function_cache_id = $function_cache_id . CATEGORY_ID;
}
if (!isset($depth) or $depth == false) {
$depth = 0;
}
$orig_depth = $depth;
$params_o = $menu_params;
if ($orig_depth == 0) {
$cache_content = $this->app->cache_manager->get($function_cache_id, $cache_group);
if (!isset($no_cache) and $cache_content != false) {
// return $cache_content;
}
}
$params = array();
$params['item_parent'] = $menu_id;
$menu_id = intval($menu_id);
$params_order = array();
$params_order['position'] = 'ASC';
$menus = $this->tables['menus'];
$menu_params = array();
$menu_params['parent_id'] = $menu_id;
// $menu_params['id'] = '[neq]'.$menu_id;
$menu_params['table'] = $menus;
$menu_params['order_by'] = 'position ASC';
$q = $this->app->database_manager->get($menu_params);
$has_items = false;
$active_class = '';
$a_class = '';
$auto_populate = false;
if (isset($params_o['auto_populate']) != false) {
$auto_populate = $params_o['auto_populate'];
}
/* if($auto_populate != false){
$auto_populate = trim($auto_populate);
if($auto_populate == 1){
if (isset($params_o['parent_id']) and isset($params_o['auto_populate_item']) and !empty($params_o['auto_populate_item'])) {
$menu_item = $params_o['auto_populate_item'];
if(isset($menu_item['content_id']) and intval($menu_item['content_id']) != 0){
$pt = $params_o;
$pt['parent'] = intval($menu_item['content_id']);
$pt['include_all_content'] = intval($menu_item['content_id']);
// dd($pt);
return $this->app->content_manager->pages_tree($pt);
}
// if(!is_array($q)){
// $q = array();
// }
// if(isset($menu_item['content_id']) and intval($menu_item['content_id']) != 0){
// $more_menu_items = array();
// $sub_menu_items_params = array();
// $sub_menu_items_params['parent'] = $menu_item['content_id'];
// $sub_menu_items_params['no_limit'] = true;
// $content_items = $this->app->content_manager->get($sub_menu_items_params);
// if(!empty($content_items)){
// foreach($content_items as $content_item){
// $a = array();
// $a['title'] = $content_item['title'];
// // $a['item_type'] = 'menu_item';
// $a['content_id'] = $content_item['id'];
// $a['id'] = $params_o['parent_id'].$content_item['id'];
// $a['cccid'] = $menu_item['id'];
// // $a['parent_id'] =$content_item['id'];
// $a['parent_id'] =$params_o['parent_id'];
// $a['parent_id'] = $params_o['parent_id'].$content_item['id'];;
// // $a['parent_id'] =$params_o['parent_id'];
// $a['url'] = $this->app->content_manager->link($content_item['id']);
// $more_menu_items[] =$a;
// // $q[] =$a;
// }
// }
// if(!empty($more_menu_items)){
//
// $q = array_merge($q,$more_menu_items);
// // dd($q);
// }
// //dd($more_menu_items);
// //dd($menu_item);
//
// }
}
}
}
*/
if (empty($q)) {
return false;
}
if (!isset($ul_class)) {
$ul_class = 'menu';
}
if (!isset($li_class)) {
$li_class = 'menu_element';
}
if (isset($ul_class_deep)) {
if ($depth > 0) {
$ul_class = $ul_class_deep;
}
}
if (isset($li_class_deep)) {
if ($depth > 0) {
$li_class = $li_class_deep;
}
}
if (isset($ul_tag) == false) {
$ul_tag = 'ul';
}
if (isset($li_tag) == false) {
$li_tag = 'li';
}
if (isset($params['max_depth']) != false) {
$params['maxdepth'] = $params['max_depth'];
}
if (isset($params['maxdepth']) != false) {
$maxdepth = $params['maxdepth'];
}
if (isset($params['depth']) != false) {
$maxdepth = $params['depth'];
}
if (isset($params_o['a_class']) != false) {
$a_class = $params_o['a_class'];
}
if (isset($params_o['depth']) != false) {
$maxdepth = $params_o['depth'];
}
if (isset($params_o['maxdepth']) != false) {
$maxdepth = $params_o['maxdepth'];
}
if (!isset($link) or $link == false) {
$link = '<a itemprop="url" data-item-id="{id}" class="menu_element_link {active_class} {exteded_classes} {nest_level} {a_class}" href="{url}">{title}</a>';
}
$to_print = '<' . $ul_tag . ' role="menu" class="{ul_class}' . ' menu_' . $menu_id . ' {exteded_classes}" >';
$cur_depth = 0;
$res_count = 0;
foreach ($q as $item) {
$full_item = $item;
$title = '';
$url = '';
$is_active = true;
if (intval($item['content_id']) > 0) {
$cont = $this->app->content_manager->get_by_id($item['content_id']);
if (is_array($cont) and isset($cont['is_deleted']) and $cont['is_deleted'] == 1) {
$is_active = false;
$cont = false;
}
if (is_array($cont)) {
$title = $cont['title'];
$url = $this->app->content_manager->link($cont['id']);
if ($cont['is_active'] != 1) {
$is_active = false;
$cont = false;
}
}
} elseif (intval($item['categories_id']) > 0) {
$cont = $this->app->category_manager->get_by_id($item['categories_id']);
if (is_array($cont)) {
$title = $cont['title'];
$url = $this->app->category_manager->link($cont['id']);
} else {
$this->app->database_manager->delete_by_id($menus, $item['id']);
$title = false;
$item['title'] = false;
}
} else {
$title = $item['title'];
$url = $item['url'];
}
if (trim($item['url'] != '')) {
$url = $item['url'];
}
if ($item['title'] == '') {
$item['title'] = $title;
} else {
$title = $item['title'];
}
$active_class = '';
$site_url = $this->app->url_manager->site();
$cur_url = $this->app->url_manager->current(1);
if (trim($item['url'] != '')) {
$item['url'] = $this->app->format->replace_once('{SITE_URL}', $site_url, $item['url']);
}
if (trim($item['url'] != '') and intval($item['content_id']) == 0 and intval($item['categories_id']) == 0) {
if ($item['url'] == $cur_url) {
$active_class = 'active';
} else {
$active_class = '';
}
} elseif (trim($item['url'] == '') and defined('CONTENT_ID') and CONTENT_ID != 0 and $item['content_id'] == CONTENT_ID) {
$active_class = 'active';
} elseif (trim($item['url'] == '') and defined('PAGE_ID') and PAGE_ID != 0 and $item['content_id'] == PAGE_ID) {
$active_class = 'active';
} elseif (trim($item['url'] == '') and defined('POST_ID') and POST_ID != 0 and $item['content_id'] == POST_ID) {
$active_class = 'active';
} elseif (trim($item['url'] == '') and defined('CATEGORY_ID') and CATEGORY_ID != false and intval($item['categories_id']) != 0 and $item['categories_id'] == CATEGORY_ID) {
$active_class = 'active';
} elseif (isset($cont['parent']) and defined('PAGE_ID') and PAGE_ID != 0 and $cont['parent'] == PAGE_ID) {
// $active_class = 'active';
} elseif (trim($item['url'] == '') and isset($cont['parent']) and defined('MAIN_PAGE_ID') and MAIN_PAGE_ID != 0 and $item['content_id'] == MAIN_PAGE_ID) {
$active_class = 'active';
$active_class = 'active-parent';
} elseif (trim($item['url'] != '') and $item['url'] == $cur_url) {
$active_class = 'active';
$active_class = 'active-parent';
} elseif (trim($item['url'] != '') and $item['content_id'] != 0 and defined('PAGE_ID') and PAGE_ID != 0) {
$cont_link = $this->app->content_manager->link(PAGE_ID);
if ($item['content_id'] == PAGE_ID and $cont_link == $item['url']) {
$active_class = 'active';
} elseif ($cont_link == $item['url']) {
$active_class = 'active';
}
} else {
$active_class = '';
}
if ($is_active == false) {
$title = '';
}
if (isset($item['id'])) {
if ($active_class == 'active') {
$passed_actives[] = $item['id'];
} elseif ($active_class == '') {
if (isset($cont['content_id'])) {
if (in_array($item['content_id'], $passed_actives)) {
$active_class = 'active';
}
}
}
}
if (isset($item['title']) and $item['title'] != false) {
$title = $item['title'] = strip_tags(html_entity_decode($item['title']));
}
if ($title != '') {
$has_items = true;
$item['url'] = $url;
$to_print .= '<' . $li_tag . ' class="{li_class}' . ' ' . $active_class . ' {nest_level}" data-item-id="' . $item['id'] . '" >';
$ext_classes = '';
if (isset($item['parent']) and intval($item['parent']) > 0) {
$ext_classes .= ' have-parent';
}
if (isset($item['subtype_value']) and intval($item['subtype_value']) != 0) {
$ext_classes .= ' have-category';
}
$ext_classes = trim($ext_classes);
$menu_link = $link;
foreach ($item as $key => $value) {
$menu_link = str_replace('{' . $key . '}', $value, $menu_link);
}
$menu_link = str_replace('{active_class}', $active_class, $menu_link);
$menu_link = str_replace('{a_class}', $a_class, $menu_link);
$to_print .= $menu_link;
$ext_classes = '';
// if ($orig_depth > 0) {
if ($main_menu_id == false) {
$main_menu_id = $menu_id;
$ext_classes .= ' menu-root';
} else {
if ($res_count == 0) {
// if($main_menu_id == false){
$ext_classes .= ' first-child';
$ext_classes .= ' child-' . $res_count . '';
// }
} elseif (!isset($q[$res_count + 1])) {
$ext_classes .= ' last-child';
$ext_classes .= ' child-' . $res_count . '';
} else {
$ext_classes .= ' child-' . $res_count . '';
}
}
$ext_classes .= ' menu-item-id-' . $item['id'] . '';
if (isset($item['parent_id'])) {
$ext_classes .= ' menu-item-parent-' . $item['parent_id'] . '';
}
// }
if (in_array($item['parent_id'], $passed_ids) == false) {
if ($maxdepth == false) {
if (isset($params) and is_array($params)) {
// if (isset($item['auto_populate']) and $item['auto_populate'] !=false) {
// $menu_item = $item;
////dd($menu_item);
// if(isset($menu_item['content_id']) and intval($menu_item['content_id']) != 0){
// $pt = $params_o;
// $pt['parent'] = intval($menu_item['content_id']);
// $pt['include_all_content'] = intval($menu_item['content_id']);
//
// $to_print .= $this->app->content_manager->pages_tree($pt);
//
// //$to_print .= strval($test1);
//
// }
//
//
//
// } else {
$menu_params['menu_id'] = $item['id'];
$menu_params['link'] = $link;
if (isset($menu_params['item_parent'])) {
unset($menu_params['item_parent']);
}
if (isset($ul_class)) {
$menu_params['ul_class'] = $ul_class;
}
if (isset($li_class)) {
$menu_params['li_class'] = $li_class;
}
if (isset($maxdepth)) {
$menu_params['maxdepth'] = $maxdepth;
}
if (isset($li_tag)) {
$menu_params['li_tag'] = $li_tag;
}
if (isset($ul_tag)) {
$menu_params['ul_tag'] = $ul_tag;
}
if (isset($ul_class_deep)) {
$menu_params['ul_class_deep'] = $ul_class_deep;
}
if (isset($li_class_empty)) {
$menu_params['li_class_empty'] = $li_class_empty;
}
if (isset($li_class_deep)) {
$menu_params['li_class_deep'] = $li_class_deep;
}
if (isset($depth)) {
$menu_params['depth'] = $depth + 1;
}
$test1 = $this->menu_tree($menu_params);
// }
} else {
$test1 = $this->menu_tree($item['id']);
}
} else {
if ($maxdepth != false and intval($maxdepth) > 1 and $cur_depth <= $maxdepth) {
if (isset($params) and is_array($params)) {
$test1 = $this->menu_tree($menu_params);
} else {
$test1 = $this->menu_tree($item['id']);
}
}
}
}
if (isset($li_class_empty) and isset($test1) and trim($test1) == '') {
if ($depth > 0) {
$li_class = $li_class_empty;
}
}
$to_print = str_replace('{a_class}', $a_class, $to_print);
$to_print = str_replace('{ul_class}', $ul_class, $to_print);
$to_print = str_replace('{li_class}', $li_class, $to_print);
$to_print = str_replace('{exteded_classes}', $ext_classes, $to_print);
$to_print = str_replace('{nest_level}', 'depth-' . $depth, $to_print);
if (isset($test1) and strval($test1) != '') {
$to_print .= strval($test1);
++$res_count;
}
$to_print .= '</' . $li_tag . '>';
}
++$cur_depth;
}
$to_print .= '</' . $ul_tag . '>';
if ($orig_depth == 0) {
// $this->app->cache_manager->save($to_print, $function_cache_id, $cache_group);
}
if ($has_items) {
return $to_print;
} else {
return false;
}
}