<?php
defined('PP_PATH') or exit;
class tool_control extends admin_control {
	public function index() {
		if(!empty($_POST)) {
			!empty($_POST['dbcache']) && $this->runtime->truncate();
			!empty($_POST['filecache']) && $this->un_filecache();
			
			//重新生成碎片供前台调用
			$runtime = $this->runtime->xget('block');
			if(empty($runtime)) {
				$this->cms_content->table = 'cms_block';
				$total = $this->cms_content->count();
				$cms_block_arr = $this->cms_content->list_arr(array(), 'id', -1, 0, 100, $total);
				
				$_block_list = array();
				foreach($cms_block_arr as $t){
					$_block_list	= array_merge($_block_list, array($t['pos'] =>array('isjs'=>$t['isjs'],'content'=>$t['content'])));
				}
				$this->runtime->set('block', $_block_list);
			}
			
			E(0, '清除完成！');
		}

		$this->display();
	}

	// 重新统计
	public function rebuild() {
		if(!empty($_POST)) {
			// 重新统计分类的内容数量
			if(!empty($_POST['re_cate'])) {
				$tables = $this->models->get_table_arr();
				$cids = $this->category->get_category_db();

				foreach($cids as $row) {
					if($row['mid'] == 1) continue;

					$this->cms_content->table = 'cms_'.(isset($tables[$row['mid']]) ? $tables[$row['mid']] : 'products');
					$count = $this->cms_content->find_count(array('cid'=>$row['cid']));
					
					if($row['mid'] == 2 || $row['mid'] == 3){
						$count_0 = $this->cms_content->find_count(array('cid'=>$row['cid'],'status'=>0));
						$count_1 = $this->cms_content->find_count(array('cid'=>$row['cid'],'status'=>1));
					}else{
						$count_0 = 0;
						$count_1 = 0;
					}

					$this->category->update(array('cid'=>$row['cid'], 'count'=>$count, 'count_0'=>$count_0, 'count_1'=>$count_1));

					//$this->category->update(array('cid'=>$row['cid'], 'count'=>$count));
				}
			}

			// 清空数据表的 count max 值，让其重新统计
			if(!empty($_POST['re_table'])) {
				$this->db->truncate('framework_count');
				$this->db->truncate('framework_maxid');
			}

			E(0, '重新统计完成！');
		}

		$this->display();
	}
	// 删除文件缓存
	private function un_filecache(){
		if(file_exists(RUNTIME_PATH.'_runtime.php')){
			unlink(RUNTIME_PATH.'_runtime.php');
		}
		$tpmdir = array('_control', '_model', '_view');
		foreach($tpmdir as $dir) { //nzcms_admin nzcms_member
			$appname = array('nzcms_admin','nzcms_member');
			foreach($appname as $v){
				_rmdir(RUNTIME_PATH.$v.$dir);
			}
		}
		foreach($tpmdir as $dir) {
			_rmdir(RUNTIME_PATH.F_APP_NAME.$dir);
		}
		return TRUE;
	}
	public function get_tags() {
		$_title 		= trim(strip_tags(R('title', 'P')));
		$content 		= trim(strip_tags(R('content', 'P')));
		if(strlen($content)>2400){ //在线分词服务有长度限制   
			$content =  mb_substr($content, 0, 600, 'utf-8');   
		}

		$title = rawurlencode(strip_tags($_title));
		$content =  rawurlencode(strip_tags($content));
		$url = "http://keyword.discuz.com/related_kw.html?title=$title&content=$content&ics=utf-8&ocs=utf-8";
		$xml_array 	= simplexml_load_file($url); 
		$result 	= $xml_array->keyword->result;
		$tags 		= array();
		foreach ($result->item as $key => $value) {
			array_push($tags, (string)$value->kw);
		}
		$tagarr	= implode(",",$tags);

		echo json_encode(array('tags'=>$tagarr));
	}
	public function get_pinyin() {
		$_title 		= trim(strip_tags(R('title', 'P')));

		$pinyin 	= new pinyin();
		$pinyinarr 	= $pinyin->utf8_to($_title);

		echo json_encode($pinyinarr);
	}
	private function getTags_bak($title){
		$pa = new PhpAnalysis();
		$pa->SetSource($title);
		$pa->resultType = 2;
		$pa->StartAnalysis();
		$tags = $pa->GetFinallyKeywords(4);
		return $tags;
	}
}