From 1463d3652501e7571321fe7a36719de66acc71d0 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Thu, 25 Jul 2019 15:51:12 +0200
Subject: [PATCH] hotline #95535 Authors widget: improve thumbnail fetching &
 authors count to display

---
 VERSIONS_HOTLINE/95535                        |  1 +
 .../ZendAfi/View/Helper/Accueil/Authors.php   | 40 ++++++++++++-------
 .../scenarios/AuthorPage/AuthorWidgetTest.php |  1 +
 3 files changed, 28 insertions(+), 14 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/95535

diff --git a/VERSIONS_HOTLINE/95535 b/VERSIONS_HOTLINE/95535
new file mode 100644
index 00000000000..a0b6cb98e10
--- /dev/null
+++ b/VERSIONS_HOTLINE/95535
@@ -0,0 +1 @@
+ - ticket #95535 : Boite auteurs: meilleure prise en compte du nombre d'auteurs à afficher
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Accueil/Authors.php b/library/ZendAfi/View/Helper/Accueil/Authors.php
index 4a5fb2e0b6d..c8e517641f6 100644
--- a/library/ZendAfi/View/Helper/Accueil/Authors.php
+++ b/library/ZendAfi/View/Helper/Accueil/Authors.php
@@ -20,6 +20,9 @@
  */
 
 class ZendAfi_View_Helper_Accueil_Authors extends ZendAfi_View_Helper_Accueil_Base {
+  const RECORDS_LOAD_LIMIT = 500;
+  const AUTHORS_FETCH_BIO_LIMIT = 100;
+
   public function getHTML() {
     $this->titre = $this->preferences['titre'];
     $this->contenu = $this->_renderContent();
@@ -35,11 +38,15 @@ class ZendAfi_View_Helper_Accueil_Authors extends ZendAfi_View_Helper_Accueil_Ba
 
 
   public function refreshAuthorsWithoutThumbnail() {
-    $authors = array_filter($this->_findAuthorsFromPreferences(),
-                            function($author)
-                            {
-                              return !$author->isThumbnailValid();
-                            });
+    $authors = array_slice(
+                           array_filter($this->_findAuthorsFromPreferences(false),
+                                        function($author)
+                                        {
+                                          return !$author->isThumbnailValid();
+                                        }),
+                           0,
+                           static::AUTHORS_FETCH_BIO_LIMIT);
+
     array_map(function($author)
               {
                 (new Class_CodifAuteur_Description($author))->fetchBiography();
@@ -74,9 +81,10 @@ class ZendAfi_View_Helper_Accueil_Authors extends ZendAfi_View_Helper_Accueil_Ba
   }
 
 
-  protected function _findAuthorsFromPreferences() {
+  protected function _findAuthorsFromPreferences($with_thumbnail_only = true) {
+    $this->preferences['nb_notices'] = static::RECORDS_LOAD_LIMIT;
     $records = Class_Notice::getNoticesFromPreferences($this->preferences);
-    return $this->_getAuthorsFromRecords($records);
+    return $this->_getAuthorsFromRecords($records, $with_thumbnail_only);
   }
 
 
@@ -95,14 +103,18 @@ class ZendAfi_View_Helper_Accueil_Authors extends ZendAfi_View_Helper_Accueil_Ba
   }
 
 
-  protected function _getAuthorsFromRecords($records) {
-    $authors_ids = $this->_getAuthorsIdsFromRecords($records);
+  protected function _getAuthorsFromRecords($records, $with_thumbnail_only) {
+    if (!$authors_ids = $this->_getAuthorsIdsFromRecords($records))
+      return [];
+
+    $params = ['id_auteur' => $authors_ids];
+
+    if ($with_thumbnail_only) {
+      $params['where'] = 'thumbnail_url > \'\'';
+      $params['limit'] = $this->preferences['authors_count'];
+    }
 
-    return $authors_ids
-      ? Class_CodifAuteur::findAllBy(['id_auteur' => array_slice($authors_ids,
-                                                                 0,
-                                                                 $this->preferences['authors_count'])])
-      : [];
+    return Class_CodifAuteur::findAllBy($params);
   }
 
 
diff --git a/tests/scenarios/AuthorPage/AuthorWidgetTest.php b/tests/scenarios/AuthorPage/AuthorWidgetTest.php
index d271480cad6..4c09bc448b1 100644
--- a/tests/scenarios/AuthorPage/AuthorWidgetTest.php
+++ b/tests/scenarios/AuthorPage/AuthorWidgetTest.php
@@ -184,6 +184,7 @@ class AuthorWidgetOnPageTest extends AuthorWidgetOnPageTestCase {
                   {
                     $this->assertEquals(3, $preferences['id_panier']);
                     $this->assertEquals(10, $preferences['authors_count']);
+                    $this->assertEquals(500, $preferences['nb_notices']);
                     $this->assertEquals(Class_Systeme_ModulesAccueil_Authors::AUTHORS_SELECTION_MODE_ALL,
                                         $preferences['authors_selection_mode']);
                     return Class_Notice::findAll();
-- 
GitLab