first commit

This commit is contained in:
2024-11-05 12:22:50 +01:00
commit e5682a3912
19641 changed files with 2948548 additions and 0 deletions

View File

@@ -0,0 +1,461 @@
<?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(
json_encode([
'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(
json_encode([
'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(
json_encode([
'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(
json_encode([
'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(
json_encode([
'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(
json_encode([
'success' => true,
'message' => $this->trans('Product added', [], 'Modules.Blockwishlist.Shop'),
])
);
}
private function createNewWishListAction($params)
{
if (isset($params['name'])) {
if (!Validate::isGenericName($params['name'])) {
return $this->ajaxRender(
json_encode([
'success' => false,
'message' => $this->trans('The list name is invalid.', [], 'Modules.Blockwishlist.Shop'),
'datas' => [
'name' => $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(
json_encode([
'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(
json_encode([
'success' => false,
'message' => $this->trans('Error saving the new list', [], 'Modules.Blockwishlist.Shop'),
])
);
} else {
return $this->ajaxRender(
json_encode([
'success' => false,
'message' => $this->trans('Missing name parameter', [], 'Modules.Blockwishlist.Shop'),
])
);
}
}
private function renameWishListAction($params)
{
if (isset($params['idWishList'], $params['name'])) {
if (!Validate::isGenericName($params['name'])) {
return $this->ajaxRender(
json_encode([
'success' => false,
'message' => $this->trans('The list name is invalid', [], 'Modules.Blockwishlist.Shop'),
'datas' => [
'name' => $params['name'],
'id_whishlist' => $params['idWishList'],
],
])
);
}
$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(
json_encode([
'success' => true,
'message' => $this->trans('List has been renamed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
json_encode([
'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(
json_encode([
'success' => true,
'message' => $this->trans('List has been removed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
json_encode([
'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(
json_encode([
'success' => true,
'message' => $this->trans('Product successfully removed', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
json_encode([
'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(
json_encode([
'success' => true,
'message' => $this->trans('Product successfully updated', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
json_encode([
'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(
json_encode([
'wishlists' => $infos,
])
);
}
return $this->ajaxRenderMissingParams();
}
private function generateWishListToken()
{
return strtoupper(substr(sha1(uniqid((string) rand(), true) . _COOKIE_KEY_ . $this->context->customer->id), 0, 16));
}
private function ajaxRenderMissingParams()
{
return $this->ajaxRender(
json_encode([
'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(
json_encode([
'success' => true,
'message' => $this->trans('Product added to cart', [], 'Modules.Blockwishlist.Shop'),
])
);
}
return $this->ajaxRender(
json_encode([
'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(
json_encode([
'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(
json_encode([
'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,65 @@
<?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']),
'renameUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'renameWishlist']),
'shareUrl' => $this->context->link->getModuleLink('blockwishlist', 'action', ['action' => 'getUrlByIdWishlist']),
'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,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->template = 'module:blockwishlist/views/templates/errors/forbidden.tpl';
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;
}
}