src/Controller/SerieController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\Website\Navigation\BreadcrumbHelperService;
  4. use Pimcore\Controller\FrontendController;
  5. use Pimcore\Model\DataObject\Serie;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. // ADDED
  10. class SerieController extends FrontendController
  11. {
  12.     /**
  13.      * @param Request $request
  14.      *
  15.      * @return Response
  16.      */
  17.     public function singleAction(Request $request,
  18.         BreadcrumbHelperService $breadcrumbHelperService, ): Response
  19.     {
  20.         $serie = Serie::getByDolpoId($request->get('dolpoId'), true);
  21.         if (!$serie instanceof Serie) {
  22.             throw new NotFoundHttpException('Serie not found.');
  23.         }
  24.         $description = $serie->getDescription();
  25.         /*
  26.         $firstHalfDescription = null;
  27.         $secondHalfDescription = null;
  28.         if (strlen($description) > 0) {
  29.             $words = explode(' ', $description);
  30.             $midpoint = intdiv(count($words), 2);
  31.             $firstHalfDescription = implode(' ', array_slice($words, 0, $midpoint));
  32.             $secondHalfDescription = implode(' ', array_slice($words, $midpoint));
  33.         }
  34.         */
  35.         $allTomes=$serie->getVolumes();
  36.         $allCycles=[];
  37.         $books=[];
  38.         $authors=[];
  39.         $booksOutOfCycles=[];
  40.         $booksByCycle=[];
  41.         $cycleFormatValid = [];
  42.         foreach($allTomes as $tome) {
  43.             $book=$tome->getObject();
  44.             $books[]=$book;
  45.             $cycle = $tome->getCycle();
  46.             $number = $tome->getTome();
  47.             if ($cycle) { // Si le livre a un cycle
  48.                 if (!isset($booksByCycle[$cycle])) { // Si le cycle n'est pas encore dans le tableau
  49.                     $booksByCycle[$cycle] = []; // Initialisez le cycle avec un tableau vide
  50.                     if (preg_match('/Cycle \d+ : .+/', $cycle)) {
  51.                         $cycleFormatValid[$cycle] = true;
  52.                     } else {
  53.                         $cycleFormatValid[$cycle] = false;
  54.                     }
  55.                 }
  56.                 $booksByCycle[$cycle][] = ['book' => $book, 'number' => $number]; // Ajoutez le livre à la liste de son cycle
  57.             } else {
  58.                 $booksOutOfCycles[]=$book;
  59.             }
  60.         }
  61.         // Maintenant que tous les livres ont été ajoutés, nous pouvons les trier dans chaque cycle.
  62.         foreach ($booksByCycle as $cycle => &$booksInCycle) {
  63.             usort($booksInCycle, function ($a, $b) {
  64.                 return $a['number'] <=> $b['number']; // Compare les numéros de tome pour le tri
  65.             });
  66.         }
  67.         unset($booksInCycle); // Important : détruit la référence à la dernière valeur de $booksInCycle
  68.         foreach ($booksByCycle as $cycle => &$booksInCycle) {
  69.             // Extraire uniquement les livres
  70.             $booksInCycle = array_map(function ($entry) {
  71.                 return $entry['book'];
  72.             }, $booksInCycle);
  73.         }
  74.         unset($booksInCycle); // Important : détruit la référence à la dernière valeur de $booksInCycle
  75.         uksort($booksByCycle, function ($a, $b) {
  76.             // Extraire le numéro du cycle depuis le début de la chaîne.
  77.             preg_match('/^Cycle (\d+) : /', $a, $matchesA);
  78.             preg_match('/^Cycle (\d+) : /', $b, $matchesB);
  79.             // Obtenir les numéros de cycle, ou 999999 si le cycle ne correspond pas au modèle.
  80.             $numA = isset($matchesA[1]) ? intval($matchesA[1]) : 999999;
  81.             $numB = isset($matchesB[1]) ? intval($matchesB[1]) : 999999;
  82.             // Comparer les numéros de cycle.
  83.             return $numA <=> $numB;
  84.         });
  85.         if(count($books) > 0) {
  86.             $authors = reset($books)->getAuthors();
  87.         }
  88.         foreach ($books as $book) {
  89.             $title = $book->getTitle();
  90.             $details = explode(',', $title, 2);
  91.             $seriesTitle = $details[0];
  92.             $tomeDetails = isset($details[1]) ? trim($details[1]) : '';
  93.             $tomeNumber = 0; // default tome number
  94.             $tomeTitle = '';
  95.             $isFormatValid = true; // assume the title is in the normal format
  96.             if ($tomeDetails !== '') {
  97.                 if (preg_match('/tome (\d+)( :)?/i', $tomeDetails, $matches)) {
  98.                     $tomeNumber = (int)$matches[1]; // convert to integer
  99.                     $tomeTitleStart = strpos($tomeDetails, ':');
  100.                     if ($tomeTitleStart !== false) {
  101.                         $tomeTitle = trim(substr($tomeDetails, $tomeTitleStart + 1));
  102.                     } else {
  103.                         $tomeTitle = $seriesTitle;
  104.                     }
  105.                 } else {
  106.                     $isFormatValid = false;
  107.                 }
  108.             } else {
  109.                 $isFormatValid = false;
  110.             }
  111.             $bookDetailsList[$book->getId()] = [
  112.                 'seriesTitle' => $seriesTitle,
  113.                 'tomeNumber' => $tomeNumber,
  114.                 'tomeTitle' => $tomeTitle,
  115.                 'isFormatValid' => $isFormatValid
  116.             ];
  117.         }
  118.         // Get the category to retreive the page (for breadcrumb)
  119.         $document = \Pimcore\Model\Document::getById(1);
  120.         $pageLinkedToCategory = $serie->getPageLinkedToMainCategory();
  121.         if($pageLinkedToCategory != null) {
  122.             $document = $pageLinkedToCategory;
  123.         }
  124.         // SEO
  125.         $title = $serie->getName();
  126.         foreach ($authors as $author) {
  127.             $title .= ' : '.$author->getName();
  128.         }
  129.         $document->setTitle($title);
  130.         $descriptionMeta = $description;
  131.         if(trim($descriptionMeta) == '') {
  132.             $allTomes=$serie->getVolumes();
  133.             foreach($allTomes as $tome) {
  134.                 $book = $tome->getObject();
  135.                 $descriptionMeta = $book->getDescription();
  136.             }
  137.         }
  138.         $document->setDescription(\App\Service\Website\Tool\Text::cutStringRespectingWhitespace($descriptionMeta, 250));
  139.         $params = [
  140.             'serie' => $serie,
  141.             /*
  142.             'firstHalfDescription'=>$firstHalfDescription,
  143.             'secondHalfDescription'=>$secondHalfDescription,
  144.             */
  145.             'description'=>$description,
  146.             'books'=>$books,
  147.             'authors'=>$authors,
  148.             'bookDetailsList'=>$bookDetailsList ?? [],
  149.             'booksByCycle'=>$booksByCycle,
  150.             'booksOutOfCycles'=>$booksOutOfCycles,
  151.             'cycleFormatValid'=>$cycleFormatValid,
  152.             'document' =>  $document
  153.         ];
  154.         $breadcrumbHelperService->enrichSerieDetailPage($serie);
  155.         return $this->render('serie/single.html.twig', $params);
  156.     }
  157. }