-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathProductController.php
More file actions
346 lines (284 loc) · 11.8 KB
/
ProductController.php
File metadata and controls
346 lines (284 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
namespace Extcode\CartProducts\Controller;
/*
* 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\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Service\SessionHandler;
use Extcode\Cart\Utility\CartUtility;
use Extcode\CartProducts\Domain\Model\Dto\Product\ProductDemand;
use Extcode\CartProducts\Domain\Model\Product\Product;
use Extcode\CartProducts\Domain\Repository\CategoryRepository;
use Extcode\CartProducts\Domain\Repository\Product\ProductRepository;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Pagination\SlidingWindowPagination;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use TYPO3\CMS\Extbase\Service\ExtensionService;
class ProductController extends ActionController
{
protected Cart $cart;
protected array $searchArguments = [];
protected array $cartConfiguration = [];
public function __construct(
protected readonly ExtensionService $extensionService,
protected readonly SessionHandler $sessionHandler,
protected readonly CartUtility $cartUtility,
protected readonly ProductRepository $productRepository,
protected readonly CategoryRepository $categoryRepository
) {}
protected function initializeAction()
{
$this->cartConfiguration = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
'Cart'
);
if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
static $cacheTagsSet = false;
$typoScriptFrontendController = $GLOBALS['TSFE'];
if (!$cacheTagsSet) {
$typoScriptFrontendController->addCacheTags(['tx_cartproducts']);
$cacheTagsSet = true;
}
}
$this->settings['addToCartByAjax'] = isset($this->settings['addToCartByAjax']) ? (int)$this->settings['addToCartByAjax'] : 0;
}
/**
* Create the demand object which define which records will get shown
*/
protected function createDemandObjectFromSettings(array $settings): ProductDemand
{
/** @var ProductDemand $demand */
$demand = GeneralUtility::makeInstance(
ProductDemand::class
);
if (!empty($this->searchArguments['sku'])) {
$demand->setSku($this->searchArguments['sku']);
}
if (!empty($this->searchArguments['title'])) {
$demand->setTitle($this->searchArguments['title']);
}
if ($settings['orderBy']) {
if (
!isset($settings['orderDirection'])
&& $settings['orderDirection'] !== 'DESC'
) {
$settings['orderDirection'] = 'ASC';
}
$demand->setOrder($settings['orderBy'] . ' ' . $settings['orderDirection']);
}
$this->addCategoriesToDemandObjectFromSettings($demand);
return $demand;
}
protected function addCategoriesToDemandObjectFromSettings(ProductDemand $demand): void
{
if ($this->settings['categoriesList']) {
$selectedCategories = GeneralUtility::intExplode(
',',
$this->settings['categoriesList'],
true
);
$categories = [];
if ($this->settings['listSubcategories']) {
foreach ($selectedCategories as $selectedCategory) {
$category = $this->categoryRepository->findByUid($selectedCategory);
$categories = array_merge(
$categories,
$this->categoryRepository->findSubcategoriesRecursiveAsArray($category)
);
}
} else {
$categories = $selectedCategories;
}
$demand->setCategories($categories);
}
}
protected function isActionAllowed(string $action): bool
{
$frameworkConfiguration = $this->configurationManager->getConfiguration($this->configurationManager::CONFIGURATION_TYPE_FRAMEWORK);
$allowedActions = $frameworkConfiguration['controllerConfiguration'][\Extcode\CartProducts\Controller\ProductController::class]['actions'] ?? [];
return in_array($action, $allowedActions, true);
}
/**
* When list action is called along with a product argument, we forward to show action.
*/
protected function forwardToShowActionWhenRequested(): ?ForwardResponse
{
if (!$this->isActionAllowed('show') || !$this->request->hasArgument('product')
) {
return null;
}
$forwardResponse = new ForwardResponse('show');
return $forwardResponse->withArguments(['product' => $this->request->getArgument('product')]);
}
public function listAction(int $currentPage = 1): ResponseInterface
{
$possibleRedirect = $this->forwardToShowActionWhenRequested();
if ($possibleRedirect) {
return $possibleRedirect;
}
$demand = $this->createDemandObjectFromSettings($this->settings);
$demand->setActionAndClass(__METHOD__, self::class);
$itemsPerPage = (int)($this->settings['itemsPerPage'] ?? 20);
$maximumNumberOfLinks = (int)($this->settings['maximumNumberOfLinks'] ?? 0);
$products = $this->productRepository->findDemanded($demand);
$paginator = new QueryResultPaginator(
$products,
$currentPage,
$itemsPerPage
);
$pagination = new SlidingWindowPagination($paginator, $maximumNumberOfLinks);
$this->view->assignMultiple(
[
'products' => $products,
'paginator' => $paginator,
'pagination' => $pagination,
'pages' => range(1, $pagination->getLastPageNumber()),
]
);
$this->view->assign('searchArguments', $this->searchArguments);
$this->view->assign('cartSettings', $this->cartConfiguration['settings']);
$this->assignCurrencyTranslationData();
$this->addCacheTags($products);
return $this->htmlResponse();
}
public function showAction(?Product $product = null): ResponseInterface
{
if ((int)$GLOBALS['TSFE']->page['doktype'] === 183) {
$productUid = (int)$GLOBALS['TSFE']->page['cart_products_product'];
$product = $this->productRepository->findByUid($productUid);
}
$this->view->assign('product', $product);
$this->view->assign('cartSettings', $this->cartConfiguration['settings']);
$this->assignCurrencyTranslationData();
$this->addCacheTags([$product]);
return $this->htmlResponse();
}
public function showFormAction(?Product $product = null): ResponseInterface
{
if (!$product) {
$product = $this->getProduct();
}
$this->view->assign('product', $product);
$this->view->assign('cartSettings', $this->cartConfiguration['settings']);
$this->assignCurrencyTranslationData();
return $this->htmlResponse();
}
public function teaserAction(): ResponseInterface
{
$products = $this->productRepository->findByUids($this->settings['productUids']);
$this->view->assign('products', $products);
$this->view->assign('cartSettings', $this->cartConfiguration['settings']);
$this->assignCurrencyTranslationData();
$this->addCacheTags($products);
return $this->htmlResponse();
}
public function flexformAction(): ResponseInterface
{
$contentObj = $this->request->getAttribute('currentContentObject');
$contentId = $contentObj->data['uid'];
$this->view->assign('contentId', $contentId);
return $this->htmlResponse();
}
protected function getProduct(): ?Product
{
$productUid = $this->getProductUid();
if ($productUid > 0) {
$product = $this->productRepository->findByUid($productUid);
if ($product instanceof Product) {
return $product;
}
}
return null;
}
/**
* @return int|mixed
* @throws InvalidConfigurationTypeException
*/
public function getProductUid(): mixed
{
if ((int)$GLOBALS['TSFE']->page['doktype'] === 183) {
return (int)$GLOBALS['TSFE']->page['cart_products_product'];
}
if ($this->request->getPluginName() !== 'ProductPartial') {
return 0;
}
$configurationManager = GeneralUtility::makeInstance(
ConfigurationManager::class
);
$configuration = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_FRAMEWORK);
$typoscriptService = GeneralUtility::makeInstance(
TypoScriptService::class
);
$configuration = $typoscriptService->convertPlainArrayToTypoScriptArray($configuration);
$productUid = (int)$this->request->getAttribute('currentContentObject')->cObjGetSingle($configuration['product'], $configuration['product.']);
$pluginName = array_key_exists('tx_cartproducts_showproduct', $this->request->getQueryParams())
? 'ShowProduct'
: 'ListProducts';
if ($productUid === 0) {
$configurationManager->setConfiguration([
'vendorName' => 'Extcode',
'extensionName' => 'CartProducts',
'pluginName' => $pluginName,
]);
$requestBuilder = GeneralUtility::makeInstance(
RequestBuilder::class,
$configurationManager,
$this->extensionService
);
/**
* @var Request $cartProductRequest
*/
$cartProductRequest = $requestBuilder->build($this->request);
if ($cartProductRequest->hasArgument('product')) {
$productUid = (int)$cartProductRequest->getArgument('product');
}
}
return $productUid;
}
/**
* assigns currency translation array to view
*/
protected function assignCurrencyTranslationData()
{
$this->restoreSession();
$currencyTranslationData = [
'currencyCode' => $this->cart->getCurrencyCode(),
'currencySign' => $this->cart->getCurrencySign(),
'currencyTranslation' => $this->cart->getCurrencyTranslation(),
];
$this->view->assign('currencyTranslationData', $currencyTranslationData);
}
protected function addCacheTags(iterable $products): void
{
$cacheTags = [];
foreach ($products as $product) {
// cache tag for each product record
$cacheTags[] = 'tx_cartproducts_product_' . $product->getUid();
}
if (count($cacheTags) > 0) {
$GLOBALS['TSFE']->addCacheTags($cacheTags);
}
}
protected function restoreSession(): void
{
$cart = $this->sessionHandler->restoreCart($this->cartConfiguration['settings']['cart']['pid']);
if ($cart instanceof Cart) {
$this->cart = $cart;
return;
}
$this->cart = $this->cartUtility->getNewCart($this->cartConfiguration);
$this->sessionHandler->writeCart($this->cartConfiguration['settings']['cart']['pid'], $this->cart);
}
}