public static function getCitiesForRequirements($requirementIds)
 {
     $criteria = new CDbCriteria();
     $criteria->addInCondition('requirement_id', $requirementIds);
     $cities = RequirementCities::model()->findAll($criteria);
     $result = false;
     foreach ($cities as $city) {
         $result[$city->requirement_id][] = $city->city_id;
     }
     return $result;
 }
 public function actionPost()
 {
     Yii::beginProfile('requirement_post');
     $userId = Yii::app()->user->id;
     $model = new Requirement();
     $modelState = new GeoState();
     $requirementCities = new RequirementCities();
     $requirementAmenities = new RequirementAmenities();
     $requirementPropertyTypes = new RequirementPropertyTypes();
     $requirementBedrooms = new RequirementBedrooms();
     $amenities = null;
     $propertyTypes = null;
     $bedrooms = null;
     $cities = null;
     $this->performAjaxValidation(array($model));
     if (isset($_POST['minsubmit'])) {
         $modelState->attributes = $_POST['GeoState'];
     } elseif (isset($_POST['PostRequirementMin'])) {
         $model->attributes = $_POST['Requirement'];
         if (isset($_POST['GeoCity']['city'])) {
             $cities[0] = $_POST['GeoCity']['city'];
         }
     } elseif (isset($_POST['submit'])) {
         $model->attributes = $_POST['Requirement'];
         $amenities = isset($_POST['amenity_id']) ? $_POST['amenity_id'] : null;
         $propertyTypes = isset($_POST['property_type_id']) ? $_POST['property_type_id'] : null;
         $bedrooms = isset($_POST['bedrooms']) ? $_POST['bedrooms'] : null;
         $cities = isset($_POST['city_id']) ? $_POST['city_id'] : null;
         $valid = true;
         $model->user_id = $userId;
         $valid = $valid && $model->validate();
         if ($propertyTypes == null) {
             $requirementPropertyTypes->addError('property_type_id', 'Property Type cannot be blank.');
             $valid = false;
         }
         if ($cities == null || empty($cities) || $cities[0] == '') {
             $requirementCities->addError('city_id', 'City cannot be blank.');
             $valid = false;
         }
         if ($valid) {
             $data = $_POST['Requirement'];
             if ($data['covered_area_from'] == null) {
                 $data['covered_area_from'] == 0;
             }
             if ($data['min_price'] == null) {
                 $data['min_price'] = 0;
             }
             $requirement = RequirementApi::createRequirement($userId, $data);
             if (!$requirement->hasErrors()) {
                 $addAmenities = true;
                 $addCities = true;
                 $addBedrooms = true;
                 $addPropertyTypes = true;
                 if (isset($_POST['amenity_id'])) {
                     $addAmenities = RequirementAmenitiesApi::addAmenitiesForRequirement($requirement->id, $_POST['amenity_id']);
                 }
                 if (isset($_POST['city_id'])) {
                     $addCities = RequirementCitiesApi::addCitiesForRequirement($requirement->id, $_POST['city_id']);
                 }
                 if (isset($_POST['bedrooms']) && !empty($_POST['bedrooms'])) {
                     $addBedrooms = RequirementBedroomsApi::addBedroomForRequirement($requirement->id, $_POST['bedrooms']);
                 }
                 if (isset($_POST['property_type_id'])) {
                     $addPropertyTypes = RequirementPropertyTypesApi::addPropertyTypesForRequirement($requirement->id, $_POST['property_type_id']);
                 }
                 if ($addAmenities && $addCities && $addBedrooms && $addPropertyTypes) {
                     //$this->refresh();
                     $this->redirect('/requirement/' . $requirement->id);
                 } else {
                     RequirementAmenitiesApi::deleteAllAmenitiesForRequirement($requirement->id);
                     RequirementCitiesApi::deleteAllCitiesForRequirement($requirement->id);
                     RequirementBedroomsApi::deleteAllBedroomsForRequirement($requirement->id);
                     RequirementPropertyTypesApi::deleteAllPropertyTypesForRequirement($requirement->id);
                 }
             } else {
                 if (isset($property->id)) {
                     PropertyApi::deletePropertyById($property->id);
                 }
             }
         }
     }
     $this->render('post', array('model' => $model, 'modelState' => $modelState, 'requirementCities' => $requirementCities, 'requirementAmenities' => $requirementAmenities, 'requirementPropertyTypes' => $requirementPropertyTypes, 'requirementBedrooms' => $requirementBedrooms, 'amenities' => $amenities, 'propertyTypes' => $propertyTypes, 'bedrooms' => $bedrooms, 'cities' => $cities));
     Yii::endProfile('requirement_post');
 }