%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/waritko/import/
Upload File :
Create Path :
Current File : //home/waritko/import/euromedia.php

<?php
/**
 * Created by PhpStorm.
 * User: waritko
 * Date: 20.2.16
 * Time: 20:18
 */

ini_set('memory_limit', '8192M');

function createIdentif($name)
{
    $name = trim($name);
    //setlocale(LC_CTYPE, 'cs_CZ.UTF8');
    $string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $name);
    $string = str_replace(' ', '-', $string);
    $string = preg_replace('/\\s+/', '-', $string);
    $string = strtolower($string);
    while(strstr($string, '--')) $string = str_replace('--', '-', $string);
    $string = preg_replace('~[^-a-z0-9_]+~', '', $string);
    return $string;
}

function shortAnot($text)
{
    $length = 200;
    $length = abs((int)$length);
    if(strlen($text) > $length)
    {
        $text = preg_replace("/^(.{1,$length})(\\s.*|$)/s", '\\1...', $text);
    }
    return($text);
}

function rovnitka()
{
    echo "\n===========================================================================================================================================\n\n===========================================================================================================================================\n\n";
}

$ignoredGenres = array(
5001,
5002,
5003,
5101,
5103,
5108,
5109,
5110,
5111,
5112,
5113,
5114,
5115,
5116,
5117,
5118,
5119,
5120,
5121,
5122,
5123,
5124);

//include "dibi.min.php";
include "dibi.phar";

dibi::connect(array(
   'driver'   => 'mysql',
   'host'     => 'localhost',
   'username' => 'monami_shop',
   'password' => '1312Lucie',
   'database' => 'monami_shop',
   'charset'  => 'utf8',
   'profiler' => [
                'run' => TRUE,
                'file' => 'log-' . time() . '.sql',
              ],
));
$rows = array();
$genres = array();
$f = file('genres.csv');
foreach($f as $genre)
{
    list($name, $euroid, $newid) = explode(";", $genre);
    $genres[$euroid] = explode("/", $newid);
}
//print_r($genres);

//header("Content-Type: text/plain");
$catalog = simplexml_load_file("tituly.xml");
$i = 0;
$tstamp = 0;
foreach($catalog->titul as $book)
{
//    echo "<pre>";
//    print_r($book);

    $zanr = (string)($book->zanry->zanr['id']);
    if(in_array($zanr, $ignoredGenres)) continue;
    if(isset($book->parametry->predobjenavka) && !isset($book->parametry->novinka)) continue;
    $row = array(
        'identif' => createIdentif($book->nazev),
        'identif_nouni' => createIdentif($book->nazev),
        'nazev' => (string)$book->nazev,
        'isbn' => (string)$book->isbn,
        'ean' => (string)$book->ean,
        'id_pemic' => (string)$book->id,
        'nakladatel' => (string)$book->nakladatel,
        'edice' => (string)$book->znacka,
        'dph' => (string)$book->dph,
        'rabat' => (double)$book->rabat,
        'wholesale' => sprintf("%0.2f", ((double)$book->{'dpc-aktualni'}) * (100 - ((double)$book->rabat)) / 100),
        'cena' => ((double)$book->{'dpc-aktualni'})/(1 + ((double)$book->dph / 100)),
        'cena_pohoda' => (string)$book->{'dpc-aktualni'},
        'vazba' => (string)$book->{'typ-produktu'},
        'stran' => (string)$book->{'pocet-stran'},
        'vydano' => (string)$book->{'datum-uvedeni'},
        'vyska' => (int)$book->rozmer1,
        'sirka' => (int)$book->rozmer2,
        'tloustka' => (int)$book->rozmer3,
        'hmotnost' => (double)$book->hmotnost / 1000,
        'genre' => $genres[$zanr],
//        'id_ktg' => 1,
	'obrazek' => (string)($book->obrazky->velky),
        'zobraz' => 'Y',
        'dostupnost' => 'S'
    );
    if(isset($book->parametry->novinka))
    {
        $row['datumNovinka'] = (string)($book->parametry->novinka['datum']);
    }
if(trim($row['ean']) == '') continue;

    $txt = '';
    foreach($book->zanry as $zanr)
    {
        $txt .= ($txt != '' ? "; " : "") . (string)$zanr->zanr;
    }
    $txt = trim($txt);
    $row['info'] = $txt;
    //$row['anotace'] = "Lorem ipsum dolor sit amet... " . $txt;
//    $row['id_ktg'] = $genres[$txt];

    $txt = '';
    foreach($book->autori as $autor)
    {
        $txt .= ($txt != '' ? "; " : "") . (string)$autor->autor;
    }
    $row['autor'] = $txt;


    //print_r($row); echo "</pre><br />";

    //dibi::test("INSERT INTO [katalog_tmp_euromedia]", $row);
    //dibi::query("INSERT INTO [katalog_tmp_euromedia]", $row);
    //echo "<hr />\n";
    //echo $row['identif'] . "\n";

    //if($i++ > 50) break;
    if(((int)$book->timestamp) > $tstamp) $tstamp = (int)$book->timestamp;
    $rows[$row['id_pemic']] = $row;
}
unset($catalog);
$anotations = simplexml_load_file("anotace.xml");
foreach($anotations->titul as $t)
{
    $uid = (string)$t['id'];
    //echo $uid;
    $ean = (string)$t['ean'];
    $text = trim((string)$t);
    if(isset($rows[$uid])) $rows[$uid]['anotace'] = $text;
    //dibi::test("UPDATE [katalog_tmp_euromedia] SET [anotace]=%s where [ean]=%s", $text, $ean);
    //dibi::query("UPDATE [katalog_tmp_euromedia] SET [anotace]=%s where [ean]=%s", $text, $ean);
    //echo $ean . "\n";
}

unset($anotations);
$avail = simplexml_load_file("dostupnost.xml");
foreach($avail->titul as $t)
{
    $uid = (string)$t['id'];
    $ean = (string)$t['id'];
    $text = trim((string)$t);
    //dibi::test("UPDATE [katalog_tmp_euromedia] SET [zobraz]=%s where [id_pemic]=%s", $text == '-1' ? 'N' : 'Y', $ean);
    //dibi::query("UPDATE [katalog_tmp_euromedia] SET [zobraz]=%s where [id_pemic]=%s", $text == '-1' ? 'N' : 'Y', $ean);
    //dibi::query("UPDATE [katalog] SET [zobraz]=%s where [id_pemic]=%s AND [source]='euromedia'", $text == '-1' ? 'N' : 'Y', $ean);
    //echo $ean . "\n";
    if(isset($rows[$uid])) $rows[$uid]['dostupnost'] = $text;
}
$imported = 0;
foreach($rows as $row)
{
    if($row['dostupnost'] == '-1') continue;
    if(trim($row['vydano']) == '') continue;
    $pubtime = strtotime(isset($row['datumNovinka']) ? $row['datumNovinka'] : $row['vydano']);
    //if($pubtime < 1420070400) continue; // 2016-01-01 00:00:00
    //echo "\n\n";
    //echo $pubtime; break;
    $result = dibi::query('SELECT [id_product] FROM [ps_product] WHERE [import_id]=%s', 'euromedia-' . $row['id_pemic']);
    if(count($result)) continue;

    echo "===============IMPORTED================\n            " . $imported++ . "\n===============IMPORTED================\n";
    print_r($row);
    //continue;
    ob_start();
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    //header("Content-Type: text/plain"); ob_start();
    ?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <product>
        <id_manufacturer>0</id_manufacturer>
        <id_supplier>0</id_supplier>
        <id_category_default>3</id_category_default>
        <new></new>
        <cache_default_attribute></cache_default_attribute>
        <id_default_image></id_default_image>
        <id_default_combination></id_default_combination>
        <id_tax_rules_group><?php echo $row['dph'] == '10' ? 5 : ($row['dph'] == '21' ? 4 : ($row['dph'] == '15' ? 3 : 2)); ?></id_tax_rules_group>
        <position_in_category></position_in_category>
        <type>simple</type>
        <id_shop_default>1</id_shop_default>
        <reference></reference>
        <supplier_reference></supplier_reference>
        <location></location>
        <width><?php echo $row['sirka']; ?></width>
        <height><?php echo $row['vyska']; ?></height>
        <depth><?php echo $row['tloustka']; ?></depth>
        <weight><?php echo $row['hmotnost']; ?></weight>
        <quantity_discount>0</quantity_discount>
        <ean13><?php echo $row['ean']; ?></ean13>
        <upc></upc>
        <cache_is_pack>0</cache_is_pack>
        <cache_has_attachments>0</cache_has_attachments>
        <is_virtual>0</is_virtual>
        <on_sale>0</on_sale>
        <online_only>0</online_only>
        <ecotax>0</ecotax>
        <minimal_quantity>1</minimal_quantity>
        <price><?php echo sprintf("%0.2f", $row['cena']); ?></price>
        <wholesale_price><?php echo $row['wholesale']; ?></wholesale_price>
        <unity>0</unity>
        <unit_price_ratio>0</unit_price_ratio>
        <additional_shipping_cost>0</additional_shipping_cost>
        <customizable>0</customizable>
        <text_fields>0</text_fields>
        <uploadable_files>0</uploadable_files>
        <active><?php echo (isset($row['datumNovinka']) && (strtotime($row['datumNovinka']) > (time() + 3600*24*10)) && ($row['datumNovinka'] != '0000-00-00')) ? '0' : '1'; ?></active>
        <redirect_type>404</redirect_type>
        <id_product_redirected>0</id_product_redirected>
        <available_for_order>1</available_for_order>
        <available_date><!-- treba pridat datum kdy bude dostupna --><?php if(isset($row['datumNovinka'])) echo $row['datumNovinka']; ?></available_date>
        <condition>new</condition>
        <show_price>1</show_price>
        <indexed>1</indexed>
        <visibility>both</visibility>
        <advanced_stock_management>0</advanced_stock_management>
        <date_add></date_add>
        <date_upd></date_upd>
        <pack_stock_type>3</pack_stock_type>
        <author><![CDATA[<?php echo $row['autor']; ?>]]></author>
        <import_id>euromedia-<?php echo $row['id_pemic']; ?></import_id>
        <publisher><![CDATA[<?php echo $row['nakladatel']; ?>]]></publisher>
        <pages><?php echo $row['stran']; ?></pages>
        <binding><![CDATA[<?php echo $row['vazba']; ?>]]></binding>
        <published><?php echo isset($row['datumNovinka']) ? $row['datumNovinka'] : $row['vydano']; ?></published>
        <image_link><![CDATA[<?php echo $row['obrazek']; ?>]]></image_link>
        <meta_description maxSize="255" format="isGenericName">
            <language id="1"><?php echo $row['isbn']; ?></language>
        </meta_description>
        <meta_keywords>
            <language id="1"></language>
        </meta_keywords>
        <meta_title>
            <language id="1"><![CDATA[<?php echo $row['nazev']; ?>]]></language>
        </meta_title>
        <link_rewrite>
            <language id="1"><?php echo $row['identif']; ?></language>
        </link_rewrite>
        <name>
            <language id="1"><![CDATA[<?php echo $row['nazev']; ?>]]></language>
        </name>
        <description>
            <language id="1"><![CDATA[<?php echo $row['anotace']; ?>]]></language>
        </description>
        <description_short>
            <language id="1"><![CDATA[<?php echo shortAnot($row['anotace']); ?>]]></language>
        </description_short>
        <available_now>
            <language id="1">Zboží je skladem</language>
        </available_now>
        <available_later>
            <language id="1"><![CDATA[<?php echo $row['dostupnost'] == '1' ? 'Připravovaný DOTISK, NOVINKA' : 'Skladem u dodavatele'; ?>]]></language>
        </available_later>
        <associations>
            <categories nodeType="category" api="categories">
                <category>
                    <id>2</id>
                </category>
                <?php foreach($row['genre'] as $genre) { ?>
                <category>
                    <id><?php echo $genre; ?></id>
                </category>
                <?php } ?>
            </categories>
<!--            <images nodeType="image" api="images">
                <image>
                    <id></id>
                </image>
            </images> -->
            <images nodeType="image" api="images" />
            <combinations nodeType="combination" api="combinations" />
            <product_option_values nodeType="product_option_value" api="product_option_values" />
            <!-- <product_features nodeType="product_feature" api="product_features">
                <product_feature>
                    <id required="true"></id>
                    <id_feature_value xlink:href="https://shop.varak.net/api/product_feature_values/" required="true"></id_feature_value>
                </product_feature>
            </product_features> -->
            <product_features nodeType="product_feature" api="product_features" />
            <tags nodeType="tag" api="tags" />
            <stock_availables nodeType="stock_available" api="stock_availables" />
            <accessories nodeType="product" api="products" />
            <product_bundle nodeType="product" api="products" />
        </associations>
    </product>
</prestashop><?php $xmlData = ob_get_clean(); //echo $xmlData; echo "\n===========================================================================================================================================\n\n===========================================================================================================================================\n\n";
//die;
// POST
$ch = curl_init("https://5XT188LXZSCUBLWKXRWWW6CJTAL5TJBP@shop.varak.net/api/products/");
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/xml',
    'Content-Length: ' . strlen($xmlData))
);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

//echo $result;
$xmlObj = simplexml_load_string($result, null, LIBXML_NOCDATA);
//rovnitka();
//print_r($xmlObj);
//rovnitka();

$prestaId = (string) $xmlObj->product->id;
$img = file_get_contents($row['obrazek']);
$fname = "/tmp/" . uniqid() . ".jpg";
file_put_contents($fname, $img);
//echo "/usr/bin/curl -X POST -F image=@$fname https://5XT188LXZSCUBLWKXRWWW6CJTAL5TJBP@shop.varak.net/api/images/products/$prestaId/";
exec("/usr/bin/curl -X POST -F image=@$fname https://5XT188LXZSCUBLWKXRWWW6CJTAL5TJBP@shop.varak.net/api/images/products/$prestaId/ 2>&1 ");
unlink($fname);

echo $row['autor'] . " - " . $row['nazev'] . "\n";
//if($imported++ > 2) break;
}
/*if($tstamp > 0) file_put_contents("update/timestamp", $tstamp);

unset($catalog);
$anotations = simplexml_load_file("update/anotace.xml");
foreach($anotations->titul as $t)
{
    $ean = (string)$t['ean'];
    $text = trim((string)$t);
    //dibi::test("UPDATE [katalog_tmp_euromedia] SET [anotace]=%s where [ean]=%s", $text, $ean);
    dibi::query("UPDATE [katalog_tmp_euromedia] SET [anotace]=%s where [ean]=%s", $text, $ean);
    //echo $ean . "\n";
}

unset($anotations);
$avail = simplexml_load_file("update/dostupnost.xml");
foreach($avail->titul as $t)
{
    $ean = (string)$t['id'];
    $text = trim((string)$t);
    //dibi::test("UPDATE [katalog_tmp_euromedia] SET [zobraz]=%s where [id_pemic]=%s", $text == '-1' ? 'N' : 'Y', $ean);
    dibi::query("UPDATE [katalog_tmp_euromedia] SET [zobraz]=%s where [id_pemic]=%s", $text == '-1' ? 'N' : 'Y', $ean);
    dibi::query("UPDATE [katalog] SET [zobraz]=%s where [id_pemic]=%s AND [source]='euromedia'", $text == '-1' ? 'N' : 'Y', $ean);
    //echo $ean . "\n";
}*/
echo $imported;
?>

Zerion Mini Shell 1.0