<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class nodeOModel extends X {
    public function get_menu(){
//		$sql="		SELECT c.badge_function,c.sort, c.id, c.pid, c.name, c.url,sum(b.admin) as 'admin' ,sum(b.write) as  'write' ,sum(b.read) as 'read',c.icon ";
//		$sql.="		FROM ".DB_PREFIXOA."role_user AS a, ".DB_PREFIXOA."role_node b, ".DB_PREFIXOA."node AS c ";
//		$sql.="		WHERE a.role_id = b.role_id and c.is_del=0 ";
//		$sql.="		AND c.is_del =0 ";
//		$sql.="		AND c.id = b.node_id ";
//		$sql.="		group by c.id";
//		$sql.="		ORDER BY c.sort ";
                $sql="SELECT * FROM `oa_node` WHERE is_del='0' ORDER BY sort asc";
		$rs = parent::$obj->getall($sql);
		return $rs;
	}
            public function get_user_menu($user_id=null){
		if(empty($user_id)){
			$user_id=$this->get_user_id();	
		}
		$sql="		SELECT c.badge_function,c.sort, c.id, c.pid, c.name, c.url,sum(b.admin) as 'admin' ,sum(b.write) as  'write' ,sum(b.read) as 'read',c.icon ";
		$sql.="		FROM ".DB_PREFIXOA."role_user AS a, ".DB_PREFIXOA."role_node b, ".DB_PREFIXOA."node AS c ";
		$sql.="		WHERE a.role_id = b.role_id and c.is_del=0 ";
		$sql.="		AND a.user_id ={$user_id}";
		$sql.="		AND c.is_del =0 ";
		$sql.="		AND c.id = b.node_id ";
		$sql.="		group by c.id";
		$sql.="		ORDER BY c.sort ";
		$rs = parent::$obj->getall($sql);
		return $rs;
	}
        public function get_nodelist($node_list){
         
                $sql="SELECT `id`,`pid`,`name`,`url` FROM `oa_node` WHERE `id` IN ($node_list) ORDER BY sort asc";
          
       //          $where      = " WHERE 1=1 AND id in ($node_list)";
            //     $sql        = "SELECT * FROM ".DB_PREFIXOA."node".$where;
                $array = parent::$obj->getall($sql);
              
		return $array;
        }

        public function getpidlist($id){
                 $sql = "SELECT * FROM ".DB_PREFIXOA."node WHERE pid='".$id."'";
                $array = parent::$obj->getall($sql);
		return $array;
        }

        public function get_top_menu(){
		$user_id=$this->get_user_id();
		$sql="		SELECT distinct c.id, c.pid, c.name, c.url,c.icon";
		$sql.="		FROM ".DB_PREFIXOA."role_user AS a, ".DB_PREFIXOA."role_node b, ".DB_PREFIXOA."node AS c ";
		$sql.="		WHERE a.role_id = b.role_id and c.is_del=0 ";
		$sql.="		AND a.user_id ={$user_id}";
		$sql.="		AND c.is_del =0 ";		
		$sql.="		AND c.id = b.node_id ";
		$sql.="		AND c.pid = 0 ";
	//	$sql.="		ORDER BY c.sort asc";
	//	echo $sql;die;
		$array = parent::$obj->getall($sql);
		return $array;
	}
        public function access_list($user_id=null){		
		if(empty($user_id)){
			$user_id=$this->get_user_id();	
		}
		$sql="		SELECT c.badge_function,c.sort, c.id, c.pid, c.name, c.url,sum(b.admin) as 'admin' ,sum(b.write) as  'write' ,sum(b.read) as 'read',c.icon ";
		$sql.="		FROM ".DB_PREFIXOA."role_user AS a, ".DB_PREFIXOA."role_node b, ".DB_PREFIXOA."node AS c ";
		$sql.="		WHERE a.role_id = b.role_id and c.is_del=0 ";
		$sql.="		AND a.user_id ={$user_id}";
		$sql.="		AND c.is_del =0 ";
		$sql.="		AND c.id = b.node_id ";
		$sql.="		group by c.id";
		$sql.="		ORDER BY c.sort ";
		$rs = parent::$obj->getall($sql);
		return $rs;
	}
        public function get_top_menu_data($id){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."node WHERE id='".$id."'";
                return parent::$obj->fetch_first($sql);
	}
        public function get_user_id($emp_no = null) {
                 $user_id = $_SESSION['USER_AUTH_KEY'];
	         return isset($user_id) ? $user_id : 0;
         }
         public function get_folder_menu() {
		$sql = "SELECT";
		$sql .= "  'badge_count_system_folder'    badge_function,";
		$sql .= "  a.id                           fid,";
		$sql .= "  a.sort,";
		$sql .= "  CONCAT('sfid_',a.id)        AS id,";
		$sql .= "  a.name,";
		$sql .= "  a.controller,";
		$sql .= "  a.sort,";
		$sql .= "  CONCAT('sfid_',a.pid)       AS pid,";
		$sql .= "  CONCAT(a.controller,'/folder?fid=',a.id) AS url";
		$sql .= " FROM oa_system_folder  AS a";
		$sql .= " WHERE is_del = 0";
		$sql .= " ORDER BY a.controller,a.sort asc";
		$list = parent::$obj->getall($sql);

		$data = array();
		foreach ($list as $key => $val) {
			if ($val["pid"] == 'sfid_0') {
				$sub_folder= $val['controller'] . "Folder";
                                $sql="SELECT `id` FROM `oa_node` WHERE `sub_folder` = '$sub_folder' LIMIT 1";
                                $pid=parent::$obj->fetch_first($sql);
				$val["pid"] = $pid['id'];
			}
			$data[$key] = $val;
		}
		return $data;
	}
        public function get_userfolder_menu() {
		$user_id=$this->get_user_id();

		$sql = "SELECT";
		$sql .= "  'badge_count_user_folder'    badge_function,";
		$sql .= "  a.id                           fid,";
		$sql .= "  a.sort,";
		$sql .= "  CONCAT('ufid_',a.id)        AS id,";
		$sql .= "  a.name,";
		$sql .= "  a.controller,";
		$sql .= "  a.sort,";
		$sql .= "  CONCAT('ufid_',a.pid)       AS pid,";
		$sql .= "  CONCAT(a.controller,'/folder?fid=',a.id) AS url";
		$sql .= " FROM oa_user_folder   AS a";
		$sql .= " WHERE is_del = 0 and user_id={$user_id}";
		$sql .= " ORDER BY a.controller,a.sort asc";
		$list = parent::$obj->getall($sql);

		$data = array();
		foreach ($list as $val) {
			if ($val["pid"] == 'ufid_0') {
				$sub_folder= $val['controller'] . "Folder";
                                $sql="SELECT `id` FROM `oa_node` WHERE `sub_folder` = '$sub_folder' LIMIT 1";
                                $pid=parent::$obj->fetch_first($sql);
				$val["pid"] = $pid['id'];
			}
			$data[] = $val;
		}
		return $data;
	}
        
        public function get_nodeid(){
            $sql="SELECT `id` FROM `oa_node` WHERE ( pid=0 ) ORDER BY sort asc LIMIT 1";
            $id = parent::$obj->fetch_first($sql);
            return $id['id'];
        }
        public function get_grouplist(){
            $sql="SELECT `id`,`name` FROM `oa_node` WHERE ( pid=0 ) ORDER BY sort asc";
            $list = parent::$obj->getall($sql);
            return $list;
        }
        public function get_menu_list(){
            $sql='SELECT `id`,`pid`,`name` FROM `oa_node` ORDER BY sort asc';
            $list = parent::$obj->getall($sql);
            return $list;
        }
        public function get_node_list(){
            $sql='SELECT `id`,`name` FROM `oa_node` ORDER BY sort asc';
            $list = parent::$obj->getall($sql);
            return $list;
        }
        
        public function doEdit($id,$arr){
                unset($arr['node_list']);
                unset($arr['id']);
                unset($arr['ajax']);
                unset($arr['opmode']);
                unset($arr['node_name']);           
                $result = parent::$obj->update(DB_PREFIXOA.'node', $arr, 'id='.$id.'');
                return $result;
        }  
        public function doAdd($array) {
              unset($array['ajax']);
             unset($array['opmode']);
              unset($array['node_name']);
           return parent::$obj->insert(DB_PREFIXOA.'node', $array); 
      }
      public function del($id){
        $sql = "DELETE FROM ".DB_PREFIXOA."node WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
        
}
?>
