<?php

class Infoauthors_model extends CI_Model
{

	var $top_num=false;
	var $a_active=false;
	var $a_id=false;
	var $a_email=false;
	var $order_by=false;
	
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('INFORMATION', TRUE);
    }
	
	function init()
	{
		$this->top_num=false;
		$this->a_active=false;
		$this->a_id=false;
		$this->a_email=false;
		$this->order_by=" ORDER BY ia.a_name ASC ";
	}

    //输入用户名和密码，检查是否可以正确
    function check_login($email, $password)
    {
		$sql = "SELECT TOP 1 1  \n"
			 . "FROM   infoAuthors ia \n"
			 . "WHERE  ia.a_email = ? \n"
			 . "       AND ia.a_password = ?"
			 . "       AND ia.a_active = 1 ";
        $query = $this->HT->query($sql, array($email, $password));
        //print_r($this->HT->queries);
        if ($query->num_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
	
	//已激活作者列表
	function active_list()
	{
		$this->init();
		$this->a_active=" AND ia.a_active = 1 ";
		$this->order_by=" ORDER BY ia.a_datetime DESC ";
		return $this->get_list();
	}
	
	//待激活作者列表
	function un_active_list()
	{
		$this->init();
		$this->a_active=" AND ia.a_active = 0 ";
		$this->order_by=" ORDER BY ia.a_datetime DESC ";
		return $this->get_list();
	}

	//获取未审核作者数量
	function un_active_count(){
		$sql="SELECT TOP 1 count(0) as a_count FROM infoAuthors WHERE a_active = 0";
		$query=$this->HT->query($sql);
		$result=$query->result();
		$num=0;
		if (!empty($result)) {
			$num=$result[0]->a_count;
		}
		return $num;
	}

    //获取登录用户详细信息
    function detail($email)
    {
		$this->init();
		$this->top_num=1;
		$this->a_active=" AND ia.a_active <> 2 ";
		$this->a_email=" AND ia.a_email=".$this->HT->escape($email);
        return $this->get_list();
    }

    //获取登录用户详细信息
    function detail_by_id($a_id)
    {
		$this->init();
		$this->top_num=1;
		$this->a_active=" AND ia.a_active <> 2 ";
		$this->a_id=" AND ia.a_id=".$this->HT->escape($a_id);
        return $this->get_list();
    }
		
	
    //获取用户列表
    function get_list()
    {
		$this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
		$sql .= "      ia.a_id, \n"
			 . "       ia.a_email, \n"
			 . "       ia.a_password, \n"
			 . "       ia.a_name, \n"
			 . "       ia.a_name_cn, \n"
			 . "       ia.a_mobile_phone, \n"
			 . "       ia.a_phone, \n"
			 . "       ia.a_photo, \n"
			 . "       ia.a_id_card, \n"
			 . "       ia.a_gender, \n"
			 . "       ia.a_address, \n"
			 . "       ia.a_school, \n"
			 . "       ia.a_bank, \n"
			 . "       ia.a_bank_card, \n"
			 . "       ia.a_title, \n"
			 . "       ia.a_resume, \n"
			 . "       ia.a_datetime, \n"
			 . "       ia.a_sitecode, \n"
			 . "       ia.a_active \n"
			 . "FROM   infoAuthors ia \n"
			 . "WHERE  1 = 1 \n";
		$this->a_active ? $sql.=$this->a_active : false;
		$this->a_id ? $sql.=$this->a_id : false;
		$this->a_email ? $sql.=$this->a_email : false;
		$this->order_by ? $sql.=$this->order_by : false;
		$query=$this->HT->query($sql);
		if($this->top_num==1)
		{
			if ($query->num_rows() > 0)
			{
				$row = $query->row();
				return $row;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return $query->result();
		}    
    }
	
	//用户注册
	function add($a_email, $a_password, $a_name, $a_name_cn,$a_photo, $a_mobile_phone, $a_phone, $a_id_card, $a_gender, $a_address, $a_school, $a_bank, $a_bank_card, $a_resume)
	{
		$sql = "INSERT INTO infoAuthors \n"
			 . "  ( \n"
			 . "    a_email, \n"
			 . "    a_password, \n"
			 . "    a_name, \n"
			 . "    a_name_cn, \n"
			 . "    a_photo, \n"
			 . "    a_mobile_phone, \n"
			 . "    a_phone, \n"
			 . "    a_id_card, \n"
			 . "    a_gender, \n"
			 . "    a_address, \n"
			 . "    a_school, \n"
			 . "    a_bank, \n"
			 . "    a_bank_card, \n"
			 . "    a_resume, \n"
			 . "    a_active, \n"
			 . "    a_datetime \n"			 
			 . "  ) \n"
			 . "VALUES \n"
			 . "  ( \n"
			 . "	?,?,?,?,?,?,?,?,?,?,?,?,?,?,0,getdate() \n"
			 . "  )";
		$query = $this->HT->query($sql,array($a_email, $a_password, $a_name, $a_name_cn,$a_photo, $a_mobile_phone, $a_phone, $a_id_card, $a_gender, $a_address, $a_school, $a_bank, $a_bank_card, $a_resume));
		return $query;
	}
	
	//更新用户资料
	function update($a_id,$a_email, $a_name, $a_name_cn,$a_photo, $a_mobile_phone, $a_phone, $a_id_card, $a_gender, $a_address, $a_school, $a_bank, $a_bank_card, $a_resume)
	{
		$sql = "UPDATE infoAuthors \n"
			 . "SET    a_email = ?, \n"
			 . "       a_name = ?, \n"
			 . "       a_name_cn = ?, \n"
			 . "       a_photo = ?, \n"
			 . "       a_mobile_phone = ?, \n"
			 . "       a_phone = ?, \n"
			 . "       a_id_card = ?, \n"
			 . "       a_gender = ?, \n"
			 . "       a_address = ?, \n"
			 . "       a_school = ?, \n"
			 . "       a_bank = ?, \n"
			 . "       a_bank_card = ?, \n"
			 . "       a_resume = ? \n"
			 . "WHERE  a_id = ?";
		$query = $this->HT->query($sql,array($a_email, $a_name, $a_name_cn,$a_photo, $a_mobile_phone, $a_phone, $a_id_card, $a_gender, $a_address, $a_school, $a_bank, $a_bank_card, $a_resume,$a_id));
		return $query;	
	}
	
	//设置密码
	function set_password($a_id,$a_password)
	{
		$sql = "UPDATE infoAuthors \n"
			 . "SET    a_password = ? \n"
			 . "WHERE  a_id = ?";
		$query=$this->HT->query($sql,array($a_password,$a_id));
		return $query;
	}
	
	//修改用户审核状态
	function reviwed($a_id,$a_active)
	{
		$sql = "UPDATE infoAuthors \n"
			 . "SET    a_active = ? \n"
			 . "WHERE  a_id = ? ";
		return $this->HT->query($sql,array($a_active,$a_id));
	}
        
	//修改职称
	function set_title($a_id,$a_title)
	{
		$sql = "UPDATE infoAuthors \n"
			 . "SET    a_title = ? \n"
			 . "WHERE  a_id = ? ";
		return $this->HT->query($sql,array($a_title,$a_id));
	}

	//更新用户站点标识
	function update_site($a_id, $a_sitecode)
	{
		$sql = "UPDATE infoAuthors SET a_sitecode = ? WHERE  a_id = ?";
		$query = $this->HT->query($sql,array($a_sitecode,$a_id));
		return $query;	
	}

	//获取当前站点的作者
    public function get_site_user($site_code){
        $sql = "SELECT  ia.a_id,
        				ia.a_name,
        				ia.a_name_cn,
        				ia.a_gender,
        				ia.a_title,
        				ia.a_resume,
        				ia.a_datetime,
        				ia.a_sitecode,
        				ia.a_active
        		  FROM  infoAuthors ia INNER JOIN infoaccessownsite i ON ia.a_id=i.iao_author AND i.iao_other='editor'
                 WHERE  i.iao_site=? AND ia.a_active = 1 AND ISNUMERIC(i.iao_author)>0";
        $query = $this->HT->query($sql,$site_code);
        return $query->result();
    }
	
}