<?php
/**
 * 权限规则管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class AuthRuleController extends BaseController
{
	public function index()
	{		
		$nodeData =$this->AuthRule;
		$Tree = new \Common\Vendor\Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';        
        $typearr=array('模块','菜单','其它','方法');
		foreach ($nodeData as &$values) {
			$values['str_manage'] = '<a href="'.U('add',array('id'=>$values['id'])).'" class="operate">添加子菜单</a>&nbsp;&nbsp;<a class="operate" href="'.U('edit',array('id'=>$values['id'])).'">编辑</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="$.showConfirm(\''. L('SURE_DELETE').'\',function(){window.location.href=\''.U('delete',array('id'=>$values['id'])).'\'},\'hc\')">删除</a>';
            $values['nodetype'] =$typearr[$values['type']];
			$values['status'] = '<img src="' . __ROOT__ . '/Application/Admin/View/' . get_default_theme() . '/common/images/status_' . $values['status'] . '.gif" />';
			$values['show_status'] = '<img src="' . __ROOT__ . '/Application/Admin/View/' . get_default_theme() . '/common/images/status_' . $values['show_status'] . '.gif" />';
		}
		$Tree->init($nodeData);
		$str = "<tr>
	    <td>\$id</td>
		<td><input name='sort[\$id]' class='text' tabindex='10' type='text' size='3' value='\$sort' class='input'></td>
		<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$title(\$name)</td>
                    <td>\$status</td>
                    <td>\$show_status</td>
		<td>\$str_manage</td>
		</tr>";
		$nodeString = $Tree->get_tree(0, $str);
		$this->assign('nodeString',$nodeString);
		$this->display();
	}
    /* 添加数据 */
	public function add() {
		if (IS_POST) {
			$postData = I('post.');
            if(str_exists($postData['action_name'],'&amp;'))$postData['action_name']=str_replace('&amp;','&',$postData['action_name']);
            $postData['name'] = "{$postData['module_name']}/{$postData['action_name']}";
            F('data',NULL);
unset($postData['module_name'],$postData['action_name'],$postData['dosubmit'],$postData['__hash__']); 
            $result=D('AuthRule')->add($postData);
            savecache('AuthRule');
			$this->jumpresult($result);
		}else {
			$this->getNodeTree(I('id'));
			$this->display('edit');
		}
	}
	/* 编辑数据 */
	public function edit() {
	   $moudle=D('AuthRule');
		if (IS_POST) {
			$postData = I('post.');
            if(str_exists($postData['action_name'],'&amp;'))$postData['action_name']=str_replace('&amp;','&',$postData['action_name']);
            $postData['name'] = "{$postData['module_name']}/{$postData['action_name']}";
            unset($postData['module_name'],$postData['action_name'],$postData['dosubmit'],$postData['__hash__']);
			$postData['icon'] = urlencode($postData['icon']);
			$result=$moudle->save($postData);
            savecache('AuthRule');
			$this->jumpresult($result);
		}else {
            $nodeData =$this->AuthRule;
			$info =$nodeData[I('id')];
			if($info){$info['icon'] = urldecode($info['icon']);}
			$info ? $this->assign('info',$info) : $this->error(L('PLSEASE_SELECT_ROWS'));
            $this->assign('status',$info['status']);
            $this->assign('show_status',$info['show_status']);
			$this->assign('node',explode('/', $info['name']));
			$this->getNodeTree($info['pid']);
			$this->display();
		}
	}
    /* 得到节点树 */
	private function getNodeTree($pid = '') {
		$nodeData = $this->AuthRule;
		if ($nodeData) {
			$Tree = new \Common\Vendor\Tree();
			foreach ($nodeData as &$values) {
				$values['selected'] = $values['id'] == $pid ? 'selected="selected"' : '';
			}
			$str = "<option value='\$id' \$selected>\$spacer \$title</option>";
			$Tree->init($nodeData);
			$parent_node = $Tree->get_tree(0, $str);
			$this->assign('parent_node',$parent_node);
		}
	}
	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;
	}
}
?>