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 .= '
';
if($this-> isNewPS()){
$this->_html .= ' ';
$this->_html .= ' ';
}
$this->_html .= $this->_displaySettingsForm();
$this->_html .= '
';
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 .= '
';
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 .= '