<?php

class zcommerceexportermulti extends Module {

  private $_html = '';
  private $_postErrors = array();
  private $stockManagement;

  public function __construct() {
    $this->name = 'zcommerceexportermulti';
    $this->tab = 'Tools';
    $this->version = '1.5.3';

    parent::__construct();
    if (!extension_loaded("Zip")) {
      $shops = $this->_getShops();
      foreach ($shops as $shop) {
        Configuration::updateGlobalValue('PE_HEUREKA_FORMAT_SHOP' . $shop["id_shop"], "xml");
        Configuration::updateGlobalValue('PE_HLEDEJCENY_FORMAT_SHOP' . $shop["id_shop"], "xml");
        Configuration::updateGlobalValue('PE_HYPERZBOZI_FORMAT_SHOP' . $shop["id_shop"], "xml");
        Configuration::updateGlobalValue('PE_JYXO_FORMAT_SHOP' . $shop["id_shop"], "xml");
        Configuration::updateGlobalValue('PE_NEJLEPSICENY_FORMAT_SHOP' . $shop["id_shop"], "xml");
        Configuration::updateGlobalValue('PE_ZBOZI_FORMAT_SHOP' . $shop["id_shop"], "xml");
        Configuration::updateGlobalValue('PE_GOOGLE_FORMAT_SHOP' . $shop["id_shop"], "xml");
      }
    }

    $this->displayName = $this->l('Zcommerce: XML Generátor (Multi)');
    $this->description = $this->l('Vytváří soubory pro registraci produktů do vyhledávačů zboží (s podporou pro multishop)');
  }

  public function install() {
    if (!parent::install())
      return false;

    Db::getInstance()->Execute('CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'product_messages_to_days` (
  `id_message` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `message` varchar(255) UNIQUE,
  `days` int(2) unsigned DEFAULT "3",
  PRIMARY KEY (`id_message`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1');

    $shops = $this->_getShops();
    foreach ($shops as $shop) {
      Configuration::updateGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $shop["id_shop"], "heureka_cz");
      Configuration::updateGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $shop["id_shop"], "hledejceny_cz");
      Configuration::updateGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $shop["id_shop"], "hyperzbozi_cz");
      Configuration::updateGlobalValue('PE_JYXO_NAZEV_SHOP' . $shop["id_shop"], "jyxo_cz");
      Configuration::updateGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $shop["id_shop"], "nejlepsiceny_cz");
      Configuration::updateGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $shop["id_shop"], "zbozi_cz");
      Configuration::updateGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $shop["id_shop"], "google_cz");
      
      Configuration::updateGlobalValue('PE_HEUREKA_DESCR_SHOP' . $shop["id_shop"], "short");
      Configuration::updateGlobalValue('PE_HLEDEJCENY_DESCR_SHOP' . $shop["id_shop"], "short");
      Configuration::updateGlobalValue('PE_HYPERZBOZI_DESCR_SHOP' . $shop["id_shop"], "short");
      Configuration::updateGlobalValue('PE_JYXO_DESCR_SHOP' . $shop["id_shop"], "short");
      Configuration::updateGlobalValue('PE_NEJLEPSICENY_DESCR_SHOP' . $shop["id_shop"], "short");
      Configuration::updateGlobalValue('PE_ZBOZI_DESCR_SHOP' . $shop["id_shop"], "short");
      Configuration::updateGlobalValue('PE_GOOGLE_DESCR_SHOP' . $shop["id_shop"], "short");

      if (!Configuration::getGlobalValue('PS_STOCK_MANAGEMENT', null, null, $shops[0]["id_shop"])) {
        Configuration::updateGlobalValue('PE_DELIVERY_DATE_SHOP' . $shop["id_shop"], "3");
      } else {
        Configuration::updateGlobalValue('PE_DELIVERY_DATE_SHOP_IN_STOCK' . $shop["id_shop"], "3");
        Configuration::updateGlobalValue('PE_DELIVERY_DATE_SHOP_NIN_STOCK' . $shop["id_shop"], "7");
      }
    }

    Configuration::updateGlobalValue('ZCE_CRON_PASS', mt_rand(111111, 999999));
    return true;
  }

  public function getContent() {
    $this->_html = '<h2>' . $this->displayName . '</h2>';
    $this->_postProcess();
    $this->_displayForm();
    return $this->_html;
  }

  public function cronTask($id_shop, $feeds) {
    $this->_postProcess(true, $id_shop, $feeds);
  }

  private function _getMessages() {

      $query = 'SELECT DISTINCT `available_now`, `available_later` FROM ' . _DB_PREFIX_ . 'product_lang WHERE id_lang = "' . Configuration::get("PS_LANG_DEFAULT") . '" ORDER BY available_now ASC';
      $messages = Db::getInstance()->ExecuteS($query);
      $messagesDistinct = array();

      if (sizeof($messages) > 0) {
        foreach ($messages as $message) {
          foreach ($message as $type) {
            $type = trim($type);
            if (!empty($type)) {
              if (!in_array($type, $messagesDistinct)) {
                $messagesDistinct['mes_'.md5($type)] = $type;
              }
            }
          }
        }
      }
      
      return $messagesDistinct;
  }
  
  private function _displayForm() {
    require_once dirname(__FILE__) . '/ShopConfig.php';

    if (!extension_loaded("Zip"))
      $this->_html .= '<div class="alert error">Nelze provádět export do formátu zip - na serveru není nainstalováno zip rozšíření pro php.</div>';

    $pocetShopu = Db::getInstance()->ExecuteS('SELECT COUNT(id_shop) FROM ' . _DB_PREFIX_ . 'shop');
    if (($pocetShopu > 1) && (sizeof($this->_getShops()) == 1) && (Configuration::getGlobalValue("PS_MULTISHOP_FEATURE_ACTIVE"))) {
      $this->_html .= '<div class="alert error"><strong>Free verze tohoto modulu neumožnuje využití funkce multistore. Kontakt pro získání plné verze: kopecky@zserver.cz.</strong></div>';
    }

    if ($this->stockManagement) {

      $messagesDistinct = $this->_getMessages();

      if (!empty($messagesDistinct)) {
        $token = Tools::getAdminTokenLite('AdminModules');
        $this->_html .= '
      <fieldset><legend><img src="' . $this->_path . 'logo.gif" />Převod popisků na dny</legend>
      <p>U některých produktů v e-shopu se vyskytují popisky, které se zobrazí, pokud zboží bude/nebude skladem. Níže máte možnost určit, kolik dnů do expedice daný popisek vyjadřuje. Tyto informace budou zohledněny při vytváření feedů do katalogů zboží. U každého popisku zadejte číslo.</p>
      <form action="?tab=AdminModules&configure=' . $this->name . '&token=' . $token . '&tab_module=' . $this->tab . '&module_name=' . $this->name . '" method="post">
      <br><table>';

        foreach ($messagesDistinct as $key => $message) {
          $day = Db::getInstance()->getValue('SELECT `days` FROM ' . _DB_PREFIX_ . 'product_messages_to_days WHERE `message` = "'.$message.'"');
          if (!is_numeric($day)) {
            $day = "";
          }
          $this->_html .= '
            <tr>
              <td><label for="'.$key.'" style="float:none">' . $message . ':</label></td>
              <td><input type="text" id="'.$key.'" name="message['.$key.']" value="' . $day . '" /></td>
            </tr>';
        }

        $this->_html .= '<tr><td colspan="2"><br><input name="updateMessages" class="button" type="submit" value="AKTUALIZOVAT" /></td></tr>';

        $this->_html .= '</table></form></fieldset><br />';
      }

    }

    $this->_html .= '

    <style type="text/css">
    #productexporter table { border-collapse: collapse; border: 1px solid #BBB; font-size: 12px; background: #fff; color: #000; margin: 10px 0 20px 0; }
    #productexporter tr { border: 1px solid #BBB; }
    #productexporter td { border: 1px solid #BBB; padding: 6px 12px; }
    #productexporter th { border: 1px solid #BBB; padding: 6px 12px; background: #808080; text-align: left; font-weight: bold; color: #fff; }
    .seznam { list-style-type: square; margin: 10px 0 15px 15px; line-height: 1.5 }
    </style>

    <fieldset><legend><img src="' . $this->_path . 'logo.gif" />Informace k použití</legend>
    <p>Minimální délka názvu výstupního souboru jsou 3 znaky. Název nesmí obsahovat diakritiku a speciální znaky, jako jsou mezery, otazníky aj. 
    Název souboru uvádějte bez přípony - v závislosti na Vaší volbě (xml/zip) se přidá k názvu souboru automaticky.</p>
    <p>Kliknutím na tlačítko VYGENEROVAT SOUBORY dojde k aktualizaci xml souborů potřebných pro registraci do vyhledávačů zboží. 
    Aktualizaci je nutné provést <b>po každé změně produktů nebo kategorií</b>.</p><p>Pro správnou funkci xml exportů pro vyhledávač <b>hledejceny.cz</b> 
    je třeba vyplnit u každého produktu kód zboží.</p>
</fieldset><br />';

    $shops = $this->_getShops();
    foreach ($shops as $shop) {
      $this->_displaySettings($shop);
    }

    $this->_html .= '
    <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
    <fieldset><legend><img src="' . $this->_path . 'logo.gif" />CRON</legend>';

    foreach ($shops as $shop) {
      $cron_files = array();
      if (Configuration::getGlobalValue('PE_HEUREKA_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 1;
      if (Configuration::getGlobalValue('PE_JYXO_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 2;
      if (Configuration::getGlobalValue('PE_HLEDEJCENY_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 3;
      if (Configuration::getGlobalValue('PE_NEJLEPSICENY_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 4;
      if (Configuration::getGlobalValue('PE_HYPERZBOZI_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 5;
      if (Configuration::getGlobalValue('PE_ZBOZI_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 6;
      if (Configuration::getGlobalValue('PE_GOOGLE_GENEROVAT_SHOP' . $shop["id_shop"]) == 1)
        $cron_files[] = 7;

      $this->_html .= '<h3>' . $shop["name"] . '</h3>';
      if (empty($cron_files)) {
        $this->_html .= 'V nastavení musíte nejprve vybrat, které soubory pro vyhledávače zboží chcete generovat.';
      } else {
        $this->_html .= '
          <ul class="seznam">
          ' . (in_array(1, $cron_files) ? '<li><strong>Heureka.cz:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 1) . '</em></li>' : "") . '
          ' . (in_array(2, $cron_files) ? '<li><strong>Jyxo.cz:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 2) . '</em></li>' : "") . '
          ' . (in_array(3, $cron_files) ? '<li><strong>Hledejceny.cz:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 3) . '</em></li>' : "") . '
          ' . (in_array(4, $cron_files) ? '<li><strong>Nejlepšíceny.cz:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 4) . '</em></li>' : "") . '
          ' . (in_array(5, $cron_files) ? '<li><strong>Hyperzboží.cz:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 5) . '</em></li>' : "") . '
          ' . (in_array(6, $cron_files) ? '<li><strong>Zboží.cz:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 6) . '</em></li>' : "") . '
          ' . (in_array(7, $cron_files) ? '<li><strong>Google.cz/shopping:</strong> <em>' . $this->_getCronURI($shop["id_shop"], 7) . '</em></li>' : "") . '
          <li><strong>Všechny povolené feedy:</strong> <em>' . $this->_getCronURI($shop["id_shop"]) . '</em></li>
           </ul>';
      }
    }

    $this->_html .= '
          </fieldset><br />
          </form>';
  }

  private function _getCronURI($id_shop, $feeds = null) {
    if (!empty($feeds)) {
      return Tools::getHttpHost(true, true) . __PS_BASE_URI__ . 'modules/' . $this->name . '/cron.php?secure_key=' . Configuration::getGlobalValue('ZCE_CRON_PASS') . '&id_shop=' . $id_shop . '&feeds=' . $feeds;
    }
    return Tools::getHttpHost(true, true) . __PS_BASE_URI__ . 'modules/' . $this->name . '/cron.php?secure_key=' . Configuration::getGlobalValue('ZCE_CRON_PASS') . '&id_shop=' . $id_shop;
  }

  private function _getShops() {
    require_once dirname(__FILE__) . '/ShopConfig.php';
    $shops = Db::getInstance()->ExecuteS('SELECT DISTINCT id_shop, name, domain, physical_uri FROM ' . _DB_PREFIX_ . 'shop NATURAL JOIN ' . _DB_PREFIX_ . 'shop_url WHERE main = "1"' . (!Config::getConfig() ? " LIMIT 1" : ""));
    // je zapnuty multishop nebo je shop pouze 1
    if (Tools::safeOutput(Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE')) || (sizeof($shops) == 1)) {
      return $shops;
    }
    // jinak je vypnuty multishop a musim vratit pouze hlavni shop
    else {
      $selectedKey = -1;
      foreach ($shops as $key => $shop) {
        if ($shop["physical_uri"] === __PS_BASE_URI__) {
          $selectedKey = $key;
          break;
        }
      }
      if ($selectedKey != -1) {
        return array($shops[$selectedKey]);
      } else {
        return array($shops[0]);
      }
      return $shops;
    }
  }

  private function _displaySettings($shop) {
    $shops = $this->_getShops();

    $shops_uri = array();
    foreach ($shops as $theshop) {
      $exp_uri = $theshop["domain"] . $theshop["physical_uri"] . 'rss/';
      $shops_uri[$theshop["id_shop"]] = Tools::getShopProtocol() . $exp_uri;
    }    

    $token = Tools::getAdminTokenLite('AdminModules');

    $this->_html .= '
    <form action="?tab=AdminModules&configure=' . $this->name . '&token=' . $token . '&tab_module=' . $this->tab . '&module_name=' . $this->name . '" method="post">
    <fieldset><legend><img src="' . $this->_path . 'logo.gif" />Nastavení'.( (Tools::safeOutput(Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE')) == 1) ? ' &ndash; ' . $shop["name"] : "").'</legend>
    <table>';

    if (!$this->stockManagement) {

      $this->_html .= '
        <tr>
        <td><label for="delivery_date_shop' . $shop["id_shop"] . '" style="float:none">Doba dodání ve dnech:</label></td>
        <td><input type="text" id="delivery_date_shop' . $shop["id_shop"] . '" name="pe_delivery_date_shop' . $shop["id_shop"] . '" value="' . Tools::safeOutput(Configuration::get('PE_DELIVERY_DATE_SHOP' . $shop["id_shop"])) . '" /></td>
        </tr>';
    } else {

      $this->_html .= '
        <tr>
        <td><label for="delivery_date_shop_in_stock' . $shop["id_shop"] . '" style="float:none">Doba dodání ve dnech, pokud je zboží skladem (zadejte 0 pro zobrazení "skladem"):</label></td>
        <td><input type="text" id="delivery_date_shop_in_stock' . $shop["id_shop"] . '" name="pe_delivery_date_shop_in_stock' . $shop["id_shop"] . '" value="' . Tools::safeOutput(Configuration::get('PE_DELIVERY_DATE_SHOP_IN_STOCK' . $shop["id_shop"])) . '" /></td>
          </tr>';

      $this->_html .= '
        <tr>
        <td><label for="delivery_date_shop_nin_stock' . $shop["id_shop"] . '" style="float:none">Doba dodání ve dnech, pokud není zboží skladem:</label></td>
        <td><input type="text" id="delivery_date_shop_nin_stock' . $shop["id_shop"] . '" name="pe_delivery_date_shop_nin_stock' . $shop["id_shop"] . '" value="' . Tools::safeOutput(Configuration::get('PE_DELIVERY_DATE_SHOP_NIN_STOCK' . $shop["id_shop"])) . '" /></td>
          </tr>';
    }

    $this->_html .= '
<tr>
<td>
<label for="shop_depots_shop' . $shop["id_shop"] . '" style="float:none">Identifikátor výdejního místa (pro zbozi.cz):</label>
</td>
<td>
<input type="text" id="shop_depots_shop' . $shop["id_shop"] . '" name="pe_shop_depots_shop' . $shop["id_shop"] . '" value="' . Tools::safeOutput(Configuration::get('PE_SHOP_DEPOTS_SHOP' . $shop["id_shop"])) . '" /><br>
</td>
</tr>
<tr>
<td colspan="2"><small>více <a target="_blank" href="http://napoveda.seznam.cz/cz/zbozi/napoveda-pro-internetove-obchody/specifikace-xml/#SHOP_DEPOTS">zde (dokumentace zbozi.cz)</a></small></td>
</tr>
</table>

    <div id="productexporter">
  <table>
  <tr>
    <th>#</th>
    <th>Název služby</th>
    <th>Název souboru</th>
    <th>Přípona souboru</th>
    <th>Popisek</th>
    <th>Generovat</th>
    <th>URL adresa výstupního souboru</th>
  </tr>

  <tr>
    <td>1</td>
    <td><strong>Heureka.cz</strong></td>
    <td><input type="text" name="pe_heureka_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_heureka_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_HEUREKA_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_heureka_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_HEUREKA_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_heureka_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_HEUREKA_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_heureka_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_HEUREKA_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_heureka_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_HEUREKA_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_HEUREKA_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $shop["id_shop"]) : "zbozi_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_HEUREKA_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

  <tr>
    <td>2</td>
    <td><strong>Jyxo.cz</strong></td>
    <td><input type="text" name="pe_jyxo_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_JYXO_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_jyxo_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_JYXO_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_jyxo_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_JYXO_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_jyxo_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_JYXO_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_jyxo_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_JYXO_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_jyxo_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_JYXO_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_JYXO_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_JYXO_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_JYXO_NAZEV_SHOP' . $shop["id_shop"]) : "jyxo_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_JYXO_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

  <tr>
    <td>3</td>
    <td><strong>Hledejceny.cz</strong></td>
    <td><input type="text" name="pe_hledejceny_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_hledejceny_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_HLEDEJCENY_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_hledejceny_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_HLEDEJCENY_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_hledejceny_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_HLEDEJCENY_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_hledejceny_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_HLEDEJCENY_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_hledejceny_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_HLEDEJCENY_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_HLEDEJCENY_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $shop["id_shop"]) : "hledejceny_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_HLEDEJCENY_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

  <tr>
    <td>4</td>
    <td><strong>Nejlepšíceny.cz</strong></td>
    <td><input type="text" name="pe_nejlepsiceny_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_nejlepsiceny_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_nejlepsiceny_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_nejlepsiceny_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_nejlepsiceny_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_nejlepsiceny_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_NEJLEPSICENY_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $shop["id_shop"]) : "nejlepsiceny_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

  <tr>
      <td>5</td>
    <td><strong>Hyperzboží.cz</strong></td>
    <td><input type="text" name="pe_hyperzbozi_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_hyperzbozi_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_HYPERZBOZI_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_hyperzbozi_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_HYPERZBOZI_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_hyperzbozi_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_HYPERZBOZI_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_hyperzbozi_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_HYPERZBOZI_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_hyperzbozi_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_HYPERZBOZI_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_HYPERZBOZI_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $shop["id_shop"]) : "hyperzbozi_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_HYPERZBOZI_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

  <tr>
      <td>6</td>
    <td><strong>Zboží.cz</strong></td>
    <td><input type="text" name="pe_zbozi_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_zbozi_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_ZBOZI_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_zbozi_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_ZBOZI_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_zbozi_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_ZBOZI_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_zbozi_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_ZBOZI_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_zbozi_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_ZBOZI_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_ZBOZI_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $shop["id_shop"]) : "zbozi_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_ZBOZI_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

  <tr>
      <td>7</td>
    <td><strong>Google.cz/shopping</strong></td>
    <td><input type="text" name="pe_google_nazev_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $shop["id_shop"]) . '" /></td>
    <td>
      <input type="radio" name="pe_google_format_shop' . $shop["id_shop"] . '" value="xml" ' . (( Configuration::getGlobalValue('PE_GOOGLE_FORMAT_SHOP' . $shop["id_shop"]) != "zip") ? 'checked="checked"' : "") . ' /> .xml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_google_format_shop' . $shop["id_shop"] . '" value="zip" ' . (( Configuration::getGlobalValue('PE_GOOGLE_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? 'checked="checked"' : "") . ' ' . ((!extension_loaded("Zip")) ? 'disabled="disabled"' : "") . ' /> .zip
    </td>
    <td>
      <input type="radio" name="pe_google_descr_shop' . $shop["id_shop"] . '" value="short" ' . (( Configuration::getGlobalValue('PE_GOOGLE_DESCR_SHOP' . $shop["id_shop"]) != "long") ? 'checked="checked"' : "") . ' /> krátký &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="radio" name="pe_google_descr_shop' . $shop["id_shop"] . '" value="long" ' . (( Configuration::getGlobalValue('PE_GOOGLE_DESCR_SHOP' . $shop["id_shop"]) == "long") ? 'checked="checked"' : "") . ' /> dlouhý
    </td>
    <td><input type="checkbox" name="pe_google_generovat_shop' . $shop["id_shop"] . '" value="' . Configuration::getGlobalValue('PE_GOOGLE_GENEROVAT_SHOP' . $shop["id_shop"]) . '" ' . (( Configuration::getGlobalValue('PE_GOOGLE_GENEROVAT_SHOP' . $shop["id_shop"]) == 1) ? 'checked="checked"' : "") . ' /></td>
    <td>' . $shops_uri[$shop["id_shop"]] . ((strlen(Configuration::getGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $shop["id_shop"])) > 2) ? Configuration::getGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $shop["id_shop"]) : "google_cz") . ( (sizeof($shops) > 1) ? '_' . $shop["id_shop"] : "" ) . (( Configuration::getGlobalValue('PE_GOOGLE_FORMAT_SHOP' . $shop["id_shop"]) == "zip") ? ".zip" : ".xml") . '</td>
  </tr>

</table>

    </div>

    <input type="hidden" name="id_shop" value="' . $shop["id_shop"] . '" />
    <input name="updateButton" class="button" type="submit" value="AKTUALIZOVAT NASTAVENÍ" />&nbsp;&nbsp;&nbsp;
    <input class="button" name="generovat" id="generovat" value="VYGENEROVAT SOUBORY" type="button" onclick="document.location = \'?tab=AdminModules&configure=' . $this->name . '&generate=1&id_shop=' . $shop["id_shop"] . '&token=' . $token . '&tab_module=' . $this->tab . '&module_name=' . $this->name . '\'" />

    </fieldset><br />
    </form>';
  }

  private function _postProcess($cron = false, $id_shop = null, $feeds = array()) {
    $this->stockManagement = Configuration::getGlobalValue('PS_STOCK_MANAGEMENT');
    require_once dirname(__FILE__) . '/ShopConfig.php';
    
    if (Tools::isSubmit('updateMessages')) {
      if (!empty($_POST["message"])) {
        Db::getInstance()->Execute('TRUNCATE TABLE `' . _DB_PREFIX_ . 'product_messages_to_days`');
        
        $messages = $this->_getMessages();
        
        foreach ($_POST["message"] as $key => $message) {
          if ((!empty($message) || ($message == 0)) && is_numeric($message) && array_key_exists($key, $messages)) {            
            Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'product_messages_to_days` (`message`, `days`) VALUES ("'.$messages[$key].'", "'.$message.'")');
          }
        }
      }
      
    } 
    
    if (Tools::isSubmit('updateButton') || Tools::isSubmit('generate') || ($cron)) {
      $shops = $this->_getShops();
      $shop_ids = array();
      foreach ($shops as $shop) {
        $shop_ids[] = $shop["id_shop"];
      }
      if (Tools::getValue('id_shop')) {
        $id_shop = Tools::getValue('id_shop');
      }
      if (!in_array($id_shop, $shop_ids)) {
        $this->_html .= '<div class="alert error">Chybějící ID e-shopu.</div>';
      }
    }

    if (Tools::isSubmit('updateButton')) {
      $saveError = false;
      if (!Configuration::updateGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_heureka_nazev_shop' . $id_shop)) > 2) ? $this->_getSlug(Tools::getValue('pe_heureka_nazev_shop' . $id_shop)) : "heureka_cz")) ||
              !Configuration::updateGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_hledejceny_nazev_shop' . $id_shop)) != "") ? $this->_getSlug(Tools::getValue('pe_hledejceny_nazev_shop' . $id_shop)) : "hledejceny_cz")) ||
              !Configuration::updateGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_hyperzbozi_nazev_shop' . $id_shop)) > 2) ? $this->_getSlug(Tools::getValue('pe_hyperzbozi_nazev_shop' . $id_shop)) : "hyperzbozi_cz")) ||
              !Configuration::updateGlobalValue('PE_JYXO_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_jyxo_nazev_shop' . $id_shop)) > 2) ? $this->_getSlug(Tools::getValue('pe_jyxo_nazev_shop' . $id_shop)) : "jyxo_cz")) ||
              !Configuration::updateGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_nejlepsiceny_nazev_shop' . $id_shop)) > 2) ? $this->_getSlug(Tools::getValue('pe_nejlepsiceny_nazev_shop' . $id_shop)) : "nejlepsiceny_cz")) ||
              !Configuration::updateGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_zbozi_nazev_shop' . $id_shop)) > 2) ? $this->_getSlug(Tools::getValue('pe_zbozi_nazev_shop' . $id_shop)) : "zbozi_cz")) ||
              !Configuration::updateGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_google_nazev_shop' . $id_shop)) > 2) ? $this->_getSlug(Tools::getValue('pe_google_nazev_shop' . $id_shop)) : "google_cz")) ||
              !Configuration::updateGlobalValue('PE_HEUREKA_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_heureka_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_HLEDEJCENY_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_hledejceny_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_HYPERZBOZI_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_hyperzbozi_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_JYXO_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_jyxo_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_NEJLEPSICENY_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_nejlepsiceny_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_ZBOZI_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_zbozi_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_GOOGLE_GENEROVAT_SHOP' . $id_shop, ($this->_getSlug(strlen(Tools::getValue('pe_google_generovat_shop' . $id_shop)) == 1) ? 1 : 0)) ||
              !Configuration::updateGlobalValue('PE_HEUREKA_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_heureka_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_HLEDEJCENY_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_hledejceny_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_HYPERZBOZI_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_hyperzbozi_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_JYXO_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_jyxo_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_NEJLEPSICENY_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_nejlepsiceny_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_ZBOZI_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_zbozi_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_GOOGLE_FORMAT_SHOP' . $id_shop, ((Tools::getValue('pe_google_format_shop' . $id_shop) == "zip") ? "zip" : "xml")) ||
              !Configuration::updateGlobalValue('PE_HEUREKA_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_heureka_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_HLEDEJCENY_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_hledejceny_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_HYPERZBOZI_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_hyperzbozi_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_JYXO_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_jyxo_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_NEJLEPSICENY_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_nejlepsiceny_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_ZBOZI_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_zbozi_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_GOOGLE_DESCR_SHOP' . $id_shop, ((Tools::getValue('pe_google_descr_shop' . $id_shop) == "long") ? "long" : "short")) ||
              !Configuration::updateGlobalValue('PE_SHOP_DEPOTS_SHOP' . $id_shop, Tools::getValue('pe_shop_depots_shop' . $id_shop))
      ) {
        $saveError = true;
      }

      if (!$this->stockManagement) {
        if (!Configuration::updateGlobalValue('PE_DELIVERY_DATE_SHOP' . $id_shop, Tools::getValue('pe_delivery_date_shop' . $id_shop))) {
          $saveError = true;
        }
      } else {
        if (!Configuration::updateGlobalValue('PE_DELIVERY_DATE_SHOP_IN_STOCK' . $id_shop, Tools::getValue('pe_delivery_date_shop_in_stock' . $id_shop)) ||
                !Configuration::updateGlobalValue('PE_DELIVERY_DATE_SHOP_NIN_STOCK' . $id_shop, Tools::getValue('pe_delivery_date_shop_nin_stock' . $id_shop))) {
          $saveError = true;
        }
      }

      if ($saveError) {
        $this->_html .= '<div class="alert error">Nastavení se nepodařilo aktualizovat.</div>';
      } else {
        $this->_html .= '<div class="conf confirm">Nastavení aktualizováno.</div>';
      }
    } else if (Tools::isSubmit('generate') || ($cron)) {

      require dirname(__FILE__) . '/ShopLogger.php';

      ShopLogger::getInstance()->initialize(new Logconfig());
      require dirname(__FILE__) . '/ShopExporter.php';
      set_time_limit(0);

      $filenames = array(
          ((strlen(Configuration::getGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_ZBOZI_NAZEV_SHOP' . $id_shop) : "zbozi_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_ZBOZI_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml"),
          ((strlen(Configuration::getGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_HEUREKA_NAZEV_SHOP' . $id_shop) : "heureka_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_HEUREKA_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml"),
          ((strlen(Configuration::getGlobalValue('PE_JYXO_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_JYXO_NAZEV_SHOP' . $id_shop) : "jyxo_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_JYXO_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml"),
          ((strlen(Configuration::getGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_HLEDEJCENY_NAZEV_SHOP' . $id_shop) : "hledejceny_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_HLEDEJCENY_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml"),
          ((strlen(Configuration::getGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_NEJLEPSICENY_NAZEV_SHOP' . $id_shop) : "nejlepsiceny_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_NEJLEPSICENY_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml"),
          ((strlen(Configuration::getGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_HYPERZBOZI_NAZEV_SHOP' . $id_shop) : "hyperzbozi_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_HYPERZBOZI_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml"),
          ((strlen(Configuration::getGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $id_shop)) > 2) ? Configuration::getGlobalValue('PE_GOOGLE_NAZEV_SHOP' . $id_shop) : "google_cz") . ( (sizeof($shops) > 1) ? '_' . $id_shop : "" ) . (( Configuration::getGlobalValue('PE_GOOGLE_FORMAT_SHOP' . $id_shop) == "zip") ? ".zip" : ".xml")
      );

      $export_types = array();

      if (!$cron) {

        if (Configuration::getGlobalValue('PE_HEUREKA_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::HEUREKA_CZ;
        if (Configuration::getGlobalValue('PE_JYXO_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::JYXO_CZ;
        if (Configuration::getGlobalValue('PE_HLEDEJCENY_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::HLEDEJCENY_CZ;
        if (Configuration::getGlobalValue('PE_NEJLEPSICENY_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::NEJLEPSICENY_CZ;
        if (Configuration::getGlobalValue('PE_HYPERZBOZI_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::HYPERZBOZI_CZ;
        if (Configuration::getGlobalValue('PE_ZBOZI_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::ZBOZI_CZ;
        if (Configuration::getGlobalValue('PE_GOOGLE_GENEROVAT_SHOP' . $id_shop) == 1)
          $export_types[] = ShopExporter::GOOGLE_CZ;
      } else { // je to cron - generuji, pokud je ve feeds, nebo pokud je feeds prazdny - pak je povoleno generovat vse povolene
        if ((in_array(1, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_HEUREKA_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::HEUREKA_CZ;
        if ((in_array(2, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_JYXO_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::JYXO_CZ;
        if ((in_array(3, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_HLEDEJCENY_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::HLEDEJCENY_CZ;
        if ((in_array(4, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_NEJLEPSICENY_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::NEJLEPSICENY_CZ;
        if ((in_array(5, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_HYPERZBOZI_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::HYPERZBOZI_CZ;
        if ((in_array(6, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_ZBOZI_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::ZBOZI_CZ;
        if ((in_array(7, $feeds)) || (empty($feeds) && Configuration::getGlobalValue('PE_GOOGLE_GENEROVAT_SHOP' . $id_shop) == 1))
          $export_types[] = ShopExporter::GOOGLE_CZ;
      }

      $config = new ExportConfig($filenames, $export_types);
      $exporter = new ShopExporter($config, null, $this->stockManagement);

      try {
        $this->_html .= '<fieldset><legend><img src="' . $this->_path . 'logo.gif" />Report</legend>';

        foreach ($config->export_types as $type) {
          $filename = $config->filenames[$type];
          $exp_uri = Str_Replace("//", "/", __PS_BASE_URI__ . $config->export_path . $filename);
          $exp_href = Tools::getHttpHost(true, true) . $exp_uri;
          $this->_html .= date('H:i:s', time()) . ' Zahájeno vytváření exportu typu ' . ShopExporter::$export_type[$type] . ' &nbsp; --> &nbsp; <a target="_blank" style="text-decoration: underline;" href="' . $exp_href . '" title="' . $exp_href . '">' . $exp_href . '</a><br />';
          $exporter->export($type, $id_shop);
          $this->_html .= date('H:i:s', time()) . ' Ukončeno vytváření exportu typu ' . ShopExporter::$export_type[$type] . '<br />';
        }

        $this->_html .= '</fieldset><br />';
      } catch (Exception $e) {
        
      }
    }
  }

  private function _getSlug($nazev) {
    $url = $nazev;
    $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
    $url = trim($url, "-");
    $url = $this->_cs_utf2ascii($url);
    $url = strtolower($url);
    $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
    return $url;
  }

  private function _cs_utf2ascii($s) {
    static $tbl = array("\xc3\xa1" => "a",
 "\xc3\xa4" => "a",
 "\xc4\x8d" => "c",
 "\xc4\x8f" => "d",
 "\xc3\xa9" => "e",
 "\xc4\x9b" => "e",
 "\xc3\xad" => "i",
 "\xc4\xbe" => "l",
 "\xc4\xba" => "l",
 "\xc5\x88" => "n",
 "\xc3\xb3" => "o",
 "\xc3\xb6" => "o",
 "\xc5\x91" => "o",
 "\xc3\xb4" => "o", "\xc5\x99" => "r", "\xc5\x95" => "r", "\xc5\xa1" => "s", "\xc5\xa5" => "t", "\xc3\xba" => "u", "\xc5\xaf" => "u", "\xc3\xbc" => "u", "\xc5\xb1" => "u", "\xc3\xbd" => "y", "\xc5\xbe" => "z", "\xc3\x81" => "A", "\xc3\x84" => "A", "\xc4\x8c" => "C", "\xc4\x8e" => "D", "\xc3\x89" => "E", "\xc4\x9a" => "E", "\xc3\x8d" => "I", "\xc4\xbd" => "L", "\xc4\xb9" => "L", "\xc5\x87" => "N", "\xc3\x93" => "O", "\xc3\x96" => "O", "\xc5\x90" => "O", "\xc3\x94" => "O", "\xc5\x98" => "R", "\xc5\x94" => "R", "\xc5\xa0" => "S", "\xc5\xa4" => "T", "\xc3\x9a" => "U", "\xc5\xae" => "U", "\xc3\x9c" => "U", "\xc5\xb0" => "U", "\xc3\x9d" => "Y", "\xc5\xbd" => "Z");
    return strtr($s, $tbl);
  }

}
