Last active
August 29, 2015 14:03
-
-
Save flymio/36dfd42a602968040041 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "./DbSimple/Generic.php"; | |
class PampersTrash { | |
private static $instance; | |
private static $dbConf = array( | |
"user" => "root", | |
"password" => "******", | |
"database" => "storages", | |
"host" => "localhost", | |
); | |
public $DB; | |
public $changes; | |
public $instances; | |
public function descruct () { | |
if (sizeof($this->changes)>0){ | |
$this->DB->query("UPDATE items SET changed = 0 WHERE (code IN (".implode(",", $this->changes)."))"); | |
} | |
echo "destruct ok\n"; | |
} | |
public function __construct(array $arguments = array()) { | |
if (!empty($arguments)) { | |
foreach ($arguments as $property => $argument) { | |
$this->{$property} = $argument; | |
} | |
} | |
if (empty($this->{"database"})){ | |
$this->{"database"} = PampersTrash::$dbConf; | |
} | |
$this->DB = DbSimple_Generic::connect(sprintf("mysql://%s:%s@%s/%s", | |
$this->{'database'}{'user'}, | |
$this->{'database'}{'password'}, | |
$this->{'database'}{'host'}, | |
$this->{'database'}{'database'} | |
)); | |
$this->DB->query("SET NAMES 'utf8'"); | |
$this->instances = array(); | |
return $this; | |
} | |
public function updateChanges(){ | |
$prices = $this->DB->select("SELECT * FROM items WHERE (changed>0)"); | |
foreach($prices as $id => $price){ | |
$this->changes[]=$price['code']; | |
$this->price(array($price['article'], $price['price']), $price['valute']); | |
$this->priceCustom(array($price['article'], array("piter" => $price['price2']), $price['valute'])); | |
$this->priceCustom(array($price['article'], array("opt" => $price['price_opt']), $price['valute'])); | |
} | |
$this->commit(); | |
$this->descruct(); | |
} | |
public function append($child) { | |
$this->instances[] = $child; | |
} | |
public function __call($method, $arguments) { | |
if (sizeof($this->instances)>0){ | |
foreach($this->instances as $child){ | |
if (is_callable(array($child,$method))) { | |
call_user_func_array(array($child,$method), $arguments); | |
} | |
} | |
} | |
} | |
} | |
class Pamper { | |
private static $instance; | |
private static $dbConf = array( | |
"user" => "root", | |
"password" => "******", | |
"database" => "storages", | |
"host" => "localhost", | |
); | |
public $Data; | |
public $DataPrices; | |
public $File; | |
public $DataChanged; | |
public $DataChangedCustom; | |
public $Commit; | |
private static $pamperConf = array( | |
"site" => "mebelcafe.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"price" => "", | |
"marking" => "", | |
"catalog" => 1, | |
"data" => array( | |
"bitrix" => | |
array( | |
"iblock_id" => array(3, 32), | |
"url" => "/api/v1/export_price.php" | |
), | |
"hostcms" => array( | |
"url" => "/api/update_prices.php", | |
) | |
) | |
); | |
public static $dbInstance; | |
public $DB; | |
public function __getCurrency(){ | |
$content = file_get_contents("http://www.klenmarket.ru/api/update_prices.php?get=".urlencode('set names "utf8"; SELECT code,exchange_rate FROM shop_currencies WHERE (1)')); | |
foreach(explode("\n",$content) as $line){ | |
$line=trim($line); | |
if ($line){ | |
$data = explode(";",$line); | |
$this->valutes[$data[0]]=$data[1]; | |
} | |
} | |
} | |
public function __construct(array $arguments = array()) { | |
if (!empty($arguments)) { | |
foreach ($arguments as $property => $argument) { | |
$this->{$property} = $argument; | |
} | |
} | |
if (empty($this->{"database"})){ | |
$this->{"database"} = Pamper::$dbConf; | |
} | |
if (empty($this->{"pamper"})){ | |
$this->{"pamper"} = Pamper::$pamperConf; | |
} | |
$this->DB = DbSimple_Generic::connect(sprintf("mysql://%s:%s@%s/%s", | |
$this->{'database'}{'user'}, | |
$this->{'database'}{'password'}, | |
$this->{'database'}{'host'}, | |
$this->{'database'}{'database'} | |
)); | |
if (isset($this->{'pamper'}{'loadCurrency'})){ | |
$this->__getCurrency(); | |
} | |
$this->DB->query("SET NAMES 'utf8'"); | |
return $this; | |
} | |
public function __call($method, $arguments) { | |
$arguments = array_merge(array("Pamper" => $this), $arguments); // Note: method argument 0 will always referred to the main class ($this). | |
if (isset($this->{$method}) && is_callable($this->{$method})) { | |
return call_user_func_array($this->{$method}, $arguments); | |
} | |
} | |
public function forceUpdate(){ | |
$prices = $this->DB->select("SELECT * FROM items WHERE (price>0)"); | |
foreach($prices as $key => $price){ | |
$price['price'] = floatval($price['price']); | |
if (isset($this->DataPrices[$price['article']])){ | |
$price_old = floatval($this->DataPrices[$price['article']]); | |
if ($price['price']!=$price_old){ | |
print $price['article']."\t".$price['price']."\n"; | |
$this->price(array($price['article'], $price['price'])); | |
//$kp->price(array('ад030', 35199)); | |
} | |
} | |
} | |
//$this->commit(); | |
//print_r($this->DataPrices); | |
} | |
public function price($data){ | |
/* | |
params: id => price | |
params: id => price_id => price | |
*/ | |
if (!empty($data)){ | |
if (is_array($data)){ | |
list ($marking, $price) = $data; | |
if (isset($this->Data[$marking]) && !is_array($this->Data[$marking])){ | |
$this->Data[$marking]=array($this->Data[$marking]); | |
} | |
if (isset($this->Data[$marking])){ | |
// change price with own data | |
if (is_array($price)){ | |
$region = array_keys($price); $region = array_pop($region); | |
$price = array_values($price); $price = array_pop($price); | |
foreach($this->Data[$marking] as $mark){ | |
echo "change price ".$region . "__" . $mark."\t".$price."\n"; | |
$this->DataChanged[$region . "__" . $mark] = $price; | |
} | |
} | |
else{ | |
foreach($this->Data[$marking] as $mark){ | |
echo "change price ".$mark."\t".$price."\n"; | |
$this->DataChanged[$mark] = $price; | |
} | |
} | |
} | |
} | |
else{ | |
// change price from database | |
if ($this->Data[$data]){ | |
$price = $this->DB->selectCell("SELECT price FROM items WHERE (article=?)", $data); | |
if ($price){ | |
$this->DataChanged[$this->Data[$data]] = $price; | |
} | |
} | |
} | |
} | |
} | |
public function __getData(){ | |
// get pamper data: list(id, marking, price) for bitrix sites | |
/* | |
params: none | |
*/ | |
// check last update | |
$check = $this->DB->select("SELECT price as tm FROM pamper_data WHERE (domain=? AND marking='tm')", $this->{'pamper'}{'site'}); | |
if ($check) { | |
$check = array_shift($check); | |
} | |
$this->File = rand(1, time()) + time(); | |
$this->File .= ".sql"; | |
$articlesData = array(); | |
if (empty($check['tm'])){ | |
// if last update not defined, insert new timestamp | |
$this->DB->select("INSERT INTO pamper_data SET domain=?, price=?, marking='tm'", | |
$this->{'pamper'}{'site'}, | |
time() | |
); | |
} | |
else{ | |
// else — get pampers data for caching | |
$articles = $this->DB->select("SELECT * FROM pamper_data WHERE (domain=?)",$this->{'pamper'}{'site'}); | |
foreach ($articles as $id => $offer) { | |
$articlesData[$offer['id']] = $offer; | |
$this->DataPrices[$offer['marking']] = $offer['price']; | |
$this->Data[$offer['marking']][] = $offer['id']; | |
} | |
} | |
if ($this->{'pamper'} && (isset($this->{'pamper'}{'data'}{'bitrix'}) || isset($this->{'pamper'}{'data'}{'hostcms'})) && (empty($check['tm']) || (time() - $check['tm']) > 43200 ) ){ | |
// get new pamper data (id, marking, price) for new site if last update is expired or never running before | |
if (isset($this->{'pamper'}{'price'})){ | |
$this->{'pamper'}{'price_column'} = $this->{'pamper'}{'price'}; | |
if (is_array($this->{'pamper'}{'price'})){ | |
$column = array_keys($this->{'pamper'}{'price'}); $column = array_pop($column); | |
$prop_id = array_values($this->{'pamper'}{'price'}); $prop_id = array_pop($prop_id); | |
$this->{'pamper'}{'price_column'} = $column; | |
} | |
} | |
$data = $this->loadData(); | |
if (is_callable(array($this,'loadDataCustom'))) { | |
$data.=call_user_func_array(array($this,'loadDataCustom'), array(1,2,3)); | |
} | |
// get data | |
if (strlen($data)){ | |
// when data is not null filling new offers | |
foreach(explode("\n", $data) as $line){ | |
$line = trim($line); | |
if ($line && strlen($line) > 10){ | |
list ($id, $marking, $price) = explode(";", $line); | |
$id = trim($id); $marking = trim($marking); $price = trim($price); | |
if (empty($articlesData[$id]) && $id){ | |
$this->DB->query("INSERT IGNORE INTO pamper_data SET domain=?, price=?, marking=?, id=?", | |
$this->{'pamper'}{'site'}, | |
$price, | |
$marking, | |
$id | |
); | |
} | |
} | |
} | |
} | |
} | |
// update last update timestamp | |
$this->DB->query("DELETE FROM pamper_data WHERE (domain=? AND marking='tm')", | |
$this->{'pamper'}{'site'} | |
); | |
$this->DB->query("INSERT INTO pamper_data SET domain=?, price=?, marking='tm'", | |
$this->{'pamper'}{'site'}, | |
time() | |
); | |
} | |
public function loadData() { | |
// generating url for prices update | |
$url = sprintf("http://%s%s?catalogs=%s&marking=%s&price=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'bitrix'}{'url'}, | |
(is_array($this->{'pamper'}{'data'}{'bitrix'}{'iblock_id'}) ? implode(",", $this->{'pamper'}{'data'}{'bitrix'}{'iblock_id'}) : $this->{'pamper'}{'data'}{'bitrix'}{'iblock_id'}), | |
$this->{'pamper'}{'marking'}, | |
$this->{'pamper'}{'price_column'} | |
); | |
return file_get_contents($url); | |
} | |
function update(){ | |
if (file_exists($this->File)){ | |
$url = sprintf("http://%s%s&post=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'url'}, | |
urlencode(sprintf("http://93.174.55.82:82/%s",$this->File)) | |
); | |
echo file_get_contents($url); | |
} | |
} | |
} | |
class BitrixPamper extends Pamper { | |
public function __construct(array $arguments = array()) { | |
parent::__construct($arguments); | |
if (!empty($arguments)) { | |
foreach ($arguments as $property => $argument) { | |
$this->{$property} = $argument; | |
} | |
} | |
$this->__getData(); | |
} | |
public function commit(){ | |
$iblocks = $this->{'pamper'}{'data'}{'bitrix'}{'iblock_id'}; | |
if (!is_array($iblocks)){ | |
$iblocks = array($iblocks); | |
} | |
$SQL = ""; | |
if ($this->DataChanged){ | |
foreach($this->DataChanged as $id => &$price){ | |
if (is_array($this->{'pamper'}{'price'})){ | |
$column = array_keys($this->{'pamper'}{'price'}); $column = array_pop($column); | |
$prop_id = array_values($this->{'pamper'}{'price'}); $prop_id = array_pop($prop_id); | |
foreach($iblocks as $iblock_id){ | |
$SQL .= "UPDATE b_iblock_element_property SET VALUE='".$price."' WHERE (IBLOCK_ELEMENT_ID='".$id."' AND IBLOCK_PROPERTY_ID='".$prop_id."' AND VALUE_TYPE='text');\n"; | |
} | |
} | |
else{ | |
$SQL .= "UPDATE b_catalog_price SET price='".$price."' WHERE (product_id='".$id."');\n"; | |
} | |
} | |
} | |
if ($SQL){ | |
$h = fopen("./".$this->File,"a"); | |
fwrite($h, $SQL); | |
fclose($h); | |
} | |
$this->DataChanged = null; | |
$this->update(); | |
echo " ok\n"; | |
} | |
} | |
class HostCMS6Pamper extends BitrixPamper { | |
public function loadData(){ | |
$url = sprintf("http://%s%s?get=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'hostcms'}{'url'}, | |
urlencode('set names "utf8"; SELECT id,marking,price,price_opt FROM shop_items WHERE (marking!="")') | |
); | |
return file_get_contents($url); | |
} | |
public function loadDataCustom(){ | |
echo "load custom data"; | |
$url = sprintf("http://%s%s?get=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'hostcms'}{'url'}, | |
urlencode('set names "utf8"; SELECT s1.shop_item_id,concat(s1.shop_price_id,"_",s2.marking),s1.value FROM shop_item_prices as s1, shop_items as s2 WHERE (s2.id=s1.shop_item_id);') | |
); | |
return file_get_contents($url); | |
} | |
public function commit(){ | |
$SQL=""; | |
if ($this->DataChanged){ | |
foreach($this->DataChanged as $id => $price){ | |
if (preg_match("/opt__/",$id)){ | |
$id = str_replace("opt__","",$id); | |
$SQL.= "UPDATE shop_items SET price_opt='".$price."' WHERE (id='".$id."');\n"; | |
} | |
else if (preg_match("/piter__/",$id)){ | |
$id = str_replace("piter__","",$id); | |
$SQL.= "UPDATE shop_item_prices SET value='".$price."' WHERE (shop_item_id='".$id."' AND shop_price_id=1);\n"; | |
} | |
else{ | |
$SQL.= "UPDATE shop_items SET price='".$price."' WHERE (id='".$id."');\n"; | |
} | |
} | |
} | |
if ($SQL){ | |
$h = fopen("./".$this->File,"a"); | |
fwrite($h, $SQL); | |
fclose($h); | |
} | |
$this->DataChanged = null; | |
$this->update(); | |
echo " ok\n"; | |
} | |
public function priceCustom($data){ | |
/* print_r($data); */ | |
parent::price($data); | |
} | |
//fwrite($handle,"UPDATE shop_items SET price='".$item."',price_opt='".$info_prices_new2[$k]."' WHERE (id='$k');\n"); | |
} | |
class HostCMS5Pamper extends HostCMS6Pamper { | |
public function loadData(){ | |
$url = sprintf("http://%s%s?get=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'hostcms'}{'url'}, | |
urlencode('set names "utf8"; SELECT shop_items_catalog_item_id,shop_items_catalog_marking,shop_items_catalog_price FROM shop_items_catalog_table WHERE (shop_items_catalog_marking!="")') | |
); | |
return file_get_contents($url); | |
} | |
public function loadDataCustom(){ | |
return ""; | |
} | |
public function commit(){ | |
$SQL=""; | |
if ($this->DataChanged){ | |
foreach($this->DataChanged as $id => $price){ | |
$SQL.= "UPDATE shop_items_catalog_table SET shop_items_catalog_price='".$price."' WHERE (shop_items_catalog_item_id='".$id."');\n"; | |
} | |
} | |
if ($SQL){ | |
$h = fopen("./".$this->File,"a"); | |
fwrite($h, $SQL); | |
fclose($h); | |
} | |
$this->DataChanged = null; | |
$this->update(); | |
echo " ok\n"; | |
} | |
} | |
class InstantCmsPamper extends HostCMS5Pamper { | |
public function loadData(){ | |
$url = sprintf("http://%s%s?get=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'hostcms'}{'url'}, | |
urlencode('set names "utf8"; select id,art_no,price from cms_shop_items where (art_no!="");') | |
); | |
return file_get_contents($url); | |
} | |
public function loadDataCustom(){ | |
return ""; | |
} | |
public function price($data, $valute = null){ | |
if (is_array($data)){ | |
list ($article, $price) = $data; | |
} | |
if (isset($valute)){ | |
if ($valute == 'EUR'){ | |
echo "price in EUR\n"; | |
$price*=$this->valutes['EUR']; | |
} | |
else if ($valute == 'USD'){ | |
echo "price in USD\n"; | |
$price*=$this->valutes['USD']; | |
} | |
else { | |
echo "price in RUB\n"; | |
} | |
} | |
$price=floatval($price); | |
parent::price(array($article,$price)); | |
} | |
public function commit(){ | |
$SQL=""; | |
if ($this->DataChanged){ | |
foreach($this->DataChanged as $id => $price){ | |
if (!preg_match("/__/",$id)){ | |
$SQL.= "UPDATE cms_shop_items SET price='".$price."' WHERE (id='$id');\n"; | |
} | |
} | |
} | |
if ($SQL){ | |
$h = fopen("./".$this->File,"a"); | |
fwrite($h, $SQL); | |
fclose($h); | |
} | |
$this->DataChanged = null; | |
$this->update(); | |
echo " ok\n"; | |
} | |
} | |
class HostCMS6PamperPiter extends HostCMS6Pamper { | |
public function loadData(){ | |
$url = sprintf("http://%s%s?get=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'hostcms'}{'url'}, | |
urlencode('set names "utf8"; SELECT id,marking,price FROM shop_items WHERE (marking!="")') | |
); | |
echo $url; | |
return file_get_contents($url); | |
} | |
public function loadDataCustom(){ | |
return ""; | |
} | |
public function priceCustom($data, $valute = null){ | |
if (is_array($data)){ | |
list ($article, $price) = $data; | |
} | |
if (preg_match("/piter__(.+?)$/",$article, $matches)){ | |
$article = $matches[1]; | |
parent::price(array($article,$price)); | |
} | |
} | |
public function price($data, $valute = null){ | |
if (is_array($data)){ | |
list ($article, $price) = $data; | |
} | |
if (isset($valute)){ | |
if ($valute == 'EUR'){ | |
echo "price in EUR\n"; | |
$price*=$this->valutes['EUR']; | |
} | |
else if ($valute == 'USD'){ | |
echo "price in USD\n"; | |
$price*=$this->valutes['USD']; | |
} | |
else { | |
echo "price in RUB\n"; | |
} | |
} | |
$price=intval($price); | |
parent::price(array($article,$price)); | |
} | |
public function commit(){ | |
$SQL=""; | |
if ($this->DataChanged){ | |
foreach($this->DataChanged as $id => $price){ | |
if (!preg_match("/__/",$id)){ | |
$SQL.= "UPDATE shop_items SET price='".$price."' WHERE (id='".$id."');\n"; | |
} | |
} | |
} | |
if ($SQL){ | |
$h = fopen("./".$this->File,"a"); | |
fwrite($h, $SQL); | |
fclose($h); | |
} | |
$this->DataChanged = null; | |
$this->update(); | |
echo " ok\n"; | |
} | |
} | |
class HostCMS5PamperPiter extends HostCMS6PamperPiter { | |
public function loadData(){ | |
$url = sprintf("http://%s%s?get=%s", | |
$this->{'pamper'}{'site'}, | |
$this->{'pamper'}{'data'}{'hostcms'}{'url'}, | |
urlencode('set names "utf8"; SELECT shop_items_catalog_item_id,shop_items_catalog_marking,shop_items_catalog_price FROM shop_items_catalog_table WHERE (shop_items_catalog_marking!="")') | |
); | |
return file_get_contents($url); | |
} | |
public function priceCustom($data, $valute = null){ | |
if (is_array($data)){ | |
list ($article, $price) = $data; | |
} | |
if (preg_match("/piter__(.+?)$/",$article, $matches)){ | |
$article = $matches[1]; | |
parent::price(array($article,$price)); | |
} | |
} | |
public function commit(){ | |
$SQL=""; | |
if ($this->DataChanged){ | |
foreach($this->DataChanged as $id => $price){ | |
$SQL.= "UPDATE shop_items_catalog_table SET shop_items_catalog_price='".$price."' WHERE (shop_items_catalog_item_id='".$id."');\n"; | |
} | |
} | |
if ($SQL){ | |
$h = fopen("./".$this->File,"a"); | |
fwrite($h, $SQL); | |
fclose($h); | |
} | |
$this->DataChanged = null; | |
$this->update(); | |
echo " ok\n"; | |
} | |
} | |
$trash = new PampersTrash(); | |
$ari = new HostCMS5PamperPiter(array("pamper" => array( | |
"loadCurrency" => true, | |
"site" => "ariada-piter.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"data" => array( | |
"hostcms" => | |
array( | |
"url" => "/api/update_prices.php" | |
) | |
) | |
))); | |
$kt = new HostCMS6PamperPiter(array("pamper" => array( | |
"loadCurrency" => true, | |
"site" => "klentorg.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"data" => array( | |
"hostcms" => | |
array( | |
"url" => "/api/update_prices.php" | |
) | |
) | |
))); | |
$in = new InstantCmsPamper(array("pamper" => array( | |
"loadCurrency" => true, | |
"site" => "startmart.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"data" => array( | |
"hostcms" => | |
array( | |
"url" => "/api/update_prices.php" | |
) | |
) | |
))); | |
$trash->append($in); | |
$km = new HostCMS6Pamper(array("pamper" => array( | |
"site" => "klenmarket.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"data" => array( | |
"hostcms" => | |
array( | |
"url" => "/api/update_prices.php" | |
) | |
) | |
))); | |
$kp = new HostCMS5Pamper(array("pamper" => array( | |
"site" => "kupiholod.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"data" => array( | |
"hostcms" => | |
array( | |
"url" => "/api/update_prices.php" | |
) | |
) | |
))); | |
$pizza = new BitrixPamper(array("pamper" => array( | |
"site" => "pizzagroup.ru", | |
"url" => "/api/update_prices.php?key=****************", | |
"price" => array("PROPERTY_PRICE" => 6), | |
"marking" => "PROPERTY_MARKING", | |
"data" => array( | |
"bitrix" => | |
array( | |
"iblock_id" => array(1), | |
"url" => "/api/v1/export_price.php" | |
) | |
) | |
))); | |
$mb = new BitrixPamper(); // mebelcafe | |
//добавляем в очередь для обновления сайты | |
$trash->append($ari); | |
$trash->append($kt); | |
$trash->append($in); | |
$trash->append($km); | |
$trash->append($mb); | |
$trash->append($kp); | |
$trash->append($pizza); | |
//обновляем на сайтах | |
$trash->updateChanges(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment