garancia/modules/blockpopupnewsletter/blockpopupnewsletter.php
2016-10-10 15:24:25 +02:00

344 lines
7.1 KiB
PHP
Executable File

<?php
if (!defined('_CAN_LOAD_FILES_'))
{
exit;
}
class BlockPopupNewsletter extends Module {
public function __construct()
{
$this->name = 'blockpopupnewsletter';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Block Popup Newsletter');
$this->description = $this->l('Show newsletter popup by condition');
$this->confirmUninstall = $this->l('Are you sure ?');
$this->context = Context::getContext();
}
public function install()
{
$this->createTab();
if ( !parent::install()
|| !$this->registerHook('displayFooter')
){
$this->uninstall();
$this->deleteTab();
return false;
}
Configuration::updateValue('BPM_rule_1_active', false);
Configuration::updateValue('BPM_rule_1_timer', 40);
Configuration::updateValue('BPM_rule_2_active', false);
Configuration::updateValue('BPM_rule_2_timer', 20);
Configuration::updateValue('BPM_rule_3_active', false);
Configuration::updateValue('BPM_rule_3_timer', 60 * 2);
return true;
}
public function uninstall()
{
if (!parent::uninstall())
{
return false;
}
$this->deleteTab();
Configuration::deleteByName('BPM_rule_1_active');
Configuration::deleteByName('BPM_rule_1_timer');
Configuration::deleteByName('BPM_rule_2_active');
Configuration::deleteByName('BPM_rule_2_timer');
Configuration::deleteByName('BPM_rule_3_active');
Configuration::deleteByName('BPM_rule_3_timer');
return true;
}
public function createTab()
{
$langs = Language::getLanguages();
$idLang = (int) Configuration::get('PS_LANG_DEFAULT');
$tabs_i18n = array(
'fr' => array(
'Block Popup Newsletter'
),
'en' => array(
'Block Popup Newsletter'
)
);
$tab = new Tab();
$tab->class_name = 'AdminBlockPopupNewsletterConfiguration';
$tab->module = 'blockpopupnewsletter';
$tab->id_parent = 16;
$tab->position = 5;
foreach ($langs as $lang)
{
if (isset($tabs_i18n[$lang['iso_code']]))
{
$tab->name[$lang['id_lang']] = $tabs_i18n[$lang['iso_code']][0];
}
else
{
$tab->name[$lang['id_lang']] = $tabs_i18n['en'][0];
}
}
$tab->save();
}
public function deleteTab()
{
if ($id_tab = Tab::getIdFromClassName('AdminBlockPopupNewsletterConfiguration'))
{
$tab = new Tab((int) $id_tab);
$tab->delete();
}
}
public function hookdisplayFooter($params) {
if ($this->context->getMobileDevice() == false){
if (Tools::getValue('email_popup'))
{
$this->addInNewsletter(Tools::getValue('email_popup'));
}
if (!$this->isLogged() && !$this->isOnNewsletterTable() && !$this->hasClosePopup())
{
$content = '';
if ($this->checkRule2())
{
$content = $this->showPopup(Configuration::get('BPM_rule_2_timer'));
}
elseif ($this->checkRule1() || $this->checkRule3())
{
$content = $this->showPopup();
}
return $content;
}
}
}
protected function addInNewsletter($email)
{
if (Validate::isEmail($email) && !$this->isNewsletterRegistered($email))
{
$sql = 'INSERT INTO ' . _DB_PREFIX_ . 'newsletter (id_shop, id_shop_group, email, newsletter_date_add, ip_registration_newsletter, http_referer, active, id_lang)
VALUES
(
' . $this->context->shop->id . ',
' . $this->context->shop->id_shop_group . ',
"' . pSQL($email) . '",
NOW(),
"' . pSQL(Tools::getRemoteAddr()) . '",
"' . pSQL($_SERVER['HTTP_REFERER']) . '",
1,
' . $this->context->language->id . '
)
';
DB::getInstance()->execute($sql);
// syncro mailchimp
$MailchimpSync = new MailchimpSync();
$MailchimpSync->registerPopup($email, array('optin_ip' => Tools::getRemoteAddr()));
Mail::Send($this->context->language->id, 'newsletter_conf', Mail::l('Inscription Newsletter', $this->context->language->id), array('{discount}' => $code), $email, null, null, null, null, null, dirname(__FILE__).'/mails/');
}
}
private function isNewsletterRegistered($customerEmail)
{
$sql = 'SELECT `email`
FROM '._DB_PREFIX_.'newsletter
WHERE `email` = \''.pSQL($customerEmail).'\'
AND id_shop = '.$this->context->shop->id;
if (Db::getInstance()->getRow($sql))
return true;
$sql = 'SELECT `newsletter`
FROM '._DB_PREFIX_.'customer
WHERE `email` = \''.pSQL($customerEmail).'\'
AND id_shop = '.$this->context->shop->id;
if (!$registered = Db::getInstance()->getRow($sql))
return false;
if ($registered['newsletter'] == '1')
return true;
return false;
}
protected function checkRule1()
{
if (Configuration::get('BPM_rule_1_active'))
{
if (!$this->hasSessionCookie('RULE_1'))
{
$this->createSessionCookie('RULE_1', time());
}
if ($this->isHerFirstVisits() &&
$this->checkCookieTimer(
'RULE_1',
Configuration::get('BPM_rule_1_timer')
)
)
{
return true;
}
}
return false;
}
protected function checkRule2()
{
if (Configuration::get('BPM_rule_2_active'))
{
if ($this->isAnProductPage())
{
return true;
}
}
return false;
}
protected function checkRule3()
{
if (Configuration::get('BPM_rule_3_active'))
{
if (!$this->hasSessionCookie('RULE_3'))
{
$this->createSessionCookie('RULE_3', time());
}
if (!$this->cartIsEmpty() &&
$this->checkCookieTimer(
'RULE_3',
Configuration::get('BPM_rule_3_timer')
)
){
return true;
}
}
return false;
}
protected function checkCookieTimer($cookieName, $timer)
{
$cookieTime = $this->getSessionCookie($cookieName);
if ($cookieTime == null) {
return false;
}
return time() > $cookieTime + $timer;
}
protected function showPopup($timer = 1)
{
$this->setPopupIsClosed();
$this->context->controller->addCSS(($this->_path).'css/blockpopupnewsletter.css', 'all');
$this->context->smarty->assign(array(
'BPM_show_popup_time' => $timer * 1000
));
return $this->display(__FILE__, 'popup.tpl');;
}
protected function setPopupIsClosed()
{
$this->createSessionCookie('BPM_CLOSE_POPUP', true);
}
protected function hasClosePopup()
{
return $this->hasSessionCookie('BPM_CLOSE_POPUP');
}
protected function isLogged()
{
return $this->context->customer->isLogged();
}
protected function isOnNewsletterTable()
{
$total = Db::getInstance()->getRow('
SELECT COUNT(*) `total`
FROM `' . _DB_PREFIX_ . 'newsletter`
WHERE `ip_registration_newsletter` = "' . pSQL(Tools::getRemoteAddr()) . '"
');
if (!empty($total))
{
return (bool) $total['total'];
}
return false;
}
protected function isHerFirstVisits()
{
return true;
}
protected function isAnProductPage()
{
return Dispatcher::getInstance()->getController() == 'product';
}
protected function cartIsEmpty()
{
return !$this->context->cart->nbProducts();
}
private function createSessionCookie($name, $value)
{
setcookie($this->getCookieName($name), $value);
}
private function hasSessionCookie($name)
{
return isset($_COOKIE[$this->getCookieName($name)]);
}
private function getSessionCookie($name)
{
return $_COOKIE[$this->getCookieName($name)];
}
private function getCookieName($name)
{
return sha1($name);
}
}