<?php
namespace App\Controller\Website;
use App\BlockManager\ContentBlockManager;
use App\BlockManager\SidebarBlockManager;
use App\Snippet\Button;
use App\Utils\SchemaManager;
use Spatie\SchemaOrg\Schema;
use Sulu\Bundle\MediaBundle\Api\Media;
use Sulu\Component\Content\Compat\StructureInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends SuluExtendController
{
private Request $request;
public function __construct(
private readonly ContentBlockManager $contentBlockManager,
private readonly SidebarBlockManager $sidebarBlockManager,
public RequestStack $requestStack,
) {
$this->request = $this->requestStack->getCurrentRequest();
}
public function viewAction(
StructureInterface $structure,
ContentBlockManager $contentBlockManager,
SchemaManager $schemaManager,
Button $button,
$attributes = [],
$preview = false,
$partial = false,
): Response
{
$attributes = $this->getAttributes([], $structure);
$attributes['breadcrumbs'] = $this->getBreadcrumbs('page', $attributes['uuid']);
$attributes['content_section_props'] = [
'is_container' => false,
'spacing' => 'top-0',
];
$schema = $schemaManager->getDefaultSchemas($attributes['breadcrumbs']);
$pageSchema = Schema::webPage();
// ---------------------------------------
// Header
// ---------------------------------------
$attributes['template_content']['post'] = [
'header' => [
'title' => $attributes['content']['title'],
'perex' => $attributes['content']['description'],
],
];
// ---------------------------------------
// Content
// ---------------------------------------
$attributes['content_blocks'] = [];
$mappedContentBlocks = $this->contentBlockManager->mapBlocksAttributes($attributes['content']['content_blocks']);
$pageSchemaImages = [];
foreach ($mappedContentBlocks as $block) {
if($block['type'] == 'image' || $block['type'] == 'imageWithText') {
$imgUrl = $this->request->getSchemeAndHttpHost() . $block['content']['img']?->getUrl();
$pageSchemaImages[] = $imgUrl;
}
elseif($block['type'] == 'gallery' && isset($block['content']['images'])) {
/** @var Media $image */
foreach ($block['content']['images'] as $image) {
$imgUrl = $this->request->getSchemeAndHttpHost() . $image?->getUrl();
$pageSchemaImages[] = $imgUrl;
}
}
$attributes['content_blocks'][] = $block;
}
$pageSchema->image($pageSchemaImages);
$schema[] = $pageSchema;
// ---------------------------------------
// Sidebar
// ---------------------------------------
if ($attributes['content']['sidebar'] && array_key_exists('blocks', $attributes['content']['sidebar'])) {
$attributes['props']['aside'] = true;
$attributes['sidebar']['blocks'] = $this->sidebarBlockManager->mapBlocksAttributes($attributes['content']['sidebar']['blocks']);
}
// ---------------------------------------
// Blocks under post
// ---------------------------------------
if(isset($attributes['content']['blocks'])) {
$blockList = $this->resolveBlocks($attributes['content']['blocks'], $schema);
$attributes['content']['blocks'] = $blockList->getBlocks();
$schema = $blockList->getSchema();
}
$attributes['_schema'] = implode('', $schema);
return $this->renderAll('pages/default.html.twig', $attributes, $preview, $partial);
}
}