* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7541 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) exit; class Criteo extends Module { public function __construct() { $this->name = 'criteo'; $this->tab = 'advertising_marketing'; $this->version = '1.0'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Criteo'); $this->description = $this->l('Criteo product export and tag display.'); } public function install() { return (parent::install() AND $this->registerHook('header') AND $this->registerHook('productfooter') AND $this->registerHook('home') AND $this->registerHook('shoppingCartExtra') AND $this->registerHook('orderConfirmation')); } public function uninstall() { return parent::uninstall(); } private function _postProcess() { $isUpdated = false; if (Tools::getValue('id_criteo_conversion')) { Configuration::updateValue('CRITEO_ID_CONVERSION', pSQL(Tools::getValue('id_criteo_conversion'))); $isUpdated = true; } if (Tools::getValue('id_criteo_normal')) { Configuration::updateValue('CRITEO_ID_NORMAL', pSQL(Tools::getValue('id_criteo_normal'))); $isUpdated = true; } if (Tools::getValue('url_widget_criteo')) { Configuration::updateValue('CRITEO_URL_WIDGET', pSQL(Tools::getValue('url_widget_criteo'))); $isUpdated = true; } if ($isUpdated) return true; return false; } public function getContent() { return '
'.($this->_postProcess() ? $this->displayConfirmation($this->l('Settings are updated').'') : '').' '.$this->l('Criteo Export').'






'.$this->l('URL to communicate to Criteo:').' '.Tools::getProtocol().$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/'.$this->name.'/export_csv.php?token='.sha1(_COOKIE_KEY_.'exportCriteo').'
'.$this->l('URL to communicate to Criteo:').' '.Tools::getProtocol().$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/'.$this->name.'/export_xml.php?token='.sha1(_COOKIE_KEY_.'exportCriteo').'
'; } private static function getAllSubCats(&$all_cats, $id_cat, $id_lang) { $category = new Category(intval($id_cat)); $sub_cats = $category->getSubcategories($id_lang); if (count($sub_cats) > 0) foreach ($sub_cats AS $sub_cat) { $all_cats[] = $sub_cat['id_category']; self::getAllSubCats($all_cats, $sub_cat['id_category'], $id_lang); } } public static function buildCSV() { global $cookie, $country_infos; $cookie = new Cookie('ps'); $country_infos = array('id_group' => 0, 'id_tax' => 1); $html = ''; /* First line, columns */ $columns = array('id', 'name', 'smallimage', 'bigimage', 'producturl', 'description', 'price', 'retailprice', 'discount', 'recommendable', 'instock'); foreach ($columns AS $column) $html .= $column.'|'; $html = rtrim($html, '|')."\n"; /* Setting parameters */ $conf = Configuration::getMultiple(array( 'PS_REWRITING_SETTINGS', 'PS_LANG_DEFAULT', 'PS_SHIPPING_FREE_PRICE', 'PS_SHIPPING_HANDLING', 'PS_SHIPPING_METHOD', 'PS_SHIPPING_FREE_WEIGHT', 'PS_COUNTRY_DEFAULT', 'PS_SHOP_NAME', 'PS_CURRENCY_DEFAULT', 'PS_CARRIER_DEFAULT')); /* Language */ $language = new Language(intval($conf['PS_LANG_DEFAULT'])); /* Link instance for products links */ $link = new Link(); $result = Db::getInstance()->ExecuteS(' SELECT DISTINCT p.`id_product`, i.`id_image` FROM `'._DB_PREFIX_.'product` p JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.id_product = p.id_product) LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.id_product = p.id_product) WHERE p.`active` = 1 AND i.id_image IS NOT NULL GROUP BY p.id_product'); foreach ($result AS $k => $row) { if (Pack::isPack(intval($row['id_product']))) continue; $product = new Product(intval($row['id_product']), true); if (Validate::isLoadedObject($product)) { $imageObj = new Image($row['id_image']); $line = array(); $line[] = $product->id; $line[] = $product->manufacturer_name.' - '.$product->name[intval($conf['PS_LANG_DEFAULT'])]; $line[] = Tools::getProtocol().$_SERVER['HTTP_HOST']._THEME_PROD_DIR_.$imageObj->getExistingImgPath().'-small.jpg'; $line[] = Tools::getProtocol().$_SERVER['HTTP_HOST']._THEME_PROD_DIR_.$imageObj->getExistingImgPath().'-thickbox.jpg'; $line[] = $link->getProductLink(intval($product->id), $product->link_rewrite[intval($conf['PS_LANG_DEFAULT'])], $product->ean13).'&utm_source=criteo&aff=criteo'; $line[] = str_replace(array("\n", "\r", "\t", '|'), '', strip_tags(html_entity_decode($product->description_short[intval($conf['PS_LANG_DEFAULT'])], ENT_COMPAT, 'UTF-8'))); $price = $product->getPrice(true, intval(Product::getDefaultAttribute($product->id))); $line[] = number_format($price, 2, '.', ''); $line[] = number_format($product->getPrice(true, intval(Product::getDefaultAttribute(intval($product->id))), 6, NULL, false, false), 2, '.', ''); $line[] = $product->getPrice(true, NULL, 2, NULL, true); $line[] = '1'; $line[] = '1'; foreach ($line AS $column) $html .= trim($column).'|'; $html = rtrim($html, '|')."\n"; } } echo $html; } public static function buildXML() { global $cookie, $country_infos; $cookie = new Cookie('ps'); $country_infos = array('id_group' => 0, 'id_tax' => 1); $html = ''."\n"; /* First line, columns */ $columns = array('id', 'name', 'smallimage', 'bigimage', 'producturl', 'description', 'price', 'retailprice', 'discount', 'recommendable', 'instock'); /* Setting parameters */ $conf = Configuration::getMultiple(array( 'PS_REWRITING_SETTINGS', 'PS_LANG_DEFAULT', 'PS_SHIPPING_FREE_PRICE', 'PS_SHIPPING_HANDLING', 'PS_SHIPPING_METHOD', 'PS_SHIPPING_FREE_WEIGHT', 'PS_COUNTRY_DEFAULT', 'PS_SHOP_NAME', 'PS_CURRENCY_DEFAULT', 'PS_CARRIER_DEFAULT')); /* Language */ $language = new Language(intval($conf['PS_LANG_DEFAULT'])); /* Link instance for products links */ $link = new Link(); /* Searching for products */ $result = Db::getInstance()->ExecuteS(' SELECT DISTINCT p.`id_product`, i.`id_image` FROM `'._DB_PREFIX_.'product` p JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.id_product = p.id_product) LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.id_product = p.id_product) WHERE p.`active` = 1 AND i.id_image IS NOT NULL GROUP BY p.id_product'); foreach ($result AS $k => $row) { if (Pack::isPack(intval($row['id_product']))) continue; $product = new Product(intval($row['id_product']), true); if (Validate::isLoadedObject($product)) { $imageObj = new Image($row['id_image']); $imageObj->id_product = intval($product->id); $line = array(); $line[] = $product->manufacturer_name.' - '.$product->name[intval($conf['PS_LANG_DEFAULT'])]; $line[] = Tools::getProtocol().$_SERVER['HTTP_HOST']._THEME_PROD_DIR_.$imageObj->getExistingImgPath().'-small.jpg'; $line[] = Tools::getProtocol().$_SERVER['HTTP_HOST']._THEME_PROD_DIR_.$imageObj->getExistingImgPath().'-thickbox.jpg'; $line[] = $link->getProductLink(intval($product->id), $product->link_rewrite[intval($conf['PS_LANG_DEFAULT'])], $product->ean13).'&utm_source=criteo&aff=criteo'; $line[] = str_replace(array("\n", "\r", "\t", '|'), '', strip_tags(html_entity_decode($product->description_short[intval($conf['PS_LANG_DEFAULT'])], ENT_COMPAT, 'UTF-8'))); $price = $product->getPrice(true, intval(Product::getDefaultAttribute($product->id))); $line[] = number_format($price, 2, '.', ''); $line[] = number_format($product->getPrice(true, intval(Product::getDefaultAttribute(intval($product->id))), 6, NULL, false, false), 2, '.', ''); $line[] = $product->getPrice(true, NULL, 2, NULL, true); $line[] = '1'; $line[] = '1'; $cptXML = 1; $html .= "\t".''."\n"; foreach ($line AS $column) { $html .= "\t\t".'<'.$columns[$cptXML].'>'.$column.''."\n"; $cptXML++; } $html .= "\t".''."\n"; } } $html .= ''."\n"; echo $html; } public function hookProductFooter($params) { return ''; } public function hookProductList($params) { $i = 0; $strproducts = ''; if (!isset($params['products']) OR !$params['products']) return ''; foreach ($params['products'] AS $product) { if ($i > 2) break; $strproducts .= 'i'.++$i.'='.$product['id_product'].'&'; } return ''; } public function hookShoppingCartExtra($params) { foreach ($params['products'] AS $product) $strproducts .= 'i'.++$i.'='.$product['id_product'].'&p'.$i.'='.$product['price'].'&q'.$i.'='.$product['quantity'].'&'; $strproducts = substr($strproducts, 0, -1); return ''; } public function hookOrderConfirmation($params) { global $cookie, $country_infos; $cookie = new Cookie('ps'); $country_infos = array('id_group' => 0, 'id_tax' => 1); $cart = new Cart(intval($params['objOrder']->id_cart)); $products = $cart->getProducts(); $strproducts = ''; $i = 0; foreach ($products AS $product) $strproducts .= 'i'.++$i.'='.$product['id_product'].'&p'.$i.'='.$product['price'].'&q'.$i.'='.$product['quantity'].'&'; $strproducts = substr($strproducts, 0, -1); return ''; } public function hookHeader($params) { return ''; } public function hookHome($params) { return ''; } }