From b7dee95b8bdabc0b821cd57f351ffd4c5af1675c Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Tue, 3 Nov 2015 17:17:04 +0100
Subject: [PATCH] dev #23223 fix sito param + start adding pagination

---
 library/ZendAfi/View/Helper/Accueil/Sito.php  | 26 +++++++---
 .../ProfilOptionsControllerTest.php           | 50 ++++++++++++++++++-
 .../ZendAfi/View/Helper/Accueil/SitoTest.php  |  2 -
 3 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/library/ZendAfi/View/Helper/Accueil/Sito.php b/library/ZendAfi/View/Helper/Accueil/Sito.php
index 8be9497830e..d3b068c22ce 100644
--- a/library/ZendAfi/View/Helper/Accueil/Sito.php
+++ b/library/ZendAfi/View/Helper/Accueil/Sito.php
@@ -56,9 +56,13 @@ class ZendAfi_View_Helper_Accueil_Sito extends ZendAfi_View_Helper_Accueil_Base
 
 
   public function getHtml() {
+    if ($this->getPreferences('rss'))
+      $this->rss_interne = $this->_getRSSurl('sito', 'sito-rss');
+
     $strategy = $this->getStrategy();
     $this->titre = $strategy->getTitle();
     $this->contenu = $strategy->getContent();
+
     return $this->getHtmlArray();
   }
 
@@ -237,22 +241,28 @@ class SelectedStrategy extends SitoDisplayStrategy {
                                                      explode('-',
                                                              $this->getPreference('id_categorie')));
 
-    $nb_aff = $this->getNbSitesToDisplay($sites);
-
     if (static::ORDER_RANDOM == $this->getPreference('display_order')) {
       shuffle($sites);
-      return $this->renderSitesSlice($sites, $nb_aff);
+      return $this->renderSitesWithPagination($sites, $this->getPreference('nb_aff'));
     }
 
-    return $this->renderSitesSlice($sites, $nb_aff);
+    return $this->renderSitesSlice($sites, count($sites));
   }
 
 
-  protected function getNbSitesToDisplay($sites) {
-    if (static::ORDER_RANDOM == $this->getPreference('display_order'))
-      return $this->getPreference('nb_aff');
+  protected function renderSitesWithPagination($sites, $nb_sites_by_pages) {
+    if(count($sites) <= $nb_sites_by_pages)
+      return $this->renderSitesSlice($sites, $nb_sites_by_pages);
+
+    return $this->renderSitesSlice($sites, $nb_sites_by_pages) . $this->getPaginator($sites, $nb_sites_by_pages);
+  }
+
 
-    return count($sites);
+  protected function getPaginator($items, $items_by_page, $page = 0) {
+    $paginator = (new Zend_Paginator(new Zend_Paginator_Adapter_Null(count($items))))
+      ->setItemCountPerPage($items_by_page)
+      ->setCurrentPageNumber($page);
+    return $this->view->paginationControl($paginator);
   }
 }
 
diff --git a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
index 77290066426..c0beaac1f45 100644
--- a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
+++ b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
@@ -1300,7 +1300,7 @@ class ProfilOptionsControllerProfilJeunesseViewPageJeuxTest extends ProfilOption
 
 
 
-class ProfilOptionsControllerPagesJeuxWithSitotheque extends ProfilOptionsControllerProfilJeunesseWithPagesJeuxMusiqueTestCase {
+class ProfilOptionsControllerPagesJeuxWithSitothequeTest extends ProfilOptionsControllerProfilJeunesseWithPagesJeuxMusiqueTestCase {
   public function setUp() {
     parent::setUp();
 
@@ -1369,6 +1369,54 @@ class ProfilOptionsControllerPagesJeuxWithSitotheque extends ProfilOptionsContro
 
 
 
+class ProfilOptionsControllerPagesJeuxWithSitothequeAndPaginationTest extends ProfilOptionsControllerProfilJeunesseWithPagesJeuxMusiqueTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $this->page_jeux->setCfgAccueil(['modules' => [
+
+                                                   '10' => ['division' => '2',
+                                                            'type_module' => 'SITO',
+                                                            'preferences' => ['rss' => 1,
+                                                                              'id_categorie' => '2',
+                                                                              'nb_aff' => 1,
+                                                                              'type_aff' => 1,
+                                                                              'display_order' => 'Random',
+                                                                              'group_by_categorie' => 0]]],
+                                     'options' =>   []]);
+
+    $this->fixture('Class_SitothequeCategorie',
+                   ['id' => 2,
+                    'libelle' => 'My sites',
+                    'sitotheques' => [
+                                      $this->fixture('Class_Sitotheque',
+                                                     ['id' => 34,
+                                                      'titre' => 'Thot cursus',
+                                                      'url' => 'http://cursus.edu/',
+                                                      'description' => 'Top notch site',
+                                                      'tags' => 'VOD']),
+
+                                      $this->fixture('Class_Sitotheque',
+                                                     ['id' => 35,
+                                                      'titre' => 'Pharo',
+                                                      'url' => 'http://pharo.org/',
+                                                      'description' => 'Cool programming language',
+                                                      'tags' => 'Smalltalk'])]
+                   ]);
+
+
+    $this->dispatch('/opac/index/index/clef/zork?id_profil=12', true);
+  }
+
+
+  /** @test */
+  public function paginationShouldBePresent() {
+    $this->assertXPathContentContains('//a', '2', $this->_response->getBody());
+  }
+}
+
+
+
 class ProfilOptionsControllerViewProfilJeunesseAccueilTest extends ProfilOptionsControllerProfilJeunesseWithPagesJeuxMusiqueTestCase {
   protected function _loginHook($account) {}
 
diff --git a/tests/library/ZendAfi/View/Helper/Accueil/SitoTest.php b/tests/library/ZendAfi/View/Helper/Accueil/SitoTest.php
index eddd3d52a43..93e9fac65b3 100644
--- a/tests/library/ZendAfi/View/Helper/Accueil/SitoTest.php
+++ b/tests/library/ZendAfi/View/Helper/Accueil/SitoTest.php
@@ -146,7 +146,6 @@ class SitoViewHelperSelectItemsByRandomOrderTest extends SitoViewHelperTestCase
 
 
 
-
 class SitoViewHelperSelectItemsAndCatsTest extends SitoViewHelperTestCase {
   protected $_preferences = ['titre' => 'Ma sito',
                              'type_aff' => 1,
@@ -303,7 +302,6 @@ class SitoViewHelperHierarchicalCategoryTest extends SitoViewHelperTestCase {
     $this->assertXPathContentContains($this->html,
                                       '//ul[@class="sitotheque"]//li//a',
                                       'RMLL');
-
   }
 
 
-- 
GitLab