Files
VITEC-website/packages/vitec/Classes/UserFunc/DownloadcardJsonRenderer.php
khaccount 4541c58fe7 VITEC headless v14: 5-bug fix unifies plugin renderers + cleanup
Resolves a cascade of TYPO3 v14 breaking changes that left every VITEC
plugin's JSON output silently empty:

1. `list_type` column dropped — all renderer page-discovery queries now
   filter by `CType = 'vitec_X'`.
2. `#[AsAllowedCallable]` attribute added to every public render() —
   without it v14 throws AllowedCallableException, which headless
   silently filters via its "Oops, an error occurred!" guard.
3. `$GLOBALS['TSFE']->id` is null inside JSON cObj context — page id
   now read from the `frontend.page.information` request attribute
   (TSFE fallback kept for legacy entry points).
4. page.tsconfig wizard items migrated to `CType = vitec_X` directly
   (legacy `CType=list, list_type=...` no longer exists in v14).
5. ContainerChildrenProcessor + ContentElementResolver dispatch the
   PLUGIN_RENDERERS map by CType (primary) with list_type fallback.

Beyond the bug fix this commit also contains:
- Datasheets renderer rewritten to "products with newest datasheet"
  semantics (filtered via Download.hideondatasheets).
- New vitec/card content block — multi-purpose card with backend
  preview and layout/background/aspect/alignment/border/shadow options.
- New vitec_container CType (b13 single-column container, FlexForm
  cssClass propagated into the JSON envelope).
- ContentElementResolver service for contentelement/contentelementcta
  link resolution; ContainerChildrenProcessor for nested plugin
  resolution inside b13 containers.
- Vitecset setup.typoscript: ContentElement import moved to top so
  lib.contentElement is defined before VITEC's tt_content blocks.
- Cleanup: 98 .bak.<timestamp> debugging backups removed; *.bak.* and
  /public/_assets_install/ added to .gitignore.
2026-06-15 14:42:33 +02:00

299 lines
12 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace Evomedien\Vitec\UserFunc;
use TYPO3\CMS\Core\Attribute\AsAllowedCallable;
use Doctrine\DBAL\ParameterType;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* UserFunc to render the Downloadcard plugin as JSON for headless output.
*
* Behaviour:
* - settings.download set -> single download object (key: "download")
* - else settings.product set -> all downloads of that product (key: "downloads",
* plus a small "product" context)
* - neither -> empty (key removed by headless ifEmptyUnsetKey)
*
* `settings.layout` / magstyle / magheader / magtext / maglink / adddetaillink
* are always passed through under the "settings" block for the frontend.
*
* `render()` performs page discovery; `renderForRecord()` processes a specific
* tt_content row and is reused by ContainerChildrenProcessor /
* ContentElementResolver. Exception-safe.
*/
class DownloadcardJsonRenderer
{
#[AsAllowedCallable]
public function render(string $content, array $conf): string
{
$pageId = 0;
$req = $GLOBALS["TYPO3_REQUEST"] ?? null;
if ($req !== null) {
$pi = $req->getAttribute("frontend.page.information");
if ($pi !== null) { $pageId = (int)$pi->getId(); }
}
if ($pageId <= 0) { $pageId = (int)($GLOBALS["TSFE"]->id ?? 0); }
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tt_content');
// After v14 migration the CType is the plugin signature; for legacy
// (unmigrated) records the CType is still "list" with list_type set.
$rows = $qb
->select('*')
->from('tt_content')
->where(
$qb->expr()->eq('pid', $qb->createNamedParameter($pageId, ParameterType::INTEGER)),
$qb->expr()->or(
$qb->expr()->eq('CType', $qb->createNamedParameter('vitec_downloadcard', ParameterType::STRING)),
$qb->expr()->and(
$qb->expr()->eq('CType', $qb->createNamedParameter('list', ParameterType::STRING)),
$qb->expr()->eq('CType', $qb->createNamedParameter('vitec_downloadcard', ParameterType::STRING))
)
),
$qb->expr()->eq('deleted', 0),
$qb->expr()->eq('hidden', 0)
)
->executeQuery()
->fetchAllAssociative();
if (empty($rows)) {
return '';
}
return $this->renderForRecord($rows[0]);
}
/**
* @param array<string,mixed> $contentElement
*/
public function renderForRecord(array $contentElement): string
{
try {
$pageId = (int)($GLOBALS['TSFE']->id ?? 0);
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
$flexFormData = $flexFormService->convertFlexFormContentToArray($contentElement['pi_flexform'] ?? '');
$settings = $flexFormData['settings'] ?? [];
$downloadUid = (int)($settings['download'] ?? 0);
$productUid = (int)($settings['product'] ?? 0);
$debugMode = (bool)($settings['debug'] ?? false);
// Settings always carried through to the frontend
$settingsOut = [
'layout' => (string)($settings['layout'] ?? ''),
'magstyle' => (bool)($settings['magstyle'] ?? false),
'magheader' => (string)($settings['magheader'] ?? ''),
'magtext' => (string)($settings['magtext'] ?? ''),
'maglink' => (string)($settings['maglink'] ?? ''),
'adddetaillink' => (bool)($settings['adddetaillink'] ?? false),
];
// --- Mode 1: single download ---
if ($downloadUid > 0) {
$download = $this->fetchDownload($downloadUid);
if (!$download) {
return $debugMode
? json_encode(['error' => 'Download not found', 'debug' => ['downloadUid' => $downloadUid]])
: '';
}
$response = [
'mode' => 'single',
'download' => $this->serializeDownload($download),
'settings' => $settingsOut,
];
if ($debugMode) {
$response['debug'] = [
'pageId' => $pageId, 'downloadUid' => $downloadUid, 'settings' => $settings,
];
}
return json_encode($response);
}
// --- Mode 2: all downloads of a product ---
if ($productUid > 0) {
$downloads = $this->fetchDownloadsForProduct($productUid);
$product = $this->fetchProductContext($productUid);
$response = [
'mode' => 'product-downloads',
'product' => $product,
'downloads' => array_map(fn($r) => $this->serializeDownload($r), $downloads),
'settings' => $settingsOut,
];
if ($debugMode) {
$response['debug'] = [
'pageId' => $pageId,
'productUid' => $productUid,
'downloadCount' => count($downloads),
'settings' => $settings,
];
}
return json_encode($response);
}
// Neither selected: nothing to render.
return $debugMode
? json_encode(['error' => 'No download or product selected', 'debug' => ['settings' => $settings]])
: '';
} catch (\Throwable $e) {
return '';
}
}
/**
* @return array<string,mixed>|false
*/
private function fetchDownload(int $uid)
{
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_vitec_domain_model_download');
return $qb
->select('*')
->from('tx_vitec_domain_model_download')
->where(
$qb->expr()->eq('uid', $qb->createNamedParameter($uid, ParameterType::INTEGER)),
$qb->expr()->eq('deleted', 0),
$qb->expr()->eq('hidden', 0),
$qb->expr()->eq('hideonwebsite', 0)
)
->executeQuery()
->fetchAssociative();
}
/**
* @return array<int,array<string,mixed>>
*/
private function fetchDownloadsForProduct(int $productUid): array
{
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_vitec_domain_model_download');
return $qb
->select('d.*')
->from('tx_vitec_domain_model_download', 'd')
->join('d', 'tx_vitec_product_download_mm', 'mm', 'mm.uid_foreign = d.uid')
->where(
$qb->expr()->eq('mm.uid_local', $qb->createNamedParameter($productUid, ParameterType::INTEGER)),
$qb->expr()->eq('d.deleted', 0),
$qb->expr()->eq('d.hidden', 0),
$qb->expr()->eq('d.hideonwebsite', 0)
)
->orderBy('mm.sorting', 'ASC')
->executeQuery()
->fetchAllAssociative();
}
/**
* @return array<string,mixed>|null
*/
private function fetchProductContext(int $productUid): ?array
{
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_vitec_domain_model_product');
$row = $qb
->select('uid', 'title', 'slug', 'subtitle', 'teaser')
->from('tx_vitec_domain_model_product')
->where(
$qb->expr()->eq('uid', $qb->createNamedParameter($productUid, ParameterType::INTEGER)),
$qb->expr()->eq('deleted', 0),
$qb->expr()->eq('hidden', 0)
)
->executeQuery()
->fetchAssociative();
if (!$row) {
return null;
}
return [
'uid' => (int)$row['uid'],
'title' => (string)($row['title'] ?? ''),
'slug' => (string)($row['slug'] ?? ''),
'subtitle' => (string)($row['subtitle'] ?? ''),
'teaser' => (string)($row['teaser'] ?? ''),
'link' => '/product/' . (string)($row['slug'] ?? ''),
];
}
/**
* @param array<string,mixed> $download
* @return array<string,mixed>
*/
protected function serializeDownload(array $download): array
{
$uid = (int)$download['uid'];
return [
'uid' => $uid,
'title' => (string)($download['title'] ?? ''),
'slug' => (string)($download['slug'] ?? ''),
'teaser' => (string)($download['teaser'] ?? ''),
'description' => (string)($download['description'] ?? ''),
'keywords' => (string)($download['keywords'] ?? ''),
'icon' => (string)($download['icon'] ?? ''),
'filepath' => (string)($download['filepath'] ?? ''),
'fileprefix' => (string)($download['fileprefix'] ?? ''),
'private_download' => (bool)($download['private_download'] ?? false),
'hideonapp' => (bool)($download['hideonapp'] ?? false),
'hideonwebsite' => (bool)($download['hideonwebsite'] ?? false),
'hideondatasheets' => (bool)($download['hideondatasheets'] ?? false),
'hideonproducts' => (bool)($download['hideonproducts'] ?? false),
'file' => $this->getDownloadFile($uid),
];
}
/**
* Resolve the FAL file reference (fieldname=file) for a download.
*
* @return array<string,mixed>|null
*/
protected function getDownloadFile(int $downloadUid): ?array
{
$qb = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_file_reference');
$row = $qb
->select('fr.uid', 'fr.title', 'fr.description', 'f.uid AS file_uid', 'f.identifier', 'f.name', 'f.size', 'f.extension', 'f.mime_type')
->from('sys_file_reference', 'fr')
->join('fr', 'sys_file', 'f', 'fr.uid_local = f.uid')
->where(
$qb->expr()->eq('fr.tablenames', $qb->createNamedParameter('tx_vitec_domain_model_download', ParameterType::STRING)),
$qb->expr()->eq('fr.fieldname', $qb->createNamedParameter('file', ParameterType::STRING)),
$qb->expr()->eq('fr.uid_foreign', $qb->createNamedParameter($downloadUid, ParameterType::INTEGER)),
$qb->expr()->eq('fr.deleted', 0),
$qb->expr()->eq('f.missing', 0)
)
->orderBy('fr.sorting_foreign', 'ASC')
->setMaxResults(1)
->executeQuery()
->fetchAssociative();
if (!$row) {
return null;
}
return [
'uid' => (int)$row['file_uid'],
'name' => (string)($row['name'] ?? ''),
'url' => '/fileadmin' . ($row['identifier'] ?? ''),
'size' => (int)($row['size'] ?? 0),
'extension' => (string)($row['extension'] ?? ''),
'mimeType' => (string)($row['mime_type'] ?? ''),
'title' => (string)($row['title'] ?? ''),
'description' => (string)($row['description'] ?? ''),
];
}
}