<?php
/**
 * 权限组管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Admin\Controller;
class AuthGroupController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->AuthRule = getcache('AuthRule');
    }

    function index()
    {
        $admin_list = $this->lists('AuthGroup');
        $this->assign('admin_list', $admin_list);
        $this->display();
    }

    /* 设置管理员权限规则 */
    public function memberrule()
    {
        $authgroup = D('AuthGroup');
        $id = I('id');
        if (IS_POST) {
            $status = $authgroup->where("id=" . $id)->setField('rules', implode(',', I('post.nodeid')));
            if ($status !== false) {
                $this->setGroupRulebyid($id);//更新缓存
                $this->success(L('DO_OK'), '', '', 'index');
            } else {
                $this->error(L('DO_ERROR'));
            }
        } else {
            if (isset($id)) {
                $id = isset($id) && intval($id) ? intval($id) : $this->error('参数错误');
            }
            $current = $authgroup->where('id=' . $id)->field(true)->find();
            $current ? $this->assign('current', $current) : $this->error(L('PLSEASE_SELECT_ROWS'));
            //分配已有权限规则
            if ($current['rules']) $arrrules = explode(',', $current['rules']);
            $this->assign('power', $arrrules);
            $ruleData = $this->AuthRule;
            $Tree = new \Common\Vendor\Tree();
            $Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            foreach ($ruleData as $n => $t) {
                if ($t['status'] != 1) {
                    unset($ruleData[$n]);
                    continue;
                }
                $ruleData[$n]['checked'] = in_array($t['id'], $arrrules) ? ' checked' : '';
                $ruleData[$n]['depth'] = $this->get_level($t['id'], $ruleData);
                $ruleData[$n]['pid_node'] = ($t['pid']) ? ' class="tr lt child-of-node-' . $t['pid'] . '"' : '';
            }
            $str = "<tr id='node-\$id' \$pid_node>
                    <td style='padding-left:30px;' class='rulecb'>\$spacer<input type='checkbox' name='nodeid[]' value='\$id' level='\$depth' \$checked onclick='checknode(this);' > \$title (\$name)</td>
                </tr>";
            $Tree->init($ruleData);
            $html_tree = $Tree->get_tree(0, $str);
            $this->assign('html_tree', $html_tree);
            $this->assign('ruleData', $ruleData);
            $this->display();
        }
    }

    /*获取级数*/
    private function get_level($id, $array = array(), $i = 0)
    {
        if ($id == '0') return $i;
        if ($array[$id]['pid'] == 0) return $i;
        else {
            ++$i;
            return $this->get_level($array[$id]['pid'], $array, $i);
        }
    }

    private function setGroupRulebyid($id)
    {
        if (!$id) return false;
        $AdminRule = M("AuthRule");
        $AdminGrouprule = M('AuthGroup');
        $groupValue = $AdminGrouprule->where('id=' . $id . ' and status=1')->field('id,rules')->find();
        $ruleData = $AdminRule->where("id IN(" . $groupValue['rules'] . ")  AND status=1")->order('sort,id desc')->getField('id,name,title,show_status,type,condition,pid,sort,icon');
        F("admin_rule_" . $id, $ruleData);
        unset($ruleData);
        return true;
    }
	public function _before_delete(){
		$id=I('id');
		$result=check_delete($id,7,'AuthGroupAccess','group_id','请先清除该管理组下管理员');
		if(!$result['status']){$this->error($result['info']);exit;}
	}
}
?>