Exemplo n.º 1
0
 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;
 }