<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Info_tags extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Information_model');
        $this->load->model('InfoTags_model');
    }

	/**
     * 添加标签
     */
    public function add_tag()
    {
        $this->InfoTags_model->it_title = $this->input->get_post('title');
        $this->InfoTags_model->it_memo = $this->input->get_post('memo');
        $return = $this->InfoTags_model->add_tag();
        if ($return)  
        {
            echo(json_encode(array(
                'status' => 1,
                'message' => $return
            )));
        }
        else
        {
            echo(json_encode(array(
                'status' => -1,
                'message' => '添加标签失败'
            )));
        } 
    }

    /**
     * 删除标签
     */
    public function remove_tag()
    {
        $this->InfoTags_model->it_title = $this->input->get_post('title');
        $return = $this->InfoTags_model->remove_tag();
        if ($return == 'success') 
        {
            echo(json_encode(array(
                'status' => 1,
                'message' => '删除标签成功'
            )));
        }  
        else 
        {
            echo(json_encode(array(
                'status' => -1,
                'message' => '删除标签失败'
            )));
        }
    }

    /**
     * 展示标签
     */
    public function list_tag($ic_id = '')
    {
        $ic_id ? $this->InfoTags_model->ic_id = $ic_id : $this->InfoTags_model->ic_id = $this->input->post('ic_id');
        $return = $this->InfoTags_model->list_tag();
        if ($return != 'fail') 
        {
            echo(json_encode(array(
                'status' => 1,
                'data' => $return
            )));
        }  
        else 
        {
            echo(json_encode(array(
                'status' => -1,
                'message' => '读出标签失败'
            )));
        }
    }

    /**
     * 添加关联信息+标签
     */
    public function add_tag_to_content()
    {
        $this->InfoTags_model->ic_id = $this->input->get_post('ic_id');
        $this->InfoTags_model->it_id = $this->input->get_post('it_id');
        $return = $this->InfoTags_model->add_tag_to_content();
        if ($return == 'success') 
        {
            echo(json_encode(array(
                'status' => 1,
                'message' => '关联信息标签成功'
            )));
        } 
        else if ($return == 'exist') 
        {
            echo(json_encode(array(
                'status' => 2,
                'message' => '关联信息标签已存在'
            )));
        } 
        else 
        {
            echo(json_encode(array(
                'status' => -1,
                'message' => '关联信息标签失败'
            )));
        }
    }

    /**
     * 移除关联信息+标签
     */
    public function remove_tag_to_content()
    {
        $this->InfoTags_model->ic_id = $this->input->get_post('ic_id');
        $this->InfoTags_model->it_id = $this->input->get_post('it_id');
        $return = $this->InfoTags_model->remove_tag_to_content();
        if ($return == 'success') 
        {
            echo(json_encode(array(
                'status' => 1,
                'message' => '移除关联信息标签成功'
            )));
        } 
        else 
        {
            echo(json_encode(array(
                'status' => -1,
                'message' => '移除关联信息标签失败'
            )));
        }
    }

}

