<?php
defined('IN_YUNYECMSAdmin') or exit('No permission.');
core::load_admin_class('admin');
class menu extends YUNYE_admin
{
	private $lang;  //当前根栏目
	function __construct()
	{
		parent::__construct();
		$this->lang = $this->deflang;
	}
	//加载首页
	public function init()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "category") . '\" target=\"maincontent\">内容</a></li><li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', 'category') . '\" target=\"maincontent\">栏目管理</a></li><li class=\"breadcrumb-item active\">栏目列表</li>';
		if (!getroot('users', 'logs')) {
			messagebox(Lan('no_permission'), 'back', "warn");
		}
		$pagesize = 20;
		$sqlquery = "select * from `#yunyecms_category`  ";
		$where = " where 1=1 and pid=0 ";
		$sqlcnt = " select count(*) from `#yunyecms_category` ";
		$order = " order by `addtime` desc ";
		if (isset($_REQUEST)) {
			if (!empty($_REQUEST["searchkey"])) {
				$searchkey = usafestr(trim($_REQUEST["searchkey"]));
				$where = $where . " and ( `title`  like '%{$searchkey}%'  )";
			}
		}
		$pagearr = $this->db->pagelist($sqlcnt, $sqlquery, $where, $order, $pagesize);
		if ($pagearr["count"] != 0) {
			$list = $pagearr["query"];
			$pages = $pagearr["page"];
		}
		require tpl_adm('category_list');
	}

	public function getmenu()
	{
		$menuvar = 'var menu_yyinfo,menu_yymodel,menu_yymember,menu_yyapps;';
		$menu_slider = 'menu_yymodel="";
	   menu_yymodel+="<li class=\"nav-item menu-open\">\n";
	   menu_yymodel+="<a  class=\"nav-link active\" href=\"#\">\n";
	   menu_yymodel+="<i class=\"fa fa-cog\"></i> <p>系统\n";
	   menu_yymodel+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yymodel+="</p></a>\n";
	   menu_yymodel+="<ul class=\"nav nav-treeview\">\n";
	   menu_yymodel+="<li class=\"nav-item\"><a href=\"' . url_admin('save', 'config') . '\" class=\"nav-link active\" target=\"maincontent\"><i class=\"fa fa-gear\"></i><p> 系统设置</p></a></li>\n";
	   menu_yymodel+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'homeset') . '\"  class=\"nav-link\" target=\"maincontent\"><i class=\"fa fa-home\"></i> <p>首页设置</p></a></li>\n";
	   menu_yymodel+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'msg') . '\"  class=\"nav-link\"  target=\"maincontent\"><i class=\"fa fa-commenting\"></i> <p>系统消息</p></a></li>\n";
	   menu_yymodel+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'autoupdate') . '\"  class=\"nav-link\"  target=\"maincontent\"><i class=\"fa fa-refresh\"></i> <p>自动更新</p></a></li>\n";
	   menu_yymodel+="</ul>\n";
	   menu_yymodel+="</li>\n";	
	   menu_yymodel+="<li class=\"nav-item\">\n";
	   menu_yymodel+="<a  class=\"nav-link\" href=\"#\">\n";
	   menu_yymodel+="<i class=\"fa fa-tv\"></i> <p>模板管理\n";
	   menu_yymodel+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yymodel+="</p></a>\n";
	   menu_yymodel+="<ul class=\"nav nav-treeview\">\n";
	   menu_yymodel+="<li class=\"nav-item\"><a href=\"' . url_admin('init', 'template') . '\"   class=\"nav-link\"  target=\"maincontent\"><i class=\"fa fa-list\"></i> <p>模板选择</p></a></li>\n";
	   menu_yymodel+="</ul>\n";
	   menu_yymodel+="</li>\n";
	   menu_yymodel+="<li class=\"nav-item\">\n";
	   menu_yymodel+="<a href=\"#\"  class=\"nav-link\">\n";
	   menu_yymodel+="<i class=\"fa fa-cubes\"></i> <p>模型管理\n";
	   menu_yymodel+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yymodel+="</p></a>\n";
	   menu_yymodel+="<ul class=\"nav nav-treeview\">\n";
	   menu_yymodel+="<li class=\"nav-item\"><a href=\"' . url_admin('init', 'yunyecmsmodel') . '\"   class=\"nav-link\"  target=\"maincontent\"><i class=\"fa fa-list\"></i> <p>模型管理</p></a></li>\n";
	   menu_yymodel+="<li><a href=\"' . url_admin('model_add', 'yunyecmsmodel') . '\"   class=\"nav-link\"  target=\"maincontent\"><i class=\"fa fa-plus\"></i> <p>添加模型</p></a></li>\n";
	   menu_yymodel+="</ul>\n";
	   menu_yymodel+="</li>\n";
       menu_yymodel+="<li class=\"nav-item\">\n";
	   menu_yymodel+="<a href=\"#\"  class=\"nav-link\">\n";
	   menu_yymodel+="<i class=\"fa fa-globe\"></i> <p>语言版管理\n";
	   menu_yymodel+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yymodel+="</p></a>\n";
	   menu_yymodel+="<ul class=\"nav nav-treeview\">\n";
	   menu_yymodel+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'lang') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"fa fa-list\"></i> <p>语言版管理</p></a></li>\n";
	   menu_yymodel+="<li  class=\"nav-item\"><a href=\"' . url_admin('add', 'lang') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"fa fa-plus\"></i> <p>添加语言版</p></a></li>\n";
	   menu_yymodel+="</ul>\n";
	   menu_yymodel+="</li>\n";	   
	   menu_yymember="";
	   menu_yymember+="<li class=\"nav-item  menu-open\">\n";
	   menu_yymember+="<a href=\"#\"  class=\"nav-link\">\n";
	   menu_yymember+="<i class=\"fa fa-users\"></i> <p>管理员管理\n";
	   menu_yymember+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yymember+="</p></a>\n";
	   menu_yymember+="<ul class=\"nav nav-treeview\">\n";
	   menu_yymember+="<li class=\"nav-item\"><a href=\"' . url_admin('user_pwd', 'user', array('userid' => $this->admuser['userid'])) . '\"  class=\"nav-link\"  target=\"maincontent\"><i class=\"far fa-circle nav-icon\"></i> <p>修改密码</p></a></li>\n";
	   menu_yymember+="<li class=\"nav-item\"><a href=\"' . url_admin('init', 'user') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"far fa-circle nav-icon\"></i>  <p>管理员管理</p></a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'role') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"far fa-circle nav-icon\"></i>  <p>权限管理</p></a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'department') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"far fa-circle nav-icon\"></i>  <p>部门管理</p></a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'loginlog') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"far fa-circle nav-icon\"></i>  <p>登录日志</p></a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'adminlogs') . '\" target=\"maincontent\"  class=\"nav-link\" ><i class=\"far fa-circle nav-icon\"></i>  <p>操作日志</p></a></li>\n";
	   menu_yymember+="</ul>\n";
	   menu_yymember+="</li>\n";
	   menu_yymember+="<li class=\"nav-item\">\n";
	   menu_yymember+="<a href=\"#\"  class=\"nav-link\">\n";
	   menu_yymember+="<i class=\"fa fa-user-circle-o\"></i> <p>会员管理\n";
	   menu_yymember+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yymember+="</p></a>\n";
	   menu_yymember+="<ul class=\"nav nav-treeview\">\n";	
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'member') . '\"  target=\"maincontent\"   class=\"nav-link\"><i class=\"far fa-circle nav-icon\"></i> 会员管理</a></li>\n";	
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'orders') . '\" target=\"maincontent\"   class=\"nav-link\"><i class=\"fa fa-list\"></i> 订单管理 </a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'membergroup') . '\"  target=\"maincontent\"   class=\"nav-link\"><i class=\"far fa-circle nav-icon\"></i> 会员组</a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'membergroup_goods') . '\"  target=\"maincontent\"   class=\"nav-link\"><i class=\"far fa-circle nav-icon\"></i> 会员升级产品</a></li>\n";	
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'membergroup_orders') . '\"  target=\"maincontent\"   class=\"nav-link\"><i class=\"far fa-circle nav-icon\"></i> 会员升级订单</a></li>\n";	
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'comment') . '\" target=\"maincontent\"   class=\"nav-link\"><i class=\"far fa-circle nav-icon\"></i> 评论管理</a></li>\n";
	   menu_yymember+="<li  class=\"nav-item\"><a href=\"' . url_admin('init', 'notice') . '\"  target=\"maincontent\"   class=\"nav-link\"><i class=\"far fa-circle nav-icon\"></i> 通知消息</a></li>\n";
	   menu_yymember+="</ul>\n";
	   menu_yymember+="</li>\n";
	   ';
		$menu_info = $this->getcatmenu();
		$menu_addons = $this->get_addons_menu();
		$menu_slider = $menuvar . $menu_info . $menu_slider . $menu_addons;
		exit($menu_slider);
	}

	private function getcatmenu()
	{
		if ($this->lang) {
			$menuleft = $this->db->select("select `id`,`title`,`modelid`,`tplhome` from `#yunyecms_category` where `pid`= 0 and lang={$this->lang} order by ordernum asc");
		} else {
			$menuleft = $this->db->select("select `id`,`title`,`modelid`,`tplhome` from `#yunyecms_category` where `pid`= 0 order by ordernum asc");
		}
		$menu_info = '
	   menu_yyinfo="";
		 menu_yyinfo+="<li class=\"nav-item menu-is-opening menu-open \">\n";
         menu_yyinfo+="<a href=\"#\"  class=\"nav-link active\">\n";
             menu_yyinfo+="<i class=\"fa fa-info\"></i>\n";
             menu_yyinfo+="<p>内容管理\n";
               menu_yyinfo+="<i class=\"right fas fa-angle-left\"></i>\n";
             menu_yyinfo+="</p>\n";
           menu_yyinfo+="</a>\n";
           menu_yyinfo+="<ul class=\"nav nav-treeview\" style=\"display: block;\">\n";
            ';
		$loopmenu = "";
		foreach ($menuleft as $key => $val) {
			if (is_array($val)) {
				$menuid = $val['id'];
				$catwo = $this->db->select("select `id`,`title`,`modelid`,`tplhome` from `#yunyecms_category` where `pid`= {$menuid} order by ordernum asc");
				$menuleft[$key]['catlist'] = $catwo;
				if (!empty($catwo)) {
					$link_one = "#";
				} else {
					$link_one = url_admin(getmodeltype($val["modelid"]), 'content', array("catid" => $val["id"]));
				}
				$loopmenu = $loopmenu . '
			  menu_yyinfo+="<li  class=\"nav-item\">\n";
              menu_yyinfo+="<a href=\"' . $link_one . '\" class=\"nav-link\" target=\"maincontent\"><i class=\"fas fa-circle nav-icon\"></i> <p> ' . $val['title'] . '\n";';
				if (!empty($catwo)) {
					$loopmenu = $loopmenu . '	
				    menu_yyinfo+="<i class=\"right fas fa-angle-left\"></i>\n";';
				}
				$loopmenu = $loopmenu . '
                menu_yyinfo+="</p>\n";
                menu_yyinfo+="</a>\n";';
				if ($catwo) {
					$loopmenu = $loopmenu . '  
              menu_yyinfo+="<ul class=\"nav nav-treeview\">\n";';
					foreach ($menuleft[$key]['catlist'] as $k => $v) {
						$curmid = $v["modelid"];
						if (is_array($v)) {
							$cathree = $this->db->select("select `id`,`title`,`modelid`,`tplhome` from `#yunyecms_category` where `pid`= {$v["id"]} order by ordernum asc");
							$menuleft[$key]['catlist'][$k]['sublist'] = $cathree;
							if (!empty($cathree)) {
								$link_two = "#";
								$link_two_click = '';
							} else {
								$link_two = url_admin(getmodeltype($v["modelid"]), 'content', array("catid" => $v["id"]));
								$link_two_click = 'onClick=\"leftsidebar(this);\"';
							}
							$loopmenu = $loopmenu . '
						  menu_yyinfo+="<li class=\"nav-item\">\n";
						  menu_yyinfo+="<a href=\"' . $link_two . '\"  class=\"nav-link\" target=\"maincontent\" ' . $link_two_click . '><i class=\"far fa-circle nav-icon\"></i><p>' . $v['title'] . '\n";';
							if (!empty($cathree)) {
								$loopmenu = $loopmenu .
									'menu_yyinfo+=" <i class=\"right fas fa-angle-left\"></i>\n";';
							}
							$loopmenu = $loopmenu . ' menu_yyinfo+=" </p></a>\n";';
							if ($cathree) {
								$loopmenu = $loopmenu . '  menu_yyinfo+="<ul class=\"nav nav-treeview\">\n";';
								foreach ($cathree as $ks => $vs) {
									$cursmid = $vs["modelid"];
									$link_three = url_admin(getmodeltype($vs["modelid"]), 'content', array("catid" => $vs["id"]));
									$loopmenu = $loopmenu . 'menu_yyinfo+="<li  class=\"nav-item\"><a href=\"' . $link_three . '\" class=\"nav-link\"  onClick=\"leftsidebar(this);\"  target=\"maincontent\"><i class=\"far fa-dot-circle nav-icon\"></i><p>' . $vs["title"] . '</p></a></li>\n";';
								}
								$loopmenu = $loopmenu . 'menu_yyinfo+="</ul>\n";';
							}
							$loopmenu = $loopmenu . 'menu_yyinfo+="</li>\n";';
						}
					}
					$loopmenu = $loopmenu . 'menu_yyinfo+="</ul>\n";';
				}
				$loopmenu = $loopmenu . 'menu_yyinfo+="</li>\n";';
			}
		}
		$menu_info = $menu_info . $loopmenu . 'menu_yyinfo+="</ul></li>\n";';
		$menu_column = '
	   menu_yyinfo+="<li class=\"nav-item active\">\n";
	   menu_yyinfo+="<a href=\"#\" class=\"nav-link\">\n";
	   menu_yyinfo+="<i class=\"fa fa-columns\"></i> <span>栏目管理</span>\n";
	   menu_yyinfo+="<p>\n";
	   menu_yyinfo+="<i class=\"fas fa-angle-left right\"></i>\n";
	   menu_yyinfo+="</p></a>\n";
	   menu_yyinfo+="<ul class=\"nav nav-treeview\">\n";
	   menu_yyinfo+="<li class=\"nav-item active\"><a href=\"' . url_admin('init', 'category') . '\" class=\"nav-link\" target=\"maincontent\"><i class=\"far fa-circle nav-icon\"></i> <p>栏目管理</p></a></li>\n";
	   menu_yyinfo+="<li class=\"nav-item\"><a href=\"' . url_admin('add', 'category') . '\" class=\"nav-link\" target=\"maincontent\"><i class=\"far fa-circle nav-icon\"></i> <p>添加栏目</p></a></li>\n";
	   menu_yyinfo+="</ul>\n";
	   menu_yyinfo+="</li>\n";';
		$retmenuinfo = $menu_info . $menu_column;;
		return $retmenuinfo;
	}

	private function get_addons_menu()
	{
		$menuleft = $this->db->select("select * from `#yunyecms_addons` where `status`= 1  order by ordernum asc");
		$menu_info = 'menu_yyapps="";
	   menu_yyapps+="<li class=\"nav-item\"><a href=\"' . url_admin('init', 'addons') . '\"  target=\"maincontent\" class=\"nav-link\"><i class=\"fa fa-th-large text-aqua\"></i> <p>全部应用</p></a></li>\n";
	   menu_yyapps+="<li  class=\"nav-item\"><a href=\"' . url_admin('addons_list', 'addons') . '\"  target=\"maincontent\" class=\"nav-link\"><i class=\"fa  fa-th-list text-orange\"></i> <p>已安装的应用</p></a></li>\n";
            ';
		if ($menuleft) {
			$menu_info = $menu_info . ' menu_yyapps+="<li class=\"nav-item menu-open\">\n";
	   menu_yyapps+="<a href=\"#\"  class=\"nav-link\">\n";
	   menu_yyapps+="<i class=\"fa   fa-th\"></i> <p>应用列表\n";
	   menu_yyapps+="<i class=\"right fas fa-angle-left\"></i>\n";
	   menu_yyapps+="</p></a>\n";
	   menu_yyapps+="<ul class=\"nav nav-treeview\">\n";
            ';
		}
		$loopmenu = "";
		foreach ($menuleft as $key => $val) {
			if (is_array($val)) {
				$link_one = url_addons('addons/' . $val['addons_dir'] . '/admin/index', array("id" => $val["id"]));
				$loopmenu = $loopmenu . '
			   menu_yyapps+="<li><a href=\"' . $link_one . '\" target=\"maincontent\"><img width=\"25\" src=\"' . $val['icon'] . '\">  ' . $val['title'] . ' </a></li>\n";
              ';
			}
		}
		$menu_info = $menu_info . $loopmenu . 'menu_yyapps+="</ul></li>\n";
	   ';
		return $menu_info;
	}

	public function getmenuout()
	{
		$menuvar = 'var menu_yyinfo;
		';
		$menu_info = $this->getcatmenu();
		exit($menuvar . $menu_info);
	}

	public function getmenu_addous_out()
	{
		$menuvar = 'var menu_yyapps;
		';
		$menu_info = $this->get_addons_menu();
		exit($menuvar . $menu_info);
	}
}
