/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new RolesMenus();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['RolesMenus'])) {
         $model->attributes = $_POST['RolesMenus'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
Example #2
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $aRolesDB = Roles::model()->findAll('application_id=' . BE);
     $aRoles = array();
     $aRoleID = array();
     //all roles of system
     $aSelectedRoles = array();
     foreach ($aRolesDB as $item) {
         $aRoles[$item['id']] = $item['role_name'];
         $aRoleID[] = $item['id'];
     }
     $roleMenusExist = RolesMenus::model()->findAll('menu_id="' . $model->id . '"');
     $aRoleMenuIDExist = array();
     $aRoleMenuActionExist = array();
     foreach ($roleMenusExist as $roleMenusExistItem) {
         $aRoleMenuIDExist[] = $roleMenusExistItem['role_id'];
         $aRoleMenuActionExist[$roleMenusExistItem['role_id']] = $roleMenusExistItem['actions'];
         $aSelectedRoles[] = $roleMenusExistItem['role_id'];
     }
     //
     if (isset($_POST['Menus']) && isset($_POST['actions'])) {
         //Check controller name, action name - PDQuang
         //                    MyDebug::output($_POST['Menus']);MyDebug::output($_POST['actions']);
         //                    if($_POST['Menus']['module_name']==null)
         //                    {
         //                        $checkController = ControllerActionsName::checkControllerActionsExist($_POST['Menus']['controller_name'], $_POST['actions']);
         //                    }
         //                    else {
         //                        $checkController = ControllerActionsName::checkControllerActionsExist($_POST['Menus']['controller_name'], $_POST['actions'], $_POST['Menus']['module_name']);
         //                    }
         //
         //                    if(!$checkController)
         //                    {
         //                        Yii::log('Controller, Module or Actions is wrong!');
         //                        throw new CHttpException('Controller, Module or Actions is wrong!');
         //                    }
         $model->attributes = $_POST['Menus'];
         $aSelectedRoles = array();
         $aRolesInput = array();
         if (isset($_POST['roles'])) {
             $i = 0;
             foreach ($_POST['roles'] as $roleID) {
                 $aRolesInput[] = $roleID;
                 $aSelectedRoles[] = $roleID;
                 $aRoleMenuActionExist[$roleID] = $_POST['actions'][$i];
                 $i++;
             }
         }
         if ($model->save()) {
             //RolesMenus::model()->deleteAll('menu_id="'.$model->id.'"');
             //check to delete if exitt or insert if it doesn't exist
             $index = 0;
             foreach ($aRoleID as $iRoleID) {
                 if (in_array($iRoleID, $aRoleMenuIDExist) && !in_array($iRoleID, $aRolesInput)) {
                     RolesMenus::model()->deleteAll('menu_id="' . $model->id . '" AND role_id="' . $iRoleID . '"');
                 }
                 if (in_array($iRoleID, $aRolesInput)) {
                     if (!in_array($iRoleID, $aRoleMenuIDExist)) {
                         $mRolesMenus = new RolesMenus();
                         $mRolesMenus->role_id = $iRoleID;
                         $mRolesMenus->menu_id = $model->id;
                         $mRolesMenus->actions = $_POST['actions'][$index];
                         $mRolesMenus->save();
                     } else {
                         $mRolesMenus = RolesMenus::model()->find('role_id=' . $iRoleID . ' AND menu_id=' . $model->id);
                         $mRolesMenus->actions = $_POST['actions'][$index];
                         $mRolesMenus->save();
                     }
                 }
                 $index++;
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     /*echo "<pre>";
       print_r($aRoleMenuActionExist);
       echo "</pre>";
       exit;*/
     $this->render('update', array('model' => $model, 'aRoles' => $aRoles, 'aSelectedRoles' => $aSelectedRoles, 'aRoleMenuActionExist' => $aRoleMenuActionExist, 'actions' => $this->listActionsCanAccess));
 }