Closes two annex items that turned out to be the same defect seen from two
sides: markets could link to a detail page, solutions could not, and the code
that turns a page uid into a URL existed four times over.
detail_page for Solution (B-11)
- ext_tables.sql, TCA (group on pages, maxitems 1, in showitem behind slug)
and Domain\Model\Solution mirror the Market field exactly
- SolutionShowJsonRenderer emits detailUrl
- ModelcardJsonRenderer serialized no detailUrl at all: its shared
market|solution branch never carried the field. Market modelcards therefore
gain a link here too, not just solutions.
Service\LinkResolver (B-12)
- one implementation replaces four private copies (Market list/show, Usecase
list/show). The copies had drifted: Market returned null on failure, Usecase
an empty string. The contract is now explicit -- null means "no link".
- Usecase keeps a thin resolvePageUrl() wrapper appending ?? '', because it
concatenates paths and a null would tear the strings apart.
- LocationsJsonRenderer and NewsJsonRenderer stay out on purpose: they resolve
a full parameter construct resp. slug paths plus canonical, which is not
page-uid-to-URL and does not fit the same contract.
Requires on the server, the column exists in code only:
vendor/bin/typo3 database:updateschema "*.add,*.change"
vendor/bin/typo3 cache:flush
168 lines
6.1 KiB
PHP
Executable File
168 lines
6.1 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Evomedien\Vitec\UserFunc;
|
|
|
|
use Doctrine\DBAL\ParameterType;
|
|
use Evomedien\Vitec\Service\LinkResolver;
|
|
use Evomedien\Vitec\Service\UsecaseSerializer;
|
|
use TYPO3\CMS\Core\Attribute\AsAllowedCallable;
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
use TYPO3\CMS\Core\Service\FlexFormService;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
/**
|
|
* UserFunc: render a single Success Story as JSON (headless).
|
|
*
|
|
* The story is chosen from the plugin FlexForm (settings.usecase) or, when
|
|
* empty, from the request param tx_vitec_usecaseshow[usecase] (uid or slug).
|
|
* Full serialisation is delegated to UsecaseSerializer. Exception-safe.
|
|
*/
|
|
class UsecaseShowJsonRenderer
|
|
{
|
|
private const TABLE = 'tx_vitec_domain_model_usecase';
|
|
|
|
#[AsAllowedCallable]
|
|
public function render(string $content, array $conf): string
|
|
{
|
|
$row = is_array($this->cObj->data ?? null) ? $this->cObj->data : null;
|
|
if ($row && (string)($row['CType'] ?? '') === 'vitec_usecaseshow') {
|
|
return $this->renderForRecord($row);
|
|
}
|
|
|
|
$pageId = 0;
|
|
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
|
|
if ($request !== null) {
|
|
$pageInfo = $request->getAttribute('frontend.page.information');
|
|
if ($pageInfo !== null) {
|
|
$pageId = (int)$pageInfo->getId();
|
|
}
|
|
}
|
|
if ($pageId <= 0) {
|
|
$pageId = (int)($GLOBALS['TSFE']->id ?? 0);
|
|
}
|
|
if ($pageId <= 0) {
|
|
return '';
|
|
}
|
|
|
|
$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
|
|
$ces = $qb
|
|
->select('*')
|
|
->from('tt_content')
|
|
->where(
|
|
$qb->expr()->eq('pid', $qb->createNamedParameter($pageId, ParameterType::INTEGER)),
|
|
$qb->expr()->eq('CType', $qb->createNamedParameter('vitec_usecaseshow', ParameterType::STRING)),
|
|
$qb->expr()->eq('deleted', 0),
|
|
$qb->expr()->eq('hidden', 0)
|
|
)
|
|
->executeQuery()
|
|
->fetchAllAssociative();
|
|
|
|
if (empty($ces)) {
|
|
return '';
|
|
}
|
|
|
|
return $this->renderForRecord($ces[0]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $contentElement
|
|
*/
|
|
public function renderForRecord(array $contentElement): string
|
|
{
|
|
try {
|
|
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
|
|
$flexFormData = $flexFormService->convertFlexFormContentToArray($contentElement['pi_flexform'] ?? '');
|
|
$settings = $flexFormData['settings'] ?? [];
|
|
|
|
$layout = (string)($settings['layout'] ?? 'default');
|
|
$debugMode = (bool)($settings['debug'] ?? false);
|
|
// The story is resolved from the request ONLY (detail route from the
|
|
// list plugin's detailUrl): route-enhancer arguments live in the
|
|
// `routing` PageArguments, plain GET in query params.
|
|
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
|
|
$param = null;
|
|
$routing = $request?->getAttribute('routing');
|
|
if ($routing instanceof \TYPO3\CMS\Core\Routing\PageArguments) {
|
|
$param = $routing->getRouteArguments()['tx_vitec_usecaseshow']['usecase'] ?? null;
|
|
}
|
|
if ($param === null || $param === '') {
|
|
$param = ($request?->getQueryParams() ?? [])['tx_vitec_usecaseshow']['usecase'] ?? null;
|
|
}
|
|
$usecaseUid = ($param !== null && $param !== '')
|
|
? (is_numeric($param) ? (int)$param : $this->resolveSlug((string)$param))
|
|
: 0;
|
|
|
|
if (!$usecaseUid) {
|
|
return $debugMode
|
|
? (string)json_encode(['error' => 'No usecase selected or found', 'debug' => ['settings' => $settings]])
|
|
: '';
|
|
}
|
|
|
|
$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE);
|
|
$usecase = $qb
|
|
->select('*')
|
|
->from(self::TABLE)
|
|
->where(
|
|
$qb->expr()->eq('uid', $qb->createNamedParameter($usecaseUid, ParameterType::INTEGER)),
|
|
$qb->expr()->eq('deleted', 0),
|
|
$qb->expr()->eq('hidden', 0)
|
|
)
|
|
->executeQuery()
|
|
->fetchAssociative();
|
|
|
|
if (!$usecase) {
|
|
return $debugMode
|
|
? (string)json_encode(['error' => 'Usecase not found', 'debug' => ['usecaseUid' => $usecaseUid]])
|
|
: '';
|
|
}
|
|
|
|
$serializer = GeneralUtility::makeInstance(UsecaseSerializer::class);
|
|
|
|
$backPid = (int)($settings['backPid'] ?? 0);
|
|
$response = [
|
|
'usecase' => $serializer->serializeDetail($usecase),
|
|
'layout' => $layout,
|
|
'backUrl' => $backPid > 0 ? ($this->resolvePageUrl($backPid) ?: null) : null,
|
|
];
|
|
|
|
if ($debugMode) {
|
|
$response['debug'] = ['usecaseUid' => $usecaseUid, 'layout' => $layout, 'settings' => $settings];
|
|
}
|
|
|
|
return (string)json_encode($response);
|
|
} catch (\Throwable $e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
private function resolveSlug(string $slug): int
|
|
{
|
|
$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE);
|
|
$row = $qb
|
|
->select('uid')
|
|
->from(self::TABLE)
|
|
->where(
|
|
$qb->expr()->eq('slug', $qb->createNamedParameter($slug)),
|
|
$qb->expr()->eq('deleted', 0),
|
|
$qb->expr()->eq('hidden', 0)
|
|
)
|
|
->executeQuery()
|
|
->fetchAssociative();
|
|
|
|
return (int)($row['uid'] ?? 0);
|
|
}
|
|
|
|
/**
|
|
* Resolve a page uid to its frontend path (route enhancer aware).
|
|
*
|
|
* Callers here build paths by concatenation, so the empty string stays the
|
|
* "no link" value; LinkResolver's null is coalesced away.
|
|
*/
|
|
private function resolvePageUrl(int $pageUid): string
|
|
{
|
|
return LinkResolver::pageUrl($pageUid) ?? '';
|
|
}
|
|
}
|