<?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('HT', 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;
        }
    }

    //判断是否已注册
    public function check_signup($email) {
        $sql = "SELECT TOP 1 1  \n"
                . "FROM   infoAuthors ia \n"
                . "WHERE  ia.a_email = ? \n"
                . "       AND ia.a_active != 2 ";
        $query = $this->HT->query($sql, array($email));
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //已激活作者列表
    function active_list() {
        $this->init();
        $this->a_active = " AND ia.a_active = 1 ";
        return $this->get_list();
    }

    //待激活作者列表
    function un_active_list() {
        $this->init();
        $this->a_active = " AND ia.a_active = 0 ";
        return $this->get_list();
    }

    //获取登录用户详细信息
    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_resume, \n"
                . "       ia.a_datetime, \n"
                . "       ISNULL(ia.a_sitecode,'cht') as 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 SendMail($fromName, $fromEmail, $toName, $toEmail, $subject, $body) {
        $sql = "INSERT INTO Email_AutomaticSend \n"
                . "  ( \n"
                . "	M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  \n"
                . "	M_FromName, M_State \n"
                . "  ) \n"
                . "VALUES \n"
                . "  (  \n"
                . "	?, ?, ?, ?, ?, ?, ?, ?, 0 \n"
                . "  ) ";
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toName, $toEmail, $subject, $body, 'Author', 'http://author.mycht.cn'));
        return $query;
    }

    //判断是否有网前url
    public function get_user_weburl($ic_author) {
        $sql = "select top 1 a_name from infoContents left join infoAuthors on ic_author=a_id where ic_status=1 and ic_author='$ic_author'";
        $query = $this->HT->query($sql);
        $result = $query->result();
        return $result;
    }

}
