Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Classes/Domain/DoctrineRepository/PageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Extcode\CartProducts\Domain\DoctrineRepository;

/*
* This file is part of the package extcode/cart-products.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\CartProducts\Domain\Model\Product\Product;
use TYPO3\CMS\Core\Database\ConnectionPool;

final readonly class PageRepository
{
private const TABLENAME = 'pages';

public function __construct(
private ConnectionPool $connectionPool,
) {}

public function getProductPageByProduct(Product $product): array|bool
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLENAME);

return $queryBuilder->select('*')
->from(self::TABLENAME)
->where(
$queryBuilder->expr()->eq('cart_products_product', $product->getUid())
)
->orderBy('sorting')
->setMaxResults(1)
->executeQuery()
->fetchAssociative();
}
}
25 changes: 21 additions & 4 deletions Classes/Domain/DoctrineRepository/Product/ProductRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Extcode\CartProducts\Domain\DoctrineRepository\Product;

/*
Expand All @@ -15,17 +17,32 @@

class ProductRepository
{
public const TABLENAME = 'tx_cartproducts_domain_model_product_product';

public function __construct(
private readonly ConnectionPool $connectionPool
private readonly ConnectionPool $connectionPool,
) {}

public function findProductByUid(int $uid): array|bool
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLENAME);

return $queryBuilder
->select('uid', 'title', 'images')
->from(self::TABLENAME)
->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid)))
->setMaxResults(1)
->executeQuery()
->fetchAssociative();
}

public function getStock(int $uid): int
{
$queryBuilder = $this->getQueryBuilder();

return $queryBuilder
->select('stock')
->from('tx_cartproducts_domain_model_product_product')
->from(self::TABLENAME)
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT))
)
Expand All @@ -43,7 +60,7 @@ public function addQuantityToStock(int $uid, int $quantity): void
$queryBuilder = $this->getQueryBuilder();

$queryBuilder
->update('tx_cartproducts_domain_model_product_product')
->update(self::TABLENAME)
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT))
)
Expand All @@ -64,7 +81,7 @@ private function getQueryBuilder(): QueryBuilder
{
return $this
->connectionPool
->getConnectionForTable('tx_cartproducts_domain_model_product_product')
->getConnectionForTable(self::TABLENAME)
->createQueryBuilder();
}
}
56 changes: 56 additions & 0 deletions Classes/Domain/Model/WatchlistItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Extcode\CartProducts\Domain\Model;

/*
* This file is part of the package extcode/cart-products.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use WerkraumMedia\Watchlist\Domain\Model\Item;

readonly class WatchlistItem implements Item
{
private const TYPE = 'CartProducts';

public function __construct(
private int $uid,
private int $detailPid,
private string $title,
private ?int $fileReference = null,
) {}

public function getUniqueIdentifier(): string
{
return self::TYPE . '-' . $this->uid . '-' . $this->detailPid;
}

public function getType(): string
{
return self::TYPE;
}

public function getUid(): int
{
return $this->uid;
}

public function getDetailPid(): int
{
return $this->detailPid;
}

public function getTitle(): string
{
return $this->title;
}

public function getFileReference(): ?int
{
return $this->fileReference;
}
}
57 changes: 57 additions & 0 deletions Classes/Domain/Model/WatchlistItemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Extcode\CartProducts\Domain\Model;

/*
* This file is part of the package extcode/cart-products.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\CartProducts\Domain\DoctrineRepository\Product\ProductRepository;

final readonly class WatchlistItemFactory
{
public function __construct(
private ProductRepository $productRepository,
) {}

public function createFromIdentifier(
string $identifier,
): ?WatchlistItem {
list($uid, $detailPid) = explode('-', $identifier);

$product = $this->productRepository->findProductByUid((int)$uid);

if ($product === false) {
return null;
}

return new WatchlistItem(
(int)$uid,
(int)$detailPid,
(string)$product['title'],
$this->getFirstImageReference($product),
);
}

private function getFirstImageReference(array $product): ?int
{
if (is_string($product['images'] ?? null) === false || $product['images'] === '') {
return null;
}

$images = explode(',', $product['images']);

$image = array_pop($images);

if (is_numeric($image) === false) {
return null;
}

return (int)$image;
}
}
33 changes: 33 additions & 0 deletions Classes/Handler/WatchlistItemHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Extcode\CartProducts\Handler;

/*
* This file is part of the package extcode/cart-products.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\CartProducts\Domain\Model\WatchlistItemFactory;
use WerkraumMedia\Watchlist\Domain\ItemHandlerInterface;
use WerkraumMedia\Watchlist\Domain\Model\Item;

final readonly class WatchlistItemHandler implements ItemHandlerInterface
{
public function __construct(
private WatchlistItemFactory $watchlistItemFactory,
) {}

public function return(string $identifier): ?Item
{
return $this->watchlistItemFactory->createFromIdentifier($identifier);
}

public function handlesType(): string
{
return 'CartProducts';
}
}
59 changes: 59 additions & 0 deletions Classes/ViewHelpers/Product/PageUidViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Extcode\CartProducts\ViewHelpers\Product;

/*
* This file is part of the package extcode/cart-products.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\CartProducts\Domain\DoctrineRepository\PageRepository;
use Extcode\CartProducts\Domain\Model\Product\Product;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class PageUidViewHelper extends AbstractViewHelper
{
public function __construct(
private readonly PageRepository $pageRepository,
) {}

public function initializeArguments()
{
parent::initializeArguments();

$this->registerArgument(
'product',
Product::class,
'product',
true
);

$this->registerArgument(
'settings',
'array',
'settings array',
true
);
}

public function render(): string
{
$product = $this->arguments['product'];

$page = $this->pageRepository->getProductPageByProduct($product);

if ($page) {
return $page['uid'];
}
if ($product->getCategory() && $product->getCategory()->getCartProductShowPid()) {
return $product->getCategory()->getCartProductShowPid();
}
if ($this->arguments['settings']['showPageUids']) {
return $this->arguments['settings']['showPageUids'];
}

return $GLOBALS['TSFE']->id;
}
}
27 changes: 27 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Extcode\CartProducts\Configuration;

use Extcode\CartProducts\Handler\WatchlistItemHandler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use WerkraumMedia\Watchlist\Domain\ItemHandlerRegistry;

return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder) {
if ($containerBuilder->hasDefinition(ItemHandlerRegistry::class)) {
$watchlistItemHandlerDefinition = new Definition(
WatchlistItemHandler::class
);
$watchlistItemHandlerDefinition->addTag('watchlist.itemHandler')
->setAutoconfigured(true)
->setAutowired(true);
$containerBuilder->addDefinitions(
[
WatchlistItemHandler::class => $watchlistItemHandlerDefinition,
]
);
}
};
2 changes: 2 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:

Extcode\CartProducts\:
resource: '../Classes/*'
exclude:
- '../Classes/Handler/WatchlistItemHandler.php'

Extcode\CartProducts\EventListener\Create\CheckRequest:
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.. include:: ../../Includes.rst.txt

========================================
Feature: #274 - Add WatchlistItemHandler
========================================

See `Issue 274 <https://github.com/extcode/cart_products/issues/274>`__

Description
===========

After the release of the werkraummedia/watchlist extension for TYPO3 v13 the
WatchlistItemHandler for extcode/cart-products can be added again.
This allows products to be added to the watchlist.

.. index:: Frontend
20 changes: 20 additions & 0 deletions Documentation/Changelog/7.2/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. include:: ../../Includes.rst.txt

7.2 Changes
===========

**Table of contents**

.. contents::
:local:
:depth: 1

Features
^^^^^^^^

.. toctree::
:maxdepth: 1
:titlesonly:
:glob:

Feature-*
1 change: 1 addition & 0 deletions Documentation/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ChangeLog
:maxdepth: 5
:titlesonly:

7.2/Index
7.1/Index
7.0/Index
6.0/Index
Expand Down
Loading