浏览 1831 次
锁定老帖子 主题:生成后台管理菜单 admin_menu 类
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-05-05
<?php /** * 管理菜单 * */ class App_Helper_Admin_Menu { const quuid = 'q'; const qargs = 'args'; const qtitle = 'title'; const qtype = 'type'; const qparent = 'parent'; const qchildren = 'children'; const qactive = 'active'; /** * 顶级菜单标识 */ const qparent_top = '#ROOT#'; /** * 茎节点 */ const qtype_stem = 'stem'; /** * 叶节点 */ const qtype_leaf = 'leaf'; private function __construct(){ $this->init(); } /** * @return App_Helper_Admin_Menu */ static function getInstance(){ static $it = false; if (!$it){ $it = new self(); } return $it; } /** * 返回生成的 菜单 * * @return array */ private function init(){ $qid = Core_AppUtils::get(Core_Mvc_Router::queryAccessor); $active = false; $catalog = $this->aclFilter($this->loadData()); do { $item = Core_AppUtils::val($catalog,$qid,null); if (empty($item)) break; $active = $catalog[$qid][self::qactive] = true; if (self::qparent_top == $item[self::qparent]) break; $qid = $item[self::qparent]; } while(true); // 转成树 $catalog = (array) Core_AppUtils::array_to_tree($catalog,self::quuid,self::qparent,self::qchildren); // 节点过滤,规范 茎叶类型 $catalog = $this->nodeFilter($catalog); $this->catalog = $this->locateActiveItem($catalog); Core_AppUtils::dump($this->catalog); } private function loadData(){ $catalog = array(); $catalog[] = array( 'q' => 'admin.application.index', 'args' => null, 'title' => '系统设置', 'parent' => '#ROOT#', 'type' => 'stem', ); $catalog[] = array( 'q' => 'admin.user.list', 'args' => null, 'title' => '用户列表', 'parent' => 'admin.application.index', 'type' => 'stem', ); $catalog[] = array( 'q' => 'admin.user.create', 'args' => null, 'title' => '用户创建', 'parent' => 'admin.application.index', 'type' => 'leaf', ); $catalog[] = array( 'q' => 'admin.user.rolelist', 'args' => null, 'title' => '用户角色列表', 'parent' => 'admin.user.list', 'type' => 'leaf', ); $catalog[] = array( 'q' => 'admin.application.logout', 'args' => null, 'title' => '注销登录', 'parent' => '#ROOT#', 'type' => 'leaf', ); return Core_AppUtils::array_to_hashmap($catalog,self::quuid) ; } private function locateActiveItem(array $items){ $activeItem = null; foreach ($items as $offset => $item){ if (Core_AppUtils::val($item,self::qactive,false)){ $activeItem = & $items[$offset]; break; } } if (empty($activeItem)) { $activeItem = & $items[0]; $activeItem[self::qactive] = true; } if (self::qtype_stem == $activeItem[self::qtype]){ $activeItem[self::qchildren] = $this->locateActiveItem($activeItem[self::qchildren]); } return $items; } private function nodeFilter(array $items){ if (empty($items)) return $items; $data = array(); foreach ($items as $item){ switch ($item[self::qtype]) { case self::qtype_leaf: unset($item[self::qchildren]); break; case self::qtype_stem: if (empty($item[self::qchildren])){ $item[self::qtype] = self::qtype_leaf; unset($item[self::qchildren]); }else { $item[self::qchildren] = $this->nodeFilter($item[self::qchildren]); } break; default: $item = null; } if (!empty($items)) $data[] = $item; } return $data; } private function aclFilter(array $items){ if (empty($items)) return $items; if (Core_Event::isRegistered(Core_Mvc_EventId::dispatching_aclcheck)){ $data = array(); foreach ($items as $uuid => $item){ if (App_Convention::canAccess($uuid) ){ $data[$uuid] = $item; } } return $data; } return $items; } }
生成结果 写道
Array
( [0] => Array ( [q] => admin.application.index [args] => [title] => 系统设置 [parent] => #ROOT# [type] => stem [active] => 1 [children] => Array ( [0] => Array ( [q] => admin.user.list [args] => [title] => 用户列表 [parent] => admin.application.index [type] => stem [children] => Array ( [0] => Array ( [q] => admin.user.rolelist [args] => [title] => 用户角色列表 [parent] => admin.user.list [type] => leaf [active] => 1 ) ) [active] => 1 ) [1] => Array ( [q] => admin.user.create [args] => [title] => 用户创建 [parent] => admin.application.index [type] => leaf ) ) ) [1] => Array ( [q] => admin.application.logout [args] => [title] => 注销登录 [parent] => #ROOT# [type] => leaf ) ) 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |