bebeboutik/modules/trackingtag/trackingtag.php
Srv Bebeboutik 6c0978166c add modules
2016-01-04 12:49:26 +01:00

363 lines
13 KiB
PHP

<?php
class TrackingTag extends Module {
public function __construct() {
$this->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 .= '<h2>'.$this->displayName.'</h2>
<style type="text/css">
#content .alert {
width: auto;
}
</style>
';
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 .= '<p class="conf">'.$this->l('Tag successfully deleted').'</p>';
}
$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 .= '<p class="conf">'.$this->l('Tag added successfully').'</p>';
} 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 .= '<p class="conf">'.$this->l('Tag updated successfully').'</p>';
} 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 .= '
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/trackingtag/trackingtag.js"></script>
<script type="text/javascript">
i18n_delete = "'.$this->l('Are you sure you want to delete this tag?').'";
current_location = "'.$currentIndex.'&configure=trackingtag&token='.Tools::getValue('token').'&tab_module=advertising_marketing&module_name=trackingtag";
</script>
<form action="'.$currentIndex.'&configure=trackingtag&token='.Tools::getValue('token').'&tab_module=advertising_marketing&module_name=trackingtag" method="post" enctype="multipart/form-data">
<fieldset>
<legend><img src="../img/admin/cms.gif" alt="" title="" /> '.$this->l('Edit a tag').'</legend>
<label>'.$this->l('Name:').'</label>
<div class="margin-form">
<div id="name" style="float: left;">
<input name="name" type="text" value="'.($this->curtag!==NULL? $this->curtag['name']: '').'" style="width: 300px;" />
<sup> *</sup>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Start date:').'</label>
<div class="margin-form">
<div id="date_start" style="float: left;">
<input name="date_start" type="text" value="'.($this->curtag!==NULL? $this->curtag['date_start']: '').'" style="width: 150px;" />
<sup> *</sup>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('End date:').'</label>
<div class="margin-form">
<div id="date_end" style="float: left;">
<input name="date_end" type="text" value="'.($this->curtag!==NULL? $this->curtag['date_end']: '').'" style="width: 150px;" />
<sup> *</sup>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Status:').'</label>
<div class="margin-form">
<div id="enabled" style="float: left;">
<input name="enabled" type="radio" value="0"'.($this->curtag!==NULL?($this->curtag['enabled']==0? ' checked="checked"': ''):' checked="checked"').' /> '.$this->l('Disabled').'&nbsp;&nbsp;&nbsp;<input name="enabled" type="radio" value="1"'.($this->curtag!==NULL && $this->curtag['enabled']==1? ' checked="checked"': '').' /> '.$this->l('Enabled').'
<sup> *</sup>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Sponsor address:').'</label>
<div class="margin-form">
<div id="sponsor" style="float: left;">
<input name="sponsor" type="text" value="'.($this->curtag!==NULL? $this->curtag['sponsor']: '').'" style="width: 450px;" />
<p>'.$this->l('Will match all sponsors if left blank').'</p>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Tag source code:').'</label>
<div class="margin-form">
<div id="tag" style="float: left;">
<textarea name="tag" style="width: 450px; height: 250px;">'.($this->curtag!==NULL? $this->curtag['tag']: '').'</textarea>
<p>'.$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').'</p>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Tag page auth:').'</label>
<div class="margin-form">
<div id="tag_auth" style="float: left;">
<textarea name="tag_auth" style="width: 450px; height: 250px;">'.($this->curtag!==NULL? $this->curtag['tag_auth']: '').'</textarea>
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Version :').'</label>
<div class="margin-form">
<div id="version" style="float: left;">
<select name="version" id="version">
<option value="fr" '.($this->curtag!==NULL? ($this->curtag['version'] == 'fr' ? 'selected' : '') : '').'>FR</option>
<option value="es" '.($this->curtag!==NULL? ($this->curtag['version'] == 'es' ? 'selected' : '') : '').'>ES</option>
</select>
</div>
<div class="clear"></div>
</div>
<br class="clear" />
<div class="margin-form">
<input type="hidden" name="id_trackingtag" value="'.($this->curtag !== NULL? $this->curtag['id_trackingtag']: '').'" />
<input type="submit" class="button" name="submitTagAdd" value="'.$this->l('Add this tag').'" />
<input type="submit" class="button'.($this->curtag !== NULL? '': ' disable"').'" name="submitTagUpdate" value="'.$this->l('Edit this tag').'"'.($this->curtag !== NULL? '': ' disabled="disabled"').' id="submitTagUpdate" />
</div>
</fieldset>
</form>';
$this->_html .= '<fieldset class="space">
<legend><img src="'.__PS_BASE_URI__.'img/admin/prefs.gif" alt="" title="" /> '.$this->l('Tracking tags list').'</legend>
<table class="table" style="width: 100%;">
<tr>
<th>'.$this->l('ID').'</th>
<th>'.$this->l('Name').'</th>
<th>'.$this->l('Sponsor').'</th>
<th>'.$this->l('Start').'</th>
<th>'.$this->l('End').'</th>
<th>'.$this->l('Enabled').'</th>
<th>'.$this->l('Version').'</th>
<th style="width: 55px;">'.$this->l('Actions').'</th>
</tr>
';
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 .= '<tr>
<td>'.$tag['id_trackingtag'].'</td>
<td>'.$tag['name'].'</td>
<td>'.$tag['sponsor'].'</td>
<td>'.$tag['date_start'].'</td>
<td>'.$tag['date_end'].'</td>
<td>'.($tag['enabled']==0? $this->l('No'): $this->l('Yes')).'</td>
<td>'.$tag['version'].'</td>
<td>
<img style="cursor: pointer;" onclick="itemEdition('.$tag['id_trackingtag'].')" title="'.$this->l('Edit this tag').'" alt="" src="../img/admin/edit.gif">
<img style="cursor: pointer;" onclick="itemDeletion('.$tag['id_trackingtag'].')" title="'.$this->l('Delete this tag').'" alt="" src="../img/admin/delete.gif">
</td>
</tr>';
}
$this->_html .= '</table>
</fieldset>
<br class="clear" />';
}
public function hookAccountValidation($params) {
global $cookie, $site_version_front;
$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) {
$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.='
<!-- Google Code for Lead Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 955803382;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "b3IJCJi4ymAQ9s3hxwM";
var google_conversion_value = 1.00;
var google_conversion_currency = "EUR";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/955803382/?value=1.00&amp;currency_code=EUR&amp;label=b3IJCJi4ymAQ9s3hxwM&amp;guid=ON&amp;script=0"/>
</div>
</noscript>
';
}
return $output;
}
}