<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tags extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjAdmin');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
                $this->load->helper('file');
	}


	public function index()
	{
                $this->load->view('admin/tags','');
        }

	public function edit()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/vod/lists/'));
            $sql="SELECT * FROM ".CS_SqlPrefix."tags where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
            $data['tags']=$row[0];
            $this->load->view('admin/tags_edit',$data);
        }

	public function del()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/vod/lists/'));

            $this->db->query("delete from ".CS_SqlPrefix."tags where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }

	public function list_save()
	{
            $this->CsdjAdmin->Admin_Qx('40');
 	    $id = intval($this->input->post('CS_ID'));
 	    $name = $this->input->post('CS_Name',TRUE);
            if($id==0 || empty($name)) $this->CsdjAdmin->Admin_Url('<font color=red>名称不能为空！</font>','javascript:history.back();');

            $name=str_replace('"',"&quot;",$name);
            if ( ! defined('CS_Tags_'.$id)){
	                $strs="<?php"."\r\n";
	                $strs.="define(\"CS_Laiyuan\",\"".CS_Laiyuan."\"); //来源  \r\n";
	                $strs.="define(\"CS_Yuyan\",\"".CS_Yuyan."\"); //语言    \r\n";
	                $strs.="define(\"CS_Diqu\",\"".CS_Diqu."\"); //地区  \r\n";
	                $strs.="define(\"CS_Year\",\"".CS_Year."\");  //年份  \r\n";
	                $strs.="define(\"CS_Zuoz\",\"".CS_Zuoz."\");  //作者   \r\n";
                        if(defined('CS_Tags_1')) $strs.="define(\"CS_Tags_1\",\"".CS_Tags_1."\");  \r\n";
                        if(defined('CS_Tags_2')) $strs.="define(\"CS_Tags_2\",\"".CS_Tags_2."\");  \r\n";
                        if(defined('CS_Tags_3')) $strs.="define(\"CS_Tags_3\",\"".CS_Tags_3."\");  \r\n";
                        if(defined('CS_Tags_4')) $strs.="define(\"CS_Tags_4\",\"".CS_Tags_4."\");  \r\n";
                        if(defined('CS_Tags_5')) $strs.="define(\"CS_Tags_5\",\"".CS_Tags_5."\");  \r\n";
                        if(defined('CS_Tags_6')) $strs.="define(\"CS_Tags_6\",\"".CS_Tags_6."\");  \r\n";
                        if(defined('CS_Tags_7')) $strs.="define(\"CS_Tags_7\",\"".CS_Tags_7."\");  \r\n";
	                $strs.="define(\"CS_Tags_".$id."\",\"".$name."\");  \r\n";
            }else{
	                $strs=read_file("./csdj/lib/Cs_Rigin.php");
	                $strs=preg_replace('/"CS_Tags_'.$id.'","(.*?)"/','"CS_Tags_'.$id.'","'.$name.'"',$strs);
            }
	    if(!write_file('./csdj/lib/Cs_Rigin.php', $strs)){
                $this->CsdjAdmin->Admin_Url('<font color=red>文件./csdj/lib/Cs_Rigin.php，没有写入权限！</font>','javascript:history.back();');
            }else{
                $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，修改成功！</font>',site_url('admin/tags'),'ok');
            }
        }

	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('40');
 	    $CS_ID = intval($this->input->post('CS_ID'));
 	    $data['CS_CID'] = intval($this->input->post('CS_CID'));
 	    $data['CS_Name'] = $this->input->post('CS_Name',TRUE);
            if($data['CS_CID']==0 || empty($data['CS_Name'])) $this->CsdjAdmin->Admin_Url('<font color=red>分类、名称不能为空！</font>','javascript:history.back();');

            if($CS_ID==0){
                 $this->CsdjDB->get_insert('tags',$data);
            }else{
                 $this->CsdjDB->get_update('tags',$CS_ID,$data);
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/tags'),'ok');
        }

	public function res()
	{
		$data['f'] = $this->input->get_post('f', TRUE);
                $sql="SELECT * FROM ".CS_SqlPrefix."tags";
	        $data['tags']=$this->CsdjDB->get_all($sql);
                $this->load->view('admin/tags_res',$data);
        }
}
