Marion Muszynski 956251c132 nw trad
2015-10-29 12:53:58 +01:00

471 lines
16 KiB
PHP

<?php
if (!defined('_PS_VERSION_'))
exit;
class BlockNewsletterOverride extends BlockNewsletter
{
public function install() {
if (!parent::install() || !$this->registerHook('displayNewsletterFooter')|| !$this->registerHook('displayleftPostCms'))
return false;
/*return Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'newsletter_cmsps` (
`id_customer` int(11) NULL ,
`id_newsletter` int(11) NULL,
`id_category` int(11) NOT NULL
) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8');*/
}
public function ajaxCall($email, $action = 0, $page, $id_post = false) {
if (empty($email) || !Validate::isEmail($email)) {
return array(
'has_error' => true,
'css' => 'alert alert-danger',
'message' => Tools::displayError('Invalid email address.')
);
}
if ($action == 0) {
$cmsCategory = false;
if($id_post != false) {
$cmsCategory = $this->searchCmsCategory($id_post, false);
}
$register_status = $this->isNewsletterRegistered($email);
$email = pSQL($email);
if ($register_status > 0) {
if (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory != false){
if ($this->isNewsletterCmsPsRegistered($email, $cmsCategory['id_category'], $register_status)) {
$this->sendConfirmationEmail($email, $cmsCategory);
return array(
'has_error' => false,
'css' => 'alert alert-success',
'message' => Tools::displayError('You will recieve your PDF.')
);
}
}
} elseif (!$this->isRegistered($register_status)) {
if ($this->register($email, $register_status)) {
if (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory) {
$register_status = $this->isNewsletterRegistered($email);
if ($this->isNewsletterCmsPsRegistered($email, $cmsCategory['id_category'], $register_status)) {
$this->sendConfirmationEmail($email, $cmsCategory);
return array(
'has_error' => false,
'css' => 'alert alert-success',
'message' => Tools::displayError('You have successfully subscribed to this newsletter.')
);
}
}
} else {
return array(
'has_error' => true,
'css' => 'alert alert-danger',
'message' => Tools::displayError('An error occurred during the process.')
);
}
if (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory) {
$this->sendConfirmationEmail($email, $cmsCategory);
}
}
}
return array(
'has_error' => true,
'css' => 'alert alert-danger',
'message' => Tools::displayError('An error occurred during the subscription process.')
);
}
/**
* Register in block newsletter
*/
protected function newsletterRegistration()
{
if (empty($_POST['email']) || !Validate::isEmail($_POST['email'])){
return $this->error = $this->l('Invalid email address.');
} else if ($_POST['action'] == '1') {
/* Unsubscription */
$register_status = $this->isNewsletterRegistered($_POST['email']);
if ($register_status < 1) {
return $this->error = $this->l('This email address is not registered.');
}
if (!$this->unregister($_POST['email'], $register_status)) {
return $this->error = $this->l('An error occurred while attempting to unsubscribe.');
}
return $this->valid = $this->l('Unsubscription successful.');
} else if ($_POST['action'] == '0') {
/* Subscription */
$cmsCategory = false;
$genericPdf = false;
if(isset($_POST['id_post'])) {
$cmsCategory = $this->searchCmsCategory($_POST['id_post'], false);
} elseif(isset($_POST['id_cat'])) {
$cmsCategory = $this->searchCmsCategory($_POST['id_cat'], true);
} elseif(isset($_POST['page'])) {
if ($_POST['page'] == 'index' || $_POST['page'] == 'homeedito' || $_POST['page'] == 'postedito'){
$content = '';
if (glob(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf')) {
$content = file_get_contents(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf');
}
$genericPdf = array(
'content' => $content,
'name' => 'Tout-pratique.pdf',
'mine' => 'application/pdf'
);
}
}
$register_status = $this->isNewsletterRegistered($_POST['email']);
$email = pSQL($_POST['email']);
if ($register_status > 0){
if ($code = Configuration::get('NW_VOUCHER_CODE') && !$cmsCategory && !$genericPdf) {
if (!$this->isNewsletterCmsPsRegistered($email, 9999, $register_status, false)) {
if ($this->registerNewsletterCmsPs($email, 9999, $register_status)) {
$this->valid = $this->l('You will recieve your code.');
$this->sendVoucher($email, Configuration::get('NW_VOUCHER_CODE'));
}
} else {
return $this->error = $this->l('This email address is already registered.');
}
} elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory != false){
// enregistrement pour la categorie si pas enregistré
if ($this->isNewsletterCmsPsRegistered($email, $cmsCategory['id_category'], $register_status)) {
$this->valid = $this->l('You will recieve your PDF.');
$this->sendConfirmationEmail($email, $cmsCategory);
} else {
return $this->error = $this->l('An error occurred during the subscription process.');
}
} elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $genericPdf != false) {
$this->valid = $this->l('You will recieve your PDF.');
$this->sendConfirmationEmail($email, false, $genericPdf);
} else {
return $this->error = $this->l('This email address is already registered.');
}
}
if (!$this->isRegistered($register_status))
{
if (Configuration::get('NW_VERIFICATION_EMAIL'))
{
// create an unactive entry in the newsletter database
if ($register_status == self::GUEST_NOT_REGISTERED) {
$this->registerGuest($email, false);
}
if (!$token = $this->getToken($email, $register_status)) {
return $this->error = $this->l('An error occurred during the subscription process.');
}
$this->sendVerificationEmail($email, $token);
return $this->valid = $this->l('A verification email has been sent. Please check your inbox.');
} else {
if ($this->register($email, $register_status)) {
$this->valid = $this->l('You have successfully subscribed to this newsletter.');
} else {
return $this->error = $this->l('An error occurred during the subscription process.');
}
$register_status = $this->isNewsletterRegistered($email);
if ($code = Configuration::get('NW_VOUCHER_CODE') && !$cmsCategory && !$genericPdf) {
if ($this->isNewsletterCmsPsRegistered($email, 9999, $register_status)) {
$this->sendVoucher($email, Configuration::get('NW_VOUCHER_CODE'));
}
} elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory != false) {
if($this->isNewsletterCmsPsRegistered($email, $cmsCategory['id_category'], $register_status)){
$this->sendConfirmationEmail($email, $cmsCategory);
} else {
return $this->error = $this->l('An error occurred during the subscription process.');
}
} elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $genericPdf != false) {
$this->sendConfirmationEmail($email, false, $genericPdf);
}
}
}
}
}
public function searchCmsCategory($id, $strict = false){
if ($strict) {
$cmsCategory = Db::getInstance()->ExecuteS("
SELECT cc.`id_category`,cc.`id_parent`, ccl.`slug`
FROM `"._DB_PREFIX_."cmsps_categories` as cc
LEFT JOIN `"._DB_PREFIX_."cmsps_categories_lang` as ccl ON (ccl.`id_category` = cc.`id_category`)
WHERE cc.`id_category`= ".(int)$id." AND ccl.`id_lang` = ".(int)$this->context->language->id."
");
} else {
$cmsCategory = Db::getInstance()->ExecuteS("
SELECT cc.`id_category`,cc.`id_parent`, ccl.`slug`
FROM `"._DB_PREFIX_."cmsps_categories` as cc
LEFT JOIN `"._DB_PREFIX_."cmsps_posts` as cp ON (cp.`id_category` = cc.`id_category`)
LEFT JOIN `"._DB_PREFIX_."cmsps_categories_lang` as ccl ON (ccl.`id_category` = cc.`id_category`)
WHERE cp.`id_post`= ".(int)$id." AND ccl.`id_lang` = ".(int)$this->context->language->id."
");
}
while ($cmsCategory[0]['id_parent'] != 0) {
$cmsCategory = Db::getInstance()->ExecuteS("
SELECT cc.`id_category`, cc.`id_parent`, ccl.`slug`
FROM `"._DB_PREFIX_."cmsps_categories` as cc
LEFT JOIN `"._DB_PREFIX_."cmsps_categories_lang` as ccl ON (ccl.`id_category` = cc.`id_category`)
WHERE cc.`id_category` = ".(int)$cmsCategory[0]['id_parent']." AND ccl.`id_lang` = ".(int)$this->context->language->id."
");
}
return $cmsCategory[0];
}
protected function isNewsletterCmsPsRegistered($email, $id_category, $register_status, $haveToRegister = true)
{
if ($register_status == self::GUEST_REGISTERED) {
$sql = 'SELECT n.`id`
FROM '._DB_PREFIX_.'newsletter n
LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_newsletter` = n.`id`)
WHERE n.`email` = \''.pSQL($email).'\'
AND n.`id_shop` = '.$this->context->shop->id.'
AND nc.`id_category`='.(int)$id_category;
} elseif ($register_status == self::CUSTOMER_REGISTERED) {
$sql = 'SELECT c.`id_customer`
FROM '._DB_PREFIX_.'customer c
LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_customer` = c.`id_customer`)
WHERE c.`email` = \''.pSQL($email).'\'
AND c.`newsletter` = 1
AND nc.`id_customer` IS NOT NULL
AND nc.`id_category`='.(int)$id_category.'
AND c.`id_shop` = '.$this->context->shop->id;
} else {
return false;
}
if (Db::getInstance()->getRow($sql)){
return true;
} elseif ($haveToRegister) {
return $this->registerNewsletterCmsPs($email, $id_category, $register_status);
}
return false;
}
protected function registerNewsletterCmsPs($email, $id_category, $register_status)
{
if ($register_status == self::GUEST_REGISTERED) {
$sql = 'INSERT INTO '._DB_PREFIX_.'newsletter_cmsps (id_newsletter, id_category)
VALUES
(
(
SELECT n.`id`
FROM '._DB_PREFIX_.'newsletter n
WHERE n.`email` = \''.pSQL($email).'\'
AND id_shop = '.$this->context->shop->id.'
),
'.(int)$id_category.'
)';
} elseif ($register_status == self::CUSTOMER_REGISTERED) {
$sql = 'INSERT INTO '._DB_PREFIX_.'newsletter_cmsps (id_category, id_customer)
VALUES
(
'.(int)$id_category.',
(
SELECT c.`id_customer`
FROM '._DB_PREFIX_.'customer c
WHERE c.`email` = \''.pSQL($email).'\'
AND c.`newsletter` = \'1\'
AND id_shop = '.$this->context->shop->id.'
)
)';
}
if(Db::getInstance()->execute($sql)) {
return true;
}
return false;
}
/**
* @Override
*/
protected function unregister($email, $register_status)
{
if (!$this->unregisterNewsletterCmsPs($email, $id_category, $register_status)) {
return false;
}
if ($register_status == self::GUEST_REGISTERED) {
$sql = 'DELETE FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($_POST['email']).'\' AND id_shop = '.$this->context->shop->id;
} elseif ($register_status == self::CUSTOMER_REGISTERED) {
$sql = 'UPDATE '._DB_PREFIX_.'customer SET `newsletter` = 0 WHERE `email` = \''.pSQL($_POST['email']).'\' AND id_shop = '.$this->context->shop->id;
}
if (!isset($sql) || !Db::getInstance()->execute($sql)) {
return false;
}
return true;
}
protected function unregisterNewsletterCmsPs($email, $register_status)
{
if ($register_status == self::GUEST_REGISTERED) {
$sql = 'DELETE FROM '._DB_PREFIX_.'newsletter_cmsps nc
WHERE nc.`id_newsletter` =
(
SELECT n.`id`
FROM '._DB_PREFIX_.'newsletter n
WHERE n.`email` = \''.pSQL($email).'\'
AND id_shop = '.$this->context->shop->id.'
)';
} elseif ($register_status == self::CUSTOMER_REGISTERED) {
$sql = 'DELETE FROM '._DB_PREFIX_.'newsletter_cmsps nc
WHERE nc.`id_customer` =
(
SELECT c.`id_customer`
FROM '._DB_PREFIX_.'customer c
WHERE c.`email` = \''.pSQL($email).'\'
AND c.`newsletter` = \'1\'
AND id_shop = '.$this->context->shop->id.'
)';
}
if(Db::getInstance()->execute($sql)) {
return true;
}
return false;
}
/**
* @Override
*/
protected function sendConfirmationEmail($email, $cmsCategory = false, $genericPdf = false)
{
$data = array();
$fileAttachment = array();
if ($cmsCategory != false) {
if (glob(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$cmsCategory['slug'].'.pdf')) {
$fileAttachment['content'] = file_get_contents(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$cmsCategory['slug'].'.pdf');
$fileAttachment['name'] = 'Tout-pratique-'.$cmsCategory['slug'].'.pdf';
$fileAttachment['mime'] = "application/pdf";
$data['{attached_file_url}'] = _CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$cmsCategory['slug'].'.pdf';
$data['{attached_file}'] = "Tout-pratique-".$cmsCategory['slug'];
} else {
// on envoie le pdf générique
if (glob(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf')) {
$fileAttachment['content'] = file_get_contents(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf');
}
$fileAttachment['name'] = 'Tout-pratique.pdf';
$fileAttachment['mime'] = "application/pdf";
$data['{attached_file_url}'] = _CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf';
$data['{attached_file}'] = "Tout-pratique";
}
} elseif($genericPdf != false) {
$fileAttachment = $genericPdf;
$data['{attached_file_url}'] = _CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf';
$data['{attached_file}'] = $genericPdf['name'];
}
return Mail::Send($this->context->language->id, 'newsletter_conf', Mail::l('Newsletter confirmation', $this->context->language->id), $data, pSQL($email), null, null, null, $fileAttachment, null, _THEME_DIR_.'modules/blocknewsletter/mails/', false, $this->context->shop->id);
}
/**
* @Override
*/
protected function _prepareHook($params)
{
if (Tools::isSubmit('submitNewsletter'))
{
$this->newsletterRegistration();
if ($this->error)
{
$this->smarty->assign(
array(
'color' => 'red',
'msg' => $this->error,
'nw_value' => isset($_POST['email']) ? pSQL($_POST['email']) : false,
'nw_error' => true,
'action' => $_POST['action'],
'nw_block' => $_POST['block'],
)
);
}
else if ($this->valid)
{
$this->smarty->assign(
array(
'color' => 'green',
'msg' => $this->valid,
'nw_error' => false,
'nw_block' => $_POST['block'],
)
);
}
}
$this->smarty->assign('this_path', $this->_path);
}
public function hookdisplayNewsletterFooter($params)
{
if (!isset($this->prepared) || !$this->prepared) {
$this->_prepareHook($params);
}
if(isset($params['id_cmspost'])) {
$this->context->smarty->assign('post_id',$params['id_cmspost']);
}
$this->prepared = true;
return $this->display(__FILE__, 'blocknewsletter_footer.tpl');
}
public function hookdisplayleftPostCms($params)
{
if (!isset($this->prepared) || !$this->prepared) {
$this->_prepareHook($params);
}
$this->prepared = true;
if(isset($params['id_cmspost'])) {
$this->context->smarty->assign('post_id',$params['id_cmspost']);
}
return $this->display(__FILE__, 'blocknewsletter_left.tpl');
}
public function hookdisplayFooter($params)
{
if($this->context->customer->isLogged()) {
$register_status = $this->isNewsletterRegistered($this->context->customer->email);
}
if((isset($register_status) && $register_status>0) || !isset($register_status)) {
if (!isset($this->prepared) || !$this->prepared) {
$this->_prepareHook($params);
}
$this->prepared = true;
if(isset($params['id_cmspost'])) {
$this->context->smarty->assign('post_id',$params['id_cmspost']);
}
$this->context->smarty->assign('nw_url','/modules/blocknewsletter/ajax_ligthbox.php');
return $this->display(__FILE__, 'blocknewsletter_lightbox.tpl');
}
}
}