Ejemplo n.º 1
0
 /**
  * Creates a new Hotel model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Hotel();
     $modelHotelData = new HotelData();
     $languages = Lang::getAll();
     $roomTypes = RoomType::getRoomTypes();
     $countries = Country::getAllCountries();
     $savePath = Yii::getAlias($model::FILE_PATH);
     $department = Department::getAll();
     $modelHotelDepartmentMapping = new HotelDepartmentMapping();
     $modelHotelRoomTypeMapping = new HotelRoomTypeMapping();
     if ($model->load(Yii::$app->request->post())) {
         //            $modelCity = City::getOrCreateByNameAndCountryId($_POST['Hotel']['cityName'], $model->country_id);
         //            $model->city_id = $modelCity->id;
         if ($model->save()) {
             foreach ($_POST['HotelRoomTypeMapping']['room_type_id'] as $roomTypeId) {
                 $modelHotelRoomTypeMapping = new HotelRoomTypeMapping();
                 $modelHotelRoomTypeMapping->setAttribute('hotel_id', $model->getAttribute('id'));
                 $modelHotelRoomTypeMapping->setAttribute('room_type_id', $roomTypeId);
                 $modelHotelRoomTypeMapping->save();
             }
             foreach ($_POST['HotelDepartmentMapping']['department_id'] as $departmentId) {
                 $modelHotelDepartmentMapping = new HotelDepartmentMapping();
                 $modelHotelDepartmentMapping->setAttribute('hotel_id', $model->getAttribute('id'));
                 $modelHotelDepartmentMapping->setAttribute('department_id', $departmentId);
                 $modelHotelDepartmentMapping->save();
             }
             foreach ($languages as $key => $language) {
                 $modelHotelData = new HotelData();
                 $modelHotelData->setAttribute('hotel_id', $model->id);
                 $modelHotelData->setAttribute('lang_id', Yii::$app->request->post('HotelData')['lang_id'][$key]);
                 $modelHotelData->setAttribute('description', Yii::$app->request->post('HotelData')['description'][$key]);
                 $modelHotelData->setAttribute('location', Yii::$app->request->post('HotelData')['location'][$key]);
                 $modelHotelData->setAttribute('price_information', Yii::$app->request->post('HotelData')['price_information'][$key]);
                 $modelHotelData->setAttribute('other', Yii::$app->request->post('HotelData')['other'][$key]);
                 $modelHotelData->save();
             }
             $files = $model->loadFiles($model->fileAttribute);
             if (count($files) !== 0) {
                 foreach ($files as $file) {
                     if ($file->saveAs($savePath . $file->name)) {
                         $image = new Image();
                         $image->setAttribute('name', $file->name);
                         $image->setAttribute('hotel_id', $model->id);
                         $image->setAttribute('path', $model::FILE_PATH . $file->name);
                         $image->save();
                     }
                 }
             }
             $model->setAttribute('preview_image_id', !empty($image) ? $image->id : $model::IMAGE_NOT_SET);
             $model->save();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     //        if ($model->load(Yii::$app->request->post()) && $model->save()) {
     //            return $this->redirect(['view', 'id' => $model->id]);
     //        } else {
     //
     //        }
     return $this->render('create', ['model' => $model, 'modelHotelData' => $modelHotelData, 'countries' => $countries, 'roomTypes' => $roomTypes, 'modelHotelRoomTypeMapping' => $modelHotelRoomTypeMapping, 'department' => $department, 'modelHotelDepartmentMapping' => $modelHotelDepartmentMapping, 'languages' => $languages]);
 }