<?php
// Wy��czenie kontroli czasu wykonywania skryptu
include ('../../_config/conf/baza.inc.php');
include ('../../_config/tabele.inc.php');
include ('../../_config/general_config.inc');
// Po��czenie z baz� danych
$host_port = (defined('DB_PORT') ? DB_HOST
. ':' . DB_PORT
: DB_HOST
); $sql = "SET NAMES 'utf8'"; // wskazane przy MySQL >= 4.1, w tym przypadku zak�adamy, �e baza danych zawiera teksty w ISO-8859-2, natomiast mo�e to by� te� np. UTF-8
// Nag��wek
header('Content-Type: text/xml');
$ShopName = CONF_SHOP_NAME;
$ShopSkapiecID = CONF_SHOP_SKAPIEC_ID;
$ShopURL = CONF_SHOP_URL;
$xmlForHeader = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
<pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
<pasaz:Body>
<loadOffers xmlns="urn:ExportB2B">
<offers>
XML;
print ($xmlForHeader);
// Pobieranie i budowa drzewa kategorii
// (przy ka�dej ofercie powinna by� wy�wietlana kategoria wraz z kategoriami nadrz�dnymi)
$sql = "SELECT *
FROM " . TABLE_CATEGORIES . "," . TABLE_CATEGORY_TRANSLATIONS . "
WHERE " . TABLE_CATEGORY_TRANSLATIONS . ".lang = 'pl'
AND " . TABLE_CATEGORY_TRANSLATIONS . ".cid = " . TABLE_CATEGORIES .
".category_id
";
$results = mysql_query($sql) or die(mysql_error());
$categoryArray = array();
while ($category = mysql_fetch_assoc($results))
{
$categoryArray[$category['category_id']] = $category;
}
foreach ($categoryArray as $category)
{
if (ereg('\.', $categoryArray[$category['category_id']]['level']))
{
$rodzice = explode(".", $categoryArray[$category['category_id']]['level']);
$ileR = count($rodzice);
$categoryArray[$category['category_id']]['level'] = $rodzice[$ileR - 1];
}
$categoryParent = $categoryArray[$category['category_id']]['level'];
$categoryPatch = $categoryArray[$category['category_id']]['cat_name'];
$counter = 0;
while ($categoryParent)
{
$counter++;
if ($counter > 100)
break;
if ($categoryParent != $categoryArray[$categoryParent]['level'])
{
$categoryPatch = $categoryArray[$categoryParent]['cat_name'] . " / " . $categoryPatch;
$categoryParent = $categoryArray[$categoryParent]['level'];
} else
{
$categoryParent = 0;
}
}
$categoryArray[$category['category_id']]['patch'] = $categoryPatch;
}
// Pobranie ofert z bazy danych oraz zapis ich do pliku
$sql = "
SELECT
" . TABLE_PRODUCTS . " .product_id,
IF( " . TABLE_PROMOTIONS . ".special_price, " . TABLE_PROMOTIONS .
".special_price, " . TABLE_PRODUCTS . ".price ) as price,
" . TABLE_PRODUCTS . " .category_id as id_category,
" . TABLE_PRODUCTS . " .producer_id,
" . TABLE_PRODUCTS . " .in_stock,
" . TABLE_MANUFACTURER . ".name,
product_name,
description,
unic_name,
" . TABLE_PRODUCTS . " .product_code
FROM
" . TABLE_PRODUCTS . "
LEFT JOIN " . TABLE_GFX . " ON (" . TABLE_PRODUCTS . ".product_id = " .
TABLE_GFX . ".prod_id AND " . TABLE_GFX . ".main_gfx = 1 )
LEFT JOIN " . TABLE_PROMOTIONS . " ON (" . TABLE_PRODUCTS . ".product_id = " .
TABLE_PROMOTIONS . ".pid AND date_from < UNIX_TIMESTAMP() AND date_to > UNIX_TIMESTAMP() )
INNER JOIN " . TABLE_MANUFACTURER . " ON (" . TABLE_PRODUCTS .
".producer_id = " . TABLE_MANUFACTURER . ".producent_id)
INNER JOIN " . TABLE_PRODUCT_TRANSLATIONS . " ON (" . TABLE_PRODUCTS .
".product_id = " . TABLE_PRODUCT_TRANSLATIONS . ".pid AND " .
TABLE_PRODUCT_TRANSLATIONS . ".lang = 'pl' AND " . TABLE_PRODUCT_TRANSLATIONS .
".active = 1)
";
$results = mysql_query($sql) or die(mysql_error() . $sql);
while ($offer = mysql_fetch_assoc($results))
{
if($offer['in_stock'] > 0){
$offer['dostepnosc'] = 1;
}else{
$offer['dostepnosc'] = 7;
}
$offer['category'] = $categoryArray[$offer['id_category']]['patch'];
$offer['url'] = CONF_SHOP_URL . "/index.php?products=product&prod_id=" . $offer['product_id'];
foreach ($offer as $k => $v)
{
// dodajemy CDATA dla wi�kszo�ci p�l, poza kilkoma wymienionymi
// pozwala to na u�ycie dowolnych znak�w w XML-u
if (false == in_array($k, array('product_id', 'price', 'in_stock', 'code',
'shipping', 'category', 'unic_name', 'dostepnosc')))
{
$offer[$k] = '<![CDATA[' . $v . ']]>';
}
}
if (isset($offer['unic_name']))
{
$offer['image'] = CONF_SHOP_URL . '/_var/gfx/' . $offer['unic_name'] . '.jpg';
}
$xmlForOffer = <<< XML
<offer>
<id>{$offer['product_id']}</id>
<name>{$offer['product_name']}</name>
<price>{$offer['price']}</price>
<url>{$offer['url']}</url>
<categoryId>{$offer['category']}</categoryId>
<image>{$offer['image']}</image>
<attributes>
<name>Kod_producenta</name>
<value>{$offer['product_code']}</value>
</attributes>
<attributes>
<name>Producent</name>
<value>{$offer['name']}</value>
</attributes>
<availability>{$offer['dostepnosc']}</availability>
</offer>
XML;
print ($xmlForOffer);
}
// Zapisanie stopki
$xmlForFooter = <<< XML
</offers>
</loadOffers>
</pasaz:Body>
</pasaz:Envelope>
XML;
print ($xmlForFooter);
?>