diff --git a/modules/ant_trackingtag/AdminAntTrackingTag.php b/modules/ant_trackingtag/AdminAntTrackingTag.php new file mode 100644 index 00000000..14b00598 --- /dev/null +++ b/modules/ant_trackingtag/AdminAntTrackingTag.php @@ -0,0 +1,307 @@ +_object = false; + $this->controller = 'AdminModules'; + $this->module_name = 'ant_trackingtag'; + $this->config_tab = (bool)$config_tab; + if ($config_tab) { + $this->controller = 'AdminAntTrackingTag'; + } + $this->helperForm = new HelperFormBootstrap(); + $this->helperForm->_select2 = true; + $this->helperForm->_inputSwitch = true; + } + + public function display() + { + parent::displayForm(); + + $this->_html = ''; + + $this->_postProcess(); + + $this->_addCss(); + $this->_html .= $this->helperForm->renderStyle(); + + $this->_displayForm(); + $this->_displayList(); + + $this->_html .='
'; + $this->_addJs(); + $this->_html .= $this->helperForm->renderScript(); + echo $this->_html; + } + + + protected function _addJs() + { + $this->helperForm->_js .= ''; + } + + protected function _addCss() + { + $this->helperForm->_css .=' + .table tr th { + background: #565485; + background: rgba(86,84,133,0.9); + color: #fff; + font-size: 12px; + } + .table tr:nth-child(even) { + background: #F1F1F1; + } + .table .input-group-btn .btn { + padding: 4px 5px; + color: #504d8b; + + } + .table .input-group-btn .btn .anticon{ + font-size: 12px; + } + .bg-grey{ + background: #EFEFEF; + border-radius:4px; + } + .bg-grey .div-title { + border-bottom: 2px solid #504D8B; + } + '; + } + + protected function _displayList() + { + global $cookie, $currentIndex; + + $id_lang = (int)$cookie->id_lang; + + $tags = AntTrackingTag::getTags(false); + $_current_index = ($this->config_tab ? $currentIndex . '&token=' . Tools::getAdminTokenLite($this->controller) : $_SERVER['REQUEST_URI']); + + $this->_html .=' +
+
+
+

'.$this->l('Liste des tags').'

+
+
+
+ + + + + + + + + + + + + '; + foreach ($tags as $tag) + { + $customer_numbers = AntTrackingTag::getCountCustomerByTag((int)$tag['id_trackingtag']); + $total = 0; + + $this->_html .=' + + + + + + + + + '; + } + $this->_html .=' + +
'.$this->l('ID').''.$this->l('Name').''.$this->l('tag').''.$this->l('Active').''.$this->l('Nb').''.$this->l('Total').''.$this->l('Action').'
#'.$tag['id_trackingtag'].''.$tag['name'].''.$tag['tag'].''.((int)$tag['enabled']?'':'').''; + foreach ($customer_numbers as $info) { + $total += (int)$info['nb']; + $this->_html.=' + '; + } + $this->_html.= + ''.$total.' +
+ + +
+
+
+
+
'; + + } + + protected function _postProcess() + { + if (Tools::isSubmit('newTag')) { + + } elseif (Tools::isSubmit('addTag')) { + $this->_addOrUpdateTag(false); + } elseif (Tools::isSubmit('editTag')) { + $this->_addOrUpdateTag(true); + } elseif (Tools::isSubmit('loadTag') && Tools::getValue('id')) { + $this->_object = new AntTrackingTag((int)Tools::getValue('id')); + } elseif (Tools::isSubmit('deleteTag') && Tools::getValue('id')) { + $deleted_group = new AntTrackingTag((int)Tools::getValue('id')); + $res = $deleted_group->delete(); + if ($res) { + $this->_html .= HelperFormBootstrap::displaySuccess($this->l('Tag deleted : #') . (int)Tools::getValue('id')); + } else { + $this->_html .= HelperFormBootstrap::displayError($this->l('Tag cannot be deleted : #') . (int)Tools::getValue('id')); + } + return $res; + } + } + + protected function _addOrUpdateTag($edit = false) + { + if ($edit) { + $trackingTag = new AntTrackingTag((int)Tools::getValue('id_trackingtag')); + } else { + $trackingTag = new AntTrackingTag(); + } + + $trackingTag->name = Tools::getValue('name'); + $trackingTag->tag = Tools::getValue('tag'); + $trackingTag->enabled = (int)Tools::getValue('enabled'); + + if ($trackingTag->save()) { + if ($edit) { + $this->_html .= HelperFormBootstrap::displaySuccess($this->l('Tag has been updated')); + } else { + $this->_html .= HelperFormBootstrap::displaySuccess($this->l('Tag has been created')); + } + } else { + $this->_html .= HelperFormBootstrap::displayError($this->l('Error occured while updating tag')); + } + } + + private function _displayForm() + { + global $cookie, $currentIndex; + if (Validate::isLoadedObject($this->_object)){ + $trackingTag = $this->_object; + } + $this->helperForm->_forms = array( + array( + 'action' => $currentIndex . '&token=' . Tools::getAdminTokenLite('AdminAntTrackingTag'), + 'title' => ' '.$this->l('Tag'), + 'class' => 'form-horizontal', + 'class_div' => 'col-md-12', + 'sections' => array( + array( + 'class' => 'col-md-6', + 'inputs' => array( + array( + 'type' => 'simpleText', + 'name' => 'name', + 'label-class' => 'col-md-5', + 'input-class' => 'col-md-6', + 'label' => $this->l('Name'), + 'default' => (isset($trackingTag)?$trackingTag->name:'') + ), + array( + 'type' => 'simpleText', + 'name' => 'tag', + 'label-class' => 'col-md-5', + 'input-class' => 'col-md-6', + 'label' => $this->l('Tag'), + 'default' => (isset($trackingTag)?$trackingTag->tag:'') + ), + array( + 'type' => 'switch', + 'label' => $this->l('Active'), + 'label-class' => 'col-md-5', + 'input-class' => 'col-md-6', + 'class-group' => 'switch', + 'name' => 'enabled', + 'title' => ' ', + 'default' => (isset($trackingTag)?($trackingTag->enabled==0?0:1):1), + 'checked' => (isset($trackingTag)?($trackingTag->enabled==0?0:1):1), + ), + ), + 'actions' => array(), + 'actions-class' => 'text-right', + ), + array( + 'class' => 'col-md-6 bg-grey', + 'title' => ' Informations', + 'inputs' => array(), + 'info_html' => ' +

Les balises doivent être présentes dans l\'url et sont trackées à l\'inscription

+

Le nouveau client est comptabilisé avec la valeur que contient la balise.
+ Par exemple :

+ +

Ici le client sera associé au tag ed qui aura pour valeur 564.

+

Le client est donc comptabilisé pour le tag ed et plus précisément pour la valeur 564 de ce tag.

+

Si plusieurs tags présents, le client sera comptabilisé dans tous les tags

+

' + ), + ), + 'actions' => array(), + 'actions-class' => 'text-right', + ) + ); + + + if (isset($trackingTag)) { + $this->helperForm->_forms[0]['sections'][0]['title'] = "Editer le tag ".$trackinTag->name; + $this->helperForm->_forms[0]['sections'][0]['inputs'][] = array( + 'type' => 'hidden', + 'name' => 'id_trackingtag', + 'value' => $trackingTag->id, + 'class' => 'large' + ); + $this->helperForm->_forms[0]['sections'][0]['actions'] = array( + array( + 'type' => 'submit', + 'class' => 'btn-default', + 'name' => 'newTag', + 'value' => $this->l('Nouveau Tag') + ), + array( + 'type' => 'submit', + 'class' => 'btn-primary', + 'name' => 'editTag', + 'value' => $this->l('Editer le Tag') + ), + ); + } else { + $this->helperForm->_forms[0]['sections'][0]['title'] = "Ajouter un tag"; + $this->helperForm->_forms[0]['sections'][0]['actions'] = array( + array( + 'type' => 'submit', + 'class' => 'btn-primary', + 'name' => 'addTag', + 'value' => $this->l('Ajouter le tag') + ), + ); + } + + $this->helperForm->renderForm(); + } +} diff --git a/modules/ant_trackingtag/ant_trackingtag.php b/modules/ant_trackingtag/ant_trackingtag.php new file mode 100644 index 00000000..f0eabe7e --- /dev/null +++ b/modules/ant_trackingtag/ant_trackingtag.php @@ -0,0 +1,129 @@ +name = 'ant_trackingtag'; + $this->tab = 'advertising_marketing'; + $this->version = '1.0'; + $this->author = 'Antadis'; + $this->need_instance = 0; + + $this->displayName = $this->l('Tracking Tag'); + $this->description = $this->l('Allows to track tags set in url'); + } + + public function install() { + + # Add tables + $query = ' + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ant_trackingtag` ( + `id_trackingtag` INTEGER NOT NULL AUTO_INCREMENT, + `enabled` BOOL NOT NULL, + `name` VARCHAR(255) NOT NULL, + `tag` VARCHAR(255) NOT NULL, + `date_add` DATETIME NOT NULL, + `date_upd` DATETIME NOT NULL, + PRIMARY KEY(`id_trackingtag`) + ) ENGINE=MyIsam DEFAULT CHARSET=utf8 + '; + if(!Db::getInstance()->Execute($query)) { + return false; + } + + $query = ' + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ant_trackingtag_customer` ( + `id_trackingtag` INTEGER NOT NULL, + `id_customer` INTEGER NOT NULL, + `value` VARCHAR(255) NOT NULL, + PRIMARY KEY(`id_trackingtag`, `id_customer`, `value`) + ) ENGINE=MyIsam DEFAULT CHARSET=utf8 + '; + if(!Db::getInstance()->Execute($query)) { + return false; + } + + # Register hooks + if(!parent::install() + || !$this->registerHook('createAccount') + || !$this->registerHook('createAccountForm') + || !$this->registerHook('PrivateSales_Block') + ) { + return false; + } + + return true; + } + + public function uninstall() { + + if(parent::uninstall() == false) { + return false; + } + + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'ant_trackingtag`'); + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'ant_trackingtag_customer`'); + + return true; + } + + public function hookCreateAccount($params) { + global $cookie; + + $newCustomer = $params['newCustomer']; + if(!Validate::isLoadedObject($newCustomer)) { + return false; + } + if(Tools::getValue('trackingtags')){ + $id_tags = Tools::getValue('trackingtags'); + foreach ($id_tags as $id_tag => $value) { + if(AntTrackingTag::isExistAndEnabled($id_tag)){ + Db::getInstance()->ExecuteS(' + INSERT INTO `'._DB_PREFIX_.'ant_trackingtag_customer` + VALUES ( + '.$id_tag.', + '.(int) $newCustomer->id.', + "'.pSQL($value).'" + ) + '); + } + } + } + } + + public function hookCreateAccountForm($params) { + global $smarty, $cookie; + + $tags = AntTrackingTag::getTags(); + $tracking_tags = []; + foreach ($tags as $tag) { + if($value = Tools::getValue($tag['tag'])) { + $tracking_tags[(int)$tag['id_trackingtag']] = $value; + } + } + + $smarty->assign(array( + 'tracking_tags' => $tracking_tags, + )); + return $this->display(__FILE__, 'authentication.tpl'); + } + + public function hookPrivateSales_Block($params) { + global $smarty, $cookie; + + $tags = AntTrackingTag::getTags(); + $tracking_tags = []; + foreach ($tags as $tag) { + if($value = Tools::getValue($tag['tag'])) { + $tracking_tags[(int)$tag['id_trackingtag']] = $value; + } + } + + $smarty->assign(array( + 'tracking_tags' => $tracking_tags, + )); + } +} diff --git a/modules/ant_trackingtag/authentication.tpl b/modules/ant_trackingtag/authentication.tpl new file mode 100644 index 00000000..a94bcefc --- /dev/null +++ b/modules/ant_trackingtag/authentication.tpl @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/modules/ant_trackingtag/models/AntTrackingTag.php b/modules/ant_trackingtag/models/AntTrackingTag.php new file mode 100644 index 00000000..70aa4dd1 --- /dev/null +++ b/modules/ant_trackingtag/models/AntTrackingTag.php @@ -0,0 +1,116 @@ + 'isUnsignedId', + 'enabled' => 'isBool', + 'name' => 'isString', + 'tag' => 'isString', + 'date_add' => 'isDate', + 'date_upd' => 'isDate', + ); + + protected $table = 'ant_trackingtag'; + protected $identifier = 'id_trackingtag'; + + + public function getFields() + { + parent::validateFields(); + + $fields['id_trackingtag'] = (int)$this->id_trackingtag; + $fields['enabled'] = (int)$this->enabled; + $fields['name'] = pSQL($this->name); + $fields['tag'] = pSQL($this->tag); + $fields['date_add'] = pSQL($this->date_add); + $fields['date_upd'] = pSQL($this->date_upd); + + return $fields; + } + + public function delete() + { + return true; + } + + /** + * Get demands + * @param $states array get only demands in specific states + * @return Array Groups + */ + public static function getTags($enabled = true, $where = false) + { + if($where){ + return Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'ant_trackingtag` tt + WHERE '.$where.' + '.($enabled ? ' AND tt.`enabled` = 1' : '').' + '); + } else { + return Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'ant_trackingtag` tt + WHERE 1 + '.($enabled ? ' AND tt.`enabled` = 1' : '').' + '); + } + } + + public function save($nullValues = false, $autodate = true) + { + if (parent::save($nullValues,$autodate)) { + return true; + } + } + + public static function getTagByName($name) + { + return Db::getInstance()->getValue(" + SELECT `id_trackingtag` + FROM `"._DB_PREFIX_."ant_trackingtag` + WHERE `name` = " .pSQL($name) + ); + } + + public static function isExistAndEnabled($id_trackingtag) + { + return Db::getInstance()->getValue(" + SELECT `id_trackingtag` + FROM `"._DB_PREFIX_."ant_trackingtag` + WHERE `id_trackingtag` = " .pSQL($id_trackingtag)." + AND `enabled`=1" + ); + } + + public static function getCountCustomerByTag($id_trackingtag) + { + return Db::getInstance()->ExecuteS(" + SELECT COUNT(id_customer) as `nb`, `value` + FROM `"._DB_PREFIX_."ant_trackingtag_customer` + WHERE `id_trackingtag` = " . (int)$id_trackingtag." + GROUP BY `value`" + ); + } + + public static function getCustomer($id_trackingtag) + { + return Db::getInstance()->ExecuteS(" + SELECT c.* + FROM `" . _DB_PREFIX_ . "customer` c + LEFT JOIN `" . _DB_PREFIX_ . "ant_trackingtag_customer` t ON (t.id_customer = c.id_customer) + WHERE sd.`id_trackingtag` = " . (int)$id_trackingtag + ); + } +} +