%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /www/varak.net/shop.varak.net/import/
Upload File :
Create Path :
Current File : /www/varak.net/shop.varak.net/import/Kniha.php

<?php

/**
 * Created by PhpStorm.
 * User: Ludek
 * Date: 9. 9. 2016
 * Time: 15:13
 */
include "config.php";

class Kniha
{
//    public $nazev;
//    public $autor;

    private $xml;
    private $prestaId = null;
    private $nastavenyObrazek = false;


    /**
     * Kniha constructor.
     */
    public function __construct($id)
    {
        if($id)
        {
            $this->nacti($id);
        }
        else
        {
            $this->xml = simplexml_load_file(__DIR__ . '/prazdny.xml');
        }
//        print_r($this->xml);
    }


    function nacti($id)
    {
        global $prestaUrl;
        global $apiUser;

        $ch = curl_init();
        $url = $prestaUrl . "products/" . $id;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 90); //timeout after 90 seconds
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_USERPWD, "$apiUser:");
        $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
        $result = curl_exec ($ch);
        curl_close ($ch);
        $this->xml = simplexml_load_string($result, null, LIBXML_NOCDATA);
        $this->prestaId = (string) $this->xml->product->id;
    }


    function uloz()
    {
        global $prestaUrl;
        global $apiUser;
        global $onlineEnabled;
        $this->predUlozenim();

        if(!$onlineEnabled)
        {
            return;
        }
//        echo $this->xml->asXML();

        $xmlData = $this->xml->asXML();
//        if (!$this->nastavenyObrazek)
//            echo "neni nastaveny obrazek \n";
//            return;

//        echo $xmlData; echo "\n\n ========= \n\n";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_USERPWD, "$apiUser:");
        if($this->prestaId)
        {
            curl_setopt($ch, CURLOPT_URL, $prestaUrl . "products/" . $this->prestaId);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");     // update
        }
        else
        {
            curl_setopt($ch, CURLOPT_URL, $prestaUrl . "products/");
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    // insert
        }
        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))
        );
        $result = curl_exec($ch);
//        print_r($result);
        curl_close($ch);
        $xmlObj = simplexml_load_string($result, null, LIBXML_NOCDATA);
        $this->prestaId = (string) $xmlObj->product->id;

//        echo $xmlData; echo "\n\n ========= \n\n";
//        echo $result;
        $this->poUlozeni();
//        echo "\nKonec po prvnim zaznamu \n";
//        die;
    }


    function nastavImportId($idNakladatel)
    {
        $this->xml->product->import_id = $idNakladatel;
    }

    function nastavNazev($nazev)
    {
        $this->xml->product->meta_title->language = $nazev;
        $this->xml->product->name->language = $nazev;
    }

    function nastavPuvodniKodKategorie($puvodniKodKategorie)
    {
        $this->xml->product->meta_keywords->language = $puvodniKodKategorie;
    }

    function nastavCenu($cena)
    {
        $this->xml->product->price = sprintf("%0.2f", $cena);
    }

    function nastavAkci()
    {
        $this->xml->product->onsale = "1";
    }

    function nastavDph($kodDPH)
    {
        $this->xml->product->id_tax_rules_group = $kodDPH;
    }

    function nastavSirku($sirka)
    {
        $this->xml->product->width = $sirka;
    }

    function nastavVysku($vyska)
    {
        $this->xml->product->height = $vyska;
    }

    function nastavHloubku($hloubka)
    {
        $this->xml->product->depth = $hloubka;
    }

    function nastavHmotnost($hmotnost)
    {
        $this->xml->product->weight = $hmotnost;
    }

    function nastavISBN($isbn)
    {
        $this->xml->product->meta_description->language = $isbn;
    }

    function nastavVazbu($vazba)
    {
        $this->xml->product->binding = $vazba;
    }

    function nastavStran($stran)
    {
        $this->xml->product->pages = $stran;
    }

    function nastavAutor($autor)
    {
        $this->xml->product->author = $autor;
    }

    function nastavNakladatel($nakladatel)
    {
        $this->xml->product->publisher = $nakladatel;
    }

    function nastavEAN($ean)
    {
        $this->xml->product->ean13 = $ean;
    }

    function nastavNakupniCenu($nakcen)
    {
        $this->xml->product->wholesale_price = $nakcen;
    }

    function nastavJeliAktivni($aktivni)
    {
//        if ($aktivni)
//        {
            $this->xml->product->active = "1";
//        }
//        else
//        {
//            $this->xml->product->active = "0";
//        }
    }

    function nastavDostupnost($dostupnost)
    {
        $this->xml->product->available_date = $dostupnost;
    }

    function nastavDostupnostPozdeji($dostupnost)
    {
        $this->xml->product->available_later = $dostupnost;
    }

    function nastavKdyVydano($vydano)
    {
        $this->xml->product->published = $vydano;
    }

    function nastavKategorii($nacteneZanry)
    {
        $nacteneZanry[] = '2';
        unset($this->xml->product->associations->categories);
        $this->xml->product->associations->addChild('categories');
        foreach ($nacteneZanry as $nactenyZanr)
        {
            $this->xml->product->associations->categories->addChild("category")->addChild('id', $nactenyZanr);
        }
    }


    function nastavAnotaci($anotace)
    {
        $this->xml->product->description->language = $anotace;
        $this->xml->product->description_short->language = $this->shortAnot($anotace);
    }

    function nastavURLObrazku($url)
    {
        if(trim((string)$this->xml->product->image_link))
            $this->nastavenyObrazek = true;
        else
            $this->nastavenyObrazek = false;
        // Uloz si URL pro pozdejsi pouziti
        $this->xml->product->image_link = $url;
    }


    function NastavDataImportu($dataKnihyImport)
    {
        $this->nastavNazev($dataKnihyImport['name']);
        $this->nastavSirku($dataKnihyImport['width']);
        $this->nastavVysku($dataKnihyImport['height']);
        $this->nastavHloubku($dataKnihyImport['depth']);
        $this->nastavHmotnost($dataKnihyImport['weight']);
        $this->nastavISBN($dataKnihyImport['meta_title']);
        $this->nastavVazbu($dataKnihyImport['binding']);
        $this->nastavStran($dataKnihyImport['pages']);
        if (key_exists('author',$dataKnihyImport))
            $this->nastavAutor($dataKnihyImport['author']);
        $this->nastavNakladatel($dataKnihyImport['publisher']);
        $this->nastavEAN($dataKnihyImport['ean']);
        if (key_exists('meta_keywords',$dataKnihyImport))
           $this->nastavPuvodniKodKategorie($dataKnihyImport['meta_keywords']);
        $this->nastavNakupniCenu((string)$dataKnihyImport['wholesale_price']);
        $this->nastavDph($dataKnihyImport['id_tax_rules_group']);
        $this->nastavCenu((string)$dataKnihyImport['price']);
        $this->nastavImportId($dataKnihyImport['importId']);
        if (key_exists('published',$dataKnihyImport))
            $this->nastavKdyVydano($dataKnihyImport['published']);
        $this->nastavJeliAktivni($dataKnihyImport['active']);
        $this->nastavDostupnostPozdeji($dataKnihyImport['available_later']);
        if (key_exists('available_date',$dataKnihyImport))
            $this->nastavDostupnost($dataKnihyImport['available_date']);
        $this->nastavKategorii($dataKnihyImport['genres']);
        $this->nastavAnotaci($dataKnihyImport['description']);
        if (key_exists('image_link',$dataKnihyImport))
            $this->nastavURLObrazku($dataKnihyImport['image_link']);
    }


    function odesliObrazek()
    {
        global $prestaUrl;
        global $apiUser;

        if(!trim((string)$this->xml->product->image_link))
            return;
        if($this->nastavenyObrazek)
            return;
        $img = file_get_contents((string)$this->xml->product->image_link);
        $fname = "/tmp/" . uniqid() . ".jpg";
        file_put_contents($fname, $img);
        exec("/usr/bin/curl -X POST --user \"$apiUser:\" -F image=@$fname " . $prestaUrl . "images/products/$this->prestaId/ 2>&1 ");
        unlink($fname);
    }

    function predUlozenim()
    {
        $rewrite = $this->xml->product->ean13 . "-" . $this->xml->product->meta_title->language;
        $this->xml->product->link_rewrite->language = $this->vytvorRewrite($rewrite);

        // Odstraneni read-only XML elementu
        unset($this->xml->product->manufacturer_name);
        unset($this->xml->product->quantity);
    }

    function poUlozeni()
    {
        $this->odesliObrazek();
    }

    function vytvorRewrite($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);
    }

}

Zerion Mini Shell 1.0