src/Controller/DefaultController.php line 210

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\Document\Areabrick\Series;
  4. use App\Service\DolpoSearchService;
  5. use App\Service\PageHtmlCacheService;
  6. use Pimcore\Controller\FrontendController;
  7. use Pimcore\Model\DataObject\Book;
  8. use Pimcore\Model\DataObject\OnixProduct;
  9. use Pimcore\Model\DataObject\Serie;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Pimcore\Model\DataObject\Contributor;
  14. use CoreShop\Component\Core\Context\ShopperContextInterface;
  15. class DefaultController extends FrontendController
  16. {
  17.     private const DOLPO_RATING_LABELS_FALLBACK = [
  18.         '0' => 'Décevant, abandonné',
  19.         '1' => 'Décevant mais terminé',
  20.         '2' => 'A lire pour les accros au genre',
  21.         '3' => 'Bouquin sympa, à lire',
  22.         '4' => 'Vraiment bon livre',
  23.         '5' => 'Très bon livre, une référence',
  24.     ];
  25.     public function __construct(
  26.         private string $websiteUrl,
  27.         protected ShopperContextInterface $shopperContext,
  28.         private PageHtmlCacheService $pageHtmlCacheService,
  29.     ) {
  30.      
  31.     }
  32.     /**
  33.      * @param Request $request
  34.      *
  35.      * @return Response
  36.      */
  37.     public function defaultAction(Request $request): Response
  38.     {
  39.         if ($this->document) {
  40.             $homeKey = 'page_'.$this->document->getId();
  41.             $forceRefresh = $request->query->get('refresh');
  42.             if (!$forceRefresh && !$this->editmode && ($page = $this->pageHtmlCacheService->load($homeKey)) !== null) {
  43.                 // Page loaded from cache, placeholders are replaced below.
  44.             }
  45.             else {
  46.                 $page = $this->renderView('default/default.html.twig', [
  47.                     'requestforcache' => true,
  48.                 ]);
  49.                 if (!$this->editmode) {
  50.                     $this->pageHtmlCacheService->save($homeKey, $page);
  51.                 }
  52.             }
  53.             $headerContent = $this->renderView('account/_header.html.twig', [
  54.                 'websiteUrl' => $this->websiteUrl,
  55.                 'customer' => $this->shopperContext->hasCustomer() ? $this->shopperContext->getCustomer() : null
  56.             ]);
  57.             $cartwidgetContent = $this->renderView('basket/_widget.html.twig', [
  58.                 'cart' => $this->shopperContext->getCart()
  59.             ]);
  60.             $page = str_replace('[[HEADER_ACTION]]', $headerContent, $page);
  61.             $page = str_replace('[[CARTWIDGET_ACTION]]', $cartwidgetContent, $page);
  62.             return new Response($page);
  63.         }
  64.         return $this->render('default/default.html.twig');
  65.     }
  66.     /**
  67.      * @param Request $request
  68.      *
  69.      * @return Response
  70.      */
  71.     public function testcacheAction(Request $request): Response
  72.     {
  73.         // Cache Pimcore
  74.         $key = 'serie.'.'testcw';
  75.         //$cacheKey = md5( $key );
  76.         $cacheKey = 'cachetest';
  77.         var_dump($cacheKey);
  78.         echo '<br/>cache enabled : ';
  79.         var_dump(\Pimcore\Cache::isEnabled());
  80.         echo '<br/>';
  81.         //\Pimcore\Cache::setForceImmediateWrite(true);
  82.         //var_dump(\Pimcore\Cache::remove($cacheKey));
  83.         $items = (\Pimcore\Cache::load($cacheKey));
  84.         echo '<br/>';
  85.         //die();
  86.         if (!$items = \Pimcore\Cache::load($cacheKey)) {
  87.             $items = ['test1'=>'test '.date('Y-m-d H:i:s')];
  88.             echo '<br/>';
  89.             echo 'cache broken !';
  90.             echo '<br/>';
  91.             $save = \Pimcore\Cache::save(
  92.                 $items,
  93.                 $cacheKey,
  94.                 ['cache-test'],
  95.                 86400, // 86400 sec = 24h // 1800 sec = 30min
  96.                 10,
  97.                 false); // force
  98.             var_dump($save);
  99.         } else {
  100.             echo 'cache works ;)';
  101.             echo '<br/>';
  102.         }
  103.         echo '<br/>';
  104.         var_dump($items);
  105.         /*
  106.                 // Exemple de test Messenger
  107.         $message = new TestMessage('Hello, Messenger!');
  108.         // Envoi du message au bus
  109.         $messageBus->dispatch($message)->setMessageBus('async');
  110.         */
  111.         // Emtpy response
  112.         return new Response(
  113.             '',
  114.             200,
  115.         );
  116.     }
  117.     public function categoryPageAction(Request $request): Response
  118.     {
  119.         $categoryKey = 'page_'.$this->document->getId() ;
  120.         $forceRefresh = $request->query->get('refresh');
  121.         if (!$forceRefresh && !$this->editmode && ($page = $this->pageHtmlCacheService->load($categoryKey)) !== null) {
  122.             //rien tout de suite, il faut changer ensuite, changer la balise
  123.         }
  124.         else{
  125.             // Get the root Category Page
  126.             $pagesCategories = [];
  127.             $pageCategoryRoot = $this->document;
  128.             $pagesCategories[] = $pageCategoryRoot->getId();
  129.             $parent = $pageCategoryRoot->getParent();
  130.             while ($parent && strpos($parent->getController(), 'categoryPage') > 0) {
  131.                 $pageCategoryRoot = $parent;
  132.                 $pagesCategories[] = $pageCategoryRoot->getId();
  133.                 $parent = $pageCategoryRoot->getParent();
  134.             }
  135.             $pagesCategories = array_reverse($pagesCategories);
  136.             // Take the child of category root if third level
  137.             $pageCategoryRootForMenu = $pageCategoryRoot->getId();
  138.             if($this->document->getId() != $pageCategoryRoot->getId()) {
  139.                 $pageCategoryRootForMenu = $pagesCategories[1];
  140.             }
  141.             $page = $this->renderView('default/category.html.twig', 
  142.             [
  143.             'categoryPageRootId' => $pageCategoryRootForMenu,
  144.             'showMenuLeft'     => true,
  145.             'requestforcache' => true,
  146.             'websiteUrl' => $this->websiteUrl
  147.             ]);
  148.             if(!$this->editmode){
  149.                 $this->pageHtmlCacheService->save($categoryKey, $page);
  150.             }
  151.         }
  152.         
  153.         $headerContent = $this->renderView('account/_header.html.twig', [
  154.             'websiteUrl' => $this->websiteUrl,
  155.             'customer' => $this->shopperContext->hasCustomer() ? $this->shopperContext->getCustomer() : null
  156.         ]);
  157.         $cartwidgetContent = $this->renderView('basket/_widget.html.twig', [
  158.             'cart' => $this->shopperContext->getCart()
  159.         ]);
  160.     
  161.         // Remplacer la balise
  162.         $page = str_replace('[[HEADER_ACTION]]', $headerContent, $page);
  163.         $page = str_replace('[[CARTWIDGET_ACTION]]', $cartwidgetContent, $page);
  164.         return new Response($page);
  165.     }
  166.     public function articlePageAction(Request $request): Response
  167.     {
  168.         return $this->render('default/article.html.twig',
  169.             [
  170.             //'showMenuLeft'     => true,
  171.             ]
  172.         );
  173.     }
  174.     public function wikiPageAction(Request $request): Response
  175.     {
  176.         return $this->render('default/wiki.html.twig',
  177.             [
  178.             //'showMenuLeft'     => true,
  179.             ]
  180.         );
  181.     }
  182.     public function SeriesPageAction(Request $request): Response
  183.     {
  184.         return $this->render('default/series.html.twig',
  185.             [
  186.             //'showMenuLeft'     => true,
  187.             ]
  188.         );
  189.     }
  190.     private function _getObjectBookOrSerie($id)
  191.     {
  192.         $item = Book::getById((int)$id);
  193.         if (! $item instanceof Book) {
  194.             $item = Serie::getById((int)$id);
  195.         }
  196.         return $item;
  197.     }
  198.     private function _getObjectBookSerieOrOnixProduct($id)
  199.     {
  200.         $item = $this->_getObjectBookOrSerie($id);
  201.         if ($item instanceof Book || $item instanceof Serie) {
  202.             return $item;
  203.         }
  204.         return OnixProduct::getById((int)$id);
  205.     }
  206.     /*
  207.     AREABRICK RENDERLET
  208.     -------------------
  209.     */
  210.     public function articleAction(Request $request): Response
  211.     {
  212.         if ($request->get('type') === 'document') {
  213.             $params = [];
  214.             $item = \Pimcore\Model\Document::getById((int)$request->get('id'));
  215.             if($item == null) {
  216.                 throw new NotFoundHttpException('Document needed');
  217.             }
  218.             $params['item'] = $item;
  219.             $params['modelactu'] = $request->get('modelactu');
  220.             $dateTimestamp = ($item->getCreationDate());
  221.             $dateToShow = date('d/m/Y', $dateTimestamp);
  222.             if($item->getEditable('myDate') != null) {
  223.                 $dateCustom = $item->getEditable('myDate')->getData();
  224.                 if($dateCustom instanceof \Carbon\Carbon) {
  225.                     $dateToShow = $dateCustom->format('d/m/Y');
  226.                 }
  227.             }
  228.             $length = 100;
  229.             $shortDesc = $item->getEditable('intro')->getData();
  230.             $shortDesc = \App\Service\Website\Tool\Text::cutStringRespectingWhitespace($item->getEditable('intro')->getData(), $length);
  231.             $params['shortDesc'] = strip_tags($shortDesc);
  232.             $params['dateToShow'] = strip_tags($dateToShow);
  233.             return $this->render('default/article_renderlet.html.twig', $params);
  234.         }
  235.     }
  236.     /*
  237.     AREABRICK RENDERLET
  238.     -------------------
  239.     */
  240.     public function bookSelectionAction(Request $request): Response
  241.     {
  242.         if ($request->get('type') === 'object') {
  243.             $params = [];
  244.             $item = $this->_getObjectBookSerieOrOnixProduct((int)$request->get('id'));
  245.             if ($item == null) {
  246.                 throw new NotFoundHttpException('Book, Serie or OnixProduct needed');
  247.             }
  248.             $params['item'] = $item;
  249.             $params['tagsToShow'] = $request->get('tagsToShow');
  250.             $params['showCount'] = (bool)$request->get("showCount");
  251.             $params['blogger'] = $request->get('blogger') ? Contributor::getById($request->get('blogger')) : null ;
  252.             return $this->render('default/book_selection.html.twig', $params);
  253.         }
  254.     }
  255.     public function bookWikiAction(Request $request): Response
  256.     {
  257.         if ($request->get('type') === 'object') {
  258.             $params = [];
  259.             $item = $this->_getObjectBookSerieOrOnixProduct((int)$request->get('id'));
  260.             if ($item == null) {
  261.                 throw new NotFoundHttpException('Book, Serie or OnixProduct needed');
  262.             }
  263.             $params['item'] = $item;
  264.             return $this->render('default/book_wiki.html.twig', $params);
  265.         }
  266.     }
  267.     public function bookRankingAction(Request $request): Response
  268.     {
  269.         $rankKey = md5(json_encode([
  270.             $request->get('id'),
  271.             $request->get('tagsToShow'),
  272.             $request->get('type'),
  273.             $request->get('indexLoopRenderlet'),
  274.         ]));
  275.         // if ($gallery = \Pimcore\Cache::load($rankKey)) {
  276.         //     return new Response($gallery);
  277.         // }
  278.         $params = [];
  279.         $item = $this->_getObjectBookSerieOrOnixProduct((int)$request->get('id'));
  280.         if ($item == null) {
  281.             throw new NotFoundHttpException('Book, Serie or OnixProduct needed');
  282.         }
  283.         $params['item'] = $item;
  284.         $params['tagsToShow'] = $request->get('tagsToShow');
  285.         $params['indexLoopRenderlet'] = $request->get('indexLoopRenderlet');
  286.         $view = $this->renderView('default/book_ranking.html.twig', $params);
  287.         // \Pimcore\Cache::save(
  288.         //     $view,
  289.         //     $rankKey,
  290.         //     ["book_ranking"],
  291.         //     $this->cachetime);
  292.         return new Response($view);
  293.     }
  294.     public function bookHighlightOneAction(Request $request): Response
  295.     {
  296.         if ($request->get('type') === 'object') {
  297.             $params = [];
  298.             $item = $this->_getObjectBookSerieOrOnixProduct((int)$request->get('id'));
  299.             if ($item == null) {
  300.                 throw new NotFoundHttpException('Book, Serie or OnixProduct needed');
  301.             }
  302.             $params['item'] = $item;
  303.             $params['tagsToShow'] = $request->get('tagsToShow');
  304.             return $this->render('default/book_highlight_one.html.twig', $params);
  305.         }
  306.     }
  307.     public function bookHighlightTwoAction(Request $request): Response
  308.     {
  309.         $params = [];
  310.         $item = $this->_getObjectBookSerieOrOnixProduct((int)$request->get('id'));
  311.         if ($item == null) {
  312.             throw new NotFoundHttpException('Book, Serie or OnixProduct needed');
  313.         }
  314.         $params['item'] = $item;
  315.         $params['tagsToShow'] = $request->get('tagsToShow');
  316.         $params['blogger'] = $request->get('blogger') ? Contributor::getById($request->get('blogger')) : null ;
  317.         $params['showwishadd'] = true;
  318.         $view = $this->renderView('default/book_highlight_two.html.twig', $params);
  319.         return new Response($view);
  320.     }
  321.     public function bookSeriesAction(Request $request, DolpoSearchService $dolpoSearchService): Response
  322.     {
  323.         $paginationDefault = \Pimcore\Model\WebsiteSetting::getByName('paginationSeriesDefault');
  324.         $paginationDefaultVal = $paginationDefault->getData();
  325.         $pagination = $request->get('pagination');
  326.         if($pagination == 0) {
  327.             $pagination = $paginationDefaultVal;
  328.         }
  329.         // Filters JS
  330.         $tagsFilterArray = [];
  331.         if(($request->get('tagsFilter') && count($request->get('tagsFilter')))) {
  332.             foreach($request->get('tagsFilter') as $tag) {
  333.                 $tagsFilterArray[$tag->getId()] = $tag;
  334.             }
  335.         }
  336.         //TAGS SELECTED BY JS
  337.         $conditionsByTagsFiltered = [];
  338.         $tagsFilteredSelectedrRawArray = (explode(',', $request->get('tagsFiltersSelectedIds')));
  339.         if(($tagsFilteredSelectedrRawArray && count($tagsFilteredSelectedrRawArray))) {
  340.             // Selection of all sub tags of the current tag
  341.             foreach($tagsFilteredSelectedrRawArray as $tagId) {
  342.                 if($tagId != '') {
  343.                     $conditionsByTagsFiltered[$tagId] = $tagId;
  344.                 }
  345.             }
  346.         }
  347.         if(!(count($conditionsByTagsFiltered) > 0)) {
  348.             $tags = explode(',', $request->get('tagsSelectionIds'));
  349.             foreach($tags as $tagid) {
  350.                 $conditionsByTagsFiltered[$tagid] = $tagid;
  351.             }
  352.         }
  353.         // Use DolpoSearch Service with ElasticSearch
  354.         // Keep only numeric IDs (same behavior as bookGalleryAction).
  355.         $filters = [];
  356.         $tagIds = array_values(array_filter(array_map('intval', $conditionsByTagsFiltered)));
  357.         if (count($tagIds) > 0) {
  358.             $filters['DolpoTags'] = $tagIds;
  359.         }
  360.         $results = $dolpoSearchService->serieSearchFilter($filters, 0, 1000, ['o_id']);
  361.         $arrObjectSelectionIds = [];
  362.         foreach($results as $infos) {
  363.             $arrObjectSelectionIds[] = $infos['o_id'];
  364.         }
  365.         $itemsArray = $arrObjectSelectionIds;
  366.         $params = [
  367.            'tagsToShow' => false,
  368.            'pagination'        => $pagination,
  369.            'objectsSelectionIds' => implode(',', $itemsArray),
  370.            'frontFilters' => null,
  371.            'showCount' => true,
  372.            'ad' => null,
  373.            'tagsSelectionIds'  => $request->get('tagsSelectionIds'), // Tags used to selection of series shown
  374.            'tagsFilterArray'   => $tagsFilterArray, // Filters to show as button selectables
  375.            'title' => 'Résultats',
  376.            'fromAjax'          => $request->get('fromAjax', null),
  377.            'editmode'          => $request->get('editmode'),
  378.         ];
  379.         $html = $this->render('default/book_series.html.twig', $params);
  380.         return $html;
  381.     }
  382.     /*
  383.     * Format the array items;
  384.     */
  385.     private function _addKeyIdsInListing($listing)
  386.     {
  387.         $return = [];
  388.         foreach($listing as $item) {
  389.             $return[$item->getId()] = $item;
  390.         }
  391.         return $return;
  392.     }
  393.     public function bookGalleryAction(Request $request, DolpoSearchService $dolpoSearchService): Response
  394.     {
  395.         $galleryCacheKey = md5(json_encode([$request->get('tagsToShow'),
  396.                 $request->get('tagsToExcludeIds'),
  397.                 $request->get('objectsSelectionIds'),
  398.                 $request->get('tagsSelectionIds'),
  399.                 $request->get('text_search'),
  400.                 $request->get('pagination'),
  401.                 $request->get('offset', 0)]
  402.         ));
  403.         $paginationDefault = \Pimcore\Model\WebsiteSetting::getByName('paginationGalleryDefault');
  404.         $paginationDefaultVal = $paginationDefault->getData();
  405.         $pagination = $request->get('pagination');
  406.         if ($pagination == 0) {
  407.             $pagination = $paginationDefaultVal;
  408.         }
  409.         // Book Or Series added (manually or auto)
  410.         $itemsArray = [];
  411.         // for automatic render
  412.         if ($request->get('uniqueIdForAjax') != null) {
  413.             $uniqueId = $request->get('uniqueIdForAjax');
  414.         } else {
  415.             $uniqueId = uniqid();
  416.         }
  417.         // Get all Book and Series already added manually in the document
  418.         // !! The brick need to be empty before deleted (because the brick data is still in bdd).
  419.         /*
  420.         $itemsAlreadyAddedInCurrentPage = []; // Book and Series already added
  421.         $editables = $this->document->getEditables();
  422.         foreach ($editables as $editable) {
  423.             if ($editable instanceof \Pimcore\Model\Document\Editable\Renderlet) {
  424.                 $objectInRenderlet = $editable->getO();
  425.                 if ($objectInRenderlet instanceof Book || $objectInRenderlet instanceof Series) {
  426.                     $itemsAlreadyAddedInCurrentPage[$objectInRenderlet->getId()] = $objectInRenderlet->getId();
  427.                 }
  428.             }
  429.         }
  430.         */
  431.         
  432.         $filters = [];
  433.         $textSearch = $request->get('text_search');
  434.         if (is_string($textSearch)) {
  435.             $textSearch = trim($textSearch);
  436.         } else {
  437.             $textSearch = null;
  438.         }
  439.         if (($textSearch === null || $textSearch === '') && is_string($request->get('q'))) {
  440.             $textSearch = trim($request->get('q'));
  441.         }
  442.         if ($textSearch !== null && $textSearch !== '') {
  443.             $filters['TextSearch'] = $textSearch;
  444.         }
  445.         // Manual mode: if the parameter exists (even empty), keep ObjectIds in filters.
  446.         $hasObjectsSelectionIds = $request->attributes->has('objectsSelectionIds')
  447.             || $request->query->has('objectsSelectionIds')
  448.             || $request->request->has('objectsSelectionIds');
  449.         if ($hasObjectsSelectionIds) {
  450.             $objectsSelectionRaw = $request->get('objectsSelectionIds');
  451.             if (is_array($objectsSelectionRaw)) {
  452.                 $arrObjectSelectionIds = array_values(array_filter(array_map('intval', $objectsSelectionRaw)));
  453.             } else {
  454.                 $arrObjectSelectionIds = array_values(array_filter(array_map(
  455.                     'intval',
  456.                     explode(',', (string)$objectsSelectionRaw)
  457.                 )));
  458.             }
  459.             $filters['ObjectIds'] = $arrObjectSelectionIds;
  460.         }
  461.         // Automatic mode by DolpoTag Filters
  462.         //
  463.         
  464.         if ($request->get('tagsSelectionIds') != null) {
  465.             $tags = array_values(array_filter(array_map(
  466.                 'intval',
  467.                 explode(',', $request->get('tagsSelectionIds'))
  468.             )));
  469.             // Use DolpoSearch Service with ElasticSearch
  470.             $filters['DolpoTags'] = $tags; 
  471.         }
  472.         if ($request->get('tagsToExcludeIds') != null) {
  473.              $tagsExclude = array_values(array_filter(array_map(
  474.                 'intval',
  475.                 explode(',', $request->get('tagsToExcludeIds'))
  476.             )));
  477.              $filters['DolpoTagsToExclude'] = $tagsExclude; 
  478.         }
  479.         
  480.         $returnFields=['o_id', 'ean', 'book_id', 'book_title', 'title_long', 'serie_name', 'serie_dolpo_id', 'book_dolpo_id', 'authors_json', 'babelio_rating', 'babelio_reviews_number', 'dolpo_rating', 'blogger_rating', 'dolpotags_json', 'orderable_status', 'orderable', 'sort'];
  481.         $searchAfter=null;
  482.         $offset = $request->get('offset', 0);
  483.         // Pagination
  484.         $results = $dolpoSearchService->bookSearchFilter($filters, $searchAfter, $offset, $pagination, $returnFields);
  485.         $count = $dolpoSearchService->bookSearchTotal($filters);
  486.         $dolpoRatingLabels = $this->getDolpoRatingLabels();
  487.         foreach ($results as &$result) {
  488.             if (!array_key_exists('dolpo_rating', $result)) {
  489.                 continue;
  490.             }
  491.             $rating = $result['dolpo_rating'];
  492.             if ($rating === null || $rating === '') {
  493.                 continue;
  494.             }
  495.             $ratingKey = (string) ((int) $rating);
  496.             if (array_key_exists($ratingKey, $dolpoRatingLabels)) {
  497.                 $result['dolpo_rating_label'] = $dolpoRatingLabels[$ratingKey];
  498.             }
  499.         }
  500.         unset($result);
  501.         // Ad inside
  502.         $itemsSplitForAd = [];
  503.         /*
  504.         // the ad is finaly not inserted between lines
  505.         if( count($arrayPaginate) > 4 &&  (bool)$request->get("ad") == true ){
  506.             $itemsSplitForAd[] = array_slice($arrayPaginate,0,4);
  507.             $itemsSplitForAd[] = array_slice($arrayPaginate,4);
  508.         }else{*/
  509.         $itemsSplitForAd[] = $results;
  510.         //}
  511.         $params = [
  512.             'tagsToShow' => $request->get('tagsToShow'),
  513.             'tagsToExcludeIds' => $request->get('tagsToExcludeIds'),
  514.             'objectsSelectionIds' => $request->get('objectsSelectionIds'),
  515.             'tagsSelectionIds' => $request->get('tagsSelectionIds'),
  516.             'text_search' => $textSearch,
  517.             'pagination' => $pagination,
  518.             'showAd' => (bool)$request->get('ad'),
  519.             'showCount' => (bool)$request->get('showCount'),
  520.             'title' => $request->get('title'),
  521.             'count' => $count,
  522.             'offsetNext' => (int)($offset + $pagination),
  523.             'uniqueId' => $uniqueId,
  524.             'itemsSplitForAd' => $itemsSplitForAd,
  525.             'fromAjax' => $request->get('uniqueIdForAjax', null),
  526.             'loadNextAjax' => ($count > $offset + $pagination) ? true : false,
  527.             'editmode' => $request->get('editmode'),
  528.         ];
  529.         $view = $this->renderView('default/book_gallery.html.twig', $params);
  530.         return new Response($view);
  531.     }
  532.     private function getDolpoRatingLabels(): array
  533.     {
  534.         $labels = [];
  535.         $collectionDefinition = \Pimcore\Model\DataObject\Fieldcollection\Definition::getByKey('Review');
  536.         if ($collectionDefinition) {
  537.             $fieldDefinitions = $collectionDefinition->getFieldDefinitions();
  538.             if (isset($fieldDefinitions['Rating'])) {
  539.                 $options = $fieldDefinitions['Rating']->getOptions();
  540.                 foreach ($options as $option) {
  541.                     if (isset($option['value'], $option['key'])) {
  542.                         $labels[(string) $option['value']] = (string) $option['key'];
  543.                     }
  544.                 }
  545.             }
  546.         }
  547.         return count($labels) > 0 ? $labels : self::DOLPO_RATING_LABELS_FALLBACK;
  548.     }
  549.     /*
  550.     *Counting lines to better locate "voir plus"
  551.     */
  552.     private function countLines($text, $lineLength)
  553.     {
  554.         $words = explode(' ', $text);
  555.         $linesCount = 1;
  556.         $currentLength = 0;
  557.         foreach ($words as $word) {
  558.             $wordLength = strlen($word);
  559.             if ($currentLength + $wordLength <= $lineLength) {
  560.                 $currentLength += $wordLength + 1; // +1 for the space
  561.             } else {
  562.                 $linesCount++;
  563.                 $currentLength = $wordLength + 1; // Start new line, +1 for the space
  564.             }
  565.             if ($linesCount > 2) {
  566.                 return 3;
  567.             }
  568.         }
  569.         return $linesCount;
  570.     }
  571.     public function bookSelectionWithTagsAction(Request $request): Response
  572.     {
  573.         if ($request->get('type') === 'object') {
  574.             $params = [];
  575.             $item = $this->_getObjectBookSerieOrOnixProduct((int)$request->get('id'));
  576.             if ($item == null) {
  577.                 throw new NotFoundHttpException('Book, Serie or OnixProduct needed');
  578.             }
  579.             $tags = $item->getDolpoTags();
  580.             $mainTags = [];
  581.             $allTags=[];
  582.             if (count($tags) > 0) {
  583.                 foreach ($tags as $tag) {
  584.                     if($tag->getTagType() == 'category') {
  585.                         $allTags[] = $tag->getKey();
  586.                     }
  587.                     $mainDolpoTag = $tag->getMainDolpoTag();
  588.                     if($mainDolpoTag != null) {
  589.                         $key = $mainDolpoTag->getKey();
  590.                         if (!in_array($key, $mainTags)) {
  591.                             $mainTags[] = $key;
  592.                         }
  593.                     }
  594.                 }
  595.                 $tagsForInfo = array_diff($allTags, $mainTags);
  596.             }
  597.             $additionalLinesToDisplay = 3;
  598.             $title = $item->getItemTitle();
  599.             $dolpoReview = $item->getItemReviewDolpo();
  600.             $additionalLinesToDisplay -= ($this->countLines($title, 26) - 1);
  601.             if ($dolpoReview) {
  602.                 $additionalLinesToDisplay -= ($this->countLines($dolpoReview, 32));
  603.             }
  604.             $additionalLinesToDisplay = floor(1.5 * $additionalLinesToDisplay);
  605.             $descriptionSize = strlen($item->getItemDescription());
  606.             $params['item'] = $item;
  607.             $params['tagsToShow'] = $request->get('tagsToShow');
  608.             $params['showCount'] = (bool)$request->get('showCount');
  609.             $params['mainTags'] = $allTags;
  610.             $params['additionalLinesToDisplay'] = $additionalLinesToDisplay;
  611.             $params['descriptionSize'] = $descriptionSize;
  612.             return $this->render('default/book_selection_with_tags.html.twig', $params);
  613.         }
  614.     }
  615.     /*
  616.     -------------------
  617.     AREABRICK RENDERLET
  618.     */
  619.     public function sitemapPageAction(Request $request): Response
  620.     {
  621.         $params = [];
  622.         return $this->render('default/sitemap_page.html.twig', $params);
  623.     }
  624. }