/** * Get a list of investments to be added or remove. Write the investments to the database. */ public function GetInvestments() { //echo "start<br>"; if (!$this->WS()) { return null; } try { if ($this->_sid == "") { return; } $params = array('sid' => $this->_sid); $result = $this->WS()->getSC()->__soapCall("GetInvestments", array($params)); if ($result->GetInvestmentsResult->Status != 0) { return $result->GetInvestmentsResult->Message; } $buf = $result->GetInvestmentsResult->OffersZip; $f = fopen(self::TMP_ZIP_FILE, "w"); fwrite($f, $buf); fclose($f); //unzip XML file with offers $zip = new ZipArchive(); if ($zip->open(self::TMP_ZIP_FILE)) { $fp = $zip->getStream('xml.xml'); if (!$fp) { exit("failed reading xml file (" . getcwd() . ")\n"); } $contents = ''; while (!feof($fp)) { $contents .= fread($fp, 2); } fclose($fp); $zip->close(); file_put_contents(self::TMP_XML_INV_FILE, $contents); if (file_exists(self::TMP_ZIP_FILE)) { unlink(self::TMP_ZIP_FILE); } } //open and read XML file $xml = simplexml_load_file(self::TMP_XML_INV_FILE); $investmentsNode = null; //read main nodes foreach ($xml->children() as $child) { //echo "<br />" . $child->getName() . ": " . $child; if ($child->getName() == "Inwestycje") { $investmentsNode = $child; } } //read investments $importedInv = ""; $count = 0; foreach ($investmentsNode->children() as $node) { $count++; $importedInv .= $node["ID"] . ","; //read major properties $garage = $node->Garaz == "True" ? 1 : 0; $pool = $node->Basen == "True" ? 1 : 0; $ter = $node->Taras == "True" ? 1 : 0; $ac = $node->Klimatyzacja == "True" ? 1 : 0; $spec = $node->Specjalna == "True" ? 1 : 0; $proj = $node->Projekt == "True" ? 1 : 0; $investment = new Investment($node["Jezyk"], $node["ID"], CheckNumeric($node["Lp"]), $node->Numer, $node->Nazwa, $node->Opis, $node->OpisSkrot, $node->DaneKontaktowe, $node->MapMar, $garage, $pool, $ter, $ac, $proj, $spec, $node->DataUtworzenia, $node->TerminOddania, CheckNumeric($node->PowierzchniaCalkowita), CheckNumeric($node->KubaturaBrutto), CheckNumeric($node->MetrazOd), CheckNumeric($node->MetrazDo), CheckNumeric($node->CenaOd), CheckNumeric($node->CenaDo), CheckNumeric($node->CenaM2Od), CheckNumeric($node->CenaM2Do), CheckNumeric($node->PietroOd), CheckNumeric($node->PietroDo), CheckNumeric($node->PokojeOd), CheckNumeric($node->PokojeDo), $node->Kraj == "" ? null : $node->Kraj, $node->Wojewodztwo == "" ? null : $node->Wojewodztwo, $node->Powiat == "" ? null : $node->Powiat, $node->Lokalizacja == "" ? null : $node->Lokalizacja, $node->Dzielnica == "" ? null : $node->Dzielnica, $node->Rejon == "" ? null : $node->Rejon, $node->Ulica, $node->Kategoria, $node["Oddzial"]); $photosNode = null; $lngsNode = null; $buildingsNode = null; $agentsNode = null; //read properties foreach ($node->children() as $propNode) { if ($propNode->getName() == "Zdjecia") { $photosNode = $propNode; } else { if ($propNode->getName() == "Jezyki") { $lngsNode = $propNode; } else { if ($propNode->getName() == "Budynki") { $buildingsNode = $propNode; } else { if ($propNode->getName() == "Agenci") { $agentsNode = $propNode; } else { if ($propNode->getName() == "PolaDynamiczne") { //delete unuse properties from offer $addedProperties = array(); foreach ($propNode->children() as $listNode) { $pname = (string) $listNode["Nazwa"]; $investment->__set($pname, $listNode); } } } } } } } //save investment object to database Investments::AddEditInvestment($investment); echo DataBase::GetDbInstance()->LastError(); //delete unuse properties from offer $addedProperties = array(); foreach ($node->children() as $propNode) { $pname = $propNode->getName(); if ($pname == "PolaDynamiczne") { foreach ($propNode->children() as $listNode) { $pname = (string) $listNode["Nazwa"]; $prop = Properties::GetPropertyName($pname); if ($prop != null) { $addedProperties[count($addedProperties)] = $prop->GetID(); } } } } Investments::DeleteUnUseProperties($investment->GetId(), $investment->GetIdLng(), $addedProperties); //photos if ($photosNode != null) { $addedPhotos = array(); foreach ($photosNode->children() as $photoNode) { $intro = $photoNode->intro == "True" ? 1 : 0; $photo = new OfferPhoto($photoNode['ID'], null, $investment->GetId(), $photoNode->plik, $photoNode->opis, $photoNode['lp'], $photoNode['typ'], $intro, null, (string) $photoNode->LinkFilmYouTube, (string) $photoNode->LinkMiniaturkaYouTube); OfferPhotos::AddEditPhoto($photo); echo DataBase::GetDbInstance()->LastError(); $addedPhotos[count($addedPhotos)] = $photo->GetId(); } OfferPhotos::DeleteUnUsePhotos(0, $addedPhotos, $investment->GetId()); } //buildings if ($buildingsNode != null) { $importedBlds = ""; foreach ($buildingsNode->children() as $buildingNode) { $importedBlds .= $buildingNode["ID"] . ","; $building = new InvestmentBuilding(null, $buildingNode['ID'], $buildingNode->Nazwa, $buildingNode['Symbol'], $buildingNode->Opis, $investment->GetId(), CheckNumeric($buildingNode['Metraz']), $buildingNode->TerminOddania, CheckNumeric($buildingNode['LiczbaPieter'])); Investmentbuildings::AddEditInvestmentBuilding($building); echo DataBase::GetDbInstance()->LastError(); //add offers to building $offersNode = $buildingNode->Oferty; Investmentbuildings::AddOffersToBuilding($offersNode, $building); } //delete buildings $importedBlds = substr($importedBlds, 0, strlen($importedBlds) - 1); if ($importedBlds != "") { $result = DataBase::GetDbInstance()->ExecuteQuery("SELECT id FROM #S#investments_buildings WHERE investments_id=" . $investment->GetId() . " AND id NOT IN({$importedBlds})"); while ($row = DataBase::GetDbInstance()->FetchArray($result)) { Investmentbuildings::DeleteInvestmentBuilding($row[0]); echo DataBase::GetDbInstance()->LastError(); } } } //agenci if ($agentsNode != null) { foreach ($agentsNode->children() as $aNode) { $a = Agents::GetAgent($aNode['wartosc']); if ($a != null) { Investments::AddInvestmentsAgent($investment, $a); } } } } //delete investments $importedInv = substr($importedInv, 0, strlen($importedInv) - 1); if ($importedInv == "") { $importedInv = "-1"; } $result = DataBase::GetDbInstance()->ExecuteQuery("SELECT id FROM #S#investments WHERE id NOT IN({$importedInv})"); while ($row = DataBase::GetDbInstance()->FetchArray($result)) { Investments::DeleteInvestment($row[0]); //delete agents_investments relation Investments::DelInvestmentsAgents($row[0]); echo DataBase::GetDbInstance()->LastError(); } return $count; } catch (Exception $ex) { Errors::LogError("WebServiceVirgo:GetInvestments", $ex->getMessage() . "; " . $ex->getTraceAsString()); return 0; } }
<?php class Investment { public $interest; public $amount; public $result; function calculate($interest, $amount) { $this->result = $this->amount * $this->interest + $this->amount; } } $investment = new Investment(); $investment->interest = 0.1; $investment->amount = 250; $investment->calculate($investment->interest, $investment->amount); echo "Finally I calculated that from <br>" . $investment->amount . " \n if we have " . $investment->interest . " we will get " . $investment->result . ". <hr>";
/** * Save value of given property in given investment. * @param Property $prop * @param Investment $inv * @param string $value * @param bool $isNew * @param array $dbValuesList */ public static function SavePropertyValueForInv(Property $prop, Investment $inv, $value, $isNew, $dbValuesList) { is_numeric($inv->GetId()) ? $get_id = (int) $inv->GetId() : ($get_id = $inv->GetId()); is_numeric($inv->GetIdLng()) ? $get_lng_id = (int) $inv->GetIdLng() : ($get_lng_id = $inv->GetIdLng()); is_numeric($prop->GetID()) ? $get_prop_id = (int) $prop->GetID() : ($get_prop_id = $prop->GetID()); if ($isNew) { //if offer is new, then directly make insert, instead of checking if record exist in db if (is_array($value)) { foreach ($value as $val) { $result = DataBase::GetDbInstance()->ExecuteQueryWithParams("INSERT INTO #S#investments_properties (investments_id, investments_id_lng, properties_id, value, `set`, hash) VALUES(?, ?, ?, ?, true, ?)", array($get_id, $get_lng_id, $get_prop_id, $val, md5($val))); } } else { $query = "INSERT INTO #S#investments_properties (investments_id, investments_id_lng, properties_id, value, `set`, hash) VALUES(?, ?, ?, ?, false, ?)"; $params = array($get_id, $get_lng_id, $get_prop_id, $value, md5($value)); $result = DataBase::GetDbInstance()->ExecuteQueryWithParams($query, $params); } } else { //if offer exist, check if value exist in db if (is_array($value)) { $dbvalues = array(); if (array_key_exists($prop->GetID(), $dbValuesList)) { foreach ($dbValuesList[$prop->GetID()] as $dbval) { if (in_array($dbval["value"], $value)) { $dbvalues[count($dbvalues)] = $dbval["value"]; } else { //delete from database $result2 = DataBase::GetDbInstance()->ExecuteQueryWithParams("DELETE FROM #S#investments_properties WHERE id=?", array((int) $dbval['id'])); } } } foreach ($value as $val) { if (!in_array($val, $dbvalues)) { //insert to database $result = DataBase::GetDbInstance()->ExecuteQueryWithParams("INSERT INTO #S#investments_properties (investments_id, investments_id_lng, properties_id, value, `set`, hash) VALUES(?, ?, ?, ?, true, ?)", array($get_id, $get_lng_id, $get_prop_id, $val, md5($val))); } } } else { $row = null; if (array_key_exists($prop->GetID(), $dbValuesList)) { $row = $dbValuesList[$prop->GetID()][0]; } $query = ""; if ($row == null) { $query = "INSERT INTO #S#investments_properties (investments_id, investments_id_lng, properties_id, value, `set`, hash) VALUES(?, ?, ?, ?, false, ?)"; $params = array($get_id, $get_lng_id, $get_prop_id, $value, md5($value)); } else { if ($row['value'] != $value) { $query = "UPDATE #S#investments_properties SET value=?, hash=? WHERE investments_id=? AND investments_id_lng=? AND properties_id=?"; $params = array($value, md5($value), (int) $get_id, (int) $get_lng_id, (int) $get_prop_id); } } if ($query != "") { $result = DataBase::GetDbInstance()->ExecuteQueryWithParams($query, $params); } } } }
/** * Add or edit if exists, given investment object. * @param Investment $inv */ public static function AddEditInvestment(Investment $inv) { $i = self::GetInvestment($inv->GetId(), $inv->GetIdLng()); if ($i == null) { self::AddInvestment($inv); } else { self::EditInvestment($inv); } }