123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | menu.php 后台管理 左侧菜单管理
- |--------------------------------------------------------------------------
- */
- namespace MazeApp\Manage;
- use Maze;
- Maze::load('manage/auth.init');
- class Menu
- {
- /**
- * fast
- *
- * @var array
- */
- private $fast = array();
-
- /**
- * data
- *
- * @var array
- */
- private $data = array();
-
- /**
- * __construct
- *
- * @return mixed
- */
- public function __construct()
- {
-
- }
-
- public function checkKey($id = 0)
- {
- $id = Maze::input('where_id');
- $key = Maze::input('update_key');
-
- if($key && !$id)
- {
- $info = Maze::load('manage/menu-key', array('where_key' => $key));
- if($info)
- {
- Maze::abert('该菜单唯一key已被使用,请换一个吧');
- }
- }
- elseif($id > 0)
- {
- $info = Maze::load('manage/menu-keyId', array('where_key' => $key, 'where_id' => $id));
- if($info)
- {
- Maze::abert('该菜单唯一key已被使用,请换一个吧');
- }
- }
- }
-
- /**
- * menu 从数据库中得到所有菜单,如果为空,则从项目中读取一次
- *
- * @return array
- */
- private function get($auth = '')
- {
- $menu = Maze::load('manage/menu-main');
-
- if($menu)
- {
- $this->fast = array();
- $child = Maze::load('manage/menu-child');
-
- if($auth && is_array($auth))
- {
- $auth = implode(',', $auth);
- }
- foreach($menu as $k => $v)
- {
- if($auth && strpos($auth, $k . '.') === false)
- {
- unset($menu[$k]);
- continue;
- }
-
- if(isset($child[$v['id']]))
- {
- foreach($child[$v['id']] as $i => $j)
- {
- if($auth && strpos($auth, $i) === false)
- {
- //unset($child[$parent][$i]);
- //continue;
- }
- else
- {
- if($j['link'])
- {
- $j['link'] = Maze::url($j['link'] . '&menu_id=' . $j['id']);
- }
- $menu[$k]['child'][$i] = $j;
-
- if($j['fast'] == 1)
- {
- $this->fast[$i] = $j;
- }
- }
- }
- if(!isset($menu[$k]['child']))
- {
- unset($menu[$k]);
- }
- }
- }
- }
- else
- {
- # 数据为空,读取一次,后续就得手动读取了
- $this->load();
-
- $menu = $this->get();
- }
- return $menu;
- }
-
- /**
- * 清空所有菜单
- *
- * @return array
- */
- public function delete()
- {
- $data = $menu = Maze::load('manage/menu-all');
- foreach($data as $k => $v)
- {
-
- }
- Maze::out('yes');
- }
-
- /**
- * 更新菜单
- *
- * @return array
- */
- public function update()
- {
- $this->load();
-
- Maze::out('yes');
- }
-
- /**
- * 载入从项目中定义好的菜单
- *
- * @return array
- */
- public function load()
- {
- $project = Maze::load('manage/project.get');
- # 这样写是直接调用datbase类 或者 new MazeApp\Manage\Database
- $database = Maze::load('manage/database.');
- $top = Maze::load('manage/top.');
-
- foreach($project as $k => $v)
- {
- $project[$k]['name'] = $v['lang'];
- $config = $database->config($k, $v['path']);
- if($config)
- {
- foreach($config as $i => $j)
- {
- if(isset($j['auth']) && is_array($j['auth']))
- {
- # 加入精细控制权限的菜单
- $j['auth']['name'] = $project[$k]['name'] . '-' . $j['auth']['name'];
- $top_id = $top->update($j['auth']);
- $auth = Maze::load($k . '/' . $i . '-' . $j['auth']['data']);
- if($top_id && $auth)
- {
- foreach($auth as $m => $n)
- {
- $p['key'] = $j['auth']['key'] . '_' . $n['id'];
- $p['name'] = $n[$j['auth']['col']];
- $p['top'] = $top_id;
- $p['value'] = $n['id'];
- $top->update($p);
- }
- }
- }
- if(!isset($j['menu']))
- {
- $j['menu'] = true;
- }
- if(isset($j['manage']) && $j['menu'] != false)
- {
- if(is_string($j['menu']))
- {
- $m = $j['menu'];
- }
- else
- {
- $m = $k;
- }
-
- if($m == $k)
- {
- $key = $k . '.' . $i;
- }
- else
- {
- $key = $m . '.' . $k . '.' . $i;
- }
- $project[$m]['child'][$i]['key'] = $key;
- $project[$m]['child'][$i]['name'] = $j['lang'];
- $project[$m]['child'][$i]['link'] = 'project/database/list?key=' . $k . '&table=' . $i . '&menu=' . $m;
- $project[$m]['child'][$i]['order'] = isset($j['order']) ? $j['order'] : 1;
- $project[$m]['child'][$i]['fast'] = isset($j['fast']) ? $j['fast'] : 2;
- }
- }
- }
- else
- {
- unset($project[$k]);
- }
- }
-
- //print_r($project);die;
-
- # 将所有数据都记录到菜单中
- if($project)
- {
- foreach($project as $k => $v)
- {
- if(!isset($v['child']))
- {
- unset($project[$k]);
- continue;
- }
- if($v && isset($v['lang']) && $v['child'])
- {
- $id = $this->updateMenu($k, $v['lang'], $v['path'] . ':' . $v['lib'], -1, $v['order'], 2);
- if($id > 0)
- {
- foreach($v['child'] as $i => $j)
- {
- $this->updateMenu($j['key'], $j['name'], $j['link'], $id, $j['order'], $j['fast']);
- }
- }
- }
- }
- }
-
- return $project;
- }
-
- /**
- * 更新菜单到数据库
- *
- * @return array
- */
- public function updateMenu($key, $name, $link, $menu, $order, $fast)
- {
- $info = Maze::load('manage/menu-key', array('where_key' => $key));
- if(!$info)
- {
- $update['add_key'] = $key;
- $update['add_name'] = $name;
- $update['add_link'] = $link;
- $update['add_menu_id'] = $menu;
- $update['add_reorder'] = $order;
- $update['add_fast'] = $fast;
- $update['add_state'] = 1;
- $update['add_type'] = 2;
- $info['id'] = Maze::load('manage/menu-insert', $update);
- }
- /*
- elseif($link != $info['link'])
- {
- $update['set_link'] = $link;
- $id = $update['where_id'] = $info['id'];
- Maze::load('manage/menu-update', $update);
- }
- */
-
- return $info['id'];
- }
- /**
- * menu 生成左侧的菜单 此处应加上权限来显示是否管理员有该菜单的权限
- *
- * @return array
- */
- public function left($state = false)
- {
- if($this->data)
- {
- return $this->data;
- }
-
- $auth = '';
- if($state == false)
- {
- # 需要验证权限
- $auth = Maze::load('manage/auth.admin');
- }
- //$auth = 'manage_admin';
- $menu = $this->get($auth);
-
- $this->data = $menu;
-
- return $menu;
- /*
- //$auth = 'manage_admin,image_thumb';
-
- foreach($project as $k => $v)
- {
- if($auth && strpos($auth, $k . '_') === false)
- {
- unset($project[$k]);
- continue;
- }
-
- if($auth && strpos($auth, $k . '_' . $i) === false)
- {
- //unset($project[$k]['child'][$i]);
- continue;
- }
-
- if(!isset($v['child']))
- {
- unset($project[$k]);
- continue;
- }
- if($k == 'manage')
- {
- $project[$k]['child']['manage_project'] = array
- (
- 'name' => '管理菜单设置',
- 'link' => Maze::load('manage/menu.link#project'),
- 'order' => 1,
- );
- $project[$k]['child']['manage_log'] = array
- (
- 'name' => '管理操作日志',
- 'link' => Maze::load('manage/menu.link#log'),
- 'order' => 1,
- );
- //print_r($project[$k]);die;
- }
- //array_multisort($v['child_order'], SORT_DESC, $v['child']);
- $order = array();
- foreach($project[$k]['child'] as $i => $j)
- {
- $order[] = $j['order'];
- }
- array_multisort($order, SORT_DESC, $project[$k]['child']);
- }
-
- //此处加缓存吧,不过要针对用户,加到session中也可以
- return $project;
- */
- }
-
- /**
- * menu 获取快捷管理菜单
- *
- * @return array
- */
- public function fast()
- {
- $this->left();
- return $this->fast;
- }
- /**
- * menu 对一些固定的菜单,生成其链接
- *
- * @return array
- */
- public function link()
- {
- return array
- (
- 'project' => Maze::url('project/list?menu=manage&table=manage_project'),
- 'log' => Maze::url('log/list?menu=manage&table=manage_log'),
- 'logout' => Maze::url('auth.quit'),
- 'index' => Maze::url(''),
- );
- }
- /**
- * database
- *
- * @return array
- */
- public function database()
- {
- $this->config();
- $result = array();
- $method = $this->project . '/' . $this->table . '-list';
- $data = Maze::load($method);
- //print_r($data);die;
- //print_r(Helper::page("current"));die;
- if($data)
- {
- foreach($data as $k => $v)
- {
- $result[$k] = '<tr>';
- foreach($this->config['manage']['list'] as $ki => $vi)
- {
- if(isset($v[$vi['col']]) && $v[$vi['col']])
- {
- if(isset($vi['value']))
- {
- $eval = '$value = ' . str_replace('{col}', $v[$vi['col']], $vi['value']) . ';';
- eval($eval);
- if($value) $v[$vi['col']] = $value;
- }
- elseif(isset($vi['modal']))
- {
- $v[$vi['col']] = '<a href="#" data-toggle="modal" data-target="#maze_modal" onclick="$(\'#maze_modal_body\').html($(this).next().html())">' . $vi['modal'] . '</a><div style="display:none;">' . $v[$vi['col']] . '</div>';
- }
- elseif(isset($vi['option']))
- {
- $v[$vi['col']] = $vi['option'][$v[$vi['col']]];
- }
- $result[$k] .= '<td>' . $v[$vi['col']] . '</td>';
- }
- elseif($vi['col'] == 'manage' && isset($vi['value']) && is_array($vi['value']))
- {
- $result[$k] .= '<td>';
- foreach($vi['value'] as $kj => $vj)
- {
- $result[$k] .= '<a href="' . $this->url($kj, $v['id']) . '" class="oper_' . $kj . '">' . $vj . '</a>';
- }
- $result[$k] .= '</td>';
-
- }
- }
- $result[$k] .= '</tr>';
- }
- }
- return $result;
- }
- }
|