<?php
namespace Home\Controller;
use Common\Controller\BaseController;

class HelpController extends BaseController {
	
	/*
	帮助中心首页
	*/
    public function index(){
	$where['pid']=array('eq','0');
	$data=D('HelpType')->where($where)->order('paixi desc')->select();
	$map['pid']=array('neq','0');
	$list=D('HelpType')->where($map)->order('paixi desc')->select();
	
	$assign=array(
	'data'=>$data,
	'list'=>$list,
	);
	$this->assign($assign);
    $this->display();
    }
	
	/*
	帮助文章分类列表
	*/
	
	public function  doc(){
	
	$data=D('HelpType')->getTreeData('level','paixi','title');
	
	//当前栏目属性
	$where2['id']=I('get.type');
	$nav=D('HelpType')->where($where2)->find();
	if($nav==false){$this->error('栏目不存在或已删除');}
	$id=I('get.type');
	$pid=$this->getpid($id);//获取当前子目录的一级目录
	
	//输出当前ID及子ID的文章列表
	
	$allids = $this->getAllChildcateIds(I('get.type'));
	$map['type']=array('in',$allids);
	$count = D('Help')->where($map)->count();// 查询满足要求的总记录数
	$Page = new \Think\Page($count,C('PAGE_NUM'));// 实例化分页类 
	$show = $Page->show();// 分页显示输出
	$list=D('Help')->where($map)->order('adddate desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	
	
	
	$assign=array(
	'data'=>$data,
	'list'=>$list,
	'page'=>$show,
	'pid'=>$pid,
	'nav'=>$nav,
	);
	$this->assign($assign);
    $this->display();
    }
	
	/*
	文章内容展示
	*/
	
	public function  view(){
	$where['id']=I('get.id');
	$view=D('Help')->where($where)->find();	
	if($view==false){$this->error('栏目不存在或已删除');}
	$view['content']=htmlspecialchars_decode($view['content']);	
	D('Help')->where($where)->setInc('view',1); // 文章阅读数加1	
	$id=$view['type'];//当前文章的分类ID
	$pid=$this->getpid($id);//获取当前子目录的一级目录
	$data=D('HelpType')->getTreeData('level','paixi','title');
	
	$assign=array(
	'view'=>$view,
	'data'=>$data,
	'pid'=>$pid,
	'id'=>$id,
	);	
	$this->assign($assign);
	$this->display();	
	}	
	
	/*查询顶级分类的ID
	AUTHOR  Musto   www.ew80.com
	*/
	protected  function  getpid($id=0){
	global $pid;
	$tj['id']=array('eq',$id);
	$rs=D('HelpType')->where($tj)->find();
	if($rs['pid']!='0'){
	$id=$rs['pid'];
	$this->getpid($id);
	}else{
	$pid=$rs['id'];
	}
	return  $pid;
	
	}
	
	/*
	获取所有子类ID列表
	*/
	protected function getAllChildcateIds($categoryID)
    {
        //初始化ID数组
        $array[] = $categoryID;
        do
        {
            $ids = '';
            $where['pid'] = array('in',$categoryID);
            $cate = D('HelpType')->where($where)->select();
            foreach ($cate as $k=>$v)
            {
                $array[] = $v['id'];
                $ids .= ',' . $v['id'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $categoryID = $ids;
        }
        while (!empty($cate));
        $ids = implode(',', $array);
        return $ids;    //  返回字符串
        //return $array //返回数组
    }
	
	/*
	搜索展示页
	*/
	public function s(){
		
	$where['h.title']=array('LIKE',"%".I('get.k')."%");	
	$count = D('Help h')->where($where)->count();// 查询满足要求的总记录数
	$Page = new \Think\Page($count,C('PAGE_NUM'));// 实例化分页类 
	$show = $Page->show();// 分页显示输出
	$list=D('Help')->alias('h')->
	join('__HELP_TYPE__ t ON  h.type=t.id','LEFT')->
	field('h.id cid,t.id,h.title,t.title t_title,t.pid')->
	where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	
	$type=D('HelpType')->field('title,id')->select();
	$assign=array(
	'list'=>$list,
	'type'=>$type,
	'page'=>$show,
	);
	$this->assign($assign);
	$this->display();
		
	}
	
	
	

}