/** * 获取菜单 */ public function getmenusAction() { // 请求参数 $request = $this->getRequest()->getParams(); // 请求菜单的层级ID $parent_id = isset($request['parent_id']) ? $request['parent_id'] : 0; $option = isset($request['option']) ? $request['option'] : null; $menu = new Home_Model_Menu(); //echo date('H:i:s').'<br>'; if ($option == 'treedata') { echo Zend_Json::encode($menu->getTreeData($parent_id)); } else { $data = $menu->getMenuData($parent_id); /* echo '<pre>'; print_r($data); exit; */ $json = Zend_Json::encode($data); $patterns[0] = '/"disabled":"0"/'; $patterns[1] = '/"disabled":"1"/'; $patterns[2] = '/"handler":"menuClick"/'; $replacements[2] = '"disabled":0'; $replacements[1] = '"disabled":1'; $replacements[0] = '"handler":menuClick'; // 转换JSON中的数据格式(临时解决办法) echo preg_replace($patterns, $replacements, $json); } //echo date('H:i:s').'<br>'; exit; }
/** * 删除当前菜单及下级菜单的所有当前角色 * @param number $user_id * @param number $role_id */ public function deleteRoleFromMenu($role_id, $menu_id) { $menu = new Home_Model_Menu(); // 检查当前菜单是否存在下级菜单 $m = $menu->fetchAll("parent_id = " . $menu_id); // 当前菜单拥有下级菜单 if ($m->count() > 0) { $data = $m->toArray(); foreach ($data as $d) { // 从下级菜单中清除当前角色 $this->deleteRoleFromMenu($role_id, $d['id']); } } // 从当前菜单清除角色 $this->delete("role_id = " . $role_id . " and menu_id = " . $menu_id); }
public function editmenuAction() { // 返回值数组 $result = array('success' => true, 'info' => '编辑成功'); $request = $this->getRequest()->getParams(); $now = date('Y-m-d H:i:s'); $user_session = new Zend_Session_Namespace('user'); $user = $user_session->user_info['user_id']; $json = json_decode($request['json']); $updated = $json->updated; $inserted = $json->inserted; $deleted = $json->deleted; $menu = new Home_Model_Menu(); $menurole = new Home_Model_Menurole(); if (count($updated) > 0) { foreach ($updated as $val) { if ($menu->fetchAll("id != " . $val->id . " and text = '" . $val->text . "'")->count() > 0) { $result['result'] = false; $result['info'] = '菜单重名'; echo Zend_Json::encode($result); exit; } else { $data = array('parent_id' => $val->parentId, 'text' => $val->text, 'iconCls' => $val->iconCls, 'tooltip' => $val->tooltip, 'handler' => $val->handler, 'url' => $val->url, 'disabled' => $val->disabled, 'params' => $val->params, 'update_time' => $now, 'update_user' => $user); $where = "id = " . $val->id; try { $menu->update($data, $where); } catch (Exception $e) { $result['result'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } } } if (count($inserted) > 0) { foreach ($inserted as $val) { if ($menu->fetchAll("text = '" . $val->text . "'")->count() > 0) { $result['result'] = false; $result['info'] = '菜单重名'; echo Zend_Json::encode($result); exit; } else { $data = array('parent_id' => $val->parentId, 'text' => $val->text, 'iconCls' => $val->iconCls, 'tooltip' => $val->tooltip, 'handler' => $val->handler, 'url' => $val->url, 'disabled' => $val->disabled, 'params' => $val->params, 'create_time' => $now, 'create_user' => $user, 'update_time' => $now, 'update_user' => $user); try { $menu_id = $menu->insert($data); try { $user_role = new Admin_Model_Role(); $menurole->insert(array('menu_id' => $menu_id, 'role_id' => $user_role->getAdminId())); } catch (Exception $e) { $result['result'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } catch (Exception $e) { $result['result'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } } } if (count($deleted) > 0) { foreach ($deleted as $val) { if ($menurole->fetchAll("menu_id = " . $val->id)->count() == 0) { try { $menu->deleteMenuTreeData($val->id); } catch (Exception $e) { $result['result'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } else { $result['result'] = false; $result['info'] = '角色ID' . $val->id . '已使用,不能删除'; echo Zend_Json::encode($result); exit; } } } echo Zend_Json::encode($result); exit; }