/**
  * Creates a new UePartidaEntidad model.
  * For ajax request will return json object
  * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $request = Yii::$app->request;
     $model = new UePartidaEntidad();
     $ue = ArrayHelper::map(UnidadEjecutora::find()->asArray()->all(), 'id', 'nombre');
     $tipo_entidad = ArrayHelper::map(TipoEntidad::find()->asArray()->all(), 'id', 'nombre');
     if (!$request->isAjax) {
         /*
          *   Process for ajax request
          */
         Yii::$app->response->format = Response::FORMAT_JSON;
         if ($request->isGet) {
             return ['title' => "Create new UePartidaEntidad", 'content' => $this->renderAjax('create', ['model' => $model, 'ue' => $ue, 'tipo_entidad' => $tipo_entidad]), '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()) && $model->save()) {
                 return ['forceReload' => '#crud-datatable-pjax', 'title' => "Create new UePartidaEntidad", 'content' => '<span class="text-success">Create UePartidaEntidad success</span>', 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])];
             } else {
                 return ['title' => "Create new UePartidaEntidad", 'content' => $this->renderAjax('create', ['model' => $model, 'ue' => $ue, 'tipo_entidad' => $tipo_entidad]), '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('create', ['model' => $model, 'ue' => $ue, 'tipo_entidad' => $tipo_entidad]);
         }
     }
 }
Beispiel #2
0
 public function UejEntidad($id_uej, $entidad)
 {
     /*
     Vaciar Si viene null
     */
     if ($id_uej == null) {
         $id_uej = '';
     }
     /*
     Query para buscar si quitaron una unidad si trae algo hay q borrarlas
     */
     $ace = UePartidaEntidad::find()->select('ue_partida_entidad.id')->where(['cuenta' => $this->cuenta, 'partida' => $this->partida, 'id_tipo_entidad' => $entidad])->andWhere(['not in', 'id_ue', $id_uej])->asArray()->all();
     /*
     Si encontró algo, son las unidades que deben ser eliminadas
     */
     if ($ace != null) {
         foreach ($ace as $key => $value) {
             $model_cambiar = UePartidaEntidad::findOne($value);
             $model_cambiar->delete();
         }
     }
     /*
     Ya se borraron ahora query para buscar si agregaron una unidad nueva,
     si es asi almacenar y guardar
     */
     $ace = UePartidaEntidad::find()->select('id_ue')->where(['cuenta' => $this->cuenta, 'partida' => $this->partida, 'id_tipo_entidad' => $entidad])->andWhere(['in', 'id_ue', $id_uej])->asArray()->all();
     /*
     Declaro arreglo donde se guardará los nuevos elementos agregados
     */
     foreach ($ace as $key => $value) {
         if (isset($value['id_ue'])) {
             $tabla[] = $value['id_ue'];
         }
     }
     //si viene null, lo coloco vacio
     if ($id_uej == null) {
         $id_uej = [];
     }
     if (!isset($tabla)) {
         $tabla = [];
     }
     /*
     Guardo en $nuevo los elementos nuevos que se han agregado.
     */
     $nuevo = array_diff($id_uej, $tabla);
     foreach ($nuevo as $key => $value) {
         $model_uej = new UePartidaEntidad();
         $model_uej->id_ue = $value;
         $model_uej->cuenta = $this->cuenta;
         $model_uej->partida = $this->partida;
         $model_uej->id_tipo_entidad = $entidad;
         $model_uej->save();
     }
 }