name = 'trackingtag'; $this->tab = 'advertising_marketing'; $this->author = 'Antadis'; $this->version = '1.0'; parent::__construct(); $this->displayName = $this->l('Tracking Tag'); $this->description = $this->l('Adds tracking tags on your shop'); $this->_html = ''; $this->_errors = array(); } public function install() { if(count(Db::getInstance()->ExecuteS(' SELECT `id_hook` FROM `'._DB_PREFIX_.'hook` WHERE `name` = "accountValidation" LIMIT 1 ')) == 0) { Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'hook` VALUES (DEFAULT, "accountValidation", "Account validation", "Called when a user creates an account successfully", 0, 0) '); } $query = ' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'trackingtag` ( `id_trackingtag` INTEGER NOT NULL AUTO_INCREMENT, `date_start` DATETIME NOT NULL, `date_end` DATETIME NOT NULL, `enabled` BOOL NOT NULL, `date_upd` DATETIME NOT NULL, `name` VARCHAR(255) NOT NULL, `sponsor` VARCHAR(255), `tag` TEXT, PRIMARY KEY(`id_trackingtag`) ) ENGINE=MyIsam DEFAULT CHARSET=utf8 '; if(!Db::getInstance()->Execute($query)) { return FALSE; } if(!parent::install() || !$this->registerHook('accountValidation')) { return FALSE; } return TRUE; } public function uninstall() { Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'trackingtag`'); if(parent::uninstall() == FALSE) { return FALSE; } return TRUE; } public function getContent() { global $currentIndex; $this->curtag = NULL; if($id = (int) Tools::getValue('tid')) { $this->curtag = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'trackingtag` WHERE `id_trackingtag` = '.$id.' '); if(!$this->curtag) { Tools::redirectAdmin($currentIndex.'&configure=trackingtag&token='.Tools::getValue('token').'&tab_module=advertising_marketing&module_name=trackingtag'); } } $this->_html .= '

'.$this->displayName.'

'; if(Tools::getValue('submitTagAdd') || Tools::getValue('submitTagUpdate')) { $this->_postValidation(); if(sizeof($this->_errors)) { foreach($this->_errors as $error) { $this->_html .= $this->displayError($error); } } } elseif(Tools::getValue('del')) { Db::getInstance()->Execute(' DELETE FROM `'._DB_PREFIX_.'trackingtag` WHERE `id_trackingtag` = '.$id.' '); $this->curtag = NULL; $this->_html .= '

'.$this->l('Tag successfully deleted').'

'; } $this->_displayForm(); return $this->_html; } private function _postValidation() { global $cookie; if(Tools::getValue('submitTagAdd')) { if(Tools::getValue('name')) { Db::getInstance()->Execute(' INSERT INTO `'._DB_PREFIX_.'trackingtag` VALUES ( DEFAULT, "'.pSQL(Tools::getValue('date_start')).'", "'.pSQL(Tools::getValue('date_end')).'", '.(int) (bool) Tools::getValue('enabled').', NOW(), "'.pSQL(Tools::getValue('name', '')).'", "'.pSQL(Tools::getValue('sponsor', '')).'", "'.pSQL(Tools::getValue('tag', ''), TRUE).'", "'.pSQL(Tools::getValue('tag_auth', ''), TRUE).'", "'.pSQL(Tools::getValue('version', 'fr'), TRUE).'" ) '); $this->_html .= '

'.$this->l('Tag added successfully').'

'; } else { $this->_errors[] = $this->l('The tag name is required'); } } elseif(Tools::getValue('submitTagUpdate')) { $id = (int) Tools::getValue('id_trackingtag', 0); if(Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'trackingtag` WHERE `id_trackingtag` = '.(int) $id.' ')) { if(Tools::getValue('name')) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'trackingtag` SET `date_start` = "'.pSQL(Tools::getValue('date_start')).'", `date_end` = "'.pSQL(Tools::getValue('date_end')).'", `enabled` = '.(int) (bool) Tools::getValue('enabled').', `date_upd` = NOW(), `name` = "'.pSQL(Tools::getValue('name')).'", `sponsor` = "'.pSQL(Tools::getValue('sponsor', '')).'", `tag` = "'.pSQL(Tools::getValue('tag', ''), TRUE).'", `tag_auth` = "'.pSQL(Tools::getValue('tag_auth', ''), TRUE).'", `version` = "'.pSQL(Tools::getValue('version', 'fr'), TRUE).'" WHERE `id_trackingtag` = '.$id.' '); $this->_html .= '

'.$this->l('Tag updated successfully').'

'; } else { $this->_errors[] = $this->l('The tag name is required'); } } else { $this->_errors[] = $this->l('Wrong tag ID'); } } } private function _displayForm() { global $cookie, $currentIndex; $this->_html .= '
'.$this->l('Edit a tag').'
*
*
*
curtag!==NULL?($this->curtag['enabled']==0? ' checked="checked"': ''):' checked="checked"').' /> '.$this->l('Disabled').'   curtag!==NULL && $this->curtag['enabled']==1? ' checked="checked"': '').' /> '.$this->l('Enabled').' *

'.$this->l('{id_customer} will be replaced by the customer ID, {email} will be replaced by the customer email, {sponsor_id} will be replaced by the sponsor ID, {sponsor_email} will be replaced by the sponsor email, {date} will be replaced by the registration date').'


curtag !== NULL? '': ' disabled="disabled"').' id="submitTagUpdate" />
'; $this->_html .= '
'.$this->l('Tracking tags list').' '; foreach(Db::getInstance()->ExecuteS(' SELECT `id_trackingtag`, `name`, `sponsor`, `date_start`, `date_end`, `enabled`, `version` FROM `'._DB_PREFIX_.'trackingtag` ORDER BY `date_start` DESC, `date_end` DESC ') as $tag) { $this->_html .= ''; } $this->_html .= '
'.$this->l('ID').' '.$this->l('Name').' '.$this->l('Sponsor').' '.$this->l('Start').' '.$this->l('End').' '.$this->l('Enabled').' '.$this->l('Version').' '.$this->l('Actions').'
'.$tag['id_trackingtag'].' '.$tag['name'].' '.$tag['sponsor'].' '.$tag['date_start'].' '.$tag['date_end'].' '.($tag['enabled']==0? $this->l('No'): $this->l('Yes')).' '.$tag['version'].'

'; } public function hookAccountValidation($params) { global $cookie, $site_version_front; if($cookie->isLogged()) { $customer = new Customer((int)$cookie->id_customer); $accountCreationDateTime = new DateTime($customer->date_add); $nowDateTime = new DateTime(); $maxTrackingDateInterval = new DateInterval('PT8S'); //8 seconds if ($nowDateTime > $accountCreationDateTime->add($maxTrackingDateInterval)) { return ''; } } $tags = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'trackingtag` WHERE (`date_start` <= NOW() OR `date_start` = "0000-00-00 00:00:00") AND (`date_end` > NOW() OR `date_end` = "0000-00-00 00:00:00") AND `enabled` = 1 AND `version` = "'.pSQL($site_version_front).'" AND ( '.(isset($params['sponsor']) && Validate::isLoadedObject($params['sponsor'])? '`sponsor` = "'.pSQL($params['sponsor']->email).'" OR ': '').' `sponsor` = "" ) '); $output = ''; foreach($tags as $tag) { if((int)$tag['id_trackingtag'] == 2 && $cookie->id_lang == 3){ continue; } $output .= str_replace(array( '{id_customer}', '{email}', '{sponsor_id}', '{sponsor_email}', '{date}', ), array( (int) $cookie->id_customer, $cookie->email, isset($params['sponsor']) && Validate::isLoadedObject($params['sponsor'])? $params['sponsor']->id: '', isset($params['sponsor']) && Validate::isLoadedObject($params['sponsor'])? $params['sponsor']->email: '', mktime(), ), $tag['tag']); } if ($cookie->id_lang == 3) { $output.=' '; } return $output; } }