From 221ed01a219b211205cbab51fa2fb694a2d67480 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Tue, 24 Jan 2017 16:54:38 +0100
Subject: [PATCH] hotline #53821 when search done via a POST request, redirects
 to well formed url from search criterias in order to have javascripts based
 on this URL running correctly

---
 VERSIONS_HOTLINE/53821                        |  1 +
 .../opac/controllers/RechercheController.php  | 44 ++++++++++++-------
 .../controllers/RechercheControllerTest.php   | 11 ++++-
 3 files changed, 39 insertions(+), 17 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/53821

diff --git a/VERSIONS_HOTLINE/53821 b/VERSIONS_HOTLINE/53821
new file mode 100644
index 00000000000..a66f1ce8ee6
--- /dev/null
+++ b/VERSIONS_HOTLINE/53821
@@ -0,0 +1 @@
+ - ticket #53821 : lorsqu'on réserve depuis le résultat de recherche et que la réservation est impossible, les critères de recherche initiaux sont gardés
\ No newline at end of file
diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index 6a7b0611b63..c4aa59fca46 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -81,19 +81,30 @@ class RechercheController extends ZendAfi_Controller_Action {
       return;
     }
 
-    $search_start_time = microtime(true);
-
     $criteres_recherche = $this->newCriteresRecherches($params);
 
     if ($this->view->statut == 'guidee')
       $criteres_recherche->updateRubrique('guidee');
 
+    if ($this->_request->isPost())
+      return $this->_redirect($this->view->url($criteres_recherche->getUrlCriteresWithFacettes(), null, true),
+                              ['prependBase' => false]);
+
+    $this->getFrontController()->getRouter()->getCurrentRoute()
+         ->match(str_replace(BASE_URL,
+                             '',
+                             $this->view->url($criteres_recherche->getCriteres())));
+
+    $this->_runSearchAndRender($criteres_recherche);
+  }
+
+
+  protected function _runSearchAndRender($criteres_recherche) {
+    $search_start_time = microtime(true);
+
     $search_result = $this->moteur->lancerRecherche($criteres_recherche);
 
-    if (($ig = Zend_Controller_Front::getInstance()
-         ->getPlugin('ZendAfi_Controller_Plugin_InspectorGadget'))
-        && $ig->isEnabled())
-      $ig->logRecord($this->view->searchInspector($this->moteur));
+    $this->_logSearch();
 
     if ('json' == $this->_getParam('format', ''))
       return $this->_renderJsonResult($search_result);
@@ -101,27 +112,30 @@ class RechercheController extends ZendAfi_Controller_Action {
     if ('atom' == $this->_getParam('format', ''))
       return $this->_renderAtomResult($search_result);
 
-    $this->getFrontController()->getRouter()->getCurrentRoute()
-         ->match(str_replace(BASE_URL,
-                             '',
-                             $this->view->url($criteres_recherche->getCriteres())));
+    $this->preferences['liste_format'] = $this->_getParam('liste_format', $this->preferences['liste_format']);
+
+    if (!$search_result->isError())
+      $this->addHistoRecherche(1, $criteres_recherche);
 
     $this->view->titre = $this->getTitreRechercheSimple($criteres_recherche);
 
     if($criteres_recherche->getPertinence())
       $this->view->titre .= $this->_(' (recherche élargie triée par pertinence)');
 
-    $this->preferences['liste_format'] = $this->_getParam('liste_format', $this->preferences['liste_format']);
-
-    if (!$search_result->isError())
-      $this->addHistoRecherche(1, $criteres_recherche);
-
     $this->view->search_result = $search_result
       ->setDuration(microtime(true) - $search_start_time)
       ->setSettings($this->preferences);
   }
 
 
+  protected function _logSearch() {
+    if (($ig = Zend_Controller_Front::getInstance()
+         ->getPlugin('ZendAfi_Controller_Plugin_InspectorGadget'))
+        && $ig->isEnabled())
+      $ig->logRecord($this->view->searchInspector($this->moteur));
+  }
+
+
   protected function _extractMultifacetsPost() {
     if (!$this->_request->isPost())
       return;
diff --git a/tests/application/modules/opac/controllers/RechercheControllerTest.php b/tests/application/modules/opac/controllers/RechercheControllerTest.php
index ff3bcd0a069..cd7a281e65f 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerTest.php
@@ -3380,8 +3380,7 @@ class RechercheControllerMultiFacettesTest extends RechercheControllerNoticeTest
       ->whenCalled('fetchAll')
       ->answers([ [$this->notice->getId(), $this->notice->getFacettes()] ]);
 
-    $this->postDispatch('/recherche/simple',
-                        ['expressionRecherche' => 'pomme']);
+    $this->dispatch('/recherche/simple/expressionRecherche/pomme');
     $this->assertXPath('//div[@class="facette"]/form[@method="POST"][@action="/recherche/simple/expressionRecherche/pomme"]');
   }
 
@@ -3394,6 +3393,14 @@ class RechercheControllerMultiFacettesTest extends RechercheControllerNoticeTest
   }
 
 
+  /** @test */
+  public function postQueryWithoutMultifacetsShouldRedirectToZendFormedUrl() {
+    $this->postDispatch('/recherche/simple',
+                        ['expressionRecherche' => 'pear']);
+    $this->assertRedirectTo('/recherche/simple/expressionRecherche/pear');
+  }
+
+
   /** @test */
   public function activeFacetsShouldBeT3() {
     $this->dispatch('/recherche/simple/expressionRecherche/pomme/tri/*/multifacets/T3');
-- 
GitLab