<?php
/**
*管理员类
*孙新乐，2013/12/23
*
*/
class adminUser{
    
    protected $id;
    protected $db;
    protected $table = "ls_admin";
    protected $pagesize=15;
    public $keywords;
    public $error;
	
	//构造函数，给用户信息赋值
    function __construct($user){
        require_once("conn.php");
        $this->db = new mydb();
        $this->db->query("set names utf8");

        $sql = "select * from ".$this->table." where adminName='".$user."'";
        $row = $this->db->OneRow($sql);
        $this->id = $row['ID'];
    }

	//获得所有表的信息
    public function getAllBranch($table){
        $sql = "select * from ".$table;
        return $this->db->AllRows($sql);
    }
    
    //获得用户的基本信息
    public function getInfo($id=''){
        if($id==''){
            $id = $this->id;
        }
        $sql = "select * from ".$this->table." where ID='".$id."'";
        $row = $this->db->OneRow($sql);
        if($row['branchID']!=0){
            $sql = "select branchName from ls_branch where ID=".$row['branchID'];
            $row1 = $this->db->OneRow($sql);
        }
        if($row['roleID']!=0){
            $sql = "select roleName from ls_role where ID=".$row['roleID'];
            $row2 = $this->db->OneRow($sql);
        }
        if(is_array($row1)){
            $row = array_merge($row,$row1);
        }
        if(is_array($row2)){
            $row = array_merge($row,$row2);
        }
        return $row;
    }

    //获取用户的权限  return数组
    public function getRole(){
        $list = $this->getInfo();
        $sql = "select * from ls_role where ID=".$list['roleID'];
        $nums = $this->db->OneRow($sql);
        $roles = explode(",",$nums['rightID']);
        return $roles;
    }

    //获得admin表的所有字段
    public function getZi(){
        $data=array();
        $sql = "show fields from ".$this->table;//最主要是这SQL得到全表信息
        $query = $this->db->query($sql);
        while($row=mysql_fetch_array($query)){
        $data[]=$row['Field'];
        //print_r($row);
        }
        return $data;
    }
	
	
    public  function getAll($page=''){
        if($page==''){$page=1;}
        $row = array();

        if($this->keywords!=''){
            $sql_str = " where userName like '%".$this->keywords."%' ";
        }

        $sql = "select ID from ".$this->table." ".$sql_str ;
        $rs = $this->db->AllRows($sql);
        $this->allpage =  count($rs);
        $pagesize = $this->pagesize;
        $n = $pagesize*($page-1);
        $sql = "select * from ls_admin ".$sql_str."limit $n,$pagesize";
        $rs = $this->db->AllRows($sql);
        foreach($rs as $v){
            $row[] = $this->getInfo($v['ID']);
        }
        return $row;

    }

	//获得分页字符串
    public function getPageStr($page=''){
        if($page<1){$page=1;}
        
        if($this->allpage <= $this->pagesize){
            $page_count = 1;
        }else if($this->allpage%$this->pagesize == 0){
            $page_count = $this->allpage/$this->pagesize;
        }else{
            $page_count = $this->allpage/$this->pagesize +1;
        }

        $page_count = (int)$page_count;
        // 翻页链接
        $page_string = '';
            
        if( $page == 1 ){
           $page_string .= '首页 | 上一页 |';
        }
        else{
           $page_string .= '<a href="?keywords='.$this->keywords.'&page=1"><u><font color=\"#0000FF\">首页</font></u></a> | <a href=?keywords='.$this->keywords.'&page='.($page-1).'><u><font color=\"#0000FF\">上一页</font></u></a> | ';
        }
        if( ($page == $page_count) || ($page_count == 0) ){
           $page_string .= ' 下一页 | 尾页';
        }
        else{
           $page_string .= ' <a href=?keywords='.$this->keywords.'&page='.($page+1).'><u><font color=\"#0000FF\">下一页</font></u></a> | <a href=?keywords='.$this->keywords.'&page='.$page_count.'><u><font color=\"#0000FF\">尾页</font></u></a>';
        }
             $page_string .= " &nbsp;&nbsp;页次：<font color='#FF0000'><b>".$page."</b></font>/<b>".$page_count."</b>页";
             $page_string .= " &nbsp;&nbsp;<b>".$this->pagesize."</b>个每页";

             echo $page_string;
    }



    /*
ID	
adminName 用户名	
adminPwd	
adminTime 注册时间
userName  姓名
tel       电话
email     邮箱
fax
skype   
memo
branchID  所属部门	
role    角色ID
Num
Enable
*/
    public function addAdmin($data){
        
        if(!$this->checkAdmin($data['adminName'])){
            $this->error = "用户名重复";
            return false;
        }

        $key= " ";
        $list = " ";
        $zi = $this->getZi();
        foreach($data as $k=>$v){
            $v = addslashes($v);
            for($i=0,$j=count($zi);$i<$j;$i++){
                if($zi[$i]==$k){
                    $key .=$zi[$i].",";
                    $list .= "'$v',";
                }
            }
        }
        //$key .= "adminTime";
        //$list .= "'".date("Y-m-d")."'";
        $key = substr($key,0,strlen($key)-1);
        $list = substr($list,0,strlen($list)-1);
        $sql = "insert into ls_admin ($key) values ($list)";
        return $this->db->query($sql);
    }

    public function  modiAdmin($id,$data){
        
        $list = " ";
        $zi = $this->getZi("ls_admin");
        foreach($data as $k=>$v){
            $v = addslashes($v);
            for($i=0,$j=count($zi);$i<$j;$i++){
                if($zi[$i] == $k){
                    $list .= $k . "="."'$v',";
                }
            }
        }
        $list = substr($list,0,strlen($list)-1);
        $sql = "update ".$this->table." set $list where ID=$id";
        if($this->db->query($sql)){
            return true;
        }else{
            return false;
        }
    }

    //检查用户名是否存在
    public function checkAdmin($user){
        $sql = "select * from ".$this->table." where adminName='$user'";
        $rs = $this->db->query($sql);
        if(mysql_num_rows($rs)==0){
            return true;
        }else{
            return false;
        }
    }

}

$admin = new adminUser($_SESSION['username']);
//$arr = array(adminName=>"admin",adminPwd=>"ls2120",adminTime=>"1244521",userName=>"xiaoli",tel=>"152032890455",email=>"jjdj@123.com");
//print_r($admin->getAll());

