Exemplo n.º 1
0
 /**
  * Updates an existing AcEspUej 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, $accion_centralizada)
 {
     $request = Yii::$app->request;
     $unidades_ejecutoras = ArrayHelper::map(UnidadEjecutora::find()->all(), 'id', 'nombre');
     $verificar = ArrayHelper::map(AcEspUej::find()->where('id_ac_esp= :id', ['id' => $id])->all(), 'id', 'id_ue');
     $model = new AcEspUej();
     if ($request->isAjax) {
         /*
          *   Process for ajax request
          */
         Yii::$app->response->format = Response::FORMAT_JSON;
         if ($request->isGet) {
             if (count($verificar) > 0) {
                 return ['title' => "Editar Unidades Ejecutoras", 'content' => $this->renderAjax('_form', ['model' => $model, 'accion_central' => $accion_centralizada, 'accion_especifica' => $id, 'unidades_ejecutoras' => $unidades_ejecutoras, 'precarga' => $verificar]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Editar', ['class' => 'btn btn-primary', 'type' => "submit"])];
                 exit;
             }
             /* return [
                    'title'=> "Update AcEspUej #".$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 {
             if ($model->load($request->post())) {
                 //se cuenta las unidades ejecutoras seleccionadas y se van guardando de una a una
                 $uni_eje = $request->post('id_ue');
                 $i = 0;
                 $id_ac_esp = $request->post('AcEspUej');
                 $connection = \Yii::$app->db;
                 $transaction = $connection->beginTransaction();
                 try {
                     AcEspUej::deleteAll("id_ac_esp='" . $id_ac_esp['id_ac_esp'] . "'");
                     while (count($request->post('id_ue')) != $i) {
                         $model = new AcEspUej();
                         $model->id_ue = $uni_eje[$i];
                         $model->id_ac_esp = $id_ac_esp['id_ac_esp'];
                         $i++;
                         $model->isNewRecord = true;
                         $model->id = NULL;
                         if ($model->save()) {
                         } else {
                             $transaction->rollback();
                             $i = count($request->post('id_ue'));
                             //print_r($model->errors);
                         }
                     }
                     $transaction->commit();
                     return ['forceReload' => 'false', 'title' => "Editando Unidades Ejecutoras", 'contenedorId' => '#especifica-pjax', 'contenedorUrl' => Url::to(['ac-ac-espec/index', 'ac_centralizada' => $request->post('accion_central')]), 'content' => '<span class="text-success">Se Edito Correctamente</span>', 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])];
                 } catch (Exception $e) {
                     $transaction->rollback();
                 }
             } else {
                 return ['title' => "Update AcEspUej #" . $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]);
         }
     }
 }
Exemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getIdUnidadEjecutora()
 {
     return $this->hasOne(UnidadEjecutora::className(), ['id' => 'id_unidad_ejecutora']);
 }