From adfffd739ed7f65d3480f018d0c5d340d1b2de94 Mon Sep 17 00:00:00 2001
From: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date: Thu, 22 Oct 2015 15:33:28 +0200
Subject: [PATCH] dev#25569: Add category selection for most recent sites.

---
 VERSIONS_WIP/25569                            |  1 +
 .../views/scripts/accueil/sitotheque.phtml    |  1 -
 .../opac/controllers/SitoController.php       | 28 +++++---
 library/Class/Sitotheque.php                  |  8 ++-
 .../opac/controllers/SitoControllerTest.php   | 72 ++++++++++++++++---
 5 files changed, 90 insertions(+), 20 deletions(-)
 create mode 100644 VERSIONS_WIP/25569

diff --git a/VERSIONS_WIP/25569 b/VERSIONS_WIP/25569
new file mode 100644
index 00000000000..c94f8bded98
--- /dev/null
+++ b/VERSIONS_WIP/25569
@@ -0,0 +1 @@
+ - ticket #25569 : Boite sitothèque : afficher les derniers sites d'une catégorie
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/accueil/sitotheque.phtml b/application/modules/admin/views/scripts/accueil/sitotheque.phtml
index 3d3d72d1c5c..202ee6de0ba 100644
--- a/application/modules/admin/views/scripts/accueil/sitotheque.phtml
+++ b/application/modules/admin/views/scripts/accueil/sitotheque.phtml
@@ -9,7 +9,6 @@ function toggleDisplayTypeDependents() {
 $("#type_aff").change(function () {
   toggleDisplayTypeDependents();
   var value = $("#type_aff").val();
-  $(".treeselect").treeselect("toggleVisibility", (value == "1" || value == "3"));
 });
 
 toggleDisplayTypeDependents();
diff --git a/application/modules/opac/controllers/SitoController.php b/application/modules/opac/controllers/SitoController.php
index e10c79e9ddc..2e75be99e53 100644
--- a/application/modules/opac/controllers/SitoController.php
+++ b/application/modules/opac/controllers/SitoController.php
@@ -38,14 +38,26 @@ class SitoController extends Zend_Controller_Action {
 
 
   public function viewrecentAction() {
-    $class_sito = new Class_Sitotheque();
-    $nb_sito = (int)$this->_request->getParam('nb', 10);
-    $limit = ($nb_sito < 1) ? 10 : $nb_sito;
-
-    $sites = Class_Sitotheque::findAllBy(['order' => 'date_maj desc',
-                                          'limit' => $limit]);
-    $this->view->sitos = $sites;
-    $this->view->title = $this->view->_('Derniers Sites');
+    $id_category = $this->_getParam('id_cat');
+    if ($id_category) {
+      $ids = $this->_getParam('id', $this->_getParam('id_items'));
+      $sitos = Class_Sitotheque::getSitesFromIdsAndCategories(
+                                                              explode('-', $ids),
+                                                              explode('-', $this->_getParam('id_cat')),
+      true);
+
+      $this->view->sitos = $sitos;
+      $this->view->title = $this->view->_('Derniers Sites de catégorie');
+    } else {
+      $class_sito = new Class_Sitotheque();
+      $nb_sito = (int)$this->_request->getParam('nb', 10);
+      $limit = ($nb_sito < 1) ? 10 : $nb_sito;
+
+      $sites = Class_Sitotheque::findAllBy(['order' => 'date_maj desc',
+                                            'limit' => $limit]);
+      $this->view->sitos = $sites;
+      $this->view->title = $this->view->_('Derniers Sites');
+    }
     $this->renderScript('sito/viewsitos.phtml');
   }
 
diff --git a/library/Class/Sitotheque.php b/library/Class/Sitotheque.php
index 357b97b6943..34b1f44858d 100644
--- a/library/Class/Sitotheque.php
+++ b/library/Class/Sitotheque.php
@@ -42,7 +42,7 @@ class SitothequeLoader extends Storm_Model_Loader {
    * @param array $id_categories
    * @return array
    */
-  public function getSitesFromIdsAndCategories($id_sites, $id_categories) {
+  public function getSitesFromIdsAndCategories($id_sites, $id_categories, $sorted = false) {
     $sites = [];
     $feeds = [];
 
@@ -65,6 +65,12 @@ class SitothequeLoader extends Storm_Model_Loader {
       $feeds = array_merge($feeds, $categorie->getSitotheques());
     }
 
+    if ($feeds && $sorted == true) {
+      usort($feeds, function($a, $b) {
+                                        if ($a->getDateMaj() == $b->getDateMaj()) return 0;
+                                        return ($a->getDateMaj() < $b->getDateMaj() ? 1 : -1);
+                                      });
+    }
     return $this->_filterOnId(array_filter($feeds));
   }
 
diff --git a/tests/application/modules/opac/controllers/SitoControllerTest.php b/tests/application/modules/opac/controllers/SitoControllerTest.php
index e6be26938dc..34055f583d7 100644
--- a/tests/application/modules/opac/controllers/SitoControllerTest.php
+++ b/tests/application/modules/opac/controllers/SitoControllerTest.php
@@ -60,7 +60,7 @@ abstract class SitoControllerTestCase extends AbstractControllerTestCase {
 
 
 
-abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase {
+abstract class SitoControllerViewCategoryAndRecentTestCase extends SitoControllerTestCase {
   public function setUp() {
     parent::setUp();
     $collectif = $this->fixture('Class_SitothequeCategorie',
@@ -78,12 +78,13 @@ abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase
                              'parent_categorie' => $associations,
                              'libelle' => 'Libre' ]);
 
-    $this->fixture('Class_Sitotheque',
+    $quadrature = $this->fixture('Class_Sitotheque',
                    ['id' => 280,
                     'categorie' => $libre,
                     'titre' => 'La quadrature du net',
                     'description' => 'Internet et libertés',
                     'url' => 'http://laquadrature.net']);
+    $quadrature->setDateMaj('1970-01-01 00:00:00');
 
     $this->fixture('Class_Sitotheque',
                    ['id' => 282,
@@ -92,13 +93,20 @@ abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase
                     'description' => 'Promouvoir le logiciel libre',
                     'url' => 'http://april.org']);
 
-    $this->fixture('Class_Sitotheque',
+    $framasoft = $this->fixture('Class_Sitotheque',
                    ['id' => 281,
                     'categorie' => $collectif,
                     'titre' => 'Framasoft',
                     'description' => 'Degooglisons internet',
                     'url' => 'http://framasoft.org']);
+    $framasoft->setDateMaj('9999-12-12 00:00:00');
 
+  }
+}
+
+abstract class SitoControllerViewCategoryTestCase extends SitoControllerViewCategoryAndRecentTestCase {
+  public function setUp() {
+    parent::setUp();
     Class_Profil::getCurrentProfil()
       ->setCfgAccueil(['modules' => ['1' => ['division' => '2',
                                              'type_module' => 'SITO',
@@ -106,13 +114,11 @@ abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase
                                                                'type_aff' => '3',
                                                                'id_items' => '280-281']]],
                        'options' => []]);
+
   }
 }
 
-
-
-
-class SitoControllerViewCategoyTest extends SitoControllerViewCategoyTestCase {
+class SitoControllerViewCategoryTest extends SitoControllerViewCategoryTestCase {
   public function setUp() {
     parent::setUp();
     $this->dispatch('/sito/viewcategory/id_cat/12/start_cat/12', true);
@@ -148,8 +154,6 @@ class SitoControllerViewCategoyTest extends SitoControllerViewCategoyTestCase {
 }
 
 
-
-
 class SitoControllerViewRecentTest extends SitoControllerTestCase {
   public function setUp() {
     parent::setUp();
@@ -163,12 +167,60 @@ class SitoControllerViewRecentTest extends SitoControllerTestCase {
     $this->assertXPath('//div[@class="contenu"]//div[@class="sitotheque"]//a[@href="http://linuxfr.org"]',
                        $this->_response->getBody());
   }
+
+}
+
+
+class SitoControllerViewRecentTestCase extends SitoControllerViewCategoryAndRecentTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    Class_Profil::getCurrentProfil()
+      ->setCfgAccueil(['modules' => ['1' => ['division' => '2',
+                                             'type_module' => 'SITO',
+                                             'preferences' => ['id_categorie' => '12',
+                                                               'type_aff' => '2',
+                                                               'id_items' => '280-281']]],
+                                                               'options' => []]);
+  }
+}
+
+
+class SitoControllerViewRecentWithCategoryTest extends SitoControllerViewRecentTestCase {
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/sito/viewrecent/nb/10/id_cat/12', true);
+  }
+
+  /** @test */
+  public function linuxFrShouldNotBeShown() {
+    $this->assertNotXPath('//div[@class="sitotheque"]//a[@href="http://linuxfr.org"]',
+                          $this->_response->getBody());
+  }
+
+  /** @test */
+  public function framasoftShouldBeShownFirst() {
+    $this->assertXPath('(//div[@class="sitotheque"])[1]//a[@href="http://framasoft.org"]',
+                       $this->_response->getBody());
+  }
+
+  /** @test */
+  public function aprilShouldBeShownSecond() {
+    $this->assertXPath('(//div[@class="sitotheque"])[2]//a[@href="http://april.org"]',
+                       $this->_response->getBody());
+  }
+
+  /** @test */
+  public function quadratureShouldBeShownLast() {
+    $this->assertXPath('(//div[@class="sitotheque"])[3]//a[@href="http://laquadrature.net"]',
+                       $this->_response->getBody());
+  }
 }
 
 
 
 
-class SitoControllerViewCategorySearchTest extends SitoControllerViewCategoyTestCase {
+class SitoControllerViewCategorySearchTest extends SitoControllerViewCategoryTestCase {
   public function setUp() {
     parent::setUp();
 
-- 
GitLab