Marion Muszynski a8a9cc26a6 suite modif
2015-09-23 14:35:24 +02:00

307 lines
10 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;
}
public function ajaxCall($email, $action = 0, $page, $id_post = false){
/*$_POST['email'] = $email;
$_POST['action'] = $action;
$_POST['page'] = $page;
if($id_post)
$_POST['id_post'] = $id_post;
$result = $this->newsletterRegistration();
if($result && $this->error != '')
die(Tools::jsonEncode(array(
'has_error' => true,
'message' => $this->error
)));
elseif($result && $this->valid!='')
die(Tools::jsonEncode(array(
'has_error' => false,
'message' => $this->valid
)));*/
if (empty($email) || !Validate::isEmail($email))
return array(
'has_error' => true,
'css' => 'alert alert-danger',
'message' => 'Invalid email address.'
);
if ($action == 0){
$cmsCategory = false;
if($id_post)
$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){
$this->sendConfirmationEmail($email, $cmsCategory);
return array(
'has_error' => false,
'css' => 'alert alert-success',
'message' => 'You will recieve your PDF.'
);
}
}
elseif (!$this->isRegistered($register_status)) {
if ($this->register($email, $register_status))
return array(
'has_error' => false,
'css' => 'alert alert-success',
'message' => 'You have successfully subscribed to this newsletter.'
);
else
return array(
'has_error' => true,
'css' => 'alert alert-danger',
'message' => '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' => '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.');
/* Unsubscription */
else if ($_POST['action'] == '1')
{
$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.');
}
/* Subscription */
else if ($_POST['action'] == '0')
{
$cmsCategory = false;
$genericPdf = false;
if(isset($_POST['id_post']) && $id_post = $_POST['id_post'])
$cmsCategory = $this->searchCmsCategory($id_post, false);
elseif(isset($_POST['id_cat']) && $id_cat = $_POST['id_cat'])
$cmsCategory = $this->searchCmsCategory($id_cat, true);
elseif(isset($_POST['page']) && ($_POST['page'] == 'index' || $_POST['page'] == 'homeedito' || $_POST['page'] == 'postedito'))
$genericPdf = array(
'content' => _CMS_CAT_IMG_DIR_.'pdf/Tout-pratique.pdf',
'name' => 'Tout-pratique.pdf',
'mine' => 'application/pdf'
);
$register_status = $this->isNewsletterRegistered($_POST['email']);
$email = pSQL($_POST['email']);
if ($register_status > 0){
// Pas d'enregistrement mais envoie du pdf
if (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory){
$this->valid = $this->l('You will recieve your PDF.');
$this->sendConfirmationEmail($email, $cmsCategory);
}
elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $genericPdf){
$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.');
if ($code = Configuration::get('NW_VOUCHER_CODE') && !$cmsCategory && !$genericPdf)
$this->sendVoucher($email, Configuration::get('NW_VOUCHER_CODE'));
elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory)
$this->sendConfirmationEmail($email, $cmsCategory);
elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $genericPdf)
$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_category']." AND ccl.`id_lang` = ".(int)$this->context->language->id."
");
return $cmsCategory[0];
}
/**
* @Override
*/
protected function sendConfirmationEmail($email, $cmsCategory = false, $genericPdf = false)
{
$data = array();
$fileAttachment = array();
if($cmsCategory){
if(glob(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$cmsCategory['slug'].'.pdf')){
$fileAttachment['content'] = _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
$fileAttachment['content'] = _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){
$fileAttachment = $genericPdf;
$data['{attached_file_url}'] = $genericPdf['content'];
$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');
}
}
}