/**
  * 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 = AuthItem::model()->with('parents.parentItem')->findByPk($id);
     $operationsList = $tasksList = $listModels = array();
     if ($model->type == AuthItem::TYPE_TASK) {
         $operations = AuthItem::model()->findAll('type = :type', array(':type' => AuthItem::TYPE_OPERATION));
         foreach ($operations as $op) {
             $listModels[$op->name] = $op->description . "({$op->name})";
         }
     } elseif ($model->type == AuthItem::TYPE_ROLE) {
         $tasks = AuthItem::model()->findAll('type = :type', array(':type' => AuthItem::TYPE_TASK));
         foreach ($tasks as $task) {
             $listModels[$task->name] = $task->description . "({$task->name})";
         }
     }
     foreach ($model->parents as $item) {
         if ($item->childItem->type == AuthItem::TYPE_OPERATION) {
             $operationsList[$item->childItem->name] = $item->childItem->description . " ({$item->childItem->name})";
         } elseif ($item->childItem->type == AuthItem::TYPE_TASK) {
             $tasksList[$item->childItem->name] = $item->childItem->description . " ({$item->childItem->name})";
         }
     }
     if (Yii::app()->request->isPostRequest && isset($_POST['AuthItem'])) {
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $model->attributes = $_POST['AuthItem'];
             if ($model->save()) {
                 $items = array();
                 if ($model->type == AuthItem::TYPE_TASK) {
                     $items = Yii::app()->request->getPost('operations', array());
                 } elseif ($model->type == AuthItem::TYPE_ROLE) {
                     $items = Yii::app()->request->getPost('tasks', array());
                 }
                 // удалим и создадим чайлдов заново
                 AuthItemChild::model()->deleteAll('parent = :parent', array(':parent' => $model->name));
                 if (count($items)) {
                     foreach ($items as $name) {
                         $child = new AuthItemChild();
                         $child->setAttributes(array('parent' => $model->name, 'child' => $name));
                         if (!$child->save()) {
                             throw new CException('Ошибка при сохранении связанных объектов!');
                         }
                     }
                 }
                 $transaction->commit();
                 Yii::app()->user->setFlash('success', 'Действие изменено!');
                 $this->redirect(array('update', 'id' => $model->name));
             }
         } catch (Exception $e) {
             Yii::app()->user->setFlash('error', $e->getMessage());
             $transaction->rollback();
         }
     }
     $this->render('update', array('model' => $model, 'operations' => $operationsList, 'tasks' => $tasksList, 'listModels' => $listModels));
 }
 private function updateAuthItemChildren(AuthItem $item)
 {
     $criteria = new CDbCriteria();
     // для операций доступны только операции, для задач - операции и задачи, для ролей - роли, задачи и операции
     $criteria->addInCondition('type', array_slice([AuthItem::TYPE_OPERATION, AuthItem::TYPE_TASK, AuthItem::TYPE_ROLE], 0, $item->type + 1));
     // не может наследовать себя
     $criteria->addNotInCondition('name', [$item->name]);
     $availableChildren = AuthItem::model()->findAll($criteria);
     // названия ролей, которые могут бы потомками
     $availableChildrenName = array_keys(CHtml::listData($availableChildren, 'name', 'description'));
     // уберем те, которые не могут быть потомками
     $children = array_intersect(Yii::app()->getRequest()->getPost('ChildAuthItems', []), $availableChildrenName);
     AuthItemChild::model()->deleteAll('parent = :parent', [':parent' => $item->name]);
     foreach ($children as $name) {
         $child = new AuthItemChild();
         $child->setAttributes(['parent' => $item->name, 'child' => $name]);
         if (!$child->save()) {
             throw new CDbException(Yii::t('RbacModule.rbac', 'There is an error occurred when saving data!'));
         }
     }
 }