Exemplo n.º 1
0
 /**
  * Edits the model, runs validation and renders the edit form.
  *
  * @throws CHttpException
  * @throws Exception
  */
 public function editModel()
 {
     $this->assetManager->registerScriptFile('js/oeadmin/edit.js');
     $errors = array();
     if (Yii::app()->request->isPostRequest) {
         $post = Yii::app()->request->getPost($this->modelName);
         if (empty($post)) {
             $this->modelName = str_replace("\\", "_", $this->modelName);
             $post = $_POST[$this->modelName];
         }
         if (array_key_exists('id', $post) && $post['id']) {
             $this->model->attributes = $post;
         } else {
             $this->model = new $this->modelName();
             $this->model->attributes = $post;
         }
         if (!$this->model->validate()) {
             $errors = $this->model->getErrors();
         } else {
             if (!$this->model->save()) {
                 throw new CHttpException(500, 'Unable to save ' . $this->modelName . ': ' . print_r($this->model->getErrors(), true));
             }
             $this->audit('edit', $this->model->id);
             $return = '/' . $this->controller->uniqueid . '/list';
             if (Yii::app()->request->getPost('returnUriEdit')) {
                 $return = urldecode(Yii::app()->request->getPost('returnUriEdit'));
             }
             $this->controller->redirect($return);
         }
     } else {
         $defaults = Yii::app()->request->getParam('default', array());
         foreach ($defaults as $key => $defaultValue) {
             if ($this->model->hasAttribute($key)) {
                 $this->model->{$key} = $defaultValue;
             }
         }
     }
     $this->render($this->editTemplate, array('admin' => $this, 'errors' => $errors));
 }