229 lines
10 KiB
Plaintext
Executable File
229 lines
10 KiB
Plaintext
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Evomedien\Vitec\UserFunc;
|
|
|
|
use Doctrine\DBAL\ParameterType;
|
|
use Evomedien\Vitec\Domain\Repository\ProductRepository;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use TYPO3\CMS\Core\Database\Connection;
|
|
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
|
|
use TYPO3\CMS\Core\Resource\FileReference;
|
|
use TYPO3\CMS\Core\Resource\ResourceFactory;
|
|
use TYPO3\CMS\Core\Service\FlexFormService;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
use TYPO3\CMS\Extbase\Service\ImageService;
|
|
|
|
/**
|
|
* UserFunc to render product list as JSON for headless
|
|
*/
|
|
class ProductListJsonRenderer
|
|
{
|
|
public function render(string $content, array $conf): string
|
|
{
|
|
// IMPORTANT: Headless creates new cObj contexts when rendering JSON fields,
|
|
// losing the current content element context. Also, Extbase repositories don't work
|
|
// in UserFunc context because the full Extbase framework isn't bootstrapped.
|
|
//
|
|
// Solution: Query tt_content directly to find the plugin configuration,
|
|
// then use direct database queries for products instead of Extbase repositories.
|
|
|
|
// Get current page UID
|
|
$pageId = (int)($GLOBALS['TSFE']->id ?? 0);
|
|
|
|
// Query tt_content for vitec_productlist on this page
|
|
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
|
|
->getQueryBuilderForTable('tt_content');
|
|
|
|
$contentElements = $queryBuilder
|
|
->select('*')
|
|
->from('tt_content')
|
|
->where(
|
|
$queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pageId, ParameterType::INTEGER)),
|
|
$queryBuilder->expr()->eq('list_type', $queryBuilder->createNamedParameter('vitec_productlist', ParameterType::STRING)),
|
|
$queryBuilder->expr()->eq('deleted', 0),
|
|
$queryBuilder->expr()->eq('hidden', 0)
|
|
)
|
|
->executeQuery()
|
|
->fetchAllAssociative();
|
|
|
|
if (empty($contentElements)) {
|
|
return json_encode(['debug' => 'No vitec_productlist on page ' . $pageId]);
|
|
}
|
|
|
|
// Take the first one (there should typically be only one)
|
|
$contentElement = $contentElements[0];
|
|
|
|
// Parse FlexForm
|
|
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
|
|
$flexFormData = $flexFormService->convertFlexFormContentToArray($contentElement['pi_flexform'] ?? '');
|
|
$settings = $flexFormData['settings'] ?? [];
|
|
|
|
// Extract category UIDs and debug setting
|
|
$categoryUids = array_filter(
|
|
array_map('intval', explode(',', (string)($settings['categories'] ?? '')))
|
|
);
|
|
$debugMode = (bool)($settings['debug'] ?? false);
|
|
$allProducts = (bool)($settings['allproducts'] ?? false);
|
|
|
|
// Extbase repositories don't work in UserFunc context
|
|
// Use direct database query instead
|
|
$productQueryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
|
|
->getQueryBuilderForTable('tx_vitec_domain_model_product');
|
|
|
|
$productQuery = $productQueryBuilder
|
|
->select('p.*')
|
|
->from('tx_vitec_domain_model_product', 'p')
|
|
->where(
|
|
$productQueryBuilder->expr()->eq('p.deleted', 0),
|
|
$productQueryBuilder->expr()->eq('p.hidden', 0),
|
|
$productQueryBuilder->expr()->eq('p.legacy', 0),
|
|
$productQueryBuilder->expr()->eq('p.supportproduct', 0),
|
|
$productQueryBuilder->expr()->eq('p.hideonwebsite', 0),
|
|
$productQueryBuilder->expr()->eq('p.hideonproducts', 0),
|
|
$productQueryBuilder->expr()->eq('p.subproduct', 0)
|
|
);
|
|
|
|
// Add category filter if specified and allProducts is not enabled
|
|
if (!empty($categoryUids) && !$allProducts) {
|
|
// Join with sys_category_record_mm to filter by categories
|
|
$productQuery
|
|
->join(
|
|
'p',
|
|
'sys_category_record_mm',
|
|
'mm',
|
|
'mm.uid_foreign = p.uid AND mm.tablenames = ' . $productQueryBuilder->createNamedParameter('tx_vitec_domain_model_product', ParameterType::STRING) . ' AND mm.fieldname = ' . $productQueryBuilder->createNamedParameter('categories', ParameterType::STRING)
|
|
)
|
|
->andWhere(
|
|
$productQueryBuilder->expr()->in('mm.uid_local', $productQueryBuilder->createNamedParameter($categoryUids, Connection::PARAM_INT_ARRAY))
|
|
)
|
|
->groupBy('p.uid');
|
|
}
|
|
|
|
$products = $productQuery->executeQuery()->fetchAllAssociative();
|
|
|
|
// Serialize products (they're already associative arrays from the query)
|
|
$productsData = [];
|
|
foreach ($products as $product) {
|
|
// Get product images (FAL references)
|
|
$images = $this->getProductImages((int)$product['uid']);
|
|
|
|
$productsData[] = [
|
|
'uid' => (int)$product['uid'],
|
|
'title' => $product['title'] ?? '',
|
|
'subtitle' => $product['subtitle'] ?? '',
|
|
'slug' => $product['slug'] ?? '',
|
|
'teaser' => $product['teaser'] ?? '',
|
|
'description' => $product['description'] ?? '',
|
|
'link' => '/product/' . ($product['slug'] ?? ''),
|
|
'images' => $images,
|
|
// Add more fields as needed
|
|
];
|
|
}
|
|
|
|
// If debug mode is enabled, return object with products and debug info
|
|
if ($debugMode) {
|
|
return json_encode([
|
|
'products' => $productsData,
|
|
'debug' => [
|
|
'pageId' => $pageId,
|
|
'categoryUids' => $categoryUids,
|
|
'productCount' => count($productsData),
|
|
'settings' => $settings
|
|
]
|
|
]);
|
|
}
|
|
|
|
// Otherwise return products array directly (backward compatible)
|
|
return json_encode($productsData);
|
|
}
|
|
|
|
/**
|
|
* Get product images from FAL (sys_file_reference)
|
|
* Processes images through ImageService and generates srcset for responsive images
|
|
*/
|
|
protected function getProductImages(int $productUid): array
|
|
{
|
|
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
|
|
->getQueryBuilderForTable('sys_file_reference');
|
|
|
|
$fileReferences = $queryBuilder
|
|
->select('sfr.uid', 'sfr.uid_local', 'sfr.title', 'sfr.description', 'sfr.alternative', 'sfr.crop')
|
|
->from('sys_file_reference', 'sfr')
|
|
->where(
|
|
$queryBuilder->expr()->eq('sfr.tablenames', $queryBuilder->createNamedParameter('tx_vitec_domain_model_product', ParameterType::STRING)),
|
|
$queryBuilder->expr()->eq('sfr.fieldname', $queryBuilder->createNamedParameter('productimage', ParameterType::STRING)),
|
|
$queryBuilder->expr()->eq('sfr.uid_foreign', $queryBuilder->createNamedParameter($productUid, ParameterType::INTEGER)),
|
|
$queryBuilder->expr()->eq('sfr.deleted', 0),
|
|
$queryBuilder->expr()->eq('sfr.hidden', 0)
|
|
)
|
|
->orderBy('sfr.sorting_foreign')
|
|
->executeQuery()
|
|
->fetchAllAssociative();
|
|
|
|
$imageService = GeneralUtility::makeInstance(ImageService::class);
|
|
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
|
|
|
|
$images = [];
|
|
foreach ($fileReferences as $fileRefData) {
|
|
try {
|
|
// Get FAL FileReference object
|
|
$fileReference = $resourceFactory->getFileReferenceObject((int)$fileRefData['uid']);
|
|
|
|
// Define image sizes for srcset
|
|
$sizes = [
|
|
'small' => ['width' => 400, 'height' => null],
|
|
'medium' => ['width' => 800, 'height' => null],
|
|
'large' => ['width' => 1200, 'height' => null],
|
|
'xlarge' => ['width' => 1600, 'height' => null],
|
|
];
|
|
|
|
$srcset = [];
|
|
foreach ($sizes as $sizeName => $dimensions) {
|
|
$processedImage = $imageService->applyProcessingInstructions(
|
|
$fileReference,
|
|
[
|
|
'width' => $dimensions['width'],
|
|
'height' => $dimensions['height'],
|
|
'crop' => $fileRefData['crop'] ?? null
|
|
]
|
|
);
|
|
|
|
$imageUri = $imageService->getImageUri($processedImage);
|
|
$srcset[] = [
|
|
'url' => $imageUri,
|
|
'width' => $dimensions['width'],
|
|
'descriptor' => $dimensions['width'] . 'w'
|
|
];
|
|
}
|
|
|
|
// Get original/default image
|
|
$defaultProcessed = $imageService->applyProcessingInstructions(
|
|
$fileReference,
|
|
['width' => 800, 'crop' => $fileRefData['crop'] ?? null]
|
|
);
|
|
|
|
$images[] = [
|
|
'uid' => (int)$fileRefData['uid'],
|
|
'url' => $imageService->getImageUri($defaultProcessed),
|
|
'title' => $fileRefData['title'] ?? '',
|
|
'alternative' => $fileRefData['alternative'] ?? '',
|
|
'description' => $fileRefData['description'] ?? '',
|
|
'srcset' => $srcset,
|
|
'properties' => [
|
|
'width' => $fileReference->getProperty('width'),
|
|
'height' => $fileReference->getProperty('height'),
|
|
'mimeType' => $fileReference->getProperty('mime_type')
|
|
]
|
|
];
|
|
} catch (\Exception $e) {
|
|
// Skip images that can't be processed
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return $images;
|
|
}
|
|
}
|