Initial Commit
This commit is contained in:
199
packages/vitec/Classes/Controller/ProductController.php
Normal file
199
packages/vitec/Classes/Controller/ProductController.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Evomedien\Vitec\Controller;
|
||||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
|
||||
use Evomedien\Vitec\Domain\Repository\ProductRepository;
|
||||
use Evomedien\Vitec\PageTitle\ProductPageTitleProvider;
|
||||
use TYPO3\CMS\Core\Routing\PageLinkBuilder;
|
||||
use TYPO3\CMS\Core\LinkHandling\LinkService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
|
||||
/**
|
||||
* This file is part of the "VITEC" Extension for TYPO3 CMS.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* (c) 2025
|
||||
*/
|
||||
|
||||
/**
|
||||
* ProductController
|
||||
*/
|
||||
class ProductController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* productRepository
|
||||
*
|
||||
* @var \Evomedien\Vitec\Domain\Repository\ProductRepository
|
||||
*/
|
||||
protected $productRepository;
|
||||
|
||||
/**
|
||||
* @var \Evomedien\Vitec\PageTitle\ProductPageTitleProvider
|
||||
*/
|
||||
protected $titleProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Evomedien\Vitec\Domain\Repository\ProductRepository $productRepository
|
||||
*/
|
||||
public function injectProductRepository(\Evomedien\Vitec\Domain\Repository\ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject the TitleProvider
|
||||
*
|
||||
* @param \Evomedien\Vitec\PageTitle\ProductPageTitleProvider $titleProvider
|
||||
*/
|
||||
public function injectTitleProvider(ProductPageTitleProvider $titleProvider): void
|
||||
{
|
||||
$this->titleProvider = $titleProvider;
|
||||
}
|
||||
/**
|
||||
* @var LinkService
|
||||
*/
|
||||
protected $linkService;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ProductRepository $productRepository
|
||||
* @param ProductPageTitleProvider $titleProvider
|
||||
* @param LinkService $linkService
|
||||
*/
|
||||
public function __construct(
|
||||
ProductRepository $productRepository,
|
||||
ProductPageTitleProvider $titleProvider,
|
||||
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->titleProvider = $titleProvider;
|
||||
|
||||
}
|
||||
/**
|
||||
* action index
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function indexAction(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
return $this->htmlResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* action list
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function listAction(): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
// Extract and sanitize category UIDs from FlexForm
|
||||
$categoryUids = array_filter(
|
||||
array_map('intval', explode(',', (string)($this->settings['categories'] ?? '')))
|
||||
);
|
||||
|
||||
// Get filtered products based on categories and visibility flags
|
||||
$products = $this->productRepository->findFilteredProducts($categoryUids);
|
||||
|
||||
$this->view->assign('products', $products);
|
||||
|
||||
// Check if we're in headless mode (JSON request)
|
||||
$request = $this->request;
|
||||
$pageType = $request->getAttribute('routing')?->getPageType() ?? 0;
|
||||
|
||||
if ($pageType === 834) { // Headless JSON page type
|
||||
return $this->jsonResponse(json_encode([
|
||||
'products' => $this->serializeProducts($products)
|
||||
]));
|
||||
}
|
||||
|
||||
return $this->htmlResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize products to array for JSON output
|
||||
*/
|
||||
protected function serializeProducts($products): array
|
||||
{
|
||||
$productsData = [];
|
||||
|
||||
foreach ($products as $product) {
|
||||
$categories = [];
|
||||
if ($product->getCategories()) {
|
||||
foreach ($product->getCategories() as $category) {
|
||||
$categories[] = [
|
||||
'uid' => $category->getUid(),
|
||||
'title' => $category->getTitle(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$image = null;
|
||||
if ($product->getProductimage() && $product->getProductimage()->count() > 0) {
|
||||
$imageObject = $product->getProductimage()->current();
|
||||
$image = [
|
||||
'uid' => $imageObject->getUid(),
|
||||
'url' => $imageObject->getOriginalResource()->getPublicUrl(),
|
||||
'title' => $imageObject->getTitle(),
|
||||
'alternative' => $imageObject->getAlternative(),
|
||||
'description' => $imageObject->getDescription(),
|
||||
];
|
||||
}
|
||||
|
||||
$productsData[] = [
|
||||
'uid' => $product->getUid(),
|
||||
'title' => $product->getTitle(),
|
||||
'subtitle' => $product->getSubtitle(),
|
||||
'teaser' => $product->getTeaser(),
|
||||
'description' => $product->getDescription(),
|
||||
'slug' => $product->getSlug(),
|
||||
'categories' => $categories,
|
||||
'image' => $image,
|
||||
];
|
||||
}
|
||||
|
||||
return $productsData;
|
||||
}
|
||||
|
||||
/**
|
||||
* action show
|
||||
*
|
||||
* @param \Evomedien\Vitec\Domain\Model\Product $product
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function showAction(\Evomedien\Vitec\Domain\Model\Product $product): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
|
||||
$metaTagManager = GeneralUtility::makeInstance(MetaTagManagerRegistry::class)->getManagerForProperty('og:title');
|
||||
$metaTagManager->addProperty('og:title', ($product->getSeotitle()));
|
||||
$metaTagManager->addProperty('og:description', ($product->getDescription()));
|
||||
//$metaTagManager->addProperty('og:url', $this->request->getRequestUri());
|
||||
$metaTagManager->addProperty('og:type', 'product');
|
||||
|
||||
if ($product->getOgimage()) {
|
||||
$ogImageUrl = $product->getOgimage()->getOriginalResource()->getPublicUrl();
|
||||
$metaTagManager->addProperty('og:image', $ogImageUrl);
|
||||
}
|
||||
|
||||
// Access the layout setting from FlexForm
|
||||
$layout = $this->settings['layout'] ?? 0;
|
||||
|
||||
|
||||
// Pass the layout to the view
|
||||
$this->view->assign('layout', $layout);
|
||||
|
||||
$this->view->assign('product', $product);
|
||||
$this->titleProvider->setSeoTitle($product->getSeotitle());
|
||||
return $this->htmlResponse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user