%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/waritko/import-presta/
Upload File :
Create Path :
Current File : //home/waritko/import-presta/Nakladatel.php

<?php

/**
 * Created by PhpStorm.
 * User: Ludek
 * Date: 10. 9. 2016
 * Time: 14:05
 */
class Nakladatel
{
    private $eanCache = null;

    function eanToId($ean)
    {
        if($this->eanCache == null)
        {
            $result = dibi::query("SELECT [id_product], [import_id], [ean13] FROM [ps_product]");
            $this->eanCache = array();
            foreach ($result as $res)
            {
                $retVal = new stdClass();
                $retVal->importId = $res->import_id;
                $retVal->id = $res->id_product;
                $this->eanCache[$res->ean13] = $retVal;
            }
        }
        //print_r($this->eanCache);
//        $result = dibi::query("SELECT [id_product], [import_id] FROM [ps_product] WHERE [ean13]=%s", $ean);
//        if(count($result) == 0)
//            return false;
//        $row = $result->fetch();
//        $retVal = new stdClass();
//        $retVal->importId = $row->import_id;
//        $retVal->id = $row->id_product;
        if(!isset($this->eanCache[$ean])) return false;
        return $this->eanCache[$ean];
    }

    function zjistiDostupnost($ean)
    {
        /*        $result = dibi::query("select [ps_product].[id_product], [ps_product_lang].[available_later]  from [ps_product]
                join [ps_product_lang] on [ps_product].[id_product]=[ps_product_lang].[id_product]
                where [ps_product_lang].[id_lang]=1 AND [ps_product].[ean13]=%s", $ean)->fetch(); */

        if ($ean == "")
            return false;
        $result = dibi::query("SELECT [id_product], [published], [available_date] FROM [ps_product] WHERE [ean13]=%s", $ean);
        if(count($result) == 0)
            return false;
        $row = $result->fetch();
        $retVal = new stdClass();
        $retVal->idKnihy = $row->id_product;
        $retVal->published = $row->published;
        $retVal->available_date = $row->available_date;
        $result = dibi::query("SELECT [available_later], [Kde_dostupne] FROM [ps_product_lang] WHERE [id_product]=%s", $retVal->idKnihy);
        if(count($result) == 0)
            return false;
        $row = $result->fetch();
        $retVal->available_later = $row->available_later;
        $retVal->Kde_dostupne = $row->Kde_dostupne;
        return $retVal;
    }

    function nactiDataSQL($ean)
    {
        /*        $result = dibi::query("select [ps_product].[id_product], [ps_product_lang].[available_later]  from [ps_product]
                join [ps_product_lang] on [ps_product].[id_product]=[ps_product_lang].[id_product]
                where [ps_product_lang].[id_lang]=1 AND [ps_product].[ean13]=%s", $ean)->fetch(); */

        $result = dibi::query("SELECT [id_product], [price], [wholesale_price], [width], [height], [depth], [weight], [id_tax_rules_group], 
                        [pages], [author], [binding], [published], [publisher], [image_link], [available_date], [active] FROM [ps_product] WHERE [ean13]=%s", $ean);
        if(count($result) == 0)
            return false;
        $row = $result->fetch();
        $idKnihy =  $row->id_product;
        $dataKnihySQL = array();
        $dataKnihySQL['id_product'] = $row->id_product;
        $dataKnihySQL['price'] = round($row->price,2);
        $dataKnihySQL['wholesale_price'] = round($row->wholesale_price,2);
        $dataKnihySQL['width'] = $row->width;
        $dataKnihySQL['height'] = $row->height;
        $dataKnihySQL['depth'] = $row->depth;
        $dataKnihySQL['weight'] = $row->weight;
        $dataKnihySQL['id_tax_rules_group'] = $row->id_tax_rules_group;
        $dataKnihySQL['pages'] = $row->pages;
        $dataKnihySQL['author'] = trim($row->author);
        $dataKnihySQL['binding'] = trim($row->binding);
        $dataKnihySQL['published'] = $row->published;
        $dataKnihySQL['publisher'] = trim($row->publisher);
        $dataKnihySQL['image_link'] = $row->image_link;
        $dataKnihySQL['active'] = $row->active;
        $dataKnihySQL['available_date'] = $row->available_date;
        $result = dibi::query("SELECT [available_later], [meta_title], [meta_description], [description], [Kde_dostupne] FROM [ps_product_lang] WHERE [id_product]=%s", $idKnihy);
        if(count($result) == 0)
            return false;
        $row2 = $result->fetch();
        $dataKnihySQL['Kde_dostupne'] = trim($row2->Kde_dostupne);
        $dataKnihySQL['available_later'] = trim($row2->available_later);
        $dataKnihySQL['meta_title'] = trim($row2->meta_title);
        $dataKnihySQL['meta_description'] = trim($row2->meta_description);
        $dataKnihySQL['description'] = trim($row2->description);
        return $dataKnihySQL;
    }

    function PorovnejAZapisPamametryKnihy($dataKnihyImport,$dataKnihySQL)
    {
//        $dataKnihyImport['zanry'] = $nacteneZanry;
//        $dataKnihyImport['meta_keywords'] = (string)$kniha[15];

        $necoNesedi = False;
        $seznamDatPopisu = array('description','meta_description','meta_title','name','available_later','Kde_dostupne');
        $seznamDatShop = array('price', 'id_tax_rules_group', 'wholesale_price', 'pages', 'binding', 'author', 'publisher', 'published', 'available_date', 'image_link');
        $seznamDatPsProduct = array('width', 'height', 'depth', 'weight');
        $updateDataPsProduct = array();
        $updateDataPsProductShop = array();
        $updateDataPopisu = array();

        foreach ($dataKnihyImport as $klic => $polozka)             // prohleda pole importovanych dat, pokud najde rozdil zapise klic i hodnotu do prislusneho pole pro prikaz update
        {
            if(!key_exists($klic, $dataKnihySQL)) continue;

            $polozkaSql = trim($dataKnihySQL[$klic]);
            $polozka = trim($polozka);

            if (in_array($klic, $seznamDatPopisu))
            {
                if ($polozka != $polozkaSql && $polozka != "")
                {
                echo "Data popisu ---" .$polozka . "------" . $polozkaSql . "----\n";
                    $updateDataPopisu[$klic] = $polozka;
                }
            }
            else if (in_array($klic, $seznamDatShop))
            {
                if ($klic == 'price')
                    $polozka = round($polozka,2);
                if ($klic == 'published' or $klic == "available_date")
                {
                    $polozkaSql = date('Y-m-d', strtotime($polozkaSql));
                    $polozka = date('Y-m-d', strtotime($polozka));
                }
                if ($polozka != $polozkaSql)
                {
                    $updateDataPsProduct[$klic] = $polozka;
                    $updateDataPsProductShop[$klic] = $polozka;
                }
            }
            else if (in_array($klic, $seznamDatPsProduct))
            {
                if ($polozka != $polozkaSql)
                {
                    $updateDataPsProduct[$klic] = $polozka;
                }
            }
        }

        $idKnihy = $dataKnihySQL['id_product'];
        $kratkaAnotace = $this->shortAnot($dataKnihyImport['description']);

/*        echo " **************** Zmeny\n";
        print_r($updateDataPsProduct);
        echo " **************** /Zmeny\n";
*/
        if (!empty($updateDataPsProduct))                            // pokud je neco v poli provede prikaz update
        {
//            echo "Menim data!\n";
//            $aktivni = 1;
 //           $updateDataPsProduct['active'] = $aktivni;
 //           $updateDataPsProductShop['active'] = $aktivni;
            $necoNesedi = True;
            $this->zapisZmenuParametruProduktu($updateDataPsProduct, $updateDataPsProductShop, $idKnihy);
//            echo "\n klic a polozka -" . $klic . "---" . $polozka . "-- \n";
            echo "PS_PRODUCT\n";
            echo json_encode($updateDataPsProduct, JSON_PRETTY_PRINT);
            echo "-\n";
            echo "PS_PRODUCT_SHOP\n";
            echo json_encode($updateDataPsProductShop, JSON_PRETTY_PRINT);
            echo "-\n";
        }
        if (!empty($updateDataPopisu))                               // pokud je neco v poli provede prikaz update,
        {
            if (array_key_exists('description',$updateDataPopisu)) // pokud se ma updatovat popis, doplni i kratky popis
                $updateDataPopisu['description_short'] = $kratkaAnotace;
            $this->zapisZmenuPopisuProduktu($updateDataPopisu,$idKnihy);
            $necoNesedi = True;
            echo "PS_DESCRIPTION\n";
            foreach ($updateDataPopisu as $klic => $polozka)     // vypise polozky, ktere meni
            {
                if ($klic <> 'description')
                    echo $klic . " ==> " . $polozka . "\n";
            }
            echo " \n";
        }
        return $necoNesedi;
    }

    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 fsize($size)
    {
        $jedn = array("bytes", "Kbytes", "Mbytes", "Gbytes", "Tbytes", "Pbytes");
        //$size = filesize($name);
        $i = 0;
        while($size > 1023)
        {
            $size /= 1024;
            $i++;
        }
        return sprintf("%0.2f %s", $size, $jedn[$i]);
    }


    function downloadZipFile($url, $filepath)
    {
        $fp = fopen($filepath, 'w');
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        $i = 1;
        do
        {
            sleep(10);
            curl_exec($ch);
            $info = curl_getinfo($ch);
            $i++;
            if ($i > 60)
                return false;
            //            print_r($info);
        }
        while ($info['http_code'] != 200);
        curl_close($ch);
        fclose($fp);
        echo "Velikost nacteneho souboru je: " . $this->fsize(filesize($filepath)) . " \n";
        return (filesize($filepath) > 0)? true : false;
    }

    function zjistiCenu($ean)
    {
        return dibi::query("SELECT [price] FROM [ps_product] WHERE [ean13]=%s", $ean)->fetchSingle();
    }

    function zapisZmenuParametruProduktu($updateData,$updateDataShop,$idKnihy)
    {
//        dibi::update("ps_product", $updateData)->where("id_product = %i", $idKnihy)->test();
        dibi::update("ps_product", $updateData)->where("id_product = %i", $idKnihy)->execute();
        if (!empty($updateDataShop))
        {
//            dibi::update("ps_product_shop", $updateDataShop)->where("id_product = %i", $idKnihy)->test();
            dibi::update("ps_product_shop", $updateDataShop)->where("id_product = %i", $idKnihy)->execute();
        }
    }

    function zapisZmenuPopisuProduktu($updateData,$idKnihy)
    {
        dibi::update("ps_product_lang", $updateData)->where("id_product = %i", $idKnihy)->execute();
    }

    function zjistiKodDPH($ean)
    {
        $result = dibi::query("SELECT [id_product] FROM [ps_product] WHERE [ean13]=%s", $ean);
        if(count($result) == 0)
            return false;
        $row = $result->fetch();
        $this->idKnihy =  $row->id_product;
        $result = dibi::query("SELECT [id_tax_rules_group] FROM [ps_product_shop] WHERE [id_product]=%s", $this->idKnihy);
        if(count($result) == 0)
            return false;
        $row = $result->fetch();
        $this->cena = floatval($row->id_tax_rules_group);
        return true;
    }

}

Zerion Mini Shell 1.0