From b47eefd26190609148fa84c1f13bac852cf86ab8 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Tue, 30 Oct 2018 10:33:22 +0100
Subject: [PATCH] dev #78660 improve dashboard thumbnailing

---
 library/Class/Album.php                       | 21 +++++++++++++++++++
 library/Class/DigitalResource/Config.php      |  2 +-
 .../DigitalResource/Dashboard/Harvest.php     |  6 +++---
 .../tests/EntrepotNumeriqueMelunTest.php      | 12 ++++++++---
 4 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/library/Class/Album.php b/library/Class/Album.php
index 77f8dd5bee6..00327be53a7 100644
--- a/library/Class/Album.php
+++ b/library/Class/Album.php
@@ -54,6 +54,10 @@
  */
 
 class AlbumLoader extends Storm_Model_Loader {
+
+  use Trait_MemoryCleaner;
+
+
   public function getItemsOf($categoryId) {
     return Class_Album::findAllBy([
                                    'cat_id' => Class_AlbumCategorie::getAllCatAndSubCat([$categoryId]),
@@ -61,6 +65,23 @@ class AlbumLoader extends Storm_Model_Loader {
                                    'limit' => 1000
                                    ]);
   }
+
+
+  public function findFirstWithPosterBy($params) {
+    while($albums = Class_Album::findAllBy(array_merge($params,
+                                                       ['limitPage'=> [($page = 1), 100]]))) {
+
+      if ($album = (new Storm_Model_Collection($albums))
+        ->detect(function($model)
+                 {
+                   return $model->hasPoster();
+                 }))
+        return $album;
+
+      $this->_cleanMemory();
+      $page++;
+    }
+  }
 }
 
 
diff --git a/library/Class/DigitalResource/Config.php b/library/Class/DigitalResource/Config.php
index 4e8ee87a00b..a6e819cee89 100644
--- a/library/Class/DigitalResource/Config.php
+++ b/library/Class/DigitalResource/Config.php
@@ -87,7 +87,7 @@ class Class_DigitalResource_Config extends Class_Entity {
 
 
   public function isHarvestingEnabled() {
-    Class_Batch::findFirstBy(['type' => $this->getBatch()]);
+    return Class_Batch::findFirstBy(['type' => $this->getBatch()]);
   }
 
 
diff --git a/library/ZendAfi/View/Helper/DigitalResource/Dashboard/Harvest.php b/library/ZendAfi/View/Helper/DigitalResource/Dashboard/Harvest.php
index 7d84ca80fea..e3f8e6aa9dd 100644
--- a/library/ZendAfi/View/Helper/DigitalResource/Dashboard/Harvest.php
+++ b/library/ZendAfi/View/Helper/DigitalResource/Dashboard/Harvest.php
@@ -66,7 +66,7 @@ class ZendAfi_View_Helper_DigitalResource_Dashboard_Harvest extends ZendAfi_View
   protected function _getBatchHtml($config) {
     $batch_name = $config->getBatch();
 
-    if(!$batch = Class_Batch::findFirstBy(['type' => $batch_name]))
+    if(!$batch = $config->isHarvestingEnabled())
       return implode([$this->_tagWarning($this->_('Le moissonnage n\'est pas programmé')),
                       $this->view->button((new Class_Entity())
                                           ->setText($this->_('Activer le moissonnage'))
@@ -155,8 +155,8 @@ class ZendAfi_View_Helper_DigitalResource_Dashboard_Harvest extends ZendAfi_View
     if(!$count = $config->countAlbums())
       return $this->_tagWarning($this->_('Aucun album présent pour cette ressource'));
 
-    $first_album = Class_Album::findFirstBy(['type_doc_id' => $config->getDocType(),
-                                             'order' => 'titre asc']);
+    $first_album = Class_Album::findFirstWithPosterBy(['type_doc_id' => $config->getDocType(),
+                                                       'order' => 'titre asc']);
 
     $albums_link = $this->_tagAnchor($this->view->absoluteUrl(['module' => 'admin',
                                                                'controller' => 'album',
diff --git a/library/digital_resources/EntrepotNumeriqueMelun/tests/EntrepotNumeriqueMelunTest.php b/library/digital_resources/EntrepotNumeriqueMelun/tests/EntrepotNumeriqueMelunTest.php
index 76e30bcc140..d31ccb4beb1 100644
--- a/library/digital_resources/EntrepotNumeriqueMelun/tests/EntrepotNumeriqueMelunTest.php
+++ b/library/digital_resources/EntrepotNumeriqueMelun/tests/EntrepotNumeriqueMelunTest.php
@@ -90,10 +90,17 @@ class EntrepotNumeriqueMelunDashboardActivatedTest extends EntrepotNumeriqueMelu
                                                          'login' => 'admin',
                                                          'password' => 'admin',
                                                          'role_level' => ZendAfi_Acl_AdminControllerRoles::SUPER_ADMIN]));
+    Class_Album::getLoader()->setMemoryCleaner(function() {});
     $this->dispatch('/EntrepotNumeriqueMelun_Plugin', true);
   }
 
 
+  public function tearDown() {
+    Class_Album::getLoader()->setMemoryCleaner(null);
+    parent::tearDown();
+  }
+
+
   /** @test */
   public function shouldDisplayActivated() {
     $this->assertXPathContentContains('//button', 'Activé');
@@ -101,9 +108,8 @@ class EntrepotNumeriqueMelunDashboardActivatedTest extends EntrepotNumeriqueMelu
 
 
   /** @test */
-  public function urlSsoTitleBeDisplay() {
-    $this->assertXPathContentContains('//h4', 'URL SSO générée par /modules/' . Class_DigitalResource::getInstance()
-                                      ->getSsoActionForPlugin('EntrepotNumeriqueMelun') . ' pour l\'utilisateur "EntrepotNumeriqueMelun_test_user"');
+  public function urlSsoShouldNotBeHandled() {
+    $this->assertXPathContentContains('//p', 'Cette ressource ne prend pas en charge la connexion SSO');
   }
 
 
-- 
GitLab