Skip to content

Commit

Permalink
customer page, favorite products list
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Dec 16, 2023
1 parent 1611179 commit 7ba5238
Show file tree
Hide file tree
Showing 19 changed files with 316 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
public: true
resource: '../../src/Hook/*'
exclude:
- '../../src/Hook/{DisplayAdminCustomers.php,AbstractDisplayAdminHook.php}'
- '../../src/Hook/index.php'

Oksydan\IsFavoriteProducts\Services\FavoriteProductService:
Expand Down
52 changes: 52 additions & 0 deletions src/Hook/AbstractDisplayAdminHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsFavoriteProducts\Hook;

use Oksydan\IsFavoriteProducts\Services\FavoriteProductService;
use Twig\Environment;

abstract class AbstractDisplayAdminHook extends AbstractHook
{
/**
* @var Environment
*/
protected Environment $twig;

public function __construct(
\Is_favoriteproducts $module,
\Context $context,
FavoriteProductService $favoriteProductService,
Environment $twig
) {
parent::__construct($module, $context, $favoriteProductService);
$this->twig = $twig;
}

public function execute(array $params): string
{
if (!$this->shouldBlockBeDisplayed($params)) {
return '';
}

return $this->twig->render($this->getTemplateFullPath(), $this->getTemplateVariables($params));
}

protected function getTemplateVariables(array $params): array
{
return [];
}

protected function shouldBlockBeDisplayed(array $params)
{
return true;
}

public function getTemplateFullPath(): string
{
return "@Modules/{$this->module->name}/views/templates/hook/admin/{$this->getTemplate()}";
}

abstract protected function getTemplate(): string;
}
2 changes: 1 addition & 1 deletion src/Hook/AbstractDisplayHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function shouldBlockBeDisplayed(array $params)

public function getTemplateFullPath(): string
{
return "module:{$this->module->name}/views/templates/hook/{$this->getTemplate()}";
return "module:{$this->module->name}/views/templates/hook/front/{$this->getTemplate()}";
}

abstract protected function getTemplate(): string;
Expand Down
6 changes: 3 additions & 3 deletions src/Hook/ActionFrontControllerSetMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
namespace Oksydan\IsFavoriteProducts\Hook;

use Oksydan\IsFavoriteProducts\DTO\FavoriteProduct;
use Oksydan\IsFavoriteProducts\Presenter\FavoriteProductJsonJsonPresenter;
use Oksydan\IsFavoriteProducts\Presenter\FavoriteProductJsonPresenter;
use Oksydan\IsFavoriteProducts\Services\FavoriteProductService;

class ActionFrontControllerSetMedia extends AbstractHook
{
private FavoriteProductJsonJsonPresenter $productPresenter;
private FavoriteProductJsonPresenter $productPresenter;

public function __construct(
\Is_favoriteproducts $module,
\Context $context,
FavoriteProductService $favoriteProductService,
FavoriteProductJsonJsonPresenter $productPresenter
FavoriteProductJsonPresenter $productPresenter
) {
parent::__construct($module, $context, $favoriteProductService);
$this->productPresenter = $productPresenter;
Expand Down
65 changes: 65 additions & 0 deletions src/Hook/DisplayAdminCustomers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsFavoriteProducts\Hook;

use Oksydan\IsFavoriteProducts\Presenter\AdminFavoriteProductPresenter;
use Oksydan\IsFavoriteProducts\Provider\CustomerFavoriteProductsProvider;
use Oksydan\IsFavoriteProducts\Services\FavoriteProductService;
use PrestaShop\PrestaShop\Adapter\Validate;
use Twig\Environment;

class DisplayAdminCustomers extends AbstractDisplayAdminHook
{
protected CustomerFavoriteProductsProvider $customerFavoriteProductsProvider;

protected AdminFavoriteProductPresenter $adminFavoriteProductPresenter;

public function __construct(
\Is_favoriteproducts $module,
\Context $context,
FavoriteProductService $favoriteProductService,
Environment $twig,
CustomerFavoriteProductsProvider $customerFavoriteProductsProvider,
AdminFavoriteProductPresenter $adminFavoriteProductPresenter
) {
parent::__construct($module, $context, $favoriteProductService, $twig);
$this->customerFavoriteProductsProvider = $customerFavoriteProductsProvider;
$this->adminFavoriteProductPresenter = $adminFavoriteProductPresenter;
}

private const TEMPLATE_FILE = 'displayAdminCustomers.html.twig';

protected function getTemplate(): string
{
return self::TEMPLATE_FILE;
}

protected function getTemplateVariables(array $params): array
{
$idCustomer = (int) $params['id_customer'];
$customer = new \Customer($idCustomer);

if (!Validate::isLoadedObject($customer)) {
return [];
}

$shop = new \Shop($customer->id_shop);

if (!Validate::isLoadedObject($shop)) {
return [];
}

$productsRaw = $this->customerFavoriteProductsProvider->getFavoriteProductsByCustomer($customer, $shop);
$products = [];

foreach ($productsRaw as $productRaw) {
$products[] = $this->adminFavoriteProductPresenter->present($productRaw, $this->context->language);
}

return [
'products' => $products,
];
}
}
1 change: 1 addition & 0 deletions src/Installer/ModuleInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ModuleInstaller
'displayProductActions',
'displayCrossSellingShoppingCart',
'actionCartSave',
'displayAdminCustomers',
];

private \Is_favoriteproducts $module;
Expand Down
60 changes: 60 additions & 0 deletions src/Presenter/AdminFavoriteProductPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsFavoriteProducts\Presenter;

use Oksydan\IsFavoriteProducts\DTO\FavoriteProduct;
use Oksydan\IsFavoriteProducts\Repository\ProductLegacyRepository;
use Symfony\Component\Routing\RouterInterface;

class AdminFavoriteProductPresenter implements AdminFavoriteProductPresenterInterface
{
/**
* @var RouterInterface $router
*/
protected RouterInterface $router;

protected ProductLegacyRepository $productLegacyRepository;

protected const DATE_FORMAT = 'Y-m-d H:i:s';

public function __construct(
RouterInterface $router,
ProductLegacyRepository $productLegacyRepository
)
{
$this->router = $router;
$this->productLegacyRepository = $productLegacyRepository;
}

public function present(FavoriteProduct $favoriteProduct, \Language $language): array
{
$productObject = new \Product($favoriteProduct->getIdProduct(), false, $language->id);
$idAttribute = $favoriteProduct->getIdProductAttribute();
$productName = $productObject->name;

if ($idAttribute > 0) {
$combination = $this->productLegacyRepository->getProductCombinationForIdProductAttribute($favoriteProduct->getIdProduct(), $idAttribute, $language->id);

$combinationName = array_reduce($combination, function ($combinationReduced, $combination) {
$combinationReduced[] = $combination['group_name'] . ': ' . $combination['attribute_name'];

return $combinationReduced;
}, []);

$productName = $productName . ' - ' . implode(', ', $combinationName);
}

$admin_url = $this->router->generate('admin_product_form', [
'id' => $favoriteProduct->getIdProduct(),
]);

return [
'id' => $favoriteProduct->getIdProduct(),
'name' => $productName,
'admin_url' => $admin_url,
'date_add' => $favoriteProduct->getDateAdd()->format(self::DATE_FORMAT),
];
}
}
12 changes: 12 additions & 0 deletions src/Presenter/AdminFavoriteProductPresenterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsFavoriteProducts\Presenter;

use Oksydan\IsFavoriteProducts\DTO\FavoriteProduct;

interface AdminFavoriteProductPresenterInterface
{
public function present(FavoriteProduct $favoriteProduct, \Language $language): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Oksydan\IsFavoriteProducts\DTO\FavoriteProduct;

class FavoriteProductJsonJsonPresenter implements JsonPresenterInterface
class FavoriteProductJsonPresenter implements JsonPresenterInterface
{
public function present(FavoriteProduct $favoriteProduct): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Presenter/JsonPresenterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface JsonPresenterInterface
{
public function present(FavoriteProduct $favoriteProduct);
public function present(FavoriteProduct $favoriteProduct): string;
}
46 changes: 46 additions & 0 deletions src/Provider/CustomerFavoriteProductsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsFavoriteProducts\Provider;

use Oksydan\IsFavoriteProducts\Entity\FavoriteProduct;
use Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper;
use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository;

class CustomerFavoriteProductsProvider implements CustomerFavoriteProductsProviderInterface
{

/*
* @var FavoriteProductRepository $favoriteProductsRepository
*/
private FavoriteProductRepository $favoriteProductsRepository;

/**
* @var FavoriteProductMapper $favoriteProductMapper
*/
private FavoriteProductMapper $favoriteProductMapper;

public function __construct(
FavoriteProductRepository $favoriteProductsRepository,
FavoriteProductMapper $favoriteProductMapper
)
{
$this->favoriteProductsRepository = $favoriteProductsRepository;
$this->favoriteProductMapper = $favoriteProductMapper;
}

public function getFavoriteProductsByCustomer(\Customer $customer, \Shop $shop): array
{
$favoriteProducts = $this->favoriteProductsRepository->getFavoriteProductsByCustomer(
(int) $customer->id,
(int) $shop->id
);

$products = array_map(function (FavoriteProduct $favoriteProduct) {
return $this->favoriteProductMapper->mapFavoriteProductEntityToFavoriteProductDTO($favoriteProduct);
}, $favoriteProducts);

return $products;
}
}
11 changes: 11 additions & 0 deletions src/Provider/CustomerFavoriteProductsProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsFavoriteProducts\Provider;


interface CustomerFavoriteProductsProviderInterface
{
public function getFavoriteProductsByCustomer(\Customer $customer, \Shop $shop): array;
}
25 changes: 25 additions & 0 deletions src/Repository/ProductLegacyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,29 @@ public function checkProductActiveAndVisible(

return (bool) $qb->execute()->fetchOne();
}

public function getProductCombinationForIdProductAttribute(
int $idProduct,
int $idProductAttribute,
int $idLang
): array {
$qb = $this->connection->createQueryBuilder()
->select('alg.name AS group_name, al.`name` AS attribute_name')
->from($this->dbPrefix . 'product_attribute', 'pa')
->leftJoin('pa', $this->dbPrefix . 'product_attribute_combination', 'pac', 'pac.id_product_attribute = pa.id_product_attribute')
->leftJoin('pac', $this->dbPrefix . 'attribute', 'a', 'a.id_attribute = pac.id_attribute')
->leftJoin('a', $this->dbPrefix . 'attribute_lang', 'al', 'a.id_attribute = al.id_attribute')
->leftJoin('a', $this->dbPrefix . 'attribute_group', 'ag', 'ag.id_attribute_group = a.id_attribute_group')
->leftJoin('ag', $this->dbPrefix . 'attribute_group_lang', 'alg', 'ag.id_attribute_group = alg.id_attribute_group')
->where('pa.id_product = :id_product')
->andWhere('pa.id_product_attribute = :id_product_attribute')
->andWhere('al.id_lang = :id_lang')
->setParameter('id_product', $idProduct)
->setParameter('id_product_attribute', $idProductAttribute)
->setParameter('id_lang', $idLang)
->groupBy('pa.id_product_attribute, ag.id_attribute_group')
->orderBy('pa.id_product_attribute');

return $qb->execute()->fetchAllAssociative();
}
}
37 changes: 37 additions & 0 deletions views/templates/hook/admin/displayAdminCustomers.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% if products is not empty %}
<div class="col-md-6 col">

<div class="card customer-favorite-products-card">
<h3 class="card-header">
<i class="material-icons">favorite</i>
{{ 'Favorite products'|trans({}, 'Modules.Isfavoriteproducts.Admin') }}
<span class="badge badge-primary rounded">{{ products|length }}</span>
</h3>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>{{ 'ID'|trans({}, 'Admin.Global') }}</th>
<th>{{ 'Name'|trans({}, 'Admin.Global') }}</th>
<th>{{ 'Data add'|trans({}, 'Admin.Global') }}</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.id }}</td>
<td>
<a href="{{ product.admin_url }}">
{{ product.name }}
</a>
</td>
<td>{{ product.date_add }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

</div>
{% endif %}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7ba5238

Please sign in to comment.