123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | menu.php 后台管理 左侧菜单管理
- |--------------------------------------------------------------------------
- */
- namespace MazeApp\Manage;
- use Maze;
- Maze::load('manage/auth.init');
- class Top
- {
- /**
- * 获取所有的头部菜单权限
- *
- * @return array
- */
- public function all()
- {
- $top = Maze::load('manage/top-main');
-
- if($top)
- {
- $child = Maze::load('manage/top-child');
- foreach($top as $k => $v)
- {
- if(isset($child[$v['id']]))
- {
- $c = 0;
- foreach($child[$v['id']] as $i => $j)
- {
- $top[$k]['child'][$i] = $j;
- $c++;
- }
- if(!isset($top[$k]['child']))
- {
- unset($top[$k]);
- }
- }
- }
- }
-
- return $top;
- }
- /**
- * menu 从数据库中得到所有权限
- *
- * @return array
- */
- public function get($auth = true)
- {
- if($auth)
- {
- # 需要验证权限
- $auth = Maze::load('manage/auth.top');
- }
- $top = Maze::load('manage/top-main');
-
- if($top)
- {
- $child = Maze::load('manage/top-child');
- foreach($top as $k => $v)
- {
- if($auth && strpos($auth, $k . '_') === false)
- {
- unset($top[$k]);
- continue;
- }
-
- if(isset($child[$v['id']]))
- {
- # 如果已经保存了,就直接取出来吧
- $info = Maze::load('manage/auth.getTop', $v['key']);
- if($info)
- {
- $top[$k]['name'] = $info['name'];
- }
- $c = 0;
- foreach($child[$v['id']] as $i => $j)
- {
- if(($auth && strpos($auth, $i) === false) || ($info && $info['id'] == $j['id']))
- {
-
- }
- elseif(!$info && $c == 0)
- {
- $top[$k]['name'] = $j['name'];
- Maze::load('manage/auth._setTop', $j);
- $c++;
- }
- else
- {
- $top[$k]['child'][$i] = $j;
- $c++;
- }
- }
- if(!isset($top[$k]['child']))
- {
- unset($top[$k]);
- }
- }
- }
- }
- return $top;
- }
-
- /**
- * 更新数据到数据库
- *
- * @return array
- */
- public function update($param = array())
- {
- if(isset($param['key']))
- {
- $info = Maze::load('manage/top-key', array('where_key' => $param['key']));
- //print_r($info);die;
- if(!$info)
- {
- $update['add_key'] = $param['key'];
- $update['add_name'] = $param['name'];
- $update['add_top_id'] = isset($param['top']) ? $param['top'] : -1;
- $update['add_value'] = $param['value'];
- $update['add_state'] = isset($param['state']) ? $param['state'] : 1;
- $info['id'] = Maze::load('manage/top-insert', $update);
- }
- else
- {
- $update['set_name'] = $param['name'];
- $update['set_value'] = $param['value'];
- $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
- $update['where_id'] = $info['id'];
- Maze::load('manage/top-update', $update);
- }
-
- return $info['id'];
- }
- return false;
- }
- /**
- * 同步子权限更新到数据库
- *
- * @return array
- */
- public function sync($param = array())
- {
- if(isset($param[0]) && isset($param[1]))
- {
- $key = $param[1]['key'];
- $info = Maze::load('manage/top-key', array('where_key' => $key));
- if($info)
- {
- $update['value'] = Maze::input('where_id', $param[0]);
- $update['name'] = Maze::input('name', '-u');
- $update['top'] = $info['id'];
- $update['key'] = $key . '_' . $update['value'];
- $update['state'] = Maze::input('state', '-u');
- $this->update($update);
- }
- }
- }
- /**
- * 更新当前使用的top菜单
- *
- * @return array
- */
- public function update_action($id = false)
- {
- $id = $id ? $id : Maze::input('id');
- if($id > 0)
- {
- # 需要验证权限
- $auth = Maze::load('manage/auth.top');
- $info = Maze::load('manage/top-one', $id);
- if($info && $info['top_id'] > 0 && (!$auth || strpos($auth, $info['key']) !== false))
- {
- Maze::load('manage/auth._setTop', $info);
- }
-
- return $info['value'];
- }
- }
- }
|