<?php
namespace App\Controller\Frontend;
use App\Application\Artikel\ArtikelFrontendData;
use App\Application\Artikel\ArtikelService;
use App\Application\Artikel\ArtikelServiceQuery;
use App\Application\Content\ContentFrontendData;
use App\Application\Content\ContentService;
use App\Application\Epaper\EpaperService;
use App\Entity\Artikel\ArtikelTyp;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Cache(expires: '+10 minutes')]
class HomeController extends AbstractController
{
#[Route(path: '/', name: 'fe.home')]
public function home(ArtikelService $artikelService, ContentService $contentService, EpaperService $epaperService): Response
{
$artikelStream = [];
$artikelGalleryStream = [];
// --- Artikel Stream ---
$totArtikelInStream = 6;
// Top Lead Artikel finden
// Ist glaube ich obsolet da gar nirgends das Attribut isLeadArtikel gesetzt ist
$leadArtikel = [];
// $query = new ArtikelServiceQuery();
// $query->addFilterOneOfLeadTypeOnly(['leadartikel']);
// $query->addFilterOneOfStatusOnly(['publiziert']);
// $res = $artikelService->findArtikels(new ArtikelFrontendData(), $query);
//
// if ($res) {
// $leadArtikel = reset($res); // 1 Record im Array verwenden (nur ein Lead Artikel)
// $artikelStream[] = $leadArtikel;
// }
// end
// Zu pinnende Publireportagen Artikel finden
$pinnedPRArtikels = [];
$query = new ArtikelServiceQuery();
$query->addFilterOneOfStatusOnly(['publiziert']);
$query->addFilterNotInArtikelstreamStartseite();
$query->addFilterOneOfTypOnly([ArtikelTyp::ID_AT_4]);
$query->addFilterPrArtikelPinnedToday();
// Ist glaube ich obsolet da gar nirgends das Attribut isLeadArtikel gesetzt ist
// if ($leadArtikel) {
// //Lead Artikel entfernen, falls dies zugleich auch ein pinned Publireportage Artikel ist (theoretisch nicht nötig, da im Backend abgefangen)
// $query->addFilterExcludeArtikel([$leadArtikel['id']]);
// }
$res = $artikelService->findArtikels(new ArtikelFrontendData(), $query);
$pinnedPRArtikels = $res;
// Weitere Artikel holen und mit zu pinnenden Publireportagen mischen
$query = new ArtikelServiceQuery($totArtikelInStream);
$query->addFilterOneOfTypOnly([ArtikelTyp::ID_AT_1, ArtikelTyp::ID_AT_3]);
$query->addFilterOneOfStatusOnly(['publiziert']);
$query->addFilterNotInArtikelstreamStartseite();
if ($leadArtikel || $pinnedPRArtikels) {
// Lead Artikel entfernen
$excludeArtikelIds = $leadArtikel !== [] ? [$leadArtikel['id']] : [];
// Pinned Publireportagen entfernen
foreach ($pinnedPRArtikels as $prArtikel) {
$excludeArtikelIds[] = $prArtikel['id'];
}
$query->addFilterExcludeArtikel($excludeArtikelIds);
}
$query->addSort('publish_at', 'desc');
$res = $artikelService->findArtikels(new ArtikelFrontendData(), $query);
$artikelStream = $res;
// end
// Schiebe nun die vorhin gefundenen pinned Publireportagen dazwischen
if ($pinnedPRArtikels) {
if (count($artikelStream) > 3) {
// an Position 3 dazwischen schieben
array_splice($artikelStream, 2, 0, $pinnedPRArtikels); // splice in at position 3
} else {
// am Ende anhängen
$artikelStream = array_merge($artikelStream, $pinnedPRArtikels);
}
}
// Stream aufquellen mit [], wenn zuwenig Artikel, damit in View Layer mittels Index darauf zugriffen werden kann
while (count($artikelStream) < $totArtikelInStream) {
$artikelStream[] = [];
}
// Ende
// --- End Artikel Stream---
// --- Gallery Artikel Stream---
$totGalleryArtikel = 3;
// Top Galerie Artikel holen
$query = new ArtikelServiceQuery($totGalleryArtikel);
$query->addFilterOneOfLeadTypeOnly(['leadgalleryartikel']);
$query->addFilterOneOfStatusOnly(['publiziert']);
$query->addSort('publish_at', 'desc');
$topGalleryArtikel = $artikelService->findArtikels(new ArtikelFrontendData(), $query);
foreach ($topGalleryArtikel as $item) {
$artikelGalleryStream[] = $item;
}
// Ende
// Weitere Galerie Artikel hinzufügen
if (count($artikelGalleryStream) < $totGalleryArtikel) {
$query = new ArtikelServiceQuery($totGalleryArtikel);
$query->addFilterVideoOrImagegalleryOnly();
$query->addFilterOneOfStatusOnly(['publiziert']);
if ($topGalleryArtikel) {
$artikelIds = array_map(fn ($artikel) => $artikel['id'], $topGalleryArtikel);
$query->addFilterExcludeArtikel($artikelIds);
}
$query->addSort('publish_at', 'desc');
$res = $artikelService->findArtikels(new ArtikelFrontendData(), $query);
foreach ($res as $artikel) {
// $artikelGalleryStream bis auf Max. aufquellen
if (count($artikelGalleryStream) < $totGalleryArtikel) {
$artikelGalleryStream[] = $artikel;
}
}
}
// Stream aufquellen mit [], wenn zuwenig Artikel, damit in View Layer mittels Index darauf zugriffen werden kann
while (count($artikelGalleryStream) < $totGalleryArtikel) {
$artikelGalleryStream[] = [];
}
// Ende
// --- Ende Gallery Artikel Stream ---
// Service
$services = [
$contentService->getContentByCode('SERVICE_BOX_VERANSTALTUNGEN', new ContentFrontendData(), true),
$contentService->getContentByCode('SERVICE_BOX_ABOSERVICE', new ContentFrontendData(), true),
$contentService->getContentByCode('SERVICE_BOX_EINSENDUNGEN', new ContentFrontendData(), true),
$contentService->getContentByCode('SERVICE_BOX_TODESANZEIGEN', new ContentFrontendData(), true),
$contentService->getContentByCode('SERVICE_BOX_IMMOBILIEN', new ContentFrontendData(), true),
$contentService->getContentByCode('SERVICE_BOX_STELLEN', new ContentFrontendData(), true),
];
// Ende
return $this->render('frontend/home/home.html.twig', [
'artStream' => $artikelStream,
'galStream' => $artikelGalleryStream,
'services' => $services,
'lastIssue' => $epaperService->getLastIssueValues(),
]);
}
}