słowne public $mappings = array(); // mapowanie produktow function __construct() { // fix dla klasy link global $protocol_content; if (empty($protocol_content)) $protocol_content = 'http://'; $this->name = 'pricewars'; if($this-> isNewPS()) $this->tab = 'advertising_marketing'; else $this->tab = 'Advertisement'; $this->author = 'SEIGI Grzegorz Zawadzki'; $this->version = '1.9.12'; parent::__construct(); $mappings = glob(_PS_MODULE_DIR_.$this->name.'/mappings/*.php'); if(!defined("_PS_BASE_URL_")) define('_PS_BASE_URL_', $this->getShopHost(false)); /* The parent construct is required for translations */ $this->page = basename(__FILE__, '.php'); $this->displayName = $this->l('Price Wars!'); $this->description = $this->l('Generuje xml z ofertą sklepu dla Ceneo, Nokaut, Skąpiec, Radar, okazje.info i zakupy.onet.pl'); require(_PS_MODULE_DIR_.$this->name.'/ceneocategories.php'); $this->postProcess(); foreach ($mappings as $mapping) { require($mapping); if(isset($array_map) AND is_array($array_map)) foreach ($array_map as $array_map_key => $array_map_value) $this->mappings[$array_map_key] = $array_map_value; } } protected function getLangId() { return (Language::getIdByIso("pl") ? Language::getIdByIso("pl") : Configuration::get('PS_LANG_DEFAULT')); } public static $cache_getShopHost = NULL; protected function getShopHost($with_dir = true) { if (self::$cache_getShopHost === NULL) { if(strpos(_PS_VERSION_, '1.2') === 0) { self::$cache_getShopHost = 'http://'.$_SERVER['HTTP_HOST']; } elseif (strpos(_PS_VERSION_, '1.3') === 0) { self::$cache_getShopHost = Tools::getHttpHost(true); } else { self::$cache_getShopHost = Tools::getShopDomainSsl(true); } } return self::$cache_getShopHost . ($with_dir ? __PS_BASE_URI__ : NULL); } // Sprawdza, czy PS w wersji 1.4+ public function isNewPS() { return version_compare(_PS_VERSION_, '1.4') >= 0 ? true : false; } // Sprawdza, czy PS w wersji 1.4+ public function shouldswitchpricewars() { return version_compare(_PS_VERSION_, '1.5') >= 0 ? true : false; } // Maly alias public function getImageLink($name, $ids, $type = null) { if(class_exists('Context', false)) $link = new Link(null, 'http://'); else $link = new Link(); $url = $link->getImageLink($name, $ids, $type); if(strpos($url, 'http') !== 0) return (rtrim($this->getShopHost(false),'/').$url); return $url; } public function install() { if(!file_exists(_PS_MODULE_DIR_.$this->name.'/mappings')) mkdir(_PS_MODULE_DIR_.$this->name.'/mappings/'); if ( !parent::install() //OR ! Configuration::updateValue('PRICEWARS_SKAPIEC_SHOP_ID', '') OR ! Configuration::updateValue('PRICEWARS_EXP_WITH_ZERO', '0') OR ! Configuration::updateValue('PRICEWARS_REDUCE_AVAIL', '0') OR ! Configuration::updateValue('PRICEWARS_DEBUG', '0') OR ! Configuration::updateValue(strtoupper($this->name).'_XML_IMAGE_TYPE', 'medium') OR ! $this->registerHook('backOfficeHome') ) return false; if( $this->isNewPS() AND ( !$this->registerHook('categoryUpdate') OR !$this->registerHook('categoryDeletion') ) ) return false; $this->generateCeneoCategoriesArrayFile(); // call to regenerate CENEO Category Strucure :) return true; } public function uninstall() { if ( !Configuration::deleteByName('PRICEWARS_SKAPIEC_SHOP_ID') OR !Configuration::deleteByName('PRICEWARS_EXP_WITH_ZERO') OR !Configuration::deleteByName('PRICEWARS_REDUCE_AVAIL') OR !Configuration::deleteByName('PRICEWARS_DEBUG') OR !Configuration::deleteByName(strtoupper($this->name).'_XML_IMAGE_TYPE') OR !parent::uninstall()) return false; return true; } public function postProcess(){ if(isset($_POST['reassignCatSubmit']) AND (int) $_POST['reassignCatIdSubmit']) { $this->generateCeneoMappingFile(); } if(isset($_POST['regenerateCeneoCatArray'])) $this->generateCeneoCategoriesArrayFile(); // call to regenerate CENEO Category Strucure :) } function getContent() { $this->_html = null; $this->_postValidateProcess(); if($this->shouldswitchpricewars()) $this->_html .= '

Ta wersja PriceWars jest przeznaczona dla sklepów STARSZYCH (1.4 i wcześniej). Dla wersji sklepu 1.5. Wersję napisaną pod Prestashop 1.5+ można pobrać tutaj: Kliknij - http://pl.seigi.eu/module/pricewars2.html

'; $this->_html .= $this->displayInfoBox(); $this->_html .= '

Price wars!

'; $this->_html .= ' '; if($this-> isNewPS()){ $this->_html .= '
Mapowanie kategorii

Wybierz Kategorię do mapowania

'; $homeCat = Category::getHomeCategories($this->getLangId()); foreach ($homeCat as $cat) { $this->_html .= ''.$cat["name"].' ' .( file_exists(_PS_MODULE_DIR_.$this->name.'/mappings/'.$cat["id_category"].'.php') ? 'Zmapowane' : 'Nie zmapowane' ).'
'; } if(isset($_GET['reassignCatId'])) { $this->_html .= '

Opcje mapowania kategorii

'; $toclean = $this->CeneoCategories; array_walk_recursive($toclean, 'pw_myaddslashes' ); foreach ($toclean as $clkey => &$clvalue) $clvalue = $clkey.'::'.$clvalue; $this->_html .= ' '; $this->_html .= '
'; $this->_html .= $this->drawCategoryTree(Category::getCategories($this->getLangId()), 0, 0, $_GET['reassignCatId']); $this->_html .= '
'; } $this->_html .= '

'; $this->_html .= '
Odświeżanie XML\'a Kategorii ceneo Jeśli dopiero zainstalowałeś moduł, kliknij aby mieć najnowszy XML kategorii ceneo w sklepie
'; $this->_html .= '

'; } $this->_html .= $this->_displaySettingsForm(); $this->_html .= '
Informacje Przy nawiązywaniu współpracy z porównywarkami cen podaj następujące linki jako lokoalizacje plików xml zawierających ofertę sklepu:

'; return $this->_html; } protected function drawCategoryTree($categories, $parent = 0, $level = 0, $genForCat = FALSE) { if(class_exists('Context')) $do_check_level_at = 2; // to jest poziom, na ktorym filtracja ma zadziałać PS 1.5+ . else $do_check_level_at = 1; // to jest poziom, na ktorym filtracja ma zadziałać PS do 1.4. $return = null; foreach ($categories[$parent] as $key => $category){ $id = (int) $category['infos']['id_category']; // filtruje odnogę drzewa kategorii. Sprawdzane na poziomie 2 (do 1.4) lub 3 na ps 1.5+ if ( $genForCat > 0 AND $parent === $do_check_level_at AND $id != $genForCat) { continue; } if( $id > $do_check_level_at ) { // id 1 to Home w ps do 1.4. ID 1 to root i id 2 to home w wersji 1.5+ $return .= ''.$category['infos']['name'].''; $return .= ''; $return .= ' '; } if (isset($categories[$id])) $return .= $this-> drawCategoryTree($categories, $id, $level+1 ,$genForCat ); } return $return; } public function generateCeneoMappingFile () { $file = '$value ){ $val = explode('::',$value, 2); $mappings[$key] = $val[0]; } $file .= var_export($mappings, true); $file .= ';'; file_put_contents(_PS_MODULE_DIR_.$this->name.'/mappings/'.$_POST['reassignCatIdSubmit'].'.php', $file ); } public function generateCeneoCategoriesArrayFile () { $xml = simplexml_load_file('http://api.ceneo.pl/Kategorie/dane.xml'); $this->ceneoXmlToArray ($xml); $categories = 'CeneoCategories = '; $categories .= var_export($this->CeneoCategories, true); file_put_contents(_PS_MODULE_DIR_.$this->name.'/ceneocategories.php',$categories.';'); } // reurencyjna funkcja uzywana do zapełniania drzewa kategorii z ceneo. public function ceneoXmlToArray ($categoryArray, $prefix = NULL) { foreach ( $categoryArray as $xml ) { $this->CeneoCategories[(string)$xml->Id] = (string) $prefix . $xml->Name; if(isset($xml->Subcategories)) $this->ceneoXmlToArray($xml->Subcategories->Category, $prefix.$xml->Name.'/'); } } protected function _displaySettingsForm() { $return = null; $exportwithzero = intval(Configuration::get('PRICEWARS_EXP_WITH_ZERO')); $reduceavailibilty = intval(Configuration::get('PRICEWARS_REDUCE_AVAIL')); $xmlImageType = Configuration::get(strtoupper($this->name).'_XML_IMAGE_TYPE'); $debug = (int) Configuration::get('PRICEWARS_DEBUG'); //$skapiecShopId = Configuration::get('PRICEWARS_SKAPIEC_SHOP_ID'); if (sizeof($this->_postErrors)) $this->_displayErrors(); $return .= '
'.$this->l('Ustawienia').''; // Skapiec Zrezygnowal z Identyfikatorow sklepu, ale dla pewnosci zostawię to na razie :) /* echo '

'; */ $return .= '
Eksportuje produkty, które posiadają stan magazynowy 0. Gdy nie prowadzisz stanów magazynowych.
(domyślne) Nie eksporuj
Eksportuj (jako dostępne; stan wymuszony na 1) (Tip: Zaznacz, gdy masz na sklepie atrybuty (np. kolory), a główny produkt ma stan magazynowy zero )
Eksportuj (jako niedostępne; stan wszystkich produktów wymuszony na 0)

(domyślne) Eksportuj produkty z prawdziwym stanem magazynowym; Produkty wymuszone przez poprzenią opcję będą eksportowane ze stanem magazynowym ustalonym wyżej.
Redukuj stany większe niż 1 na 1; zero pozostaje zerem.

Nie
Tak
Tryb debugowania ułatwia stwierdzenie, z jakiego powodu produkty się nie wyświetlają. Gdy opcja jest włączona, zamiast XML będzie generowany log z informacjami co się dzieje. Aby sprawdzić dlaczego produkty się nie wyświetlają, proszę włączyć tę opcję a następnie otworzyć link do generowania XML.


'; return $return; } public $debug_cache = null; public function doDebug($msg) { if((int) Configuration::get('PRICEWARS_DEBUG')) { $this->debug_cache .= $msg."
\r\n"; } } public function showDebug() { if((int) Configuration::get('PRICEWARS_DEBUG')) { header("content-type: text/html"); echo $this->debug_cache; } } protected function _postValidateProcess(){ if (isset($_POST['submitPricewars'])){ /* if (empty($_POST['skapiecShopId'])) $this->_postErrors[] = $this->l('Musisz podać id sklepu w skapiec.pl, o ile chcesz z niego korzystać.'); else Configuration::updateValue('PRICEWARS_SKAPIEC_SHOP_ID', intval($_POST['skapiecShopId'])); */ Configuration::updateValue('PRICEWARS_EXP_WITH_ZERO', intval($_POST['exportwithzero'])); Configuration::updateValue('PRICEWARS_REDUCE_AVAIL', intval($_POST['reduceavailibilty'])); Configuration::updateValue('PRICEWARS_DEBUG', intval($_POST['pwdebug'])); Configuration::updateValue(strtoupper($this->name).'_XML_IMAGE_TYPE', $_POST['xmlimagetype']); } } protected function _displayErrors() { $nbErrors = sizeof($this->_postErrors); $this->_html .= '

'.($nbErrors > 1 ? $this->l('Wystapily') : $this->l('Wystapil')).' '.$nbErrors.' '.($nbErrors > 1 ? $this->l('bledy') : $this->l('blad')).'

    '; foreach ($this->_postErrors AS $error) $this->_html .= '
  1. '.$error.'
  2. '; $this->_html .= '
'; } protected function _getCategoryTree($delimeter = '/'){ $categories = Category::getCategories($this->getLangId(),false,false); $categoryArray = array (); foreach($categories as $category) { $categoryArray[$category['id_category']] = $category; } foreach ($categoryArray as $category) { $categoryParent = $categoryArray[$category['id_category']]['id_parent']; $categoryPath = $categoryArray[$category['id_category']]['name']; $counter = 0; while ($categoryParent) { $counter++; if ($counter > 100) break; if ($categoryParent != @$categoryArray[$categoryParent]['id_parent']) { if (@$categoryArray[$categoryParent]['id_category']!=1) $categoryPath = @$categoryArray[$categoryParent]['name'] . $delimeter . $categoryPath; $categoryParent = @$categoryArray[$categoryParent]['id_parent']; } else { $categoryParent = 0; } } $categoryArray[$category['id_category']]['path'] = $categoryPath; } return $categoryArray; } protected function generateXML(){ die('Problem z generowaniem XML Objekt nie posiada poprawnej klasy do generowania XML'); } public function getXML(){ $xml = $this->generateXML(); if((int) Configuration::get('PRICEWARS_DEBUG')) $this->showDebug(); else echo $xml->saveXML(); } protected function getXmlImageType() { return Configuration::get(strtoupper($this->name).'_XML_IMAGE_TYPE'); } /* * * * @param bool $active true aby pobrać tylko produkty aktywne * @param bool $instock true aby pobrać tylko produkty które są na magazynie. * @param bool $reducequantity true aby ustawić stan magazynowy produktu na 1 - w celu zamaskowania w xmlu prawdziwej ilosci na magazynie * * */ protected function getProducts($active = true, $instock = true) { Global $cookie; $cookie = new Cookie('ps'); //public static function getProducts($id_lang, $start, $limit, $orderBy, $orderWay, $id_category = false, $only_active = false) $Products = Product::getProducts($this->getLangId(), 0, null, 'name', 'ASC', false, (bool) $active); $exportwithzero = intval(Configuration::get('PRICEWARS_EXP_WITH_ZERO')); $reduceavailibilty = intval(Configuration::get('PRICEWARS_REDUCE_AVAIL')); foreach ($Products as $key => &$product) { if ( $exportwithzero === 0 AND $product['quantity'] <= 0) { // to usun if(method_exists('Product', 'getQuantity')) $product['quantity'] = Product::getQuantity($product['id_product']); if($product['quantity'] <= 0){ $this->doDebug(''.$product['name'].' stan magazynowy = 0, usuwanie z listy.'); unset($Products[$key]); continue; } } elseif ( $exportwithzero === 1 AND $product['quantity'] <= 0 ) { $this->doDebug(''.$product['name'].' ustawiono stan mag. na 1'); $product['quantity'] = 1; } elseif ( $exportwithzero === 2 AND $product['quantity'] <= 0 ) { $this->doDebug(''.$product['name'].' ustawiono stan mag. na 0'); $product['quantity'] = 0; } if( $reduceavailibilty === 1 AND $product['quantity'] > 1 ){ $this->doDebug(''.$product['name'].' Redukcja stanu magazynowego z '.$product['quantity'].' na 1'); $product['quantity'] = 1; } } return $Products; } public static function htmlentities( $string ) { return str_replace('&', '&', $string); return htmlentities( $string , ENT_COMPAT, 'UTF-8'); } protected function getTaxedPrice ($Product) { return round(Product::getPriceStatic($Product['id_product'], true),2); /* if($Product["tax_rate"] === NULL) $Product["tax_rate"] = 0; if(!isset($Product["tax_rate"]) OR !isset($Product["price"])) { throw new Exception('Przekazana wartość musi być tablicą produktu.'); } if($Product["tax_rate"] > 0) $tax = $Product["price"] * ( $Product["tax_rate"] / 100); else $tax = 0; return round ($Product["price"] + $tax, 2); */ } function hookcategoryUpdate(){ if( intval($_POST["id_parent"]) !== 1 AND file_exists(_PS_MODULE_DIR_.$this->name.'/mappings/'.$_POST['id_category'].'.php')) { unlink(_PS_MODULE_DIR_.$this->name.'/mappings/'.$_POST['id_category'].'.php'); } } function hookcategoryDeletion(){ @unlink(_PS_MODULE_DIR_.$this->name.'/mappings/'.$_POST['id_category'].'.php'); } private function displayInfoBox(){ $output = ''; $output .= "
"; $output .= "Moduł: {$this->displayName} w wersji {$this->version} stworzony przez SEIGI Grzegorz Zawadzki
"; $output .= "Licencja: Darmowa
"; $output .= ' Zobacz inne nasze moduły Strona modułu '; return $output .= '
'; } }