From 7922ba837c38b07263609f24d81ecd584a64da5f Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Wed, 27 Jan 2021 16:47:21 +0100
Subject: [PATCH] hotline MT #126937 improve no current loans page

---
 VERSIONS_HOTLINE/126937                       |  1 +
 .../Library/View/Wrapper/Abstract.php         | 24 +++++++++
 .../View/Wrapper/RichContent/Abstract.php     | 14 +++++
 .../View/Wrapper/RichContent/Section.php      | 19 +++++--
 .../View/Wrapper/User/RichContent/Loans.php   |  7 ++-
 .../Intonation/View/Abonne/AjaxLoans.php      |  2 +-
 .../templates/Intonation/View/Jumbotron.php   | 53 ++++++-------------
 .../Templates/TemplatesAbonneTest.php         |  5 ++
 .../Templates/TemplatesRecordsTest.php        |  4 +-
 9 files changed, 85 insertions(+), 44 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/126937

diff --git a/VERSIONS_HOTLINE/126937 b/VERSIONS_HOTLINE/126937
new file mode 100644
index 00000000000..044beb6c0d9
--- /dev/null
+++ b/VERSIONS_HOTLINE/126937
@@ -0,0 +1 @@
+ - ticket #126937 : Magasin de thèmes : amélioration de l'affichage de la liste des prêts vide.
\ No newline at end of file
diff --git a/library/templates/Intonation/Library/View/Wrapper/Abstract.php b/library/templates/Intonation/Library/View/Wrapper/Abstract.php
index 1cb748f6f42..e9744ae38a3 100644
--- a/library/templates/Intonation/Library/View/Wrapper/Abstract.php
+++ b/library/templates/Intonation/Library/View/Wrapper/Abstract.php
@@ -97,6 +97,22 @@ abstract class Intonation_Library_View_Wrapper_Abstract {
   }
 
 
+  /** @return string */
+  public function getPreviousItem() {
+    return ($rich_content = $this->getRichContent())
+      ? $rich_content->getPreviousItem()
+      : '';
+  }
+
+
+  /** @return string */
+  public function getNextItem() {
+    return ($rich_content = $this->getRichContent())
+      ? $rich_content->getNextItem()
+      : '';
+  }
+
+
   public function inJsSearch() {
     $this->_in_js_search = true;
     return $this;
@@ -135,6 +151,14 @@ abstract class Intonation_Library_View_Wrapper_Abstract {
   }
 
 
+  /** @return string */
+  public function getRichContentActions() {
+    return ($rich_content = $this->getRichContent())
+      ? $rich_content->getActions()
+      : '';
+  }
+
+
   /** @return string */
   abstract public function getMainTitle();
 
diff --git a/library/templates/Intonation/Library/View/Wrapper/RichContent/Abstract.php b/library/templates/Intonation/Library/View/Wrapper/RichContent/Abstract.php
index 905370e73d3..09fb489ac67 100644
--- a/library/templates/Intonation/Library/View/Wrapper/RichContent/Abstract.php
+++ b/library/templates/Intonation/Library/View/Wrapper/RichContent/Abstract.php
@@ -91,6 +91,20 @@ abstract class Intonation_Library_View_Wrapper_RichContent_Abstract {
   }
 
 
+  public function getPreviousItem() {
+    return ($navigation = $this->getNavigation())
+      ? $navigation->getPreviousItem()
+      : null;
+  }
+
+
+  public function getNextItem() {
+    return ($navigation = $this->getNavigation())
+      ? $navigation->getNextItem()
+      : null;
+  }
+
+
   abstract public function getNavigation();
 
   abstract public function getActions();
diff --git a/library/templates/Intonation/Library/View/Wrapper/RichContent/Section.php b/library/templates/Intonation/Library/View/Wrapper/RichContent/Section.php
index 7b5043ff3d8..ab5ab59169d 100644
--- a/library/templates/Intonation/Library/View/Wrapper/RichContent/Section.php
+++ b/library/templates/Intonation/Library/View/Wrapper/RichContent/Section.php
@@ -150,10 +150,21 @@ abstract class Intonation_Library_View_Wrapper_RichContent_Section {
   }
 
 
-  protected function _getJsCallback() {
-    return sprintf("if ($('.%s .jumbotron_section_content [data-ajax-content] > *').length) { $('.%s').removeClass('d-none text-black-50 disabled'); $('.loading_icon').parent().remove(); }",
-                   $this->getClass(),
-                   $this->getClass());
+  protected function _getJsCallback($html = '') {
+    $js = '';
+    if ($class = $this->getClass())
+      $js =
+        sprintf('$(\'.loading_icon\').parent().remove();
+                 if ($(\'.%1$s .jumbotron_section_content [data-ajax-content] > *\').length)
+                   return $(\'.%1$s\').removeClass(\'d-none text-black-50 disabled\');',
+                $class);
+
+    if ($html)
+      $js .= sprintf('$(\'div.%s.wrapper_active:not(.hidde_content)\').parent().prepend(\'%s\');',
+                     $class,
+                     $html);
+
+    return $js;
   }
 
 
diff --git a/library/templates/Intonation/Library/View/Wrapper/User/RichContent/Loans.php b/library/templates/Intonation/Library/View/Wrapper/User/RichContent/Loans.php
index c9f9747018f..6763d7fe1f8 100644
--- a/library/templates/Intonation/Library/View/Wrapper/User/RichContent/Loans.php
+++ b/library/templates/Intonation/Library/View/Wrapper/User/RichContent/Loans.php
@@ -34,10 +34,15 @@ class Intonation_Library_View_Wrapper_User_RichContent_Loans extends Intonation_
     if ($this->_content)
       return $this->_content;
 
+    $js =
+      $this->_getJsCallback($this->_view->tag('p',
+                                              $this->_('Pas de prêt en cours.'),
+                                              ['class' => 'empty_list ' . $this->getClass()]));
+
     return $this->_content = $this->_view->renderAjax('abonne',
                                                       'ajax-loans',
                                                       ['id' => $this->_model->getId()],
-                                                      $this->_getJsCallback());
+                                                      $js);
   }
 
 
diff --git a/library/templates/Intonation/View/Abonne/AjaxLoans.php b/library/templates/Intonation/View/Abonne/AjaxLoans.php
index d3823d7bdbc..ee2bb9fe290 100644
--- a/library/templates/Intonation/View/Abonne/AjaxLoans.php
+++ b/library/templates/Intonation/View/Abonne/AjaxLoans.php
@@ -78,6 +78,6 @@ class Intonation_View_Abonne_AjaxLoans extends ZendAfi_View_Helper_BaseHelper {
                                   $this->view->tag('h3', $this->_('Historique des prêts'))
                                   . $history);
 
-    return $this->view->grid(implode($html));
+    return $this->view->grid($html);
   }
 }
\ No newline at end of file
diff --git a/library/templates/Intonation/View/Jumbotron.php b/library/templates/Intonation/View/Jumbotron.php
index fc4ed39fec4..087008c9d4d 100644
--- a/library/templates/Intonation/View/Jumbotron.php
+++ b/library/templates/Intonation/View/Jumbotron.php
@@ -23,14 +23,22 @@
 class Intonation_View_Jumbotron extends ZendAfi_View_Helper_BaseHelper {
 
   protected
+    $_rich_content,
+    $_sections,
     $_element,
     $_current_url;
 
 
   public function jumbotron($element) {
     $this->_element = $element;
+
+    if ( ! $this->_rich_content = $element->getRichContent())
+      return '';
+
     $this->_current_url = $this->view->url();
 
+    $this->_sections = $this->_rich_content->getSections();
+
     $content = $this->_renderContent();
 
     $html = [$this->_renderHead(),
@@ -62,31 +70,19 @@ class Intonation_View_Jumbotron extends ZendAfi_View_Helper_BaseHelper {
     $html [] = $this->_div(['class' => 'jumbotron_nav'], $this->_nav());
 
     return $this->_div(['class' => 'bokeh_jumbotron'],
-                       $this->view->grid(implode($html),
+                       $this->view->grid($html,
                                          [],
                                          ['class' => 'justify-content-center']));
   }
 
 
   protected function _getPreviousItem() {
-    if (!$rich_content = $this->_element->getRichContent())
-      return;
-
-    if (!$navigation = $rich_content->getNavigation())
-      return;
-
-    return $navigation->getPreviousItem();
+    return $this->_element->getPreviousItem();
   }
 
 
   protected function _getNextItem() {
-    if (!$rich_content = $this->_element->getRichContent())
-      return;
-
-    if (!$navigation = $rich_content->getNavigation())
-      return;
-
-    return $navigation->getNextItem();
+    return $this->_element->getNextItem();
   }
 
 
@@ -106,34 +102,22 @@ class Intonation_View_Jumbotron extends ZendAfi_View_Helper_BaseHelper {
 
 
   protected function _actions() {
-    if (!$rich_content = $this->_element->getRichContent())
-      return;
-
-    return $rich_content->getActions();
+    return $this->_element->getRichContentActions();
   }
 
 
   protected function _getRowActions() {
     $html = $this->view->div(['class' => 'inspector_gadget'], '');
-
-    if (!$rich_content = $this->_element->getRichContent())
-      return $html;
-
-    return $rich_content->getRowActions() . $html;
+    return $this->_rich_content->getRowActions() . $html;
   }
 
 
   protected function _nav() {
-    if (!$rich_content = $this->_element->getRichContent())
-      return;
-
-    $sections = $rich_content->getSections();
-
     $html = array_map(function($item)
                       {
                         return $this->_renderNavItem($item);
                       },
-                      $sections);
+                      $this->_sections);
 
     return $this->_tag('ul',
                        implode($html),
@@ -163,18 +147,13 @@ class Intonation_View_Jumbotron extends ZendAfi_View_Helper_BaseHelper {
 
 
   protected function _renderContent() {
-    if (!$rich_content = $this->_element->getRichContent())
-      return;
-
-    $sections = $rich_content->getSections();
-
     $html = array_map(function($item)
                       {
                         return $this->_renderSection($item);
                       },
-                      $sections);
+                      $this->_sections);
 
-    $html = $this->_div(['class' => 'jumbotron_content'], $this->view->grid(implode($html)));
+    $html = $this->_div(['class' => 'jumbotron_content'], $this->view->grid($html));
 
     return $this->view->grid($html,
                              [],
diff --git a/tests/scenarios/Templates/TemplatesAbonneTest.php b/tests/scenarios/Templates/TemplatesAbonneTest.php
index e996753107f..1746c612430 100644
--- a/tests/scenarios/Templates/TemplatesAbonneTest.php
+++ b/tests/scenarios/Templates/TemplatesAbonneTest.php
@@ -247,6 +247,11 @@ class TemplatesDispatchAbonneLoansTest extends TemplatesIntonationAccountTestCas
     $this->assertXPath('//div[@class="loading_icon spinner-border"]');
   }
 
+
+  /** @test */
+  public function scriptShouldContainsNoLoanMessage() {
+    $this->assertXPathContentContains('//script', '$(\'div.user_loans.wrapper_active:not(.hidde_content)\').parent().prepend(\'<p class="empty_list user_loans">Pas de pr');
+  }
 }
 
 
diff --git a/tests/scenarios/Templates/TemplatesRecordsTest.php b/tests/scenarios/Templates/TemplatesRecordsTest.php
index 1d9057b3803..99890327dd0 100644
--- a/tests/scenarios/Templates/TemplatesRecordsTest.php
+++ b/tests/scenarios/Templates/TemplatesRecordsTest.php
@@ -371,6 +371,8 @@ class TemplatesDispatchRecordItemsTest extends TemplatesRecordsWidgetTestCase {
   /** @test */
   public function shouldRenderAjaxLoadWithInitializePopupAndSetupAnchorTarget() {
     $this->dispatch('/record/items/id/1/id_profil/1');
-    $this->assertXPathContentContains('//script', 'load("/noticeajax/author/id/1/id_profil/1", function() {if ($(\'.document_author .jumbotron_section_content [data-ajax-content] > *\').length) { $(\'.document_author\').removeClass(\'d-none text-black-50 disabled\'); $(\'.loading_icon\').parent().remove(); }initializePopups();setupAnchorsTarget();})');
+    $this->assertXPathContentContains('//script', '.load("/noticeajax/author/id/1/id_profil/1", function() {$(\'.loading_icon\').parent().remove();
+                 if ($(\'.document_author .jumbotron_section_content [data-ajax-content] > *\').length)
+                   return $(\'.document_author\').removeClass(\'d-none text-black-50 disabled\');initializePopups();setupAnchorsTarget();});');
   }
 }
-- 
GitLab