public function actionUpdate($id, $imageId = '') { // var_dump($_GET);die(); Yii::beginProfile('property_update'); $model = Property::model()->findByPk($id); if (!$model) { throw new CHttpException(404, 'The requested page does not exist.'); } $jackpot = $model->jackpot_investment; $instant = $model->instant_home; $currentImages = PropertyImagesApi::getAllImages($model->id); $localitymodel = GeoLocality::model()->find('id=:id', array(':id' => $model->locality_id)); $locality = $localitymodel->locality; if ($imageId && is_array($currentImages)) { $imageResult = false; if (array_key_exists($imageId, $currentImages)) { $imageResult = PropertyImagesApi::deleteImageByPk($imageId); } $this->renderPartial('_imageDeleted', array('result' => $imageResult)); Yii::app()->end(); } if ($model->user_id != Yii::app()->user->id) { throw new CHttpException(503, 'Unauthorized.'); } // Open session object $session = new CHttpSession(); $session->open(); $userId = Yii::app()->user->id; $model->country = 'india'; $modelCity = new GeoCity(); $propertyAmenities = new PropertyAmenities(); $propertyImages = new PropertyImages(); $amenities = PropertyAmenitiesApi::getAmenitiesIdForProperty($id); $localityNew = false; if ($model->latitude && $model->longitude) { $locationPosition = array($model->latitude, $model->longitude); } else { $locationPosition = array(20.59368, 78.96288); } $localityList = GeoLocalityApi::getAllNameList(); $this->performAjaxValidation($model); //$this->performAjaxValidation($profilesModel); if (isset($_POST['ajax']) && $_POST['ajax'] === 'property-form') { echo CActiveForm::validate($model); //echo CActiveForm::validate($profilesModel); Yii::app()->end(); } if (isset($_POST['submit'])) { $amenity_id = array(); $model->attributes = $_POST['Property']; $amenity_id = array(); if (isset($_POST['PropertyAmenitiesHouse']) && isset($_POST['PropertyAmenitiesExternal'])) { $amenity_id = array_merge($_POST['PropertyAmenitiesHouse'], $_POST['PropertyAmenitiesExternal']); } elseif (isset($_POST['PropertyAmenitiesHouse'])) { $amenity_id = $_POST['PropertyAmenitiesHouse']; } elseif (isset($_POST['PropertyAmenitiesExternal'])) { $amenity_id = $_POST['PropertyAmenitiesExternal']; } // var_dump($amenity_id);die(); $model->user_id = $userId; $valid = true; $data = $_POST['Property']; if (isset($model->city_id)) { $cityModel = GeoCity::model()->findByPk($model->city_id); if ($cityModel) { $model->state_id = $cityModel->state_id; $data['state_id'] = $cityModel->state_id; if (isset($model->locality)) { $criteria = new CDbCriteria(); $criteria->condition = 'city_id=:city_id && locality=:locality'; $criteria->params = array(':city_id' => $cityModel->id, ':locality' => $model->locality); $localityModel = GeoLocality::model()->find($criteria); $locality = $model->locality; if ($localityModel) { $model->locality_id = $localityModel->id; } else { $localityModel = new GeoLocality(); $localityModel->locality = $model->locality; $localityModel->city_id = $model->city_id; $localityModel->save(); $model->locality_id = $localityModel->id; $localityNew = $localityModel; } $data['locality_id'] = $localityModel->id; } } } $valid = $valid && $model->validate(); if ($model->latitude != '' && $model->longitude != '') { $locationPosition = array($model->latitude, $model->longitude); } if (!isset($data['property_type_id'])) { $data['property_type_id'] = $model->property_type_id; } if ($valid) { $data['property_name'] = PropertyTypesApi::getPropertyTypeById($data['property_type_id']); if ($data['jackpot_investment'] != $jackpot) { $data['jackpot_investment'] = 2; } if ($data['instant_home'] != $instant) { $data['instant_home'] = 2; } if (isset($model->video_url)) { $video = explode('&', $model->video_url); $videocode = explode('=', $video[0]); if (isset($videocode[1])) { $data['video_url'] = $videocode[1]; } } $property = PropertyApi::updatePropertyById($model->id, $data); if (!$property->hasErrors()) { if (count($amenity_id) > 0) { PropertyAmenitiesApi::deleteAllAmenitiesForProperty($property->id); $amenities = PropertyAmenitiesApi::addAmenitiesForProperty($property->id, $amenity_id); } $images = $session['PropertyImages']; if (is_array($images)) { foreach ($images as $count => $image) { $valid = $valid && PropertyImagesApi::addImage($property->id, $image) === true; } } if ($valid) { unset($session['PropertyImages']); Yii::app()->user->setFlash('success', 'Great! You have succesfully updated your property.'); $this->redirect('/property/' . $property->id); } else { if ($localityNew && !$localityNew->isNewRecord) { $localityNew->delete(); } unset($session['PropertyImages']); $propertyUpdateUrl = Yii::app()->createUrl('/property/update/' . $property->id . ''); Yii::app()->user->setFlash('error', 'Oops! We faced a problem and hence could not update your property.Please <a href=' . $propertyUpdateUrl . '>retry </a>'); } } } else { if ($localityNew && !$localityNew->isNewRecord) { $localityNew->delete(); } unset($session['PropertyImages']); // var_dump($model->getErrors()); Yii::app()->user->setFlash('error', 'There was an error while updating the property.'); } } $this->render('update', array('model' => $model, 'propertyAmenities' => $propertyAmenities, 'propertyImages' => $propertyImages, 'amenities' => $amenities, 'currentImages' => $currentImages, 'locationPosition' => $locationPosition, 'localityList' => $localityList, 'locality' => $locality)); Yii::endProfile('property_update'); }