Beispiel #1
0
 /**
  * @inheritdoc
  * */
 public function actionUpdate($id = null)
 {
     if ($id) {
         $modelObject = $this->findRecordModel($id);
     } else {
         $modelObject = Yii::createObject($this->model);
     }
     /** @var \nagser\gallery\models\GalleryRecord $modelObject * */
     if (\Yii::$app->request->isAjax) {
         if ($modelObject->load(\Yii::$app->request->post())) {
             \Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($modelObject);
         } else {
             return $this->renderAjax('update', ['model' => $modelObject]);
         }
     } else {
         if (Yii::$app->request->isPost) {
             $modelObject->file = Yii::$app->gallery->upload($modelObject);
             if ($modelObject->upload() and $modelObject->scenario = 'save' and $modelObject->load(\Yii::$app->request->post()) and $modelObject->save()) {
                 $this->redirect(Url::to(['view', 'id' => $modelObject->id]));
             }
         }
         return $this->render('update', ['model' => $modelObject]);
     }
 }
Beispiel #2
0
 public function actionCopy($id)
 {
     $model = \Yii::createObject($this->model);
     /** @var ThemesRecord $model **/
     $model = $model->findRecordModel($id);
     if (\Yii::$app->request->isAjax) {
         if ($model->load(\Yii::$app->request->post())) {
             \Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         } else {
             return $this->renderAjax('copy', ['model' => $model]);
         }
     } else {
         if ($model->load(\Yii::$app->request->post())) {
             $model->copy($id);
             $this->redirect(Url::to(['view', 'id' => $model->dir]));
         }
     }
 }
 /**
  * Create a new User model. If creation is successful, the browser will
  * be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     /** @var \amnah\yii2\user\models\User $user */
     /** @var \amnah\yii2\user\models\Profile $profile */
     /** @var \amnah\yii2\user\models\Role $role */
     // AuthAssigment
     $permissoes = AuthItem::getListToDropDownList();
     $permissoesUser = null;
     // set up new user/profile objects
     $user = $this->module->model("User", ["scenario" => "register"]);
     $profile = $this->module->model("Profile");
     $mensagem = "";
     //Informa ao usuário mensagens de erro na view
     //Permissão do usuário
     $authAssignment = new AuthAssignment();
     $user->status = 1;
     // load post data
     $post = Yii::$app->request->post();
     if ($user->load($post)) {
         // ensure profile data gets loaded
         $profile->load($post);
         //Inicia a transação:
         $transaction = \Yii::$app->db->beginTransaction();
         try {
             // validate for ajax request
             if (Yii::$app->request->isAjax) {
                 Yii::$app->response->format = Response::FORMAT_JSON;
                 return ActiveForm::validate($user, $profile);
             }
             // validate for normal request
             if ($user->validate() && $profile->validate()) {
                 $itensInseridos = true;
                 // perform registration
                 $role = $this->module->model("Role");
                 if (!$user->setRegisterAttributes($role::ROLE_USER, $user::STATUS_ACTIVE)->save()) {
                     $mensagem = "Não foi possível salvar os dados";
                     $transaction->rollBack();
                     //desfaz alterações no BD
                     $itensInseridos = false;
                 }
                 if (!$profile->setUser($user->id)->save()) {
                     $mensagem = "Não foi possível salvar os dados";
                     $transaction->rollBack();
                     //desfaz alterações no BD
                     $itensInseridos = false;
                 }
                 $idUser = $user->id;
                 if (isset($post['AuthAssignment']['item_name']) && !empty($post['AuthAssignment']['item_name'])) {
                     //Guarda as permissões escolhidas
                     $roles = $post['AuthAssignment']['item_name'];
                     foreach ($roles as $role) {
                         $user->setPermissoes($role, $idUser);
                     }
                 }
                 if ($itensInseridos) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $user->id]);
                 }
             }
         } catch (\Exception $exception) {
             $transaction->rollBack();
             $mensagem = "Ocorreu uma falha inesperada ao tentar salvar";
         }
     }
     return $this->render("create", compact("user", "profile", "permissoes", "permissoesUser", "mensagem", "authAssignment"));
 }