/** * Get a list of offers to be added or remove. Write the offers to the database. * @param boolean $archive * @param string &$log * @param array &$count_arr * @return null|int|array * @throws Exception */ private function GetOffersPartial($archive, &$log, &$count_arr) { Errors::LogSynchroStep('WebServiceVirgo - GetOffersPartial() start...'); if (!$this->WS()) { Errors::LogSynchroStep('WebServiceVirgo - NO WEBSERVICE'); return null; } try { $log .= "archive=" . (int) $archive . "\n"; $time_start = microtime_float(); if ($this->_sid == "") { return; } $params = array('sid' => $this->_sid); if ($archive) { $result = $this->WS()->getSC()->__soapCall("GetOffersArchive", array($params)); $buf = $result->GetOffersArchiveResult->OffersZip; $status = $result->GetOffersArchiveResult->Status; $msg = $result->GetOffersArchiveResult->Message; } else { $result = $this->WS()->getSC()->__soapCall("GetOffers", array($params)); $buf = $result->GetOffersResult->OffersZip; $status = $result->GetOffersResult->Status; $msg = $result->GetOffersResult->Message; } $time_end = microtime_float(); $time = $time_end - $time_start; if ($this->_DEBUG) { echo "SOAP Call execution time: {$time} seconds<br>"; } if ($status != 0) { throw new Exception($msg); } $zip_file = $archive ? self::TMP_ZIP_ARCH_FILE : self::TMP_ZIP_FILE; $f = fopen($zip_file, "w"); fwrite($f, $buf); fclose($f); $time_end2 = microtime_float(); $time = $time_end2 - $time_end; if ($this->_DEBUG) { echo "Save ZIP execution time: {$time} seconds<br>"; } //unzip XML file with offers $contents = ""; $zip = new ZipArchive(); if ($zip->open($zip_file)) { $fp = $zip->getStream('xml.xml'); if (!$fp) { exit("failed reading xml file (" . getcwd() . "), probably invalid permissions to folder\n"); } $contents = ''; while (!feof($fp)) { $contents .= fread($fp, 1024); } fclose($fp); $zip->close(); $xml_file = $archive ? self::TMP_XML_OFE_ARCH_FILE : self::TMP_XML_OFE_FILE; file_put_contents($xml_file, $contents); if (file_exists($zip_file)) { unlink($zip_file); } } $time_end3 = microtime_float(); $time = $time_end3 - $time_end2; if ($this->_DEBUG) { echo "Save XML execution time: {$time} seconds<br>"; } $times = array("read_props" => 0, "save" => 0, "del_props" => 0, "rooms" => 0, "del_offers" => 0, "rooms_del" => 0, "photos" => 0); $prevOfferId = "0"; $content = file_get_contents($xml_file); //$content = preg_replace("/<UwagiOpis>([^\<\>]*)\<\/UwagiOpis>/m", "<UwagiOpis><![CDATA[$1]]></UwagiOpis>", $content); //$content = preg_replace("/<UwagiNieruchomosc>([^\<\>]*)\<\/UwagiNieruchomosc>/m", "<UwagiNieruchomosc><![CDATA[$1]]></UwagiNieruchomosc>", $content); $fp = fopen($xml_file, 'w'); fwrite($fp, $content); fclose($fp); //open and read XML file $xml2 = new XMLReader(); $xml2->open($xml_file); $domdoc = new DOMDocument(); $ids_do_usuniecia_dodania = array(); $all_agents_ids = array(); $all_dept_ids = array(); $blokuj_agentow = true; $blokuj_oddzialy = true; $time_end4 = microtime_float(); $time = $time_end4 - $time_end3; if ($this->_DEBUG) { echo "Load XML execution time: {$time} seconds<br>"; } $xml2->read(); while ($xml2->name) { //Departments if ($xml2->name == "Oddzial") { if ($blokuj_oddzialy) { $blokuj_oddzialy = false; } $node = simplexml_import_dom($domdoc->importNode($xml2->expand(), true)); if (count($node) > 0) { $log .= "oddzial=" . $node["ID"] . " - " . $node->Nazwa . "\n"; $dep = new Department($node["ID"], $node->Nazwa, $node->Nazwa2, $node->Adres, $node->Miasto, $node->Kod, $node->Nip, $node->Wojewodztwo, $node->Www, $node->Telefon, $node->Email, $node->Fax, $node->Uwagi, $node->Naglowek, $node->Stopka, $node->PlikLogo, $node->ZdjecieWWW, $node->Subdomena, $node->Firma); array_push($all_dept_ids, (int) $node["ID"]); Departments::AddEditDepartment($dep); echo DataBase::GetDbInstance()->LastError(); } } //Agents if ($xml2->name == "Agent") { if ($blokuj_agentow) { $blokuj_agentow = false; } $node = simplexml_import_dom($domdoc->importNode($xml2->expand(), true)); if (count($node) > 0) { $log .= "agent=" . $node["ID"] . " - " . $node->Nazwa . "\n"; $kod_pracownika = 0; if (is_numeric($node->KodPracownika)) { $kod_pracownika = (int) $node->KodPracownika; } $agent = new Agent($node["ID"], $node->Nazwa, $node->Telefon, $node->Komorka, $node->Email, $node->Oddzial, $node->JabberLogin, $node->NrLicencji, $node->OdpowiedzialnyNazwa, $node->OdpowiedzialnyNrLicencji, $node->Komunikator, $node->PlikFoto, $kod_pracownika, $node->DzialFunkcja); array_push($all_agents_ids, (int) $node["ID"]); Agents::AddEditAgent($agent); echo DataBase::GetDbInstance()->LastError(); } } //Offers if ($xml2->name == "Oferty") { $node = simplexml_import_dom($domdoc->importNode($xml2->expand(), true)); if (count($node) > 0) { foreach ($node->children() as $nodeOferta) { $count_arr["suma"]++; $log .= "oferta=" . $nodeOferta["ID"] . " - " . $nodeOferta["Symbol"] . "\n"; //read major properties $rent = strtolower($nodeOferta["Wynajem"]) == "true" ? 1 : 0; $orig = strtolower($nodeOferta["Pierwotny"]) == "true" ? 1 : 0; $przedmiot = $nodeOferta["Przedmiot"]; if ($przedmiot == "Biurowiec") { $przedmiot = "Obiekt"; } $first_page = strtolower($nodeOferta->PierwszaStrona) == "true" ? 1 : 0; $zamiana = $nodeOferta->Zamiana ? 1 : 0; $loc_as_commune = strtolower($nodeOferta["LokalizacjaJakoGmina"]) == "true" ? 1 : 0; $has_swfs = 0; $has_movs = 0; $has_maps = 0; $has_projs = 0; $has_pans = 0; $has_photos = 0; if (isset($nodeOferta->Zdjecia)) { foreach ($nodeOferta->Zdjecia->children() as $zd) { switch ($zd->typ) { case "Zdjecie": $has_photos = 1; break; case "Rzut": $has_projs = 1; break; case "Mapa": $has_maps = 1; break; case "SWF": $has_swfs = 1; break; case "Filmy": $has_movs = 1; break; case "Panorama": $has_pans = 1; break; } } } $attr_arr = array("Link" => null, "ZeroProwizji" => 0); if (isset($nodeOferta->Atrybuty)) { foreach ($nodeOferta->Atrybuty->children() as $at) { if ($at["opis"] == "Link") { $attr_arr["Link"] = (string) $at; } if ($at["opis"] == "ZeroProwizji") { $attr_arr["ZeroProwizji"] = (string) $at; } } } $offer = new Offer($nodeOferta["Jezyk"], CheckNumeric($nodeOferta["ID"]), $nodeOferta["Status"], $przedmiot, $rent, $nodeOferta["Symbol"], $orig, $nodeOferta["Wojewodztwo"], $nodeOferta["Powiat"], $nodeOferta["Lokalizacja"], $nodeOferta["Dzielnica"], $nodeOferta["Rejon"], $nodeOferta["Ulica"], $nodeOferta["Pietro"], CheckNumeric($nodeOferta["Cena"]), CheckNumeric($nodeOferta["CenaM2"]), $nodeOferta["IloscPokoi"], CheckNumeric($nodeOferta["PowierzchniaCalkowita"]), CheckNumeric($nodeOferta["MapSzerokoscGeogr"]), CheckNumeric($nodeOferta["MapDlugoscGeogr"]), $nodeOferta["TechnologiaBudowlana"], $nodeOferta["MaterialKonstrukcyjny"], $nodeOferta["StanWybudowania"], $nodeOferta["RodzajBudynku"], $nodeOferta["Agent"], $nodeOferta["DataWprowadzenia"], $nodeOferta["DataWprowadzenia"], 0, empty_to_null($nodeOferta->Kraj), $nodeOferta->IloscPieter, $nodeOferta->RokBudowy, empty_to_null($nodeOferta->RodzajDomu), $first_page, empty_to_null($nodeOferta->RodzajObiektu), empty_to_null($nodeOferta->SposobPrzyjecia), $nodeOferta->IloscOdslonWWW, null, empty_to_null($nodeOferta->StatusWlasnosci), empty_to_null($nodeOferta->UmeblowanieLista), $nodeOferta->PowierzchniaDzialki, $zamiana, empty_to_null(html_entity_decode($nodeOferta->UwagiOpis)), empty_to_null(html_entity_decode($nodeOferta->UwagiNieruchomosc)), empty_to_null($attr_arr["Link"]), $attr_arr["ZeroProwizji"], $nodeOferta["DataWaznosci"], $has_swfs, $has_movs, $has_photos, $has_pans, $has_maps, $has_projs, $loc_as_commune); $photosNode = null; $roomsNode = null; $modDate = null; $attributesNode = null; $ts = microtime_float(); //properties that are in offers directly $pomin = OffersHelper::$props_arr; //atributes that are in offers directly $pomin_attr = array("Link", "ZeroProwizji"); //read other properties foreach ($nodeOferta->children() as $propNode) { $pname = $propNode->getName(); if ($pname == "StanPrawnyDom" || $pname == "StanPrawnyGruntu" || $pname == "StanPrawnyLokal" || $pname == "StanPrawnyLokalLista") { $offer->setStanPrawny($propNode); } if (in_array($pname, $pomin) === false) { if ($pname == "Zdjecia") { $photosNode = $propNode; } else { if ($pname == "DataAktualizacji") { $modDate = $propNode; } else { if ($pname == "Pomieszczenia") { $roomsNode = $propNode; } else { if ($pname == "Atrybuty") { $attributesNode = $propNode; $set = array(); foreach ($propNode->children() as $listNode) { if (array_search($listNode['opis'], $pomin_attr) === false) { $set[count($set)] = $listNode['opis'] . "#|#" . $listNode; } } $offer->__set($pname, $set); } else { if ($propNode['iset'] == true) { $set = array(); foreach ($propNode->children() as $listNode) { $set[count($set)] = $listNode; } $offer->__set($pname, $set); } else { $offer->__set($pname, $propNode); } } } } } } } if ($nodeOferta['NrLokalu']) { $offer->__set('NrLokalu', $nodeOferta['NrLokalu']); } $times["read_props"] += microtime_float() - $ts; $ts = microtime_float(); //save offer object to database if ($modDate != null) { $offer->SetModificationDate($modDate); } $ret = Offers::AddEditOffer($offer); if ($ret == "A") { $count_arr["dodane"]++; } else { if ($ret == "E") { $count_arr["zmodyfikowane"]++; } } echo DataBase::GetDbInstance()->LastError(); $times["save"] += microtime_float() - $ts; $ts = microtime_float(); //delete unuse properties from offer $addedProperties = array(); foreach ($nodeOferta->children() as $propNode) { $pname = $propNode->getName(); if ($pname == "StanPrawnyDom" || $pname == "StanPrawnyGruntu" || $pname == "StanPrawnyLokal" || $pname == "StanPrawnyLokalLista") { $pname = "StanPrawny"; } if (in_array($pname, $pomin) === false) { if ($pname != "Zdjecia" && $pname != "Pomieszczenia" && $pname != "Atrybuty" && $pname != "DataAktualizacji" || $propNode['iset'] == true) { $prop = Properties::GetPropertyName($pname); if ($prop != null) { $addedProperties[count($addedProperties)] = $prop->GetID(); } } } } if ($nodeOferta['NrLokalu']) { $addedProperties[] = Properties::GetPropertyName('NrLokalu')->GetID(); } if ($attributesNode != null) { $addedProperties[] = Properties::GetPropertyName($attributesNode->getName())->GetID(); } Offers::DeleteUnUseProperties($offer->GetId(), $offer->GetIdLng(), $addedProperties); $times["del_props"] += microtime_float() - $ts; $ts = microtime_float(); //photos $addedPhotos = array(); if ($photosNode != null) { foreach ($photosNode->children() as $photoNode) { $intro = strtolower($photoNode->intro) == "true" ? 1 : 0; $photo = new OfferPhoto($photoNode['ID'], $offer->GetId(), null, $photoNode->plik, $photoNode->opis, $photoNode->lp, $photoNode->typ, $intro, $photoNode['fotoID'], (string) $photoNode->LinkFilmYouTube, (string) $photoNode->LinkMiniaturkaYouTube); OfferPhotos::AddEditPhoto($photo); echo DataBase::GetDbInstance()->LastError(); $addedPhotos[count($addedPhotos)] = $photo->GetId(); } } OfferPhotos::DeleteUnUsePhotos($offer->GetId(), $addedPhotos, 0); $times["photos"] += microtime_float() - $ts; $ts = microtime_float(); //rooms if ($roomsNode != null) { if ($prevOfferId != $offer->GetId() . "") { OfferRooms::DeleteRooms($offer->GetId(), null); } $times["rooms_del"] += microtime_float() - $ts; foreach ($roomsNode->children() as $roomNode) { $room = new OfferRoom(0, $offer->GetId(), $offer->GetIdLng(), $roomNode['Rodzaj'], $roomNode->Lp, $roomNode->Powierzchnia, $roomNode->Poziom, $roomNode->Typ, CheckNumeric($roomNode->Wysokosc), $roomNode->RodzajKuchni, CheckNumeric($roomNode->Ilosc), $roomNode->Glazura, $roomNode->WidokZOkna, $roomNode->Opis, $roomNode->StanPodlogi, $roomNode->RodzajPomieszczenia); //sets of properties $_floors = array(); if ($roomNode->Podlogi) { foreach ($roomNode->Podlogi->children() as $listNode) { $_floors[count($_floors)] = $listNode; } } $room->SetFloors($_floors); $_windowsExhibition = array(); if ($roomNode->WystawaOkien) { foreach ($roomNode->WystawaOkien->children() as $listNode) { $_windowsExhibition[count($_windowsExhibition)] = $listNode; } } $room->SetWindowsExhibition($_windowsExhibition); $_walls = array(); if ($roomNode->Sciany) { foreach ($roomNode->Sciany->children() as $listNode) { $_walls[count($_walls)] = $listNode; } } $room->SetWalls($_walls); $_equipment = array(); if ($roomNode->Wyposazenie) { foreach ($roomNode->Wyposazenie->children() as $listNode) { $_equipment[count($_equipment)] = $listNode; } } $room->SetEquipment($_equipment); OfferRooms::AddRoom($room); echo DataBase::GetDbInstance()->LastError(); } } $times["rooms"] += microtime_float() - $ts; $prevOfferId = $offer->GetId() . ""; } } } //Deleted offers if ($xml2->name == "Usuniete") { $node = simplexml_import_dom($domdoc->importNode($xml2->expand(), true)); foreach ($node->children() as $doUsuniecia) { array_push($ids_do_usuniecia_dodania, (int) $doUsuniecia["ID"]); } } $xml2->read(); } //Delete redundant departments if (!$blokuj_oddzialy) { Departments::DeleteRedundantDepartments($all_dept_ids); } //Delete redundant agents if (!$blokuj_agentow) { Agents::DeleteRedundantAgents($all_agents_ids); } $ts = microtime_float(); $time_end5 = microtime_float(); $time = $time_end5 - $time_end4; $times["del_offers"] += microtime_float() - $ts; if ($this->_DEBUG) { echo "Saving data to db execution time: {$time} seconds<br>"; var_dump($times); } $xml2->close(); Errors::LogSynchroStep('WebServiceVirgo - GetOffersPartial() done'); return $ids_do_usuniecia_dodania; } catch (Exception $ex) { Errors::LogError("WebServiceVirgo:GetOffers", $ex->getMessage() . "; " . $ex->getTraceAsString()); return 0; } }