Ejemplo n.º 1
0
 public function actionPost($projectId = null)
 {
     Yii::beginProfile('property_post');
     $locationPosition = array(20.59368, 78.96288);
     // Open session object
     $session = new CHttpSession();
     $session->open();
     /*		$modelProject = "";
     		 if($projectId){
     			$modelProject = ProjectApi::getProjectById($projectId);
     			if($modelProject==null){
     			$this->redirect('/search');
     			}
     			}*/
     $userId = Yii::app()->user->id;
     $model = new Property();
     $model->country = 'india';
     $modelCity = new GeoCity();
     $propertyAmenities = new PropertyAmenities();
     $propertyImages = new PropertyImages();
     $amenities = null;
     $localityNew = false;
     $locality = null;
     $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['minsubmit'])) {
         $model->attributes = $_POST['Property'];
         //$modelCity->attributes=$_POST['GeoCity'];
     } elseif (isset($_POST['PostPropertyMin'])) {
         $model->attributes = $_POST['Property'];
     } elseif (isset($_POST['submit'])) {
         $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'];
         }
         $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;
                 }
             }
         }
         $model->setScenario('custom-validate');
         $valid = $valid && $model->validate();
         if ($model->latitude != '' && $model->longitude != '') {
             $locationPosition = array($model->latitude, $model->longitude);
         }
         if ($valid) {
             if (isset($model->video_url)) {
                 $video = explode('&', $model->video_url);
                 $videocode = explode('=', $video[0]);
                 if (isset($videocode[1])) {
                     $data['video_url'] = $videocode[1];
                 }
             }
             $data['property_name'] = PropertyTypesApi::getPropertyTypeById($data['property_type_id']);
             if ($data['jackpot_investment']) {
                 $data['jackpot_investment'] = 2;
             }
             if ($data['instant_home']) {
                 $data['instant_home'] = 2;
             }
             $property = PropertyApi::createProperty($userId, $data);
             if (!$property->hasErrors()) {
                 if (!empty($amenity_id) && count($amenity_id) > 0) {
                     $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']);
                     if ($data['instant_home'] == 2 && $data['jackpot_investment'] == 2) {
                         Yii::app()->user->setFlash('success', 'Congratulations! Your property was successfully posted, waiting for Jackpot Investment and Instant Home Approval.');
                     } elseif ($data['jackpot_investment'] == 2) {
                         Yii::app()->user->setFlash('success', 'Congratulations! Your property was successfully posted, waiting for Jackpot Investment Approval.');
                     } elseif ($data['instant_home'] == 2) {
                         Yii::app()->user->setFlash('success', 'Congratulations! Your property was successfully posted, waiting for Instant Home Approval.');
                     } else {
                         Yii::app()->user->setFlash('success', 'Congratulations! Your property was successfully posted.');
                     }
                     $this->redirect('/property/' . $property->id);
                 } else {
                     if ($localityNew && !$localityNew->isNewRecord) {
                         $localityNew->delete();
                     }
                     unset($session['PropertyImages']);
                     $propertyPostUrl = Yii::app()->createUrl('/property/post');
                     Yii::app()->user->setFlash('error', 'Oops! We faced a problem and hence could not post your property.Please <a href=' . $propertyPostUrl . '>retry </a>');
                     PropertyAmenitiesApi::deleteAllAmenitiesForProperty($property->id);
                     PropertyImagesApi::deleteAllImages($property->id);
                     PropertyApi::deletePropertyById($property->id);
                 }
             }
         } else {
             if ($localityNew && !$localityNew->isNewRecord) {
                 $localityNew->delete();
             }
             unset($session['PropertyImages']);
             $propertyPostUrl = Yii::app()->createUrl('/property/post');
             //	var_dump($model->getErrors());
             Yii::app()->user->setFlash('error', 'Oops! We faced a problem and hence could not post your property.Please <a href=' . $propertyPostUrl . '>retry </a>');
         }
     }
     $this->render('post', array('model' => $model, 'propertyAmenities' => $propertyAmenities, 'propertyImages' => $propertyImages, 'amenities' => $amenities, 'locationPosition' => $locationPosition, 'localityList' => $localityList, 'locality' => $locality));
     Yii::endProfile('property_post');
 }