From 62d2bc69a6eae5e8b11a23615408ab4a9c0c7fa6 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Wed, 18 Nov 2015 17:25:33 +0100
Subject: [PATCH] hotline #32189 fix reviews pagination

---
 VERSIONS_HOTLINE/32189                        |   1 +
 .../admin/controllers/ModoController.php      |  61 ++++++----
 .../admin/views/scripts/modo/avisnotice.phtml |  12 +-
 library/Class/AvisNotice.php                  | 105 ++++++++++--------
 library/ZendAfi/View/Helper/AvisBloc.php      |  13 ++-
 .../admin/controllers/ModoControllerTest.php  |  42 +++++--
 6 files changed, 149 insertions(+), 85 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/32189

diff --git a/VERSIONS_HOTLINE/32189 b/VERSIONS_HOTLINE/32189
new file mode 100644
index 00000000000..38b5a7f507c
--- /dev/null
+++ b/VERSIONS_HOTLINE/32189
@@ -0,0 +1 @@
+ - ticket #32189 : Correction de la pagination dans les modérations des notices.
\ No newline at end of file
diff --git a/application/modules/admin/controllers/ModoController.php b/application/modules/admin/controllers/ModoController.php
index 7e6867a2722..4966ad26223 100644
--- a/application/modules/admin/controllers/ModoController.php
+++ b/application/modules/admin/controllers/ModoController.php
@@ -55,32 +55,53 @@ class Admin_ModoController extends ZendAfi_Controller_Action {
 
 
   protected function subviewForStatus($status) {
-    $page = $this->_getParam('page',1);
-    $nb_bib=Class_AvisNotice::countBiblioReviews($status);
-    $nb_abo=Class_AvisNotice::countAboReviews($status);
-    $display_per_page=$page*10;
-    $avis_bib = Class_AvisNotice::findBiblioReviews($status,
-                                                    [($nb_bib<=$display_per_page)? 1: $page,10]);
-
-    $avis_abo = Class_AvisNotice::findAboReviews($status,
-                                                 [($nb_abo<=$display_per_page)? 1 : $page,10]);
-    $nb_orphans=Class_AvisNotice::countOrphanReviews($status);
-    $nb_archived=Class_AvisNotice::countArchivedReviews($status);
-    $orphans = Class_AvisNotice::findOrphanReviews($status,[($nb_orphans<=$display_per_page)? 1: $page,10]);
-    $archived = Class_AvisNotice::findArchivedReviews($status,[($nb_archived<=$display_per_page)? 1: $page,10]);
+    $page = $this->_getParam('page', 1);
+    $active_tab = $this->_getParam('active_tab', 0);
+
+    $nb_bib = Class_AvisNotice::countBiblioReviews($status);
+    $nb_abo = Class_AvisNotice::countAboReviews($status);
+    $nb_orphans = Class_AvisNotice::countOrphanReviews($status);
+    $nb_archived = Class_AvisNotice::countArchivedReviews($status);
+
+    $next_page = [$page, 10];
+    $first_page = [1, 10];
+
+    $next_page_biblio = ( 0 == $active_tab
+                          ? $next_page
+                          : $first_page);
+
+    $next_page_abo = ( 1 == $active_tab
+                       ? $next_page
+                       : $first_page);
+
+    $next_page_orphans = ( 2 == $active_tab
+                           ? $next_page
+                           : $first_page);
+
+    $next_page_arhived = ( 3 == $active_tab
+                           ? $next_page
+                           : $first_page);
+
+    $avis_bib = Class_AvisNotice::findBiblioReviews($status, $next_page_biblio);
+    $avis_abo = Class_AvisNotice::findAboReviews($status, $next_page_abo);
+    $orphans = Class_AvisNotice::findOrphanReviews($status, $next_page_orphans);
+    $archived = Class_AvisNotice::findArchivedReviews($status, $next_page_arhived);
 
     $this->view->subview = $this->view->partial('modo/avisnotice.phtml',
-                                                ['list_avis_bibliothecaires' => $avis_bib,
-                                                 'list_avis_utilisateurs' => $avis_abo,
-                                                 'list_orphan_reviews' => $orphans,
-                                                 'list_archived_reviews' => $archived,
-                                                 'display_all' => $status,
+                                                ['display_all' => $status,
+                                                 'page' => $page,
+
                                                  'nb_bib' => $nb_bib,
                                                  'nb_abo' => $nb_abo,
                                                  'nb_orphans' => $nb_orphans,
                                                  'nb_archived' => $nb_archived,
-                                                 'active_tab' => $this->_getParam('active_tab', 0),
-                                                 'page' => $page]);
+
+                                                 'list_avis_bibliothecaires' => $avis_bib,
+                                                 'list_avis_utilisateurs' => $avis_abo,
+                                                 'list_orphan_reviews' => $orphans,
+                                                 'list_archived_reviews' => $archived,
+
+                                                 'active_tab' => $active_tab]);
 
 }
   public function allreviewsAction() {
diff --git a/application/modules/admin/views/scripts/modo/avisnotice.phtml b/application/modules/admin/views/scripts/modo/avisnotice.phtml
index 0706f160451..5443bf7c18c 100644
--- a/application/modules/admin/views/scripts/modo/avisnotice.phtml
+++ b/application/modules/admin/views/scripts/modo/avisnotice.phtml
@@ -13,25 +13,29 @@ echo $this->tag('div',
                                 $this->list_avis_bibliothecaires,
                                 $this->page,
                                 $this->nb_bib,
-                                0) .
+                                0,
+                                $this->active_tab) .
 
                 $this->avisBloc($this->_('Avis d\'abonnés (%s)', $this->nb_abo),
                                 $this->list_avis_utilisateurs,
                                 $this->page,
                                 $this->nb_abo,
-                                1) .
+                                1,
+                                $this->active_tab) .
 
                 $this->avisBloc($this->_('Avis orphelins (%s)', $this->nb_orphans),
                                 $this->list_orphan_reviews,
                                 $this->page,
                                 $this->nb_orphans,
-                                2) .
+                                2,
+                                $this->active_tab) .
 
                 $this->avisBloc($this->_('Avis archivés (%s)', $this->nb_archived),
                                 $this->list_archived_reviews,
                                 $this->page,
                                 $this->nb_archived,
-                                3),
+                                3,
+                                $this->active_tab),
 
                 ['id' => 'avis-notice-liste']);
 ?>
diff --git a/library/Class/AvisNotice.php b/library/Class/AvisNotice.php
index c33fd68292d..4ca340b6e28 100644
--- a/library/Class/AvisNotice.php
+++ b/library/Class/AvisNotice.php
@@ -20,6 +20,7 @@
  */
 
 class AvisNoticeLoader extends Storm_Model_Loader {
+
   protected function _addCatalogueConditions($preferences, &$params) {
     // voir fonction Class_Catalogue::getReguetes
     $preferences["nb_notices"] = null; // charge toutes les notices avec avis
@@ -42,77 +43,83 @@ class AvisNoticeLoader extends Storm_Model_Loader {
     return $this;
   }
 
-  public function findOrphanReviews($status, $limitPage=null) {
-    $req = ['flags' => Class_AvisNotice::ORPHAN_FLAG,
-            'order' => 'date_avis desc',
-            'statut' => $status];
-    if ($limitPage)
-      $req['limitPage'] = $limitPage;
-    return Class_AvisNotice::findAllBy($req);
+
+  public function findOrphanReviews($status, $limitPage) {
+    return self::findReviews($status,
+                             $limitPage,
+                             Class_AvisNotice::ORPHAN_FLAG,
+                             null);
   }
 
-  public function findBiblioReviews($status, $limitPage=null) {
-    $req = ['flags' => Class_AvisNotice::NO_FLAG,
-            'abon_ou_bib' => Trait_Avis::$AVIS_BIBLIO,
-            'order' => 'date_avis desc',
-            'statut' => $status];
-    if ($limitPage)
-      $req['limitPage'] = $limitPage;
-    return Class_AvisNotice::findAllBy($req);
+
+  public function findBiblioReviews($status, $limitPage) {
+    return self::findReviews($status,
+                             $limitPage,
+                             Class_AvisNotice::NO_FLAG,
+                             Trait_Avis::$AVIS_BIBLIO);
   }
 
-  public function countBiblioReviews($status) {
-    $req = ['flags' => Class_AvisNotice::NO_FLAG,
-            'abon_ou_bib' => Trait_Avis::$AVIS_BIBLIO,
-            'statut' => $status];
-    return Class_AvisNotice::countBy($req);
+
+  public function findAboReviews($status, $limitPage) {
+    return self::findReviews($status,
+                             $limitPage,
+                             Class_AvisNotice::NO_FLAG,
+                             Trait_Avis::$AVIS_ABONNE);
   }
 
+
+  public function findArchivedReviews($status,$limitPage) {
+    return self::findReviews($status,
+                             $limitPage,
+                             Class_AvisNotice::ARCHIVED_FLAG,
+                             null);
+  }
+
+
+  protected function findReviews($status, $limit_page, $flag, $avis) {
+    return Class_AvisNotice::findAllBy(['flags' => $flag,
+                                        'abon_ou_bib' => $avis,
+                                        'order' => 'date_avis desc',
+                                        'statut' => $status,
+                                        'limitPage' => $limit_page]);
+  }
+
+
   public function countAboReviews($status) {
-    $req = ['flags' => Class_AvisNotice::NO_FLAG,
-            'abon_ou_bib' => Trait_Avis::$AVIS_ABONNE,
-            'statut' => $status];
-    return Class_AvisNotice::countBy($req);
+    return self::countReviews($status,
+                              Class_AvisNotice::NO_FLAG,
+                              Trait_Avis::$AVIS_ABONNE);
   }
 
 
-  public function findAboReviews($status, $limitPage=null) {
-    $req = ['flags' => Class_AvisNotice::NO_FLAG,
-            'abon_ou_bib' => Trait_Avis::$AVIS_ABONNE,
-            'order' => 'date_avis desc',
-            'statut' => $status];
-    if ($limitPage)
-      $req['limitPage'] = $limitPage;
-    return Class_AvisNotice::findAllBy($req);
+  public function countBiblioReviews($status) {
+    return self::countReviews($status,
+                              Class_AvisNotice::NO_FLAG,
+                              Trait_Avis::$AVIS_BIBLIO);
   }
 
 
   public function countOrphanReviews($status) {
-    $req = ['flags' => Class_AvisNotice::ORPHAN_FLAG,
-            'order' => 'date_avis desc',
-            'statut' => $status];
-
-    return Class_AvisNotice::countBy($req);
+    return self::countReviews($status,
+                              Class_AvisNotice::ORPHAN_FLAG,
+                              null);
   }
 
-  public function countArchivedReviews($status) {
-    $req = ['flags' => Class_AvisNotice::ARCHIVED_FLAG,
-            'order' => 'date_avis desc',
-            'statut' => $status];
 
-    return Class_AvisNotice::countBy($req);
+  public function countArchivedReviews($status) {
+    return self::countReviews($status,
+                              Class_AvisNotice::ARCHIVED_FLAG,
+                              null);
   }
 
-  public function findArchivedReviews($status,$limitPage=null) {
-    $req = ['flags' => Class_AvisNotice::ARCHIVED_FLAG,
-            'order' => 'date_avis desc',
-            'statut' => $status];
-    if ($limitPage)
-      $req['limitPage'] = $limitPage;
 
-    return Class_AvisNotice::findAllBy($req);
+  protected function countReviews($status, $flag, $avis) {
+    return Class_AvisNotice::countBy(['statut' => $status,
+                                      'flags' => $flag,
+                                      'abon_ou_bib' => $avis]);
   }
 
+
   protected function _addStatutAbonBibWhereClause($abon_ou_bib, &$params) {
     $modo_avis_abo = Class_AdminVar::get('MODO_AVIS');       // 0 apres / 1 avant de publier sur le site
     $modo_avis_bib = Class_AdminVar::get('MODO_AVIS_BIBLIO');   // 0 apres / 1 avant de publier sur le site
diff --git a/library/ZendAfi/View/Helper/AvisBloc.php b/library/ZendAfi/View/Helper/AvisBloc.php
index 33a394501fa..22ee7bf9ff4 100644
--- a/library/ZendAfi/View/Helper/AvisBloc.php
+++ b/library/ZendAfi/View/Helper/AvisBloc.php
@@ -22,11 +22,11 @@
 class ZendAfi_View_Helper_AvisBloc extends Zend_View_Helper_HtmlElement {
   protected $_label, $_list, $_page, $_nb_items, $_index;
 
-  public function avisBloc($label, $list, $page = 0, $nb_items = 0, $index = 0) {
+  public function avisBloc($label, $list, $page = 0, $nb_items = 0, $index = 0, $active_tab = -1) {
 
     $this->_label = $label;
     $this->_list = $list;
-    $this->_page = $page;
+    $this->_page = ($index == $active_tab  ? $page : 0 );
     $this->_nb_items = $nb_items;
     $this->_index = $index;
 
@@ -57,12 +57,19 @@ class ZendAfi_View_Helper_AvisBloc extends Zend_View_Helper_HtmlElement {
     if (!0 < $this->_page)
       return $html;
 
+    $nb_items_on_page = count($this->_list);
+
     $pager = $this->view->Pager($this->_nb_items,
                                 10,
                                 $this->_page,
                                 $this->view->url(['page' => null, 'active_tab' => $this->_index],
                                                  null, false));
-    return $this->view->tag('span', $this->view->_('10 résultats par page'), ['style' => 'display: bloc; text-align: right;'])
+    return $this->view->tag('span', $this->view->_plural($nb_items_on_page,
+                                                         'Il n\'y a pas de résultat sur cette page.',
+                                                         'Il y a %s résultat sur cette page.',
+                                                         'Il y a %s résultats sur cette page.',
+                                                         $nb_items_on_page),
+                            ['style' => 'display: bloc; text-align: right;'])
       . $pager
       . $html
       . $pager;
diff --git a/tests/application/modules/admin/controllers/ModoControllerTest.php b/tests/application/modules/admin/controllers/ModoControllerTest.php
index 6c069498d45..2a2906b1a4c 100644
--- a/tests/application/modules/admin/controllers/ModoControllerTest.php
+++ b/tests/application/modules/admin/controllers/ModoControllerTest.php
@@ -56,7 +56,8 @@ abstract class ModoControllerIndexActionTestCase extends Admin_AbstractControlle
                                         'note'=> 2,
                                         'id_user' => null,
                                         'avis' => 'Ce livre est vraiment bien !',
-                                        'statut' => 0]);
+                                        'statut' => 0,
+                                        'abon_ou_bib' => 1]);
 
     $this->fixture('Class_AvisNotice', ['id' => 223,
                                         'id_notice' => 1002,
@@ -66,7 +67,8 @@ abstract class ModoControllerIndexActionTestCase extends Admin_AbstractControlle
                                         'flags' => 0,
                                         'avis' => ' Pour faire aimer la biere aux enfants!',
                                         'id_notice' => 1032,
-                                        'statut' => 1]);
+                                        'statut' => 1,
+                                        'abon_ou_bib' => 1]);
 
     $this->fixture('Class_Notice', ['id' => 1032,
                                     'titre_principal' => 'B comme bière : la bière expliquée aux (grands) enfants']);
@@ -96,12 +98,14 @@ abstract class ModoControllerIndexActionTestCase extends Admin_AbstractControlle
   protected function generateReviews() {
     for($i = 110; $i <= 139; $i++)
       $this->fixture('Class_AvisNotice', ['id' => $i,
-                                          'id_notice' => 1002,
-                                          'entete' => 'Bulles',
+                                          'id_notice' => 1032,
+                                          'entete' => 'Bulles n°' . $i,
                                           'note'=> 2,
                                           'id_user' => null,
                                           'flags' => 0,
-                                          'avis' => ' Pour faire aimer la biere aux enfants!',
+                                          'avis' => $i . ' Pour faire aimer la biere aux enfants!',
+                                          'abon_ou_bib' => 0,
+                                          'date_avis' => $i,
                                           'id_notice' => 1032,
                                           'statut' => 1]);
   }
@@ -690,6 +694,18 @@ class ModoControllerAllReviewsActionTest extends ModoControllerIndexActionTestCa
   public function paginatorShouldBePresent() {
     $this->assertXPath('//div[@class="pager"]/span/a[contains(@href, "/active_tab/1/page/3")]');
   }
+
+
+  /** @test */
+  public function LinkToNonModeratedReviewsShouldBeDisplayed() {
+    $this->assertXPathContentContains('//a', 'Afficher les avis non modérés', $this->_response->getBody());
+  }
+
+
+  /** @test */
+  public function membersReviewsShouldBe31() {
+    $this->assertXPathContentContains('//div//h2', 'Avis d\'abonnés (31)');
+  }
 }
 
 
@@ -698,19 +714,27 @@ class ModoControllerAllReviewsPageActionTest extends ModoControllerIndexActionTe
 
   public function setup() {
     parent::setup();
-    $this->dispatch('admin/modo/allreviews/page/10', true);
   }
 
 
   /** @test **/
   public function moderatedReviewsShouldBeDisplayedEvenIfPageIsOutOfBound() {
-    $this->assertXpathContentContains('//div//h2', 'B comme bière : la bière expliquée aux (grands) enfants', $this->_response->getBody());
+    $this->dispatch('admin/modo/allreviews/page/10', true);
+    $this->assertXpathContentContains('//div//h2', 'B comme bière : la bière expliquée aux (grands) enfants');
+  }
+
+
+  /** @test **/
+  public function page3ShouldNotConstainsTheLastReviewSaved139() {
+    $this->dispatch('admin/modo/allreviews/page/3/active_tab/1', true);
+    $this->assertNotXpathContentContains('//div[@class="critique"]//div[@class="contenu_critique"]//a[contains(@href, "blog/viewavis")]', '139');
   }
 
 
   /** @test */
-  public function LinkToNonModeratedReviewsShouldBeDisplayed() {
-    $this->assertXPathContentContains('//a', 'Afficher les avis non modérés', $this->_response->getBody());
+  public function page1ShouldContains10Reviews() {
+    $this->dispatch('admin/modo/allreviews/page/1/active_tab/1', true);
+    $this->assertXPathContentContains('//div[2]/span', 'résultats sur cette page.', $this->_response->getBody());
   }
 }
 
-- 
GitLab