'ant_marketing_tracking', 'multilang' => true, 'primary' => 'id_tracking', 'fields' => array( 'name' => array('type' => self::TYPE_STRING), 'utm_source' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), 'utm_medium' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), 'utm_term' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), 'utm_content' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), 'utm_campaign' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), 'more_params' => array('type' => self::TYPE_STRING, 'validate' => 'isUrl'), 'id_tracking_categories' => array('type' => self::TYPE_INT), 'duree' => array('type' => self::TYPE_INT), 'redirect' => array('type' => self::TYPE_INT), 'redirect_page' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), 'subscribe_save' => array('type' => self::TYPE_INT), 'subscribe_tag' => array('type' => self::TYPE_NOTHING), 'description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName'), 'redirection' => array('type' => self::TYPE_STRING), 'cout' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'), ) ); /*public static function getTrackingInfo($name) { if(preg_match('/\&/', $name)) { $sql = 'SELECT * FROM `ps_ant_marketing_tracking` WHERE `name` = "'.str_replace('amp;', '', pSQL($name)).'";'; $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); $results_b = ObjectModel::hydrateCollection('AntTracking', $results); return $results_b[0]; } else { $collection = new Collection('AntTracking'); $collection->where('name', '=', $name); return $collection->getFirst(); } }*/ public static function getTrackingInfo($name){ if(preg_match('/\&/', $name)) { $name = explode('&', $name); $name = $name[0]; $collection = new Collection('AntTracking'); // $collection->where('name', 'LIKE', $name.'&%'); $collection->sqlWhere('(`name` LIKE \''.$name.'&%\' OR `name` = \''.$name.'\')'); //$collection->sqlWhere('`name` LIKE `'.$name.'&%`'); return $collection->getFirst(); } else { $collection = new Collection('AntTracking'); $collection->where('name', '=', $name); return $collection->getFirst(); } } public function add($autodate = true, $null_values = false) { return parent::add($autodate = true, $null_values = false); } public static function getCurrentVariable($variable) { if($variable == 'tracking_url') return self::$tracking_variable_url[0]; if($variable == 'tracking_cookie') return self::$tracking_variable_cookie[0]; if($variable == 'referer') return self::$tracking_variable_referer[0]; } public static function getNbVisitsByTracking($id_tracking) { $sql = 'SELECT nb_visits FROM `'._DB_PREFIX_.'ant_marketing_tracking_visits` WHERE id_tracking = ' . (int)$id_tracking; return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); } public static function getCustomersByTracking($id_tracking) { $sql = 'SELECT c.`id_customer`, `email`, `firstname`, `lastname`, `date_add`, `newsletter` FROM `'._DB_PREFIX_.'customer` c LEFT JOIN `'._DB_PREFIX_.'ant_marketing_tracking_users` u ON(c.id_customer=u.id_customer) WHERE u.id_tracking = ' . (int)$id_tracking . ' ORDER BY c.`id_customer` ASC'; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); } public static function getAllCustomersByTrackings($ids, $date_from = null, $date_to = null) { $result = array(); if($date_from !== null && $date_to !== null) { $date_from = date('Y-m-d', strtotime($date_from)); $date_to = date('Y-m-d', strtotime($date_to)); } foreach ($ids as $id) { $sql = 'SELECT t.`name`, c.`id_customer`, `email`, c.`date_add`, `newsletter` FROM `'._DB_PREFIX_.'customer` c LEFT JOIN `'._DB_PREFIX_.'ant_marketing_tracking_users` u ON(u.id_customer=c.id_customer) LEFT JOIN `'._DB_PREFIX_.'ant_marketing_tracking` t ON(t.id_tracking=u.id_tracking) WHERE t.id_tracking_categories = ' . (int)$id . ' AND c.`id_customer` IS NOT NULL '.($date_from !== null && $date_to !== null ? ' AND u.date >= "'.$date_from.'" AND u.date <= "'.$date_to.'"' : '').' ORDER BY u.`id_tracking` ASC'; $r = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); if($r) { $result = array_merge($result,$r); } } return $result; } public static function getOrdersByTracking($id_tracking) { $sql = 'SELECT * FROM `'._DB_PREFIX_.'ant_marketing_tracking_orders` WHERE id_tracking = ' . (int)$id_tracking; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); } public function getLink() { return Context::getContext()->shop->getBaseURL().'?'.AntTracking::getCurrentVariable('tracking_url').'='.$this->name; } public function getRedirectionLink($base_uri = false) { $url = ''; if($this->redirect == 1 && !empty($this->redirect_page)) { if(strpos($this->redirect_page, 'http://') !== false) $url = $this->redirect_page; else $url = Context::getContext()->shop->getBaseURL() . $this->redirect_page; } else { if($base_uri === false) $base_uri = $this->getLink(); $url = preg_replace('#('.self::$current_tracking_url.'=[a-zA-Z0-9_-]*)#', '', $base_uri); } return $this->pushVars($url); } public function getAgregatorLink() { $url = Context::getContext()->shop->getBaseURL().'?'.AntTracking::getCurrentVariable('tracking_url').'='.$this->name; if($this->redirect == 1 && !empty($this->redirect_page)) { if(strpos($this->redirect_page, 'http://') !== false) $url = $this->redirect_page.'?'.AntTracking::getCurrentVariable('tracking_url').'='.$this->name; else $url .= $this->redirect_page; } else { if($base_uri === false) $base_uri = $this->getLink(); $url .= preg_replace('#('.self::$current_tracking_url.'=[a-zA-Z0-9]*)#', '', $this->getLink()); } return $this->pushVars($url); } public function setCookie() { if($this->duree == 0) setcookie(AntTracking::getCurrentVariable('tracking_cookie'), $this->name, time() + 365*24*3600 , __PS_BASE_URI__); else setcookie(AntTracking::getCurrentVariable('tracking_cookie'), $this->name, time() + (int)$this->duree*24*3600, __PS_BASE_URI__); } public function addVisits() { $sql = 'INSERT INTO '._DB_PREFIX_.'ant_marketing_tracking_visits (date,id_tracking,nb_visits) VALUES ("'.date('Y-m-d').'",'.(int)$this->id.',1) ON DUPLICATE KEY UPDATE nb_visits=nb_visits+1'; return $result = Db::getInstance()->execute($sql); } public function pushVars($url) { $vars = array('utm_source', 'utm_content', 'utm_campaign', 'utm_term', 'utm_medium'); foreach($vars as $var) { if(strpos($url, $var) !== false || empty($this->$var)) continue; $url .= strpos($url, '?') === false ? '?' : '&'; $url .= $var.'='.$this->$var; } if (!empty($this->more_params)) { $url .= strpos($url, '?') === false ? '?' : '&'; $url .= $this->more_params; } return $url; } public static function getSubscribeTracking() { $res = Db::getInstance()->executeS('SELECT name FROM '._DB_PREFIX_.'ant_marketing_tracking WHERE subscribe_save = 1'); if(!$res) return array(); $array = array(); foreach($res as $r) $array[] = $r['name']; return $array; } public static function dispatchCookie() { foreach(self::$tracking_variable_cookie as $k => $var) //POUR LA COMPATIBILITE AVEC LES VARIABLES DE RECUPERATION { if($var != AntTracking::getCurrentVariable('tracking_cookie')) { if(isset($_COOKIE[$var])) { setcookie(AntTracking::getCurrentVariable('tracking_cookie'), $_COOKIE[$var], time() + 365*24*3600, __PS_BASE_URI__); setcookie($var, "", time()-3600); } } } } public static function getUrlVar() { $tracking_principal = Tools::getValue(AntTracking::getCurrentVariable('tracking_url')); $get = $_GET; if(count($get) > 1) { unset($get['tr']); $new_tracking = ''; foreach ($get as $k => $v) { $new_tracking .= '&'.$k.'='.$v; } $tracking_principal .= $new_tracking; } if($tracking_principal !== false) return htmlentities($tracking_principal); foreach(self::$tracking_variable_url as $k => $var) { if($var != AntTracking::getCurrentVariable('tracking_url') && ($tracking_secondaire = Tools::getValue($var)) !== false) { self::$current_tracking_url = $var; return htmlentities($tracking_secondaire); } } } public static function getUrlVarNoVal() { $tracking_principal = Tools::getValue(AntTracking::getCurrentVariable('tracking_url')); $get = $_GET; if(count($get) > 1) { unset($get['tr']); $new_tracking = ''; foreach ($get as $k => $v) { $new_tracking .= '&'.$k.'='; } $tracking_principal .= $new_tracking; } if($tracking_principal !== false) return htmlentities($tracking_principal); foreach(self::$tracking_variable_url as $k => $var) { if($var != AntTracking::getCurrentVariable('tracking_url') && ($tracking_secondaire = Tools::getValue($var)) !== false) { self::$current_tracking_url = $var; return htmlentities($tracking_secondaire); } } } public static function dispatchSubscribe($customer = null) { self::dispatchCookie(); //FOR COMPATIBILITY WITH OLD URL AND COOKIE VARIABLE if(isset($_COOKIE[AntTracking::getCurrentVariable('tracking_cookie')])) { $tracking_name = $_COOKIE[AntTracking::getCurrentVariable('tracking_cookie')]; $tracking = AntTracking::getTrackingInfo($tracking_name); if(Validate::isLoadedObject($tracking)) { $id_customer = Validate::isLoadedObject($customer) ? $customer->id : Context::getContext()->customer->id; Db::getInstance()->insert('ant_marketing_tracking_users', array( 'id_tracking' => (int)$tracking->id, 'id_customer' => $id_customer, 'date' => date('Y-m-d') )); } if($tracking->subscribe_save == 1) { setcookie($tracking->name, '1', time() + 365*24*3600, __PS_BASE_URI__); } } } public function addSubscribe($id_customer) { Db::getInstance()->insert('ant_marketing_tracking_users', array( 'id_tracking' => (int)$this->id, 'id_customer' => (int)$id_customer, 'date' => date('Y-m-d') )); if($tracking->subscribe_save == 1) { setcookie($tracking->name, '1', time() + 365*24*3600, __PS_BASE_URI__); } } public static function dispatchOrder($id_order) { self::dispatchCookie(); //FOR COMPATIBILITY WITH OLD URL AND COOKIE VARIABLE if(isset($_COOKIE[ AntTracking::getCurrentVariable('tracking_cookie') ])) { $tracking_name = $_COOKIE[ AntTracking::getCurrentVariable('tracking_cookie') ]; $tracking = AntTracking::getTrackingInfo($tracking_name); if(Validate::isLoadedObject($tracking)) { Db::getInstance()->insert('ant_marketing_tracking_orders', array( 'id_tracking' => (int)$tracking->id, 'id_orders' => (int)$id_order, )); } } } public static function dispatchVisits() { self::dispatchCookie(); //FOR COMPATIBILITY WITH OLD URL AND COOKIE VARIABLE $cookies = self::getUrlVar(); $cookies_no_val = self::getUrlVarNoVal(); $ex_cook=''; $str_ex_cook=''; $ex_cook = explode('&', str_replace('amp;','',$cookies)); $no_params_redirect = explode('&', str_replace('amp;','',$cookies_no_val)); unset($ex_cook[0]); $str_ex_cook = '&'.implode('&', $ex_cook); unset($no_params_redirect[0]); $no_params = '&'.implode('&', $no_params_redirect); if($no_params == '&') $no_params = ''; if($str_ex_cook == '&') $str_ex_cook = ''; if(!empty($cookies)) { if(!Validate::isLoadedObject($tracking = AntTracking::getTrackingInfo($cookies_no_val))) { self::saveReferer(); } else { $tracking->setCookie(); $tracking->addVisits(); header('Location: '.str_replace($no_params, '',$tracking->getRedirectionLink($_SERVER['REQUEST_URI'])).$str_ex_cook); exit; } } } public static function saveReferer() { if(!isset($_COOKIE[AntTracking::getCurrentVariable('referer')]) && !isset($_COOKIE[AntTracking::getCurrentVariable('tracking_cookie')]) ) { if(!empty($_SERVER['HTTP_REFERER'])) { $referer = $_SERVER['HTTP_REFERER']; setcookie(AntTracking::getCurrentVariable('referer'), 'ok', 0 , __PS_BASE_URI__); $sql = 'SELECT nb_visits as count FROM '._DB_PREFIX_.'ant_marketing_referer WHERE referer_url = "'. $referer. '"'; $nb_visits = $result = Db::getInstance()->getRow($sql); if(empty($nb_visits)) { Db::getInstance()->insert('ant_marketing_referer', array( 'referer_url' => $referer, 'nb_visits' => 1, )); } else { $sql = 'UPDATE '._DB_PREFIX_.'ant_marketing_referer SET nb_visits=nb_visits+1 WHERE referer_url = "'. $referer. '"'; $result = Db::getInstance()->execute($sql); } } // PAS DE REFERE -> ACCES DIRECT else { $sql = 'UPDATE '._DB_PREFIX_.'ant_marketing_referer SET nb_visits=nb_visits+1 WHERE referer_url = "direct_access"'; $result = Db::getInstance()->execute($sql); setcookie(AntTracking::getCurrentVariable('referer'), 'ok', 0 , __PS_BASE_URI__); } } } public static function getSubscribeCode() { $content = ''; if(Validate::isLoadedObject(Context::getContext()->customer)) { $subscribes_tracking = AntTracking::getSubscribeTracking(); foreach($subscribes_tracking as $tracking_name) { if(isset($_COOKIE[$tracking_name])) { $tracking = self::getTrackingInfo($tracking_name); if(($tag = $tracking->subscribe_tag) != '') { $customer = Context::getContext()->customer; $tag = str_replace('{customer_id}', $customer->id, $tag); $tag = str_replace('{customer_email}', $customer->email, $tag); $content .= $tag; setcookie($tracking_name, "", time()-3600); } } } } return $content; } }