createQuery() ->addWhere('estate_id = ?', $this->getId()) ->addWhere('is_main = ?', true) ->fetchOne(); if(!$main) { $main = Doctrine::getTable('Image')->createQuery() ->addWhere('estate_id = ?', $this->getId()) ->fetchOne(); if($main) { $main->setIsMain(true); $main->save(); } } return $main; } public function hasSubCategory() { return!is_null($this->getSubCategoryId()); } public function hasBuildingType() { return!is_null($this->getBuildingTypeId()); } public function hasBuildingCondition() { return!is_null($this->getBuildingConditionId()); } public function hasObjectType() { return!is_null($this->getObjectTypeId()); } public function hasOwnership() { return!is_null($this->getOwnershipId()); } public function hasUsableArea() { return $this->getUsableArea() > 0; } public function hasEstateArea() { return $this->getEstateArea() > 0; } public function hasFloorNumber() { return $this->getFloorNumber() > 0; } public function hasBrokerUser() { return!is_null($this->getBrokerUserId()); } /** * * @return array */ public function getProperties() { $result = array(); if ($this->hasSubCategory()) { $result[] = ' ' . $this->getSubCategory()->getName(); } if ($this->hasBuildingType()) { $result[] = ' ' . $this->getBuildingType()->getName(); } if ($this->hasBuildingCondition()) { $result[] = ' ' . $this->getBuildingCondition()->getName(); } if ($this->hasObjectType()) { $result[] = ' ' . $this->getObjectType()->getName(); } if ($this->hasOwnership()) { $result[] = ' ' . $this->getOwnership()->getName(); } if ($this->hasUsableArea()) { $result[] = ' ' . $this->getUsableArea() . ' m2'; } if ($this->hasEstateArea()) { $result[] = ' ' . $this->getEstateArea() . ' m2'; } if ($this->hasFloorNumber()) { $result[] = ' ' . $this->getFloorNumber(); } $result[] = ' ' . $this->getCity(); return $result; } protected function getSrealityTypeId($category_id) { return $category_id - 2; } protected function getSrealitySubTypeId() { switch($this->getCategoryId()) { case 4:// Rodinny dum return 37; // Rodinny dum break; case 6:// Kanceláře return 25; // break; case 7:// Obchodní prostory return 28; // break; case 8:// Skladové prostory return 26; // break; case 9:// Ostatni return 36; // break; case 10:// Pozemky - komerce return 18; // break; default: return (int) $this->getSubCategoryId(); } } public function getSrealityData() { $result = array( 'advert_function' => (int) $this->getTypeId(), 'advert_lifetime' => 4, // 90 dni 'advert_price' => (double) $this->getPrice(), 'advert_price_currency' => 1, //CZK 'advert_price_unit' => (int) $this->getPriceUnitId(), 'advert_type' => (int) $this->getSrealityTypeId($this->getCategoryId()), 'advert_subtype' => (int) $this->getSrealitySubTypeId(), 'description' => trim(saCommon::decodeEntity(strip_tags($this->getDescription()))), 'locality_city' => $this->getCity(), 'locality_street' => $this->getStreet(), 'locality_inaccuracy_level' => 1, 'estate_area' => (int) $this->getEstateArea(), 'usable_area' => (int) $this->getUsableArea(), 'floor_number' => (int) $this->getFloorNumber(), 'ownership' => (int) $this->getOwnershipId(), 'building_type' => (int) $this->getBuildingTypeId(), 'building_condition' => (int) $this->getBuildingConditionId(), 'object_type' => (int) $this->getObjectTypeId(), 'balcony' => false, 'loggia' => false, 'cellar' => false, 'terrace' => false, 'garage' => false, 'parking_lots' => false, 'user_status' => true, //'seller_id' => 17951, ); if(strlen($this->getBrokerUser()->getSrealityId()) > 0) { $result['seller_id'] = (int) $this->getBrokerUser()->getSrealityId(); } else { throw new Exception('Makléř nemá nastaveno Sreality ID.'); } if (strlen($this->getSrealityId())) { $result['advert_id'] = (int) $this->getSrealityId(); } // var_dump($result); // die(); return $result; } public function importToSreality() { $sreality = new saSreality(); $sreality_id = $sreality->saveAdvert($this->getSrealityData()); $this->setSrealityId($sreality_id); } public function deleteToSreality() { if (strlen($this->getSrealityId())) { $sreality = new saSreality(); $sreality->delAdvert((int) $this->getSrealityId()); $this->setSrealityId(null); } } public function preSave($event) { try { $uir = new saUIR(); $region_code = $uir->getRegionCodeByDistrict($this->getDistrict()); $district_code = $uir->getDistrictCode($this->getDistrict()); $city_code = $uir->getCityCode($this->getCity(), $district_code); $street_code = $uir->getStreetCode($this->getStreet(), $city_code); $this->setStreetCode($street_code); $this->setCityCode($city_code); $this->setRegionCode($region_code); $this->setDistrictCode($district_code); } catch (Exception $exc) { sfContext::getInstance()->getUser()->addMessage($exc->getMessage()); } if ($this->getSynchSreality()) { try { $this->importToSreality(); } catch (Exception $exc) { sfContext::getInstance()->getUser()->addMessage($exc->getMessage()); } if (strlen($this->getSrealityId())) { foreach ($this->getImages() as $image) { $image->importToSreality($this->getSrealityId()); } } } else { $this->deleteToSreality(); } if(!$this->getSynchReals()) { $this->deleteToReals(); } parent::preSave($event); } protected $importRealsFlag = true; public function importToReals() { if($this->importRealsFlag) { try { // var_dump($this->getRealsData()); $RealsRequest = new RealsRequest(sfConfig::get('app_reals_login'), sfConfig::get('app_reals_password')); $RealsRequest->Send(($this->isNew() || strlen($this->getRealsId() == 0)) ? 'AddProperty' : 'EditProperty', $this->getRealsData()); $Result = $RealsRequest->ResultAsArray(); if(isset($Result['result']['output_data']) && strlen($Result['result']['output_data']) > 0) { $this->setRealsId($Result['result']['output_data']); $this->importRealsFlag = false; $this->save(); } else { // echo '
';
//                var_dump($Result);
//                echo '
'; // die(); throw new Exception('Nepodařilo se odeslat data do Reals. '.$Result['result']["description"]); } } catch (Exception $exc) { sfContext::getInstance()->getUser()->addMessage($exc->getMessage()); } } } public function postSave($event) { if($this->getSynchReals()) { try { $this->importToReals(); } catch(Exception $e) { sfContext::getInstance()->getUser()->addMessage($e->getMessage()); } } // die(); parent::postSave($event); } public function preDelete($event) { if ($this->getSynchSreality()) { try { $this->deleteToSreality(); } catch (Exception $exc) { sfContext::getInstance()->getUser()->addMessage($exc->getMessage()); } } if ($this->getSynchReals()) { $this->deleteToReals(); } parent::preDelete($event); } protected function deleteToReals() { if (strlen($this->getRealsId())) { $RealsRequest = new RealsRequest(sfConfig::get('app_reals_login'), sfConfig::get('app_reals_password')); $RealsRequest->Send('DeleteProperty', array('id' => $this->getRealsId())); $Result = $RealsRequest->ResultAsArray(); $this->setRealsId(null); } } protected function getRealsPropertyTypeId() { return 1; } protected function getRealsData() { $data = array( 'order_number' => $this->getId(), 'order_type' => $this->getType()->getRealsId(), 'property_type' => $this->getRealsPropertyTypeId(), 'building_type' => $this->getBuildingType()->getRealsId(), 'holding' => 0, 'partnership' => 1, 'exclusive' => 1, 'status' => 1, 'visibility' => 1, 'title' => $this->getName(), 'locality' => (int) $this->getRegionCode(), 'district' => (int) $this->getDistrictCode(), 'city' => (int) $this->getCityCode(), //'ward' => 55841, 'street' => (int) $this->getStreetCode(), 'price' => $this->getPrice(), 'price_unit' => $this->getPriceUnit()->getRealsId(), 'currency' => 0, 'price_note' => '', 'description' => trim(saCommon::decodeEntity(strip_tags($this->getDescription()))), 'area' => $this->getUsableArea(), 'disposition' => '', 'accessories' => '', 'note' => '' ); if(strlen($this->getRealsId())) { $data['id'] = $this->getRealsId(); } $images = array(); foreach($this->getImages() as $image) { $images[] = array('file' => 'http://'.$_SERVER['HTTP_HOST'].$image->getSrc(Image::TYPE_BIG)); } $data['pictures'] = $images; return $data; } }