$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] = '