From cc39bab7f5e87bd2abe9119edf87c205480f7cd0 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Tue, 29 May 2018 16:58:22 +0200
Subject: [PATCH] dev #75068 redirect action when dynamic filters and display
 code on cosmogramme dynamic filter page

---
 .../opac/controllers/RechercheController.php  | 28 +++++++++++++++
 library/Class/CodifThesaurus.php              | 12 -------
 library/Class/CriteresRecherche.php           | 20 ++---------
 library/ZendAfi/View/Helper/CosmoFacets.php   | 36 +++++++++++++++----
 .../SearchResult/SearchResultTest.php         | 18 ++++------
 5 files changed, 65 insertions(+), 49 deletions(-)

diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index d4e5ab71851..de66e8fed33 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -87,6 +87,15 @@ class RechercheController extends ZendAfi_Controller_Action {
       unset($params['q']);
     }
 
+    if($dynamic_facets = $this->_extractDynamicFacets($params) ) {
+      $url = $this->newCriteresRecherches($params)
+                  ->getUrlWithMultifacetsUpdate($dynamic_facets);
+      return $this->_redirect($this->view->url($url,
+                                               null,
+                                               true),
+                              ['prependBase' => false]);
+    }
+
     if ($multifacets = $this->_extractMultifacetsPost()) {
       $url = $this->newCriteresRecherches($params)
                   ->getUrlWithMultifacetsUpdate($multifacets);
@@ -156,6 +165,25 @@ class RechercheController extends ZendAfi_Controller_Action {
   }
 
 
+  protected function _extractDynamicFacets(&$params) {
+    $facets = [];
+
+    foreach(Class_CodifThesaurus::getDynamicCodes() as $code) {
+      $param = 'rech_' . Class_CodifThesaurus::CODE_FACETTE . $code;
+      if (!$value = $this->_getParam($param))
+        continue;
+
+      unset($params[$param]);
+      $instance = Class_CodifThesaurus::findFirstBy(['code' => $code,
+                                                     'libelle' => $value]);
+      if($instance)
+         $facets[Class_CodifThesaurus::CODE_FACETTE . $instance->getIdThesaurus()] = 1;
+    }
+
+    return array_filter($facets);
+  }
+
+
   protected function _extractMultifacetsPost() {
     if (!$this->_request->isPost())
       return;
diff --git a/library/Class/CodifThesaurus.php b/library/Class/CodifThesaurus.php
index ce065e0b4f9..a84d4bce83e 100644
--- a/library/Class/CodifThesaurus.php
+++ b/library/Class/CodifThesaurus.php
@@ -415,19 +415,7 @@ class CodifThesaurusLoader extends Storm_Model_Loader {
   }
 
 
-  public function getSearchParams() {
-    $params = [];
-
-    foreach(static::getDynamicCodes() as $thesaurus) {
-      $params [] = 'rech_' . Class_CodifThesaurus::CODE_FACETTE. $thesaurus;
-    }
-
-    return $params;
-  }
-
-
   public function getDynamicCodes() {
-    xdebug_break();
     $dynamic_facets = Class_CodifThesaurus::findAllBy(['rules not' => null,
                                                        'code not' => null,
                                                        'id_thesaurus not' => null]);
diff --git a/library/Class/CriteresRecherche.php b/library/Class/CriteresRecherche.php
index 99850aeb6c1..f735b5ae7e2 100644
--- a/library/Class/CriteresRecherche.php
+++ b/library/Class/CriteresRecherche.php
@@ -128,8 +128,7 @@ class Class_CriteresRecherche {
                         'fil',
                         'bib_select',
                         'serie',
-                        'from'],
-                       Class_CodifThesaurus::getSearchParams());
+                        'from']);
   }
 
 
@@ -226,8 +225,6 @@ class Class_CriteresRecherche {
     $facettes = explode('-', $this->getParam('facettes', ''));
     $facettes[] = $this->getParam('facette', '');
 
-    $facettes = $this->_getFacetsForDynamicParams($facettes);
-
     return  array_unique(array_filter($facettes,
                                       [$this->getValidateFacette(),
                                        'isValid']
@@ -235,19 +232,6 @@ class Class_CriteresRecherche {
   }
 
 
-  protected function _getFacetsForDynamicParams($facets) {
-
-    foreach(Class_CodifThesaurus::getDynamicCodes() as $code) {
-      if (!$value = $this->getParam('rech_'.Class_CodifThesaurus::CODE_FACETTE.$code))
-          continue;
-      $instance = Class_CodifThesaurus::findFirstBy(['code' => $code,
-                                                     'libelle' => $value]);
-      $facets[] = $instance ? Class_CodifThesaurus::CODE_FACETTE.$instance->getIdThesaurus() : '';
-    }
-
-    return array_filter($facets);
-  }
-
 
   public function getMultiFacets() {
     return array_filter(array_merge(explode('-', $this->getParam('multifacets','')),
@@ -590,7 +574,7 @@ class Class_CriteresRecherche {
     $visitor->visitSearchUrl($this->getUrlCriteresWithFacettes());
 
     $this->visitByRechercheType($visitor);
-    xdebug_break();
+
     $facettes = $this->getFacettes();
     foreach($facettes as $facette)
       $visitor->visitFacette($facette);
diff --git a/library/ZendAfi/View/Helper/CosmoFacets.php b/library/ZendAfi/View/Helper/CosmoFacets.php
index 8d1b10a3323..351d21879d8 100644
--- a/library/ZendAfi/View/Helper/CosmoFacets.php
+++ b/library/ZendAfi/View/Helper/CosmoFacets.php
@@ -48,13 +48,15 @@ class ZendAfi_View_Helper_CosmoFacets extends ZendAfi_View_Helper_CosmoEmplaceme
     return $this->view
       ->tag(
         'table',
-        $this->_getTitle()
-        . $this->_getLabel()
-        . $this->_getHelp()
-        . $this->_getRules()
-        . $this->_getDisplay()
-        . $this->_getActions(),
-        ['class' => 'form', 'cellspacing' => 0, 'cellpadding' => 5]);
+            $this->_getTitle()
+            . $this->_helpCode()
+            . $this->_getCode()
+            . $this->_getLabel()
+            . $this->_getHelp()
+            . $this->_getRules()
+            . $this->_getDisplay()
+            . $this->_getActions(),
+            ['class' => 'form', 'cellspacing' => 0, 'cellpadding' => 5]);
   }
 
 
@@ -74,6 +76,26 @@ class ZendAfi_View_Helper_CosmoFacets extends ZendAfi_View_Helper_CosmoEmplaceme
   }
 
 
+  protected function _helpCode() {
+    return  '<tr>
+          <td class="form_first" align="center" colspan="2">
+            <div class="commentaire">Critère de recherche: rech_H'.
+    $this->model->getCode().'</div>
+          </td>
+        </tr>';
+  }
+
+
+  protected function _getCode() {
+    return
+      $this->view->tag(
+                       'tr',
+                       $this->view->tag('td', 'Code', [
+                                                       'class' => 'form_first', 'align' => 'right', 'width' => '35%']) .
+                       $this->view->tag('td', $this->view->formText('code', $this->model->getCode(), ['size' => 43, 'disabled' => 'disabled']), ['class' => 'form_first']));
+  }
+
+
   protected function _getHelp() {
     return '<tr>
           <td class="form_first" align="center" colspan="2">
diff --git a/tests/scenarios/SearchResult/SearchResultTest.php b/tests/scenarios/SearchResult/SearchResultTest.php
index fa5ee695214..9535425fb09 100644
--- a/tests/scenarios/SearchResult/SearchResultTest.php
+++ b/tests/scenarios/SearchResult/SearchResultTest.php
@@ -244,6 +244,7 @@ class SearchResultWithDynamicFacetTest extends AbstractControllerTestCase {
 
   public function setUp() {
     parent::setUp();
+
     $this->fixture('Class_CodifThesaurus',
                    ['id' => 3,
                     'libelle' => 'Document',
@@ -262,22 +263,15 @@ class SearchResultWithDynamicFacetTest extends AbstractControllerTestCase {
                     'code' => 'DOCU',
                     'rules' => '{"label":" 99$t "}']);
 
-    $sql = $this->mock()
-                ->whenCalled('fetchAll')
-                ->with("select id_notice, facettes from notices Where MATCH(facettes) AGAINST('+HDOCU0001' IN BOOLEAN MODE)", true, false)
-                ->answers([
-                           [1, 'HDOCU0001']
-                           ])
-                ->beStrict();
-
-    Zend_Registry::set('sql', $sql);
 
-    $this->dispatch('/opac/recherche/simple/rech_HDOCU/SIFI', true);
   }
 
 
   /** @test */
-  public function shouldReturnSearchResult() {
-    $this->assertXPath('//div');
+  public function shouldRedirectToSearchWithFacetHDOCU() {
+    $this->dispatch('/opac/recherche/simple/rech_HDOCU/SIFI', true);
+    $this->assertRedirectTo('/recherche/simple/multifacets/HDOCU0001');
   }
+
+
 }
\ No newline at end of file
-- 
GitLab