Skip to content

Commit b6f586f

Browse files
committed
[TASK] Add support for werkraummedia/watchlist
Relates: #274
1 parent 1499724 commit b6f586f

File tree

18 files changed

+393
-10
lines changed

18 files changed

+393
-10
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartProducts\Domain\DoctrineRepository;
6+
7+
/*
8+
* This file is part of the package extcode/cart-products.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\CartProducts\Domain\Model\Product\Product;
15+
use TYPO3\CMS\Core\Database\ConnectionPool;
16+
17+
final readonly class PageRepository
18+
{
19+
private const TABLENAME = 'pages';
20+
21+
public function __construct(
22+
private ConnectionPool $connectionPool,
23+
) {}
24+
25+
public function getProductPageByProduct(Product $product): array|bool
26+
{
27+
$queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLENAME);
28+
29+
return $queryBuilder->select('*')
30+
->from(self::TABLENAME)
31+
->where(
32+
$queryBuilder->expr()->eq('cart_products_product', $product->getUid())
33+
)
34+
->orderBy('sorting')
35+
->setMaxResults(1)
36+
->executeQuery()
37+
->fetchAssociative();
38+
}
39+
}

Classes/Domain/DoctrineRepository/Product/ProductRepository.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Extcode\CartProducts\Domain\DoctrineRepository\Product;
46

57
/*
@@ -15,17 +17,32 @@
1517

1618
class ProductRepository
1719
{
20+
public const TABLENAME = 'tx_cartproducts_domain_model_product_product';
21+
1822
public function __construct(
19-
private readonly ConnectionPool $connectionPool
23+
private readonly ConnectionPool $connectionPool,
2024
) {}
2125

26+
public function findProductByUid(int $uid): array|bool
27+
{
28+
$queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLENAME);
29+
30+
return $queryBuilder
31+
->select('uid', 'title', 'images')
32+
->from(self::TABLENAME)
33+
->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid)))
34+
->setMaxResults(1)
35+
->executeQuery()
36+
->fetchAssociative();
37+
}
38+
2239
public function getStock(int $uid): int
2340
{
2441
$queryBuilder = $this->getQueryBuilder();
2542

2643
return $queryBuilder
2744
->select('stock')
28-
->from('tx_cartproducts_domain_model_product_product')
45+
->from(self::TABLENAME)
2946
->where(
3047
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT))
3148
)
@@ -43,7 +60,7 @@ public function addQuantityToStock(int $uid, int $quantity): void
4360
$queryBuilder = $this->getQueryBuilder();
4461

4562
$queryBuilder
46-
->update('tx_cartproducts_domain_model_product_product')
63+
->update(self::TABLENAME)
4764
->where(
4865
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT))
4966
)
@@ -64,7 +81,7 @@ private function getQueryBuilder(): QueryBuilder
6481
{
6582
return $this
6683
->connectionPool
67-
->getConnectionForTable('tx_cartproducts_domain_model_product_product')
84+
->getConnectionForTable(self::TABLENAME)
6885
->createQueryBuilder();
6986
}
7087
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartProducts\Domain\Model;
6+
7+
/*
8+
* This file is part of the package extcode/cart-products.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use WerkraumMedia\Watchlist\Domain\Model\Item;
15+
16+
readonly class WatchlistItem implements Item
17+
{
18+
private const TYPE = 'CartProducts';
19+
20+
public function __construct(
21+
private int $uid,
22+
private int $detailPid,
23+
private string $title,
24+
private ?int $fileReference = null,
25+
) {}
26+
27+
public function getUniqueIdentifier(): string
28+
{
29+
return self::TYPE . '-' . $this->uid . '-' . $this->detailPid;
30+
}
31+
32+
public function getType(): string
33+
{
34+
return self::TYPE;
35+
}
36+
37+
public function getUid(): int
38+
{
39+
return $this->uid;
40+
}
41+
42+
public function getDetailPid(): int
43+
{
44+
return $this->detailPid;
45+
}
46+
47+
public function getTitle(): string
48+
{
49+
return $this->title;
50+
}
51+
52+
public function getFileReference(): ?int
53+
{
54+
return $this->fileReference;
55+
}
56+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartProducts\Domain\Model;
6+
7+
/*
8+
* This file is part of the package extcode/cart-products.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\CartProducts\Domain\DoctrineRepository\Product\ProductRepository;
15+
16+
final readonly class WatchlistItemFactory
17+
{
18+
public function __construct(
19+
private ProductRepository $productRepository,
20+
) {}
21+
22+
public function createFromIdentifier(
23+
string $identifier,
24+
): ?WatchlistItem {
25+
list($uid, $detailPid) = explode('-', $identifier);
26+
27+
$product = $this->productRepository->findProductByUid((int)$uid);
28+
29+
if ($product === false) {
30+
return null;
31+
}
32+
33+
return new WatchlistItem(
34+
(int)$uid,
35+
(int)$detailPid,
36+
(string)$product['title'],
37+
$this->getFirstImageReference($product),
38+
);
39+
}
40+
41+
private function getFirstImageReference(array $product): ?int
42+
{
43+
if (is_string($product['images'] ?? null) === false || $product['images'] === '') {
44+
return null;
45+
}
46+
47+
$images = explode(',', $product['images']);
48+
49+
$image = array_pop($images);
50+
51+
if (is_numeric($image) === false) {
52+
return null;
53+
}
54+
55+
return (int)$image;
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartProducts\Domain\Watchlist;
6+
7+
/*
8+
* This file is part of the package extcode/cart-products.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use TYPO3\CMS\Core\Utility\GeneralUtility;
15+
use TYPO3\CMS\Extbase\Property\PropertyMapper;
16+
use WerkraumMedia\Watchlist\Domain\Model\Item;
17+
use WerkraumMedia\Watchlist\Domain\Model\Watchlist;
18+
19+
final class WatchlistFactory
20+
{
21+
public static function fromStringOfWatchlistItems(string $items, PropertyMapper $propertyMapper): Watchlist
22+
{
23+
$watchlist = new Watchlist();
24+
25+
array_map(function (string $item) use ($watchlist, $propertyMapper) {
26+
$mappedItem = $propertyMapper->convert($item, Item::class);
27+
if (!$mappedItem instanceof Item) {
28+
return;
29+
}
30+
31+
$watchlist->addItem($mappedItem);
32+
}, GeneralUtility::trimExplode(',', $items, true));
33+
34+
return $watchlist;
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartProducts\Handler;
6+
7+
/*
8+
* This file is part of the package extcode/cart-products.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\CartProducts\Domain\Model\WatchlistItemFactory;
15+
use WerkraumMedia\Watchlist\Domain\ItemHandlerInterface;
16+
use WerkraumMedia\Watchlist\Domain\Model\Item;
17+
18+
final readonly class WatchlistItemHandler implements ItemHandlerInterface
19+
{
20+
public function __construct(
21+
private WatchlistItemFactory $watchlistItemFactory,
22+
) {}
23+
24+
public function return(string $identifier): ?Item
25+
{
26+
return $this->watchlistItemFactory->createFromIdentifier($identifier);
27+
}
28+
29+
public function handlesType(): string
30+
{
31+
return 'CartProducts';
32+
}
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Extcode\CartProducts\ViewHelpers\Product;
4+
5+
/*
6+
* This file is part of the package extcode/cart-products.
7+
*
8+
* For the full copyright and license information, please read the
9+
* LICENSE file that was distributed with this source code.
10+
*/
11+
12+
use Extcode\CartProducts\Domain\DoctrineRepository\PageRepository;
13+
use Extcode\CartProducts\Domain\Model\Product\Product;
14+
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
15+
16+
class PageUidViewHelper extends AbstractViewHelper
17+
{
18+
public function __construct(
19+
private readonly PageRepository $pageRepository,
20+
) {}
21+
22+
public function initializeArguments()
23+
{
24+
parent::initializeArguments();
25+
26+
$this->registerArgument(
27+
'product',
28+
Product::class,
29+
'product',
30+
true
31+
);
32+
33+
$this->registerArgument(
34+
'settings',
35+
'array',
36+
'settings array',
37+
true
38+
);
39+
}
40+
41+
public function render(): string
42+
{
43+
$product = $this->arguments['product'];
44+
45+
$page = $this->pageRepository->getProductPageByProduct($product);
46+
47+
if ($page) {
48+
return $page['uid'];
49+
}
50+
if ($product->getCategory() && $product->getCategory()->getCartProductShowPid()) {
51+
return $product->getCategory()->getCartProductShowPid();
52+
}
53+
if ($this->arguments['settings']['showPageUids']) {
54+
return $this->arguments['settings']['showPageUids'];
55+
}
56+
57+
return $GLOBALS['TSFE']->id;
58+
}
59+
}

Configuration/Services.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartProducts\Configuration;
6+
7+
use Extcode\CartProducts\Handler\WatchlistItemHandler;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Definition;
10+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
11+
use WerkraumMedia\Watchlist\Domain\ItemHandlerRegistry;
12+
13+
return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder) {
14+
if ($containerBuilder->hasDefinition(ItemHandlerRegistry::class)) {
15+
$watchlistItemHandlerDefinition = new Definition(
16+
WatchlistItemHandler::class
17+
);
18+
$watchlistItemHandlerDefinition->addTag('watchlist.itemHandler')
19+
->setAutoconfigured(true)
20+
->setAutowired(true);
21+
$containerBuilder->addDefinitions(
22+
[
23+
WatchlistItemHandler::class => $watchlistItemHandlerDefinition,
24+
]
25+
);
26+
}
27+
};

Configuration/Services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ services:
66

77
Extcode\CartProducts\:
88
resource: '../Classes/*'
9+
exclude:
10+
- '../Classes/Handler/WatchlistItemHandler.php'
911

1012
Extcode\CartProducts\EventListener\Create\CheckRequest:
1113
tags:

0 commit comments

Comments
 (0)