<?php
class daoruAction extends BackendAction {
	public function _initialize() {
        parent::_initialize();
        
    }

	public function index(){		
		$path = 'daoru/';	
		$upload_path = C('ftx_attach_path') . $path;	    
		$keywords = $upload_path.'keywords.txt';		
		if (file_exists($keywords)) {
		$this->assign('keywords', $keywords);	
		}		
		$this->display();
	}
	
	public function ajax_upload_txt() { 
	    $path = 'daoru/';	
		$upload_path = C('ftx_attach_path') . $path;
	    $filename = $upload_path.'keywords.txt';
	    if (file_exists($filename)) {
	    unlink($filename);
	    } else {
	    if (!empty($_FILES['file']['name'])) {            
            $result = $this->_upload($_FILES['file'], $path , array(), 'keywords');
            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() {				
        $upload_path = C('ftx_attach_path') . 'daoru/';
		$p		= I('p',1,'intval');
		
		if($p==1){
			$totalcoll = 0;
		}else{
			$totalcoll = F('totalcoll');
		}		
		$file = $upload_path.'keywords.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['k']  = $name;					
				if($name){
			      $result['item_list'][]=$itemarray;
			    }
		   }	
		}	        
		$coll=0;
		$thiscount=0;
		foreach ($result['item_list'] as $key => $val) {
			$res= $this->_ajax_tb_publish_insert($val);
			if($res>0){
				$coll++;
			}
			$totalcoll++;
		}		
		
		F('totalcoll',$totalcoll);
		if($end>$totalnum){
		unlink($file);	
		F('totalcoll',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('tag')->ajax_yg_publish($item);
        return $result;
    }
    
}