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 $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|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> */ 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|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 $download * @return array */ 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|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'] ?? ''), ]; } }