src/Controller/AuthorGridController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\DolpoSearchService;
  4. use Pimcore\Controller\FrontendController;
  5. use Pimcore\Model\DataObject\Author;
  6. use Pimcore\Model\Staticroute;
  7. // ADDED
  8. use Pimcore\Model\DataObject\DolpoTag;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. class AuthorGridController extends FrontendController
  12. {
  13.     private $cachetime=86400;
  14.     /**
  15.      * @param Request $request
  16.      *
  17.      * @return Response
  18.      */
  19.     public function singleAction(Request $request, DolpoSearchService $dolpoSearchService): Response
  20.     {
  21.         $category = $request->get('category');
  22.         $AllTopTags = DolpoTag::getByTagType('best');
  23.         //dump($category);
  24.         $cacheKey = 'topauthorsPage_.'.$category;
  25.         $forceRefresh = $request->query->get('refresh');
  26.         if (!$forceRefresh && $pageauteur = \Pimcore\Cache::load($cacheKey)) {
  27.             //dump('find');
  28.             return new Response($pageauteur);
  29.         }
  30.         $TopAuteurTag = null;
  31.         foreach ($AllTopTags as $tag) {
  32.             $MainDolpoTag = $tag->getMainDolpoTag();
  33.             if ($MainDolpoTag != null) {
  34.                 $path = $MainDolpoTag->getPage();
  35.                 $lastPartLowercase = strtolower(basename($path));
  36.                 if($category == $lastPartLowercase) {
  37.                     $TopAuteurTag = $tag;
  38.                 }
  39.             }
  40.         }
  41.         if($TopAuteurTag != null) {
  42.             $filters = ['DolpoTags' => [$TopAuteurTag->getId()]];
  43.             $authors = $dolpoSearchService->authorSearchFilter($filters, 0, 5000);
  44.             $authorsDetails = [];
  45.             $author_objs=[];
  46.             foreach($authors as $author_arr) {
  47.                 $author_id=$author_arr['o_id'];
  48.                 $auteur=Author::getById($author_id);
  49.                 if(!empty($auteur)) {
  50.                     $author_objs[]=$auteur;
  51.                     //tous les livres
  52.                     $bookslist = $auteur->getAllBooks($dolpoSearchService);
  53.                     $booksNumber = count($bookslist);
  54.                     $awardsCount=0;
  55.                     $best_three_books=[];
  56.                     $buildEditionUrl = static function (?string $ean): string {
  57.                         if ($ean === null || $ean === '') {
  58.                             return '';
  59.                         }
  60.                         $route = Staticroute::getByName('edition');
  61.                         if ($route === null) {
  62.                             return '';
  63.                         }
  64.                         return $route->assemble(['isbn' => $ean], true);
  65.                     };
  66.                     $buildThumbnailUrl = static function (?string $ean): string {
  67.                         if ($ean === null || $ean === '') {
  68.                             return '';
  69.                         }
  70.                         return '/var/img_cache/thumbnails/livre/' . $ean . '-xs.webp';
  71.                     };
  72.                     foreach ($bookslist as $book) {
  73.                         $tags = is_array($book['tags'] ?? null) ? $book['tags'] : [];
  74.                         foreach ($tags as $tag) {
  75.                             if (is_array($tag) && array_key_exists('tagtype', $tag) && $tag['tagtype'] === 'prix') {
  76.                                 $awardsCount++;
  77.                             }
  78.                             if (is_array($tag)
  79.                                 && array_key_exists('name', $tag)
  80.                                 && $tag['name'] === "Ouvrage de référence de l'auteur"
  81.                                 && count($best_three_books) < 3
  82.                             ) {
  83.                                 $ean = $book['ean'] ?? null;
  84.                                 $best_three_books[] = [
  85.                                     'url' => $buildEditionUrl($ean ? (string) $ean : null),
  86.                                     'thumbnail' => $buildThumbnailUrl($ean ? (string) $ean : null),
  87.                                     'title' => $book['title'] ?? '',
  88.                                 ];
  89.                             }
  90.                         }
  91.                     }
  92.                     $top_tags = $auteur->getAuthorBookNumberByCategory($dolpoSearchService);
  93.                     $authorsDetails[$author_id] = [
  94.                         'booksNumber' => $booksNumber,
  95.                         'awardsCount' => $awardsCount,
  96.                         'best_three_books' => $best_three_books,
  97.                         'top_tags' => $top_tags,
  98.                     ];
  99.                 }
  100.             }
  101.         }
  102.         $params = [
  103.             'authors' => $author_objs ?? [],
  104.             'authorsDetails'=>$authorsDetails ?? [],
  105.         ];
  106.         $pageauteur=$this->renderView('author-grid/single.html.twig', $params);
  107.         $l=\Pimcore\Cache::save(
  108.             $pageauteur,
  109.             $cacheKey,
  110.             ['page_auteur'],
  111.             $this->cachetime,
  112.             10,
  113.             true // 1sem | 86400 sec = 24h // 1800 sec = 30min
  114.         );
  115.         //dump($l);
  116.         return new Response($pageauteur);
  117.     }
  118. }