bebeboutik/modules/advsendtoafriend/advsendtoafriend.php

207 lines
7.8 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8005 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
class AdvSendToAFriend extends Module
{
function __construct($dontTranslate = false)
{
$this->name = 'advsendtoafriend';
$this->version = '1.1';
$this->author = 'Antadis';
$this->tab = 'front_office_features';
$this->need_instance = 0;
parent::__construct();
if(!$dontTranslate)
{
$this->displayName = $this->l('Send to a Friend module (advanced)');
$this->description = $this->l('Allows customers to send a product link to a friend.');
}
}
function install()
{
return (parent::install() AND $this->registerHook('extraLeft'));
}
function hookExtraLeft($params)
{
global $smarty;
$smarty->assign('this_path', $this->_path);
return $this->display(__FILE__, 'product_page.tpl');
}
public function displayPageForm()
{
if (!$this->active)
Tools::display404Error();
$this->displayFrontForm();
include(dirname(__FILE__).'/../../footer.php');
}
public function displayFrontForm()
{
global $smarty, $cookie, $link;
$error = false;
$confirm = false;
if(Tools::getValue('id_product')) {
/* Product informations */
$product = new Product((int)Tools::getValue('id_product'), false, (int)$cookie->id_lang);
if(!Validate::isLoadedObject($product)) {
Tools::display404Error();
}
$productLink = $link->getProductLink($product);
if (isset($_POST['submitAddtoafriend']))
{
/* Fields verifications */
if (empty($_POST['email']) OR empty($_POST['name']))
$error = $this->l('You must fill in all fields.');
elseif (empty($_POST['email']) OR !Validate::isEmail($_POST['email']))
$error = $this->l('The e-mail given is invalid.');
elseif (!Validate::isName($_POST['name']))
$error = $this->l('The name given is invalid.');
elseif (!isset($_GET['id_product']) OR !is_numeric($_GET['id_product']))
$error = $this->l('An error occurred during the process.');
else
{
/* Email generation */
$subject = ($cookie->customer_firstname ? $cookie->customer_firstname.' '.strtoupper($cookie->customer_lastname) : $this->l('A friend')).' '.$this->l('sent you a link to').' '.$product->name;
$templateVars = array(
'{product}' => $product->name,
'{product_price}' => Tools::displayPrice($product->price),
'{product_link}' => $productLink,
'{customer}' => ($cookie->customer_firstname ? $cookie->customer_firstname.' '.strtoupper($cookie->customer_lastname) : $this->l('A friend')),
'{name}' => Tools::safeOutput($_POST['name'])
);
/* Email sending */
if (!Mail::Send((int)$cookie->id_lang, 'send_to_a_friend', Mail::l('A friend sent you a link to').' '.$product->name, $templateVars, $_POST['email'], NULL, ($cookie->email ? $cookie->email : NULL), ($cookie->customer_firstname ? $cookie->customer_firstname.' '.strtoupper($cookie->customer_lastname) : NULL), NULL, NULL, dirname(__FILE__).'/mails/'))
$error = $this->l('An error occurred during the process.');
else
Tools::redirect(_MODULE_DIR_.$this->name.'/sendtoafriend-form.php?id_product='.(int)$product->id.'&submited');
}
}
/* Image */
$images = $product->getImages((int)$cookie->id_lang);
foreach ($images AS $k => $image)
if ($image['cover'])
{
$cover['id_image'] = (int)$product->id.'-'.(int)$image['id_image'];
$cover['legend'] = $image['legend'];
}
if (!isset($cover))
$cover = array('id_image' => Language::getIsoById((int)$cookie->id_lang).'-default', 'legend' => 'No picture');
$smarty->assign(array(
'cover' => $cover,
'errors' => $error,
'confirm' => $confirm,
'product' => $product,
'productLink' => $productLink
));
include(dirname(__FILE__).'/../../header.php');
echo $this->display(__FILE__, 'sendtoafriend.tpl');
} elseif(Module::isInstalled('privatesales') && Tools::getValue('id_sale')) {
$sale = new sale((int) Tools::getValue('id_sale'));
if($sale && $sale->enabled) {
$trailers_i18n = array(
'fr' => 'bande-annonce',
'en' => 'trailer',
);
$langs = Language::getLanguages();
$lang = (int) $cookie->id_lang;
$isolang = Language::getIsoById($lang);
if(Configuration::get('PS_REWRITING_SETTINGS')) {
if(count($langs) > 1) {
$canonical_url = Tools::getShopDomain(TRUE, TRUE).__PS_BASE_URI__.$iso_code.'/'.(isset($trailers_i18n[$iso_code])? $trailers_i18n[$isolang]: $trailers_i18n['en']).'/'.$sale->id.'-'.Tools::str2url($sale->title[$lang]);
} else {
$canonical_url = Tools::getShopDomain(TRUE, TRUE).__PS_BASE_URI__.(isset($trailers_i18n[$isolang])? $trailers_i18n[$isolang]: $trailers_i18n['en']).'/'.$sale->id.'-'.Tools::str2url($sale->title[$lang]);
}
} else {
$canonical_url = Tools::getShopDomain(TRUE, TRUE).__PS_BASE_URI__.'modules/privatesales/trailer.php?id_sale='.$sale->id;
}
if (isset($_POST['submitAddtoafriend']))
{
/* Fields verifications */
if (empty($_POST['email']) OR empty($_POST['name']))
$error = $this->l('You must fill in all fields.');
elseif (empty($_POST['email']) OR !Validate::isEmail($_POST['email']))
$error = $this->l('The e-mail given is invalid.');
elseif (!Validate::isName($_POST['name']))
$error = $this->l('The name given is invalid.');
else
{
/* Email generation */
$subject = ($cookie->customer_firstname ? $cookie->customer_firstname.' '.strtoupper($cookie->customer_lastname) : $this->l('A friend')).' '.$this->l('sent you a link to').' '.$product->name;
$templateVars = array(
'{sale}' => $sale->title[(int) $cookie->id_lang],
'{sale_link}' => $canonical_url,
'{sale_img}' => Tools::getShopDomain(TRUE, TRUE).__PS_BASE_URI__.'modules/privatesales/img/'.$sale->id.'/liston_'.(int) $cookie->id_lang.'.jpg',
'{customer}' => ($cookie->customer_firstname ? $cookie->customer_firstname.' '.strtoupper($cookie->customer_lastname) : $this->l('A friend')),
'{name}' => Tools::safeOutput($_POST['name'])
);
/* Email sending */
if (!Mail::Send((int)$cookie->id_lang, 'send_to_a_friend_sale', Mail::l('A friend sent you a link to').' '.$sale->title[(int) $cookie->id_lang], $templateVars, $_POST['email'], NULL, ($cookie->email ? $cookie->email : NULL), ($cookie->customer_firstname ? $cookie->customer_firstname.' '.strtoupper($cookie->customer_lastname) : NULL), NULL, NULL, dirname(__FILE__).'/mails/'))
$error = $this->l('An error occurred during the process.');
else
Tools::redirect(_MODULE_DIR_.$this->name.'/sendtoafriend-form.php?id_sale='.(int) $sale->id.'&submited');
}
}
$smarty->assign(array(
'errors' => $error,
'confirm' => $confirm,
'psale' => $sale,
'saleLink' => $canonical_url
));
include(dirname(__FILE__).'/../../header.php');
echo $this->display(__FILE__, 'sendtoafriend.tpl');
} else {
Tools::display404Error();
}
} else {
Tools::display404Error();
}
}
}