示例#1
0
 /**
  * Updates an existing AcAcEspec model.
  * For ajax request will return json object
  * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $request = Yii::$app->request;
     $model = $this->findModel($id);
     $model_nuevo = $model;
     $model->fecha_inicio = date_format(date_create($model->fecha_inicio), 'd/m/Y');
     $model->fecha_fin = date_format(date_create($model->fecha_fin), 'd/m/Y');
     $unidades_ejecutoras = ArrayHelper::map(UnidadEjecutora::find()->all(), 'id', 'nombre');
     $verificar = ArrayHelper::map(AcEspUej::find()->where('id_ac_esp= :id', ['id' => $model->id])->andwhere(['estatus' => 1])->all(), 'id', 'id_ue');
     if ($request->isAjax) {
         /*
          *   Process for ajax request
          */
         Yii::$app->response->format = Response::FORMAT_JSON;
         if ($request->isGet) {
             return ['title' => "Modificando Accion Especifica #" . $id, 'content' => $this->renderAjax('_form', ['model' => $model, 'unidades_ejecutoras' => $unidades_ejecutoras, 'precarga' => $verificar]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])];
         } else {
             if ($model->load($request->post())) {
                 $uni_eje = $request->post('id_ue');
                 $i = 0;
                 $id_ac_esp = $request->post('AcEspUej');
                 $connection = \Yii::$app->db;
                 $transaction = $connection->beginTransaction();
                 try {
                     if ($model->save()) {
                         $salvar = $model->uejecutoras($uni_eje);
                         $transaction->commit();
                         // buscando las unidades ejecutoras relacionadas para mostrarla en la vista
                         $unidad_ejecutora = new AcEspUej();
                         $ue = $unidad_ejecutora->obtener_uej_relacionadas($model->id);
                         return ['forceReload' => 'true', 'contenedorUrl' => Url::to(['ac-ac-espec/index', 'ac_centralizada' => $model->id_ac_centr]), 'title' => "Accion Especifica #" . $id, 'content' => $this->renderPartial('view', ['model' => $model, 'rows' => $ue]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::a('Edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])];
                     } else {
                         //si falla la actulización padre
                         return ['title' => "Update AcAcEspec #" . $id, 'content' => $this->renderAjax('_form', ['model' => $model, 'unidades_ejecutoras' => $unidades_ejecutoras, 'precarga' => $verificar]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])];
                     }
                 } catch (Exception $e) {
                     $transaction->rollback();
                 }
             } else {
                 return ['title' => "Update AcAcEspec #" . $id, 'content' => $this->renderPartial('update', ['model' => $this->findModel($id)]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])];
             }
         }
     } else {
         /*
          *   Process for non-ajax request
          */
         if ($model->load($request->post()) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('update', ['model' => $model]);
         }
     }
 }