/** * Add or edit if exists, given offer object. * @param Offer $ofe */ public static function AddEditOffer(Offer $ofe) { $o = self::GetOffer($ofe->GetId(), $ofe->GetIdLng()); if ($o == null) { self::AddOffer($ofe); return "A"; } else { //delete generated photos if ($o->Atrybut("ZeroProwizji") != $ofe->Atrybut("ZeroProwizji")) { OfferPhotos::DeletePhotoFromDisk(0, $ofe->GetId()); } self::EditOffer($ofe); return "E"; } }
/** * Save value of given property in given offer. * @param Property $prop * @param Offer $offer * @param string $value * @param bool $isNew * @param array $dbValuesList */ public static function SavePropertyValue(Property $prop, Offer $offer, $value, $isNew, $dbValuesList) { is_numeric($offer->GetId()) ? $get_id = (int) $offer->GetId() : ($get_id = $offer->GetId()); is_numeric($offer->GetIdLng()) ? $get_lng_id = (int) $offer->GetIdLng() : ($get_lng_id = $offer->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#offers_properties (offers_id, offers_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#offers_properties (offers_id, offers_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#offers_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#offers_properties (offers_id, offers_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#offers_properties (offers_id, offers_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#offers_properties SET value=?, hash=? WHERE offers_id=? AND offers_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); } } } }