<?php
/**
 * 文章分类管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class ArticleCatController extends BaseController{
	public function _initialize(){
		parent::_initialize();
        $this->ArticleCat=getcache('ArticleCat');
    }
    public function index(){
		$pid=I('pid')?I('pid'):1;
		 $ArticleCat= $this->ArticleCat;
		$Tree = new \Common\Vendor\Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($ArticleCat as &$values) {
			$values['str_manage'] = '<a href="'.U('add',array('pid'=>$values['id'])).'" class="operate">添加子分类</a>&nbsp;&nbsp;<a class="operate" href="'.U('edit',array('id'=>$values['id'])).'">编辑</a>&nbsp;&nbsp;';
			if($values['id'] != 4 && $values['id']!=1){
				$values['str_manage'] .= '<a href="javascript:;" onclick="$.showConfirm(\''. L('SURE_DELETE').'\',function(){window.location.href=\''.U('delete',array('id'=>$values['id'])).'\'},\'hc\')">删除</a>';				
			}
			if($values['id'] == 4){$values['dis']="disabled='disabled'";}else{$values['dis']='';}		
			$values['status'] = '<img src="' . __ROOT__ . '/Application/Admin/View/' . get_default_theme() . '/common/images/status_' . $values['status'] . '.gif" />';
		}
		$Tree->init($ArticleCat);
		$str = "<tr>
		<td><input value='\$id' name='id[]' type='checkbox' \$dis ></td>
	    <td>\$id</td>
		<td ondblclick=editeTable(this,'sort','\$id','sort')>\$sort</td>
		<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$name</td>
        <td>\$status</td>
		<td>\$str_manage</td>
		</tr>";
		$cateString = $Tree->get_tree(0, $str);
        $this->assign('cateString', $cateString);
		$this->display();
	}
	public function _before_add(){
		$this->getNodeTree();
	}
	public function _before_edit(){
		$id  = I('get.id');$info='';
        $info=$this->ArticleCat[$id];
		$this->getNodeTree($info['pid']);
	}
	public function _before_delete(){
		$id=I('id');
		if(isset($id)&&is_array($id)){
			foreach($id as $k=>$v){
				$result=check_delete($v,7,CONTROLLER_NAME);
				if(!$result['status'])unset($id[$k]);
			}
			if(empty($id)){$this->error($result['info']);exit;}
		}else{
			$result=check_delete($id,7,CONTROLLER_NAME);
			if(!$result['status']){$this->error($result['info']);exit;}
		}
		$this->id=$id;
	}
    /* 得到节点树 */
	private function getNodeTree($pid = '') {
	   $select_categorys='';
       $ArticleCat = $this->ArticleCat;			
       if(empty($pid))$pid = I('pid');
	   $Tree = new \Common\Vendor\Tree();
       $treemini=$Tree->treemini($ArticleCat);
       $select_categorys.= $Tree->treeminioption($treemini,$pid);
	   $this->assign('select_categorys',$select_categorys);
    }
}