/** * Updates an existing Hotel model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { //$this->allowAccess(Hotel::checkAccess($id)); $model = $this->findModel($id); $languages = Lang::getAll(); $roomTypes = RoomType::getRoomTypes(); $department = Department::getAll(); $modelHotelDepartmentMapping = new HotelDepartmentMapping(); $modelHotelData = HotelData::getByHotelId($id); if (!$modelHotelData) { $modelHotelData = new HotelData(); } $availableRooms = HotelRoomTypeMapping::getRoomTypesIdByHotelId($id); $hotelDepartments = HotelDepartmentMapping::getByHotelId($id); $modelHotelRoomTypeMapping = new HotelRoomTypeMapping(); $countries = Country::getAllCountries(); $savePath = Yii::getAlias($model::FILE_PATH); $imageDataProvider = new ArrayDataProvider(['models' => Image::getImagesByHotelId($id)]); 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) { $hotelRoomTypeMappings = HotelRoomTypeMapping::getRoomTypeByHotelIdAndRoomTypeId($id, $roomTypeId); if (!$hotelRoomTypeMappings) { $modelHotelRoomTypeMapping = new HotelRoomTypeMapping(); $modelHotelRoomTypeMapping->setAttribute('hotel_id', $model->getAttribute('id')); $modelHotelRoomTypeMapping->setAttribute('room_type_id', $roomTypeId); $modelHotelRoomTypeMapping->save(); } elseif ($hotelRoomTypeMappings->getAttribute('deleted') === ActiveRecord::STATUS_DELETED) { $hotelRoomTypeMappings->setAttribute('deleted', ActiveRecord::STATUS_NOT_DELETED); $hotelRoomTypeMappings->save(); } } foreach ($languages as $key => $language) { $modelHotelData = HotelData::getByLanguageIdAndHotelId($language->local, $id); if ($modelHotelData) { $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(); } else { $modelHotelData = new HotelData(); $modelHotelData->setAttribute('hotel_id', $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(); } } foreach ($availableRooms as $room) { if (!in_array($room, $_POST['HotelRoomTypeMapping']['room_type_id'])) { $hotelRoomTypeMapping = HotelRoomTypeMapping::getRoomTypeByHotelIdAndRoomTypeId($id, $room); $hotelRoomTypeMapping->setAttribute('deleted', ActiveRecord::STATUS_DELETED); $hotelRoomTypeMapping->save(); } } foreach ($_POST['HotelDepartmentMapping']['department_id'] as $departmentId) { $hotelDepartmentMapping = HotelDepartmentMapping::getByHotelIdAndDepartmentId($id, $departmentId); if (!$hotelDepartmentMapping) { $modelHotelDepartmentMapping = new HotelDepartmentMapping(); $modelHotelDepartmentMapping->setAttribute('hotel_id', $model->getAttribute('id')); $modelHotelDepartmentMapping->setAttribute('department_id', $departmentId); $modelHotelDepartmentMapping->save(); } elseif ($hotelDepartmentMapping->getAttribute('deleted') === ActiveRecord::STATUS_DELETED) { $hotelDepartmentMapping->setAttribute('deleted', ActiveRecord::STATUS_NOT_DELETED); $hotelDepartmentMapping->save(); } } foreach ($hotelDepartments as $department) { if (!in_array($department, $_POST['HotelDepartmentMapping']['department_id'])) { $modelHotelDepartmentMapping = HotelDepartmentMapping::getByHotelIdAndDepartmentId($id, $department); $modelHotelDepartmentMapping->setAttribute('deleted', ActiveRecord::STATUS_DELETED); $modelHotelDepartmentMapping->save(); } } $files = $model->loadFiles($model->fileAttribute); if ($files !== false) { foreach ($files as $file) { if ($file->saveAs($savePath . $file->name)) { $image = new Image(); $image->setAttribute('name', $file->name); $image->setAttribute('hotel_id', $id); $image->setAttribute('path', $model::FILE_PATH . $file->name); $image->save(); } } } if ($model->preview_image_id === 0 || $model->preview_image_id === null) { $model->setAttribute('preview_image_id', !empty($image) ? $image->id : $model::IMAGE_NOT_SET); $model->save(); } return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('update', ['model' => $model, 'modelHotelData' => $modelHotelData, 'modelHotelRoomTypeMapping' => $modelHotelRoomTypeMapping, 'roomTypes' => $roomTypes, 'countries' => $countries, 'availableRooms' => $availableRooms, 'hotelDepartments' => $hotelDepartments, 'imageDataProvider' => $imageDataProvider, 'department' => $department, 'modelHotelDepartmentMapping' => $modelHotelDepartmentMapping, 'languages' => $languages]); }
<!-- --><? //= $form->field($model, 'phone')->textInput(['maxlength' => 25]) ?> <!----> <!-- --><? //= $form->field($model, 'fax')->textInput(['maxlength' => 25]) ?> <!----> <!-- --><? //= $form->field($model, 'country_id')->dropDownList(ArrayHelper::map($countries, 'id', 'name')) ?> <!----> <!-- --><? //= $form->field($model, 'cityName')->textInput(['value' => ($model->isNewRecord) ? '' : $model->city->name]) ?> <!-- --><? //= $form->field($model, 'zip')->textInput() ?> <!----> <!-- --><? //= $form->field($model, 'address')->textInput(['maxlength' => 255]) ?> <? foreach ($languages as $key => $language) { $modelHotelData = \common\models\HotelData::getByLanguageIdAndHotelId($language->local, $model->id); $items[] = [ 'label' => $language->name, 'content' => $this->render('items/_hotelData', [ 'modelHotelData' => ($modelHotelData) ?: new \common\models\HotelData(), 'language' => $language, 'uniqid' => uniqid(), 'form' => $form ]) ]; } ?> <?php echo TabsX::widget(['items' => $items, 'position' => TabsX::POS_ABOVE, 'encodeLabels' => false]); ?>