This commit is contained in:
2025-04-01 00:38:54 +02:00
parent d4d4c0c09d
commit 87da06293a
22351 changed files with 5168854 additions and 7538 deletions

View File

@@ -0,0 +1,436 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (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:
* https://opensource.org/licenses/AFL-3.0
* 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.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\Module\BlockWishList\Access\CustomerAccess;
class BlockWishListActionModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
if (false === $this->context->customer->isLogged()) {
$this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('You aren\'t logged in', [], 'Modules.Blockwishlist.Shop'),
])
);
exit;
}
$params = Tools::getValue('params');
// Here we call all methods dynamically given by the path
if (method_exists($this, Tools::getValue('action') . 'Action')) {
call_user_func([$this, Tools::getValue('action') . 'Action'], $params);
exit;
}
$this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Unknown action', [], 'Modules.Blockwishlist.Shop'),
])
);
exit;
}
private function addProductToWishListAction($params)
{
$id_product = (int) $params['id_product'];
$product = new Product($id_product);
if (!Validate::isLoadedObject($product)) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('There was an error adding the product', [], 'Modules.Blockwishlist.Shop'),
])
);
}
$idWishList = (int) $params['idWishList'];
$id_product_attribute = (int) $params['id_product_attribute'];
$quantity = (int) $params['quantity'];
if (0 === $quantity) {
$quantity = $product->minimal_quantity;
}
if (false === $this->assertProductAttributeExists($id_product, $id_product_attribute) && $id_product_attribute !== 0) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('There was an error while adding the product attributes', [], 'Modules.Blockwishlist.Shop'),
])
);
}
$wishlist = new WishList($idWishList);
// Exit if not owner of the wishlist
$this->assertWriteAccess($wishlist);
$productIsAdded = $wishlist->addProduct(
$idWishList,
$this->context->customer->id,
$id_product,
$id_product_attribute,
$quantity
);
$newStat = new Statistics();
$newStat->id_product = $id_product;
$newStat->id_product_attribute = $id_product_attribute;
$newStat->id_shop = $this->context->shop->id;
$newStat->save();
if (false === $productIsAdded) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('There was an error adding the product', [], 'Modules.Blockwishlist.Shop'),
])
);
}
Hook::exec('actionWishlistAddProduct', [
'idWishlist' => $idWishList,
'customerId' => $this->context->customer->id,
'idProduct' => $id_product,
'idProductAttribute' => $id_product_attribute,
]);
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('Product added', [], 'Modules.Blockwishlist.Shop'),
])
);
}
private function createNewWishListAction($params)
{
if (isset($params['name'])) {
$wishlist = new WishList();
$wishlist->name = $params['name'];
$wishlist->id_shop_group = $this->context->shop->id_shop_group;
$wishlist->id_customer = $this->context->customer->id;
$wishlist->id_shop = $this->context->shop->id;
$wishlist->token = $this->generateWishListToken();
if (true === $wishlist->save()) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('The list has been properly created', [], 'Modules.Blockwishlist.Shop'),
'datas' => [
'name' => $wishlist->name,
'id_wishlist' => $wishlist->id,
],
])
);
}
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Error saving the new list', [], 'Modules.Blockwishlist.Shop'),
])
);
} else {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Missing name parameter', [], 'Modules.Blockwishlist.Shop'),
])
);
}
}
private function renameWishListAction($params)
{
if (isset($params['idWishList'], $params['name'])) {
$wishlist = new WishList($params['idWishList']);
// Exit if not owner of the wishlist
$this->assertWriteAccess($wishlist);
$wishlist->name = $params['name'];
if (true === $wishlist->save()) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('List has been renamed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('List could not be renamed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRenderMissingParams();
}
private function deleteWishListAction($params)
{
if (isset($params['idWishList'])) {
$wishlist = new WishList($params['idWishList']);
// Exit if not owner of the wishlist
$this->assertWriteAccess($wishlist);
if (true === (bool) $wishlist->delete()) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('List has been removed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('List deletion was unsuccessful', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRenderMissingParams();
}
private function deleteProductFromWishListAction($params)
{
if (
isset($params['idWishList'])
&& isset($params['id_product'])
&& isset($params['id_product_attribute'])
) {
// Exit if not owner of the wishlist
$this->assertWriteAccess(
new WishList($params['idWishList'])
);
$isDeleted = WishList::removeProduct(
$params['idWishList'],
$this->context->customer->id,
$params['id_product'],
$params['id_product_attribute']
);
if (true === $isDeleted) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('Product successfully removed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Unable to remove product from list', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRenderMissingParams();
}
private function updateProductFromWishListAction($params)
{
if (isset(
$params['idWishList'],
$params['id_product'],
$params['id_product_attribute'],
$params['priority'],
$params['quantity']
)) {
// Exit if not owner of the wishlist
$this->assertWriteAccess(
new WishList($params['idWishList'])
);
$isDeleted = WishList::updateProduct(
$params['idWishList'],
$params['id_product'],
$params['id_product_attribute'],
$params['priority'],
$params['quantity']
);
if (true === $isDeleted) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('Product successfully updated', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Unable to update product from wishlist', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRenderMissingParams();
}
private function getAllWishListAction()
{
$infos = WishList::getAllWishListsByIdCustomer($this->context->customer->id);
if (empty($infos)) {
$wishlist = new WishList();
$wishlist->id_shop = $this->context->shop->id;
$wishlist->id_shop_group = $this->context->shop->id_shop_group;
$wishlist->id_customer = $this->context->customer->id;
$wishlist->name = Configuration::get('blockwishlist_WishlistDefaultTitle', $this->context->language->id);
$wishlist->token = $this->generateWishListToken();
$wishlist->default = 1;
$wishlist->add();
$infos = WishList::getAllWishListsByIdCustomer($this->context->customer->id);
}
foreach ($infos as $key => $wishlist) {
$infos[$key]['shareUrl'] = $this->context->link->getModuleLink('blockwishlist', 'view', ['token' => $wishlist['token']]);
$infos[$key]['listUrl'] = $this->context->link->getModuleLink('blockwishlist', 'view', ['id_wishlist' => $wishlist['id_wishlist']]);
}
if (false === empty($infos)) {
return $this->ajaxRender(
Tools::jsonEncode([
'wishlists' => $infos,
])
);
}
return $this->ajaxRenderMissingParams();
}
private function generateWishListToken()
{
return Tools::strtoupper(substr(sha1(uniqid((string) rand(), true) . _COOKIE_KEY_ . $this->context->customer->id), 0, 16));
}
private function ajaxRenderMissingParams()
{
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Request is missing one or multiple parameters', [], 'Modules.Blockwishlist.Shop'),
])
);
}
private function addProductToCartAction($params)
{
$productAdd = WishList::addBoughtProduct(
$params['idWishlist'],
$params['id_product'],
$params['id_product_attribute'],
(int) $this->context->cart->id,
$params['quantity']
);
// Transform an add to favorite
Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'blockwishlist_statistics`
SET `id_cart` = ' . (int) $this->context->cart->id . '
WHERE `id_cart` = 0
AND `id_product` = ' . (int) $params['id_product'] . '
AND `id_product_attribute` = ' . (int) $params['id_product_attribute'] . '
AND `id_shop`= ' . $this->context->shop->id
);
if (true === $productAdd) {
return $this->ajaxRender(
Tools::jsonEncode([
'success' => true,
'message' => $this->trans('Product added to cart', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('Error when adding product to cart', [], 'Modules.Blockwishlist.Shop'),
])
);
}
private function getUrlByIdWishListAction($params)
{
$wishlist = new WishList($params['idWishList']);
return $this->ajaxRender(
Tools::jsonEncode([
'status' => 'true',
'url' => $this->context->link->getModuleLink('blockwishlist', 'view', ['token' => $wishlist->token]),
])
);
}
/**
* Stop the execution if the current customer isd not allowed to alter the wishlist
*
* @param WishList $wishlist
*/
private function assertWriteAccess(WishList $wishlist)
{
if ((new CustomerAccess($this->context->customer))->hasWriteAccessToWishlist($wishlist)) {
return;
}
$this->ajaxRender(
Tools::jsonEncode([
'success' => false,
'message' => $this->trans('You\'re not allowed to manage this list.', [], 'Modules.Blockwishlist.Shop'),
])
);
exit;
}
/**
* Check if product attribute id is related to the product
*
* @param int $id_product
* @param int $id_product_attribute
*
* @return bool
*/
private function assertProductAttributeExists($id_product, $id_product_attribute)
{
return Db::getInstance()->getValue(
'SELECT pas.`id_product_attribute` ' .
'FROM `' . _DB_PREFIX_ . 'product_attribute` pa ' .
'INNER JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` pas ON (pas.id_product_attribute = pa.id_product_attribute) ' .
'WHERE pas.id_shop =' . (int) $this->context->shop->id . ' AND pa.`id_product` = ' . (int) $id_product . ' ' .
'AND pas.id_product_attribute = ' . (int) $id_product_attribute
);
}
}

View File

@@ -0,0 +1,110 @@
<?php
/*
* 2007-2016 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-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5.0
*/
class BlockWishListCartModuleFrontController extends ModuleFrontController
{
public $ssl = true;
public function __construct()
{
parent::__construct();
$this->context = Context::getContext();
include_once($this->module->getLocalPath().'WishList.php');
}
/**
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
$context = Context::getContext();
$action = Tools::getValue('action');
$add = (!strcmp($action, 'add') ? 1 : 0);
$delete = (!strcmp($action, 'delete') ? 1 : 0);
$id_wishlist = (int)Tools::getValue('id_wishlist');
$id_product = (int)Tools::getValue('id_product');
$quantity = (int)Tools::getValue('quantity');
if (Tools::getIsset('group')) {
$id_product_attribute = (int)Product::getIdProductAttributesByIdAttributes($id_product, Tools::getValue('group'));
}
// Instance of module class for translations
$module = new BlockWishList();
if (Configuration::get('PS_TOKEN_ENABLE') == 1 &&
strcmp(Tools::getToken(false), Tools::getValue('token')) &&
$context->customer->isLogged() === true
)
echo $module->l('Invalid token', 'cart');
if ($context->customer->isLogged())
{
if ($id_wishlist && WishList::exists($id_wishlist, $context->customer->id) === true)
$context->cookie->id_wishlist = (int)$id_wishlist;
if ((int)$context->cookie->id_wishlist > 0 && !WishList::exists($context->cookie->id_wishlist, $context->customer->id))
$context->cookie->id_wishlist = '';
if (empty($context->cookie->id_wishlist) === true || $context->cookie->id_wishlist == false)
$context->smarty->assign('error', true);
if (($add || $delete) && empty($id_product) === false)
{
if (!isset($context->cookie->id_wishlist) || $context->cookie->id_wishlist == '')
{
$wishlist = new WishList();
$wishlist->id_shop = $context->shop->id;
$wishlist->id_shop_group = $context->shop->id_shop_group;
$wishlist->default = 1;
$mod_wishlist = new BlockWishList();
$wishlist->name = $mod_wishlist->default_wishlist_name;
$wishlist->id_customer = (int)$context->customer->id;
list($us, $s) = explode(' ', microtime());
srand($s * $us);
$wishlist->token = Tools::strtoupper(Tools::substr(sha1(uniqid(rand(), true)._COOKIE_KEY_.$context->customer->id), 0, 16));
$wishlist->add();
$context->cookie->id_wishlist = (int)$wishlist->id;
}
if ($add && $quantity)
WishList::addProduct($context->cookie->id_wishlist, $context->customer->id, $id_product, $id_product_attribute, $quantity);
else if ($delete)
WishList::removeProduct($context->cookie->id_wishlist, $context->customer->id, $id_product, $id_product_attribute);
}
$context->smarty->assign('products', WishList::getProductByIdCustomer($context->cookie->id_wishlist, $context->customer->id, $context->language->id, null, true));
if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/blockwishlist/blockwishlist-ajax.tpl'))
$context->smarty->display(_PS_THEME_DIR_.'modules/blockwishlist/blockwishlist-ajax.tpl');
elseif (Tools::file_exists_cache(dirname(__FILE__).'/blockwishlist-ajax.tpl'))
$context->smarty->display(dirname(__FILE__).'/blockwishlist-ajax.tpl');
else
echo $module->l('No template found', 'cart');
} else
echo $module->l('You must be logged in to manage your wishlist.', 'cart');
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* 2007-2016 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-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,69 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (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:
* https://opensource.org/licenses/AFL-3.0
* 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.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class BlockWishlistListsModuleFrontController extends ModuleFrontController
{
/**
* @var bool If set to true, will be redirected to authentication page
*/
public $auth = true;
public function initContent()
{
parent::initContent();
$this->context->smarty->assign(
[
'url' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'getAllWishlist']),
'createUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'createNewWishlist']),
'deleteListUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'deleteWishlist']),
'deleteProductUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'deleteProductFromWishlist']),
'renameUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'renameWishlist']),
'shareUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'getUrlByIdWishlist']),
'addUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'addProductToWishlist']),
'accountLink' => '#',
'wishlistsTitlePage' => Configuration::get('blockwishlist_WishlistPageName', $this->context->language->id),
'newWishlistCTA' => Configuration::get('blockwishlist_CreateButtonLabel', $this->context->language->id),
]
);
$this->context->controller->registerJavascript(
'blockwishlistController',
'modules/blockwishlist/public/wishlistcontainer.bundle.js',
[
'priority' => 200,
]
);
$this->setTemplate('module:blockwishlist/views/templates/pages/lists.tpl');
}
public function getBreadcrumbLinks()
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = $this->addMyAccountToBreadcrumb();
$breadcrumb['links'][] = [
'title' => Configuration::get('blockwishlist_WishlistPageName', $this->context->language->id),
'url' => $this->context->link->getModuleLink('blockwishlist', 'lists'),
];
return $breadcrumb;
}
}

View File

@@ -0,0 +1,247 @@
<?php
/*
* 2007-2016 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-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5.0
*/
class BlockWishListMyWishListModuleFrontController extends ModuleFrontController
{
public $ssl = true;
public function __construct()
{
parent::__construct();
$this->context = Context::getContext();
include_once($this->module->getLocalPath().'WishList.php');
}
/**
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
$action = Tools::getValue('action');
if (!Tools::isSubmit('myajax'))
$this->assign();
elseif (!empty($action) && method_exists($this, 'ajaxProcess'.Tools::toCamelCase($action)))
$this->{'ajaxProcess'.Tools::toCamelCase($action)}();
else
die(Tools::jsonEncode(array('error' => 'method doesn\'t exist')));
}
/**
* Assign wishlist template
*/
public function assign()
{
$errors = array();
if ($this->context->customer->isLogged())
{
$add = Tools::getIsset('add');
$add = (empty($add) === false ? 1 : 0);
$delete = Tools::getIsset('deleted');
$delete = (empty($delete) === false ? 1 : 0);
$default = Tools::getIsset('default');
$default = (empty($default) === false ? 1 : 0);
$id_wishlist = Tools::getValue('id_wishlist');
if (Tools::isSubmit('submitWishlist'))
{
if (Configuration::get('PS_TOKEN_ACTIVATED') == 1 && strcmp(Tools::getToken(), Tools::getValue('token')))
$errors[] = $this->module->l('Invalid token', 'mywishlist');
if (!count($errors))
{
$name = Tools::getValue('name');
if (empty($name))
$errors[] = $this->module->l('You must specify a name.', 'mywishlist');
if (WishList::isExistsByNameForUser($name))
$errors[] = $this->module->l('This name is already used by another list.', 'mywishlist');
if (!count($errors))
{
$wishlist = new WishList();
$wishlist->id_shop = $this->context->shop->id;
$wishlist->id_shop_group = $this->context->shop->id_shop_group;
$wishlist->name = $name;
$wishlist->id_customer = (int)$this->context->customer->id;
!$wishlist->isDefault($wishlist->id_customer) ? $wishlist->default = 1 : '';
list($us, $s) = explode(' ', microtime());
srand($s * $us);
$wishlist->token = Tools::strtoupper(Tools::substr(sha1(uniqid(rand(), true)._COOKIE_KEY_.$this->context->customer->id), 0, 16));
$wishlist->add();
Mail::Send(
$this->context->language->id,
'wishlink',
Mail::l('Your wishlist\'s link', $this->context->language->id),
array(
'{wishlist}' => $wishlist->name,
'{message}' => $this->context->link->getModuleLink('blockwishlist', 'view', array('token' => $wishlist->token))
),
$this->context->customer->email,
$this->context->customer->firstname.' '.$this->context->customer->lastname,
null,
strval(Configuration::get('PS_SHOP_NAME')),
null,
null,
$this->module->getLocalPath().'mails/');
Tools::redirect($this->context->link->getModuleLink('blockwishlist', 'mywishlist'));
}
}
}
else if ($add)
WishList::addCardToWishlist($this->context->customer->id, Tools::getValue('id_wishlist'), $this->context->language->id);
elseif ($delete && empty($id_wishlist) === false)
{
$wishlist = new WishList((int)$id_wishlist);
if ($this->context->customer->isLogged() && $this->context->customer->id == $wishlist->id_customer && Validate::isLoadedObject($wishlist))
$wishlist->delete();
else
$errors[] = $this->module->l('Cannot delete this wishlist', 'mywishlist');
}
elseif ($default)
{
$wishlist = new WishList((int)$id_wishlist);
if ($this->context->customer->isLogged() && $this->context->customer->id == $wishlist->id_customer && Validate::isLoadedObject($wishlist))
$wishlist->setDefault();
else
$errors[] = $this->module->l('Cannot delete this wishlist', 'mywishlist');
}
$this->context->smarty->assign('wishlists', WishList::getByIdCustomer($this->context->customer->id));
$this->context->smarty->assign('nbProducts', WishList::getInfosByIdCustomer($this->context->customer->id));
}
else
Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('blockwishlist', 'mywishlist')));
$this->context->smarty->assign(array(
'id_customer' => (int)$this->context->customer->id,
'errors' => $errors,
'form_link' => $errors,
'base_url' => $this->context->link->getBaseLink($this->context->shop->id),
));
$this->setTemplate('module:blockwishlist/views/templates/front/mywishlist.tpl');
}
public function ajaxProcessDeleteList()
{
if (!$this->context->customer->isLogged())
die(Tools::jsonEncode(array('success' => false,
'error' => $this->module->l('You aren\'t logged in', 'mywishlist'))));
$default = Tools::getIsset('default');
$default = (empty($default) === false ? 1 : 0);
$id_wishlist = Tools::getValue('id_wishlist');
$wishlist = new WishList((int)$id_wishlist);
if (Validate::isLoadedObject($wishlist) && $wishlist->id_customer == $this->context->customer->id)
{
$default_change = $wishlist->default ? true : false;
$id_customer = $wishlist->id_customer;
$wishlist->delete();
}
else
die(Tools::jsonEncode(array('success' => false,
'error' => $this->module->l('Cannot delete this wishlist', 'mywishlist'))));
if ($default_change)
{
$array = WishList::getDefault($id_customer);
if (count($array))
die(Tools::jsonEncode(array(
'success' => true,
'id_default' => $array[0]['id_wishlist']
)));
}
die(Tools::jsonEncode(array('success' => true)));
}
public function ajaxProcessSetDefault()
{
if (!$this->context->customer->isLogged())
die(Tools::jsonEncode(array('success' => false,
'error' => $this->module->l('You aren\'t logged in', 'mywishlist'))));
$default = Tools::getIsset('default');
$default = (empty($default) === false ? 1 : 0);
$id_wishlist = Tools::getValue('id_wishlist');
if ($default)
{
$wishlist = new WishList((int)$id_wishlist);
if (Validate::isLoadedObject($wishlist) && $wishlist->id_customer == $this->context->customer->id && $wishlist->setDefault())
die(Tools::jsonEncode(array('success' => true)));
}
die(Tools::jsonEncode(array('error' => true)));
}
public function ajaxProcessProductChangeWishlist()
{
if (!$this->context->customer->isLogged())
die(Tools::jsonEncode(array('success' => false,
'error' => $this->module->l('You aren\'t logged in', 'mywishlist'))));
$id_product = (int)Tools::getValue('id_product');
$id_product_attribute = (int)Tools::getValue('id_product_attribute');
$quantity = (int)Tools::getValue('quantity');
$priority = (int)Tools::getValue('priority');
$id_old_wishlist = (int)Tools::getValue('id_old_wishlist');
$id_new_wishlist = (int)Tools::getValue('id_new_wishlist');
$new_wishlist = new WishList((int)$id_new_wishlist);
$old_wishlist = new WishList((int)$id_old_wishlist);
//check the data is ok
if (!$id_product || !is_int($id_product_attribute) || !$quantity ||
!is_int($priority) || ($priority < 0 && $priority > 2) || !$id_old_wishlist || !$id_new_wishlist ||
(Validate::isLoadedObject($new_wishlist) && $new_wishlist->id_customer != $this->context->customer->id) ||
(Validate::isLoadedObject($old_wishlist) && $old_wishlist->id_customer != $this->context->customer->id))
die(Tools::jsonEncode(array('success' => false, 'error' => $this->module->l('Error while moving product to another list', 'mywishlist'))));
$res = true;
$check = (int)Db::getInstance()->getValue('SELECT quantity FROM '._DB_PREFIX_.'wishlist_product
WHERE `id_product` = '.$id_product.' AND `id_product_attribute` = '.$id_product_attribute.' AND `id_wishlist` = '.$id_new_wishlist);
if ($check)
{
$res &= $old_wishlist->removeProduct($id_old_wishlist, $this->context->customer->id, $id_product, $id_product_attribute);
$res &= $new_wishlist->updateProduct($id_new_wishlist, $id_product, $id_product_attribute, $priority, $quantity + $check);
}
else
{
$res &= $old_wishlist->removeProduct($id_old_wishlist, $this->context->customer->id, $id_product, $id_product_attribute);
$res &= $new_wishlist->addProduct($id_new_wishlist, $this->context->customer->id, $id_product, $id_product_attribute, $quantity);
}
if (!$res)
die(Tools::jsonEncode(array('success' => false, 'error' => $this->module->l('Error while moving product to another list', 'mywishlist'))));
die(Tools::jsonEncode(array('success' => true, 'msg' => $this->module->l('The product has been correctly moved', 'mywishlist'))));
}
}

View File

@@ -0,0 +1,317 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (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:
* https://opensource.org/licenses/AFL-3.0
* 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.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\Module\BlockWishList\Access\CustomerAccess;
use PrestaShop\Module\BlockWishList\Search\WishListProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrderFactory;
/**
* View the content of a personal wishlist
*/
class BlockWishlistViewModuleFrontController extends ProductListingFrontController
{
/**
* Made public as the core considers this class as an ModuleFrontController
* and other modules expects to find the $module property.
*
* @var BlockWishList
*/
public $module;
/**
* @var WishList
*/
protected $wishlist;
/**
* @var CustomerAccess
*/
private $customerAccess;
public function __construct()
{
/** @var BlockWishList $module */
$module = Module::getInstanceByName('blockwishlist');
$this->module = $module;
if (empty($this->module->active)) {
Tools::redirect('index');
}
parent::__construct();
$this->controller_type = 'modulefront';
$this->customerAccess = new CustomerAccess($this->context->customer);
}
/**
* {@inheritdoc}
*/
public function init()
{
$id_wishlist = $this->getWishlistId();
$this->wishlist = new WishList($id_wishlist);
if (false === Validate::isLoadedObject($this->wishlist)) {
Tools::redirect('index.php?controller=404');
}
parent::init();
if (false === $this->customerAccess->hasReadAccessToWishlist($this->wishlist)) {
header('HTTP/1.1 403 Forbidden');
header('Status: 403 Forbidden');
$this->errors[] = $this->trans(
'You do not have access to this wishlist.',
[],
'Modules.Blockwishlist.Shop'
);
$this->setTemplate('errors/forbidden');
return;
}
$this->context->smarty->assign(
[
'id' => $id_wishlist,
'wishlistName' => $this->wishlist->name,
'isGuest' => !$this->customerAccess->hasWriteAccessToWishlist($this->wishlist),
'url' => Context::getContext()->link->getModuleLink('blockwishlist', 'view', $this->getAccessParams()),
'wishlistsLink' => Context::getContext()->link->getModuleLink('blockwishlist', 'lists'),
'deleteProductUrl' => Context::getContext()->link->getModuleLink('blockwishlist', 'action', ['action' => 'deleteProductFromWishlist']),
]
);
}
/**
* {@inheritdoc}
*/
public function initContent()
{
parent::initContent();
if (false === $this->customerAccess->hasReadAccessToWishlist($this->wishlist)) {
return;
}
$this->context->controller->registerJavascript(
'blockwishlistController',
'modules/blockwishlist/public/productslist.bundle.js',
[
'priority' => 200,
]
);
$this->doProductSearch(
'../../../modules/blockwishlist/views/templates/pages/products-list.tpl',
[
'entity' => 'wishlist_product',
'id_wishlist' => $this->wishlist->id,
]
);
}
/**
* {@inheritdoc}
*/
public function getListingLabel()
{
return $this->trans(
'WishList: %wishlist_name%',
['%wishlist_name%' => $this->wishlist->name],
'Modules.Blockwishlist.Shop'
);
}
/**
* {@inheritdoc}
*/
protected function getProductSearchQuery()
{
$query = new ProductSearchQuery();
$query->setSortOrder(
new SortOrder(
'product',
Tools::getProductsOrder('by'),
Tools::getProductsOrder('way')
)
);
return $query;
}
/**
* {@inheritdoc}
*/
protected function getDefaultProductSearchProvider()
{
return new WishListProductSearchProvider(
Db::getInstance(),
$this->wishlist,
new SortOrderFactory($this->getTranslator()),
$this->getTranslator()
);
}
/**
* {@inheritdoc}
*/
protected function getAjaxProductSearchVariables()
{
parent::getAjaxProductSearchVariables();
$context = parent::getProductSearchContext();
$query = $this->getProductSearchQuery();
$provider = $this->getDefaultProductSearchProvider();
$resultsPerPage = (int) Tools::getValue('resultsPerPage');
if ($resultsPerPage <= 0) {
$resultsPerPage = Configuration::get('PS_PRODUCTS_PER_PAGE');
}
// we need to set a few parameters from back-end preferences
$query
->setResultsPerPage($resultsPerPage)
->setPage(max((int) Tools::getValue('page'), 1))
;
// set the sort order if provided in the URL
if (($encodedSortOrder = Tools::getValue('order'))) {
$query->setSortOrder(SortOrder::newFromString($encodedSortOrder));
}
// get the parameters containing the encoded facets from the URL
$encodedFacets = Tools::getValue('q');
$query->setEncodedFacets($encodedFacets);
$result = $provider->runQuery($context, $query);
if (!$result->getCurrentSortOrder()) {
$result->setCurrentSortOrder($query->getSortOrder());
}
// prepare the products
$products = $this->prepareMultipleProductsForTemplate($result->getProducts());
// render the facets with the core
$rendered_facets = $this->renderFacets($result);
$rendered_active_filters = $this->renderActiveFilters($result);
$pagination = $this->getTemplateVarPagination($query, $result);
// prepare the sort orders
// note that, again, the product controller is sort-orders agnostic
// a module can easily add specific sort orders that it needs to support (e.g. sort by "energy efficiency")
$sort_orders = $this->getTemplateVarSortOrders(
$result->getAvailableSortOrders(),
$query->getSortOrder()->toString()
);
$sort_selected = false;
$labelDefaultSort = '';
if (!empty($sort_orders)) {
foreach ($sort_orders as $order) {
if ($order['field'] == 'id_wishlist_product') {
$labelDefaultSort = $order['label'];
}
if (isset($order['current']) && true === $order['current']) {
$sort_selected = $order['label'];
break;
}
}
}
$searchVariables = [
'result' => $result,
'label' => $this->getListingLabel(),
'products' => $products,
'sort_orders' => $sort_orders,
'sort_selected' => $sort_selected ? $sort_selected : $labelDefaultSort,
'pagination' => $pagination,
'rendered_facets' => $rendered_facets,
'rendered_active_filters' => $rendered_active_filters,
'js_enabled' => $this->ajax,
'current_url' => $this->updateQueryString([
'q' => $result->getEncodedFacets(),
]),
];
Hook::exec('filterProductSearch', ['searchVariables' => &$searchVariables]);
Hook::exec('actionProductSearchAfter', $searchVariables);
return $searchVariables;
}
/**
* Depending on the parameters sent, checks if the current visitor may reach the page
*
* @return int|false
*/
private function getWishlistId()
{
if (Tools::getIsset('id_wishlist')) {
return (int) Tools::getValue('id_wishlist');
}
if (Tools::getIsset('token')) {
$wishlistData = WishList::getByToken(
Tools::getValue('token')
);
if (!empty($wishlistData['id_wishlist'])) {
return $wishlistData['id_wishlist'];
}
}
return false;
}
/**
* @return array
*/
private function getAccessParams()
{
if (Tools::getIsset('token')) {
return ['token' => Tools::getValue('token')];
}
if (Tools::getIsset('id_wishlist')) {
return ['id_wishlist' => Tools::getValue('id_wishlist')];
}
return [];
}
public function getBreadcrumbLinks()
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = $this->addMyAccountToBreadcrumb();
$breadcrumb['links'][] = [
'title' => Configuration::get('blockwishlist_WishlistPageName', $this->context->language->id),
'url' => $this->context->link->getModuleLink('blockwishlist', 'lists'),
];
$breadcrumb['links'][] = [
'title' => $this->wishlist->name,
'url' => Context::getContext()->link->getModuleLink('blockwishlist', 'view', $this->getAccessParams()),
];
return $breadcrumb;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (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:
* https://opensource.org/licenses/AFL-3.0
* 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.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;