<?php

class InfoTags_model extends CI_Model 
{
    //信息标签表
    var $it_title = '';
    var $it_memo = '';
    //信息标签关联表
    var $ic_id = '';
    var $it_id = '';

    function __construct() 
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    /**
     * 增加标签
     */
    function add_tag() 
    {
        if ($this->it_title) 
        {
            //查看是否已存在标签
            $sql = "SELECT TOP 1 * from infoTags  WHERE it_title = N?";
            $check = $this->HT->query($sql, array($this->it_title));

            //添加标签
            if ($check->num_rows() === 0)
            {
                $sql = "INSERT INTO infoTags (it_title, it_memo) VALUES (N?, N?)";
                $query = $this->HT->query($sql, array($this->it_title, $this->it_memo));
                return $this->HT->insert_id();
            }
            else 
            {
                $row = $check->row();
                return $row->it_id;
            }
            
        }
        return FALSE;
    }

    /**
     * 删除标签
     */
    function remove_tag() 
    {
        if ($this->it_title) 
        {
            $sql = "DELETE FROM infoTags WHERE it_title = N?";
            $query = $this->HT->query($sql, array($this->it_title));
            return 'success';
        }
        return 'fail';
    }

    /**
     * 标签列表
     */
    function list_tag() 
    {
        if ($this->ic_id) 
        {
            //展示信息的标签
            $sql = "SELECT * FROM infoContentToTag INNER JOIN infoTags ON icit_it_id = it_id WHERE icit_ic_id = ?";
            $query = $this->HT->query($sql, array($this->ic_id));
            return $query->result();
        } 
        else 
        {
            //全部展示
            $sql = "SELECT * FROM infoTags ORDER BY it_title ASC, it_date DESC";
            $query = $this->HT->query($sql);
            return $query->result();
        }
        return 'fail';
    }

    /**
     * 添加信息与标签的关联
     */
    function add_tag_to_content() 
    {
        if ($this->ic_id && $this->it_id) 
        {
            //查看是否已存在关联
            $sql = "SELECT TOP 1 * from infoContentToTag  WHERE icit_ic_id = ? AND icit_it_id = ?";
            $check = $this->HT->query($sql, array($this->ic_id, $this->it_id));
            
            //添加关联
            if ($check->num_rows() === 0)
            {
                $sql = "INSERT INTO infoContentToTag (icit_ic_id, icit_it_id) VALUES (?, ?)";
                $query = $this->HT->query($sql, array($this->ic_id, $this->it_id));
            }
            else
            {
                return 'exist';
            }
            return 'success';
        }
        return 'fail';
    }

    /**
     * 解除信息与标签的关联
     */
    function remove_tag_to_content() 
    {
        if ($this->ic_id && $this->it_id) 
        {
            $sql = "DELETE FROM infoContentToTag WHERE icit_ic_id = ? and icit_it_id = ?";
            $query = $this->HT->query($sql, array($this->ic_id, $this->it_id));
            return 'success';
        }
        return 'fail';
    }

}
