<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Link 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->load->model('CsdjSkins');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
                $this->load->helper('file');
	}

	public function index()
	{
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;

	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."link order by CS_XID asc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/link/index/')."?";
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' limit '. $per_page*($page-1) .','. $per_page;
	    $query = $this->db->query($sql_string);

	    $data['link'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/link',$data);
        }

	public function add()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_Name']="";
            $data['CS_Pic']="";
            $data['CS_CID']=1;
            $data['CS_Url']="http://";
            $data['CS_XID']=50;
            $this->load->view('admin/link_edit',$data);
        }

	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/'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."link where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                $data['title']="修改";
                $data['CS_ID']=$row[0]->CS_ID;
                $data['CS_Name']=$row[0]->CS_Name;
                $data['CS_Pic']=$row[0]->CS_Pic;
                $data['CS_CID']=$row[0]->CS_CID;
                $data['CS_Url']=$row[0]->CS_Url;
                $data['CS_XID']=$row[0]->CS_XID;
            }
            $this->load->view('admin/link_edit',$data);
        }

	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('24');
            $CS_ID=$this->input->post('CS_ID');

                $data['CS_Name']=$this->input->post('CS_Name');
                $data['CS_Pic']=$this->input->post('CS_Pic');
                $data['CS_CID']=$this->input->post('CS_CID');
                $data['CS_Url']=$this->input->post('CS_Url');
                $data['CS_XID']=$this->input->post('CS_XID');

                if(empty($data['CS_Name'])||empty($data['CS_Url'])){
                        $this->CsdjAdmin->Admin_Url('<font color=red>名称、地址、不能为空！</font>','javascript:history.back();');
                }

                if(empty($data['CS_XID'])) $data['CS_XID']=50;

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

	public function del()
	{
            $this->CsdjAdmin->Admin_Qx('24');
            $id = $this->security->xss_clean($this->uri->segment(4));   //ID
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

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

