<?php
namespace App\Controller;
use App\Service\DolpoSearchService;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Author;
use Pimcore\Model\Staticroute;
// ADDED
use Pimcore\Model\DataObject\DolpoTag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class AuthorGridController extends FrontendController
{
private $cachetime=86400;
/**
* @param Request $request
*
* @return Response
*/
public function singleAction(Request $request, DolpoSearchService $dolpoSearchService): Response
{
$category = $request->get('category');
$AllTopTags = DolpoTag::getByTagType('best');
//dump($category);
$cacheKey = 'topauthorsPage_.'.$category;
$forceRefresh = $request->query->get('refresh');
if (!$forceRefresh && $pageauteur = \Pimcore\Cache::load($cacheKey)) {
//dump('find');
return new Response($pageauteur);
}
$TopAuteurTag = null;
foreach ($AllTopTags as $tag) {
$MainDolpoTag = $tag->getMainDolpoTag();
if ($MainDolpoTag != null) {
$path = $MainDolpoTag->getPage();
$lastPartLowercase = strtolower(basename($path));
if($category == $lastPartLowercase) {
$TopAuteurTag = $tag;
}
}
}
if($TopAuteurTag != null) {
$filters = ['DolpoTags' => [$TopAuteurTag->getId()]];
$authors = $dolpoSearchService->authorSearchFilter($filters, 0, 5000);
$authorsDetails = [];
$author_objs=[];
foreach($authors as $author_arr) {
$author_id=$author_arr['o_id'];
$auteur=Author::getById($author_id);
if(!empty($auteur)) {
$author_objs[]=$auteur;
//tous les livres
$bookslist = $auteur->getAllBooks($dolpoSearchService);
$booksNumber = count($bookslist);
$awardsCount=0;
$best_three_books=[];
$buildEditionUrl = static function (?string $ean): string {
if ($ean === null || $ean === '') {
return '';
}
$route = Staticroute::getByName('edition');
if ($route === null) {
return '';
}
return $route->assemble(['isbn' => $ean], true);
};
$buildThumbnailUrl = static function (?string $ean): string {
if ($ean === null || $ean === '') {
return '';
}
return '/var/img_cache/thumbnails/livre/' . $ean . '-xs.webp';
};
foreach ($bookslist as $book) {
$tags = is_array($book['tags'] ?? null) ? $book['tags'] : [];
foreach ($tags as $tag) {
if (is_array($tag) && array_key_exists('tagtype', $tag) && $tag['tagtype'] === 'prix') {
$awardsCount++;
}
if (is_array($tag)
&& array_key_exists('name', $tag)
&& $tag['name'] === "Ouvrage de référence de l'auteur"
&& count($best_three_books) < 3
) {
$ean = $book['ean'] ?? null;
$best_three_books[] = [
'url' => $buildEditionUrl($ean ? (string) $ean : null),
'thumbnail' => $buildThumbnailUrl($ean ? (string) $ean : null),
'title' => $book['title'] ?? '',
];
}
}
}
$top_tags = $auteur->getAuthorBookNumberByCategory($dolpoSearchService);
$authorsDetails[$author_id] = [
'booksNumber' => $booksNumber,
'awardsCount' => $awardsCount,
'best_three_books' => $best_three_books,
'top_tags' => $top_tags,
];
}
}
}
$params = [
'authors' => $author_objs ?? [],
'authorsDetails'=>$authorsDetails ?? [],
];
$pageauteur=$this->renderView('author-grid/single.html.twig', $params);
$l=\Pimcore\Cache::save(
$pageauteur,
$cacheKey,
['page_auteur'],
$this->cachetime,
10,
true // 1sem | 86400 sec = 24h // 1800 sec = 30min
);
//dump($l);
return new Response($pageauteur);
}
}