<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Keywords extends Mocuz_Controller
{


    public function __construct()
    {
        parent::__construct();
       $this->load->model('keywords_model','keywords');
       $this->load->library('pagination');
       $this->load->helper('url');
       $this->load->helper('my_api');
    }

    //帖子管理
    public function index()
    {
        $data = array();
        $where=array();
        $where[]="isdelete=0";
        $total=$this->keywords->get_count($where); 
        $config['base_url']=site_url('keywords/index/');
        $config['total_rows'] =$total;
        $config['per_page'] = 6;
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li id="first">';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = '末页';
        $config['last_tag_open'] = '<li id="last">';
        $config['last_tag_close'] = '</li>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<li id="down">';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li id="top">';
        $config['prev_tag_close'] = '</li>'; 
        $config['num_links'] = 5; 
        $config['full_tag_open'] = '<div class="wrap"><div class="fenye"><ul>';
        $config['full_tag_close'] = '</ul></div></div>';
        $offset = $this->uri->segment(3);
        $this->pagination->initialize($config); 
        $page=$this->pagination->create_links();
        $data['page']=$page;
        $limit = $config["per_page"];
        $field="id,keyword,createtime";
        $dataa=$this->keywords->get_keywords($where,$field,$limit,$offset);
        foreach ($dataa as $key => &$value) {
            $value['createtime']=format_date($value['createtime']);
        }
        $this->pagination->initialize($config); 
        $page=$this->pagination->create_links();
        $data['data']=$dataa;
        $data['page']=$page;
        $this->load->view('common/innerheader');
        $this->load->view('keywords',$data);
    }

    public function jump(){
       $sharetext=$_POST['titleg'];
       $sharetext = strip_tags($sharetext);
       $sharetext = preg_replace('/((&nbsp;(\s+)?)+)/', '', $sharetext);
       $names =preg_split('/\r\n/',$sharetext);
       foreach($names as $name){
        if(!$name){
            continue;
        }
        $data = array();
        if(mb_strlen($name)>45){
           $name=str_replace(mb_substr($name, 45,mb_strlen($name)), "...", $name) ;
        }
        $data['keyword']=$name;
        $data['isdelete']=0;
        $data['createtime']=strtotime(date("Y-m-d H:i:s",time())); 
        $result = $this->keywords->insert($data);
        }
        redirect("keywords/index");   
    }

    public function keywords_del(){
        $ids = $this->input->post('tids');
        $this->keywords->bantch_delete($ids);
        echo $this->json(1,'成功');
    }

}