<?php
class groupAction extends BackendAction
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_cate_mod = D('article_cate');
		$this->_mod = D('article_cate');
		$catelist = $this->_cate_mod->select();
		$this->assign('catelist', $catelist);
    }
    public function index()
    {   
	    
        $this->display();
    }
	public function biaoti(){
            $map = array();
			($keyword = I('keyword','', 'trim')) && $map['title'] = array('like', '%'.$keyword.'%');				
            if ($this->_request("sort", 'trim')) {
                $sort = $this->_request("sort", 'trim');
            } else {
                $sort = 'id';
            }
            if ($this->_request("order", 'trim')) {
                $order = $this->_request("order", 'trim');
            } else {
                $order = 'DESC';
            }					
            //分页
            $count = D('bdlist')->where($map)->count('id');
            $pager = new Page($count, 100);
            $list = D('bdlist')->where($map)->order($sort . ' ' . $order)->limit($pager->firstRow.','.$pager->listRows)->select();
            $page = $pager->show();
            $this->assign("page", $page);            
            $this->assign('list', $list);
            $this->assign('list_table', true);
			$this->assign('search', array(              
            'keyword' => $keyword,
            ));
            $this->display();
        }
	
	
	public function tags(){
            $map = array();
			($keyword = I('keyword','', 'trim')) && $map['title'] = array('like', '%'.$keyword.'%');
			($cate_id = I('cate_id', '', 'trim'))&& $map['cate_id'] = $cate_id;		
            if ($this->_request("sort", 'trim')) {
                $sort = $this->_request("sort", 'trim');
            } else {
                $sort = 'id';
            }
            if ($this->_request("order", 'trim')) {
                $order = $this->_request("order", 'trim');
            } else {
                $order = 'DESC';
            }
			//分类信息
            $res = $this->_cate_mod->field('id,name')->select();
            $cate_list = array();
            foreach ($res as $val) {
                $cate_list[$val['id']] = $val['name'];
            }
            $this->assign('cate_list', $cate_list);			
            //分页
            $count = D('klist')->where($map)->count('id');
            $pager = new Page($count, 100);
            $list = D('klist')->where($map)->order($sort . ' ' . $order)->limit($pager->firstRow.','.$pager->listRows)->select();
            $page = $pager->show();
            $this->assign("page", $page);            
            $this->assign('list', $list);
            $this->assign('list_table', true);
			$this->assign('search', array(            
            'cate_id' => $cate_id,
            'keyword' => $keyword,
            ));
            $this->display();
        }
	public function daoru() {
        $sort = $this->_request("sort", 'trim', 'ordid');
        $order = $this->_request("order", 'trim', 'DESC');
        $tree = new Tree();
        $tree->icon = array('│ ','├─ ','└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->_mod->order($sort . ' ' . $order)->select();
        $array = array();
        foreach($result as $r) {          
            $r['knum'] = D('klist')->where(array('cate_id'=>$r['id']))->count();			
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>  
                <td align='center'>\$spacer<span>\$name</span></td> 	
				<td align='center'><span>\$knum</span></td>
                <td align='center'><span class='btn btn_blue' id='J_upload\$id' data-cid='\$id'>导入标题</span></td>
				<td align='center'><span class='btn btn_black' data-cid='\$id'>清空标题</span></td>                			
                </tr>";
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);        
        $this->assign('list_table', true);
        $this->display();
    }
	public function reset(){
		if(IS_post && IS_AJAX){
			$mod = I('mod');
			if($mod == 1){
			D('bdlist')->where(array('status'=>2))->save(array('status'=>0));	
			}
			if($mod == 2){
			D('klist')->where(array('status'=>2))->save(array('status'=>0));	
			}
			if($mod == 3){
			D('klist')->where(array('status'=>4))->save(array('status'=>1));	
			}
			$this->ajaxReturn(1, '重置成功！');
		}
		 
	}
	public function clear(){
		if(IS_post && IS_AJAX){
			$cid = I('cid');
			D('klist')->where(array('cate_id'=>$cid))->delete();
			$this->ajaxReturn(1, '清空成功！');
		}
		 
	}
	public function ajax_upload_txt() { 
	    $cid = $_GET['cid'];
	    $path = 'baiduzhidao/'.$cid.'/';	
		$upload_path = C('ftx_attach_path') . $path;
	    $filename = $upload_path.'zhidao.txt';
	    if (file_exists($filename)) {
	    unlink($filename);
	    }
	    if (!empty($_FILES['file']['name'])) {            
            $result = $this->_upload($_FILES['file'], $path , array(), 'zhidao');
            if ($result['error']) {
                $this->ajaxReturn(0, $result['info']);
            } else {
                $savename = $upload_path . $result['info'][0]['savename'];
                $this->ajaxReturn( 1, L( "operation_success" ), $savename);
            }
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
	    
    }
	public function read($file_txt){		
        $data=array();
        $i=0;
        $content=fopen($file_txt,'r');    
        if(!$content) {
        echo '读取文件失败！';
		exit;
        }
        while(!feof($content)){
        $str=fgets($content);
        //读取的数据存在乱码需要设置编码
        $encoding = mb_detect_encoding($str,array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
        if($encoding!=false){
            if ($encoding != "EUC-CN") {
                if($encoding != "CP936"){
                    $data[$i] = iconv($encoding, "UTF-8", $str);
                }else{
                    $data[$i] = $str;
                }
            }else{
                $data[$i] = iconv($encoding, "UTF-8", $str);
            }
        }else{
            $data[$i] = mb_convert_encoding($str,'UTF-8','Unicode');
        }
        $i++;
        unset($str);		
       }
	   return $data;
	}
	
    public function collect() {	
		$cid = I('cid');
		$path = 'baiduzhidao/'.$cid.'/';	
		$upload_path = C('ftx_attach_path') . $path;
		$p = I('p',1,'intval');		
		if($p==1){
			$totalcoll = 0;
		}else{
			$totalcoll = F('infosr');
		}		
		$file = $upload_path.'zhidao.txt';
		$arr = $this->read($file);				
		$totalnum = count($arr);
		$start = ($p-1)*50;
		$end = $p*50;
		if($totalnum){
		for($i=$start;$i<$end;$i++){					
				$name = $arr[$i];				
	            $name = trim($name);				
				$itemarray['cate_id']  = $cid;				
				$itemarray['title']    = $name;	
				$itemarray['status']   = 0;
				if($name && $cid){
			      $result['item_list'][]=$itemarray;
			    }
		   }	
		}	        
		$coll=0;		
		foreach ($result['item_list'] as $key => $val) {
			$res= $this->_ajax_tb_publish_insert($val);
			if($res>0){
				$coll++;
			}
			$totalcoll++;
		}		
		
		F('infosr',$totalcoll);
		if($end>$totalnum){
		unlink($file);	
		F('infosr',NULL);
		$this->ajaxReturn(0, '导入完成！');		
		}
		$this->assign('p',$p);
		$this->assign('coll', $coll); 
		$this->assign('totalnum', $totalnum);
		$this->assign('totalcoll', $totalcoll);
		$resp = $this->fetch('collect');
		$this->ajaxReturn(1, '关键词导入中', $resp);
    }
	private function _ajax_tb_publish_insert($item) { 		  
		  $result = D('klist')->ajax_yg_publish($item);
        return $result;
    }
}
?>
	 
