From 5064888a8a6dc89679fcf071e0d4e0a6541ab88b Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Wed, 15 Mar 2017 10:26:10 +0100
Subject: [PATCH] CIP #57858 replace treeselect with listCoche

---
 .../ZendAfi/Form/Element/AbstractSuggest.php  |  4 +-
 library/ZendAfi/Form/User/Settings.php        | 43 +++++++++++++----
 .../ZendAfi/View/Helper/TagListeCoches.php    | 48 +++++++++++--------
 .../AbonneControllerSettingsTest.php          |  1 -
 4 files changed, 65 insertions(+), 31 deletions(-)

diff --git a/library/ZendAfi/Form/Element/AbstractSuggest.php b/library/ZendAfi/Form/Element/AbstractSuggest.php
index 568ca6eea53..7fca9ee371b 100644
--- a/library/ZendAfi/Form/Element/AbstractSuggest.php
+++ b/library/ZendAfi/Form/Element/AbstractSuggest.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 abstract class ZendAfi_Form_Element_AbstractSuggest extends Zend_Form_Element_Xhtml {
   /** @var string */
@@ -63,7 +63,7 @@ abstract class ZendAfi_Form_Element_AbstractSuggest extends Zend_Form_Element_Xh
    * @return ZendAfi_Form_Element_ListeSuggestion
    */
   public function setRubrique($rubrique) {
-    $this->_rubrique = (string)$rubrique;
+    $this->_rubrique = $rubrique;
     return $this;
   }
 
diff --git a/library/ZendAfi/Form/User/Settings.php b/library/ZendAfi/Form/User/Settings.php
index b36e6d6a029..55b4a8530f7 100644
--- a/library/ZendAfi/Form/User/Settings.php
+++ b/library/ZendAfi/Form/User/Settings.php
@@ -54,24 +54,49 @@ class ZendAfi_Form_User_Settings extends ZendAfi_Form {
   protected function addLibrariesElement() {
     if (!Class_User_Settings::isBookmarkLibraryReady())
       return false;
-    return $this->addElement('domainSelect',
+    return $this->addElement('cochesSuggestion',
                              'library_ids',
                              ['label' => $this->_('Mes bibliothèques préférées'),
-                              'url' => Class_Url::assemble(['module' => 'opac',
-                                                            'controller' => 'abonne',
-                                                            'action' => 'viewable-libraries-json'])]);
+                              'name' => 'library_ids',
+                              'rubrique' => function()
+                              {
+                                $libraries = Class_Profil::getCurrentProfil()->getLibraries();
+                                return (new Class_Entity())
+                                ->whenCalledDo('getList', function() use ($libraries)
+                              {
+                                $datas = [];
+                                foreach($libraries as $library)
+                                  $datas[$library->getId()] = $library->getLabel();
+
+                                return $datas;
+                              });
+                              },
+                              'selected_all_means_nothing' => false]);
   }
 
 
   protected function addDomainsElement() {
     if (!Class_User_Settings::isBookmarkDomainsReady())
       return false;
-    return $this->addElement('domainSelect',
+
+    return $this->addElement('cochesSuggestion',
                              'domain_ids',
-                             ['label' => $this->_('Mes thèmes préférés'),
-                              'url' => Class_Url::assemble(['module' => 'opac',
-                                                            'controller' => 'abonne',
-                                                            'action' => 'viewable-domains-json'])]);
+                             ['label' => $this->_('Mes thèmes préférées'),
+                              'name' => 'domain_ids',
+                              'rubrique' => function()
+                              {
+                                $domains = Class_Catalogue::findAllCataloguesAIndexer();
+                                return (new Class_Entity())
+                                ->whenCalledDo('getList',
+                                               function() use ($domains)
+                              {
+                                $datas = [];
+                                foreach($domains as $domain)
+                                  $datas[$domain->getId()] = $domain->getLibelle();
+                                return $datas;
+                              });
+                              },
+                              'selected_all_means_nothing' => false]);
   }
 
 
diff --git a/library/ZendAfi/View/Helper/TagListeCoches.php b/library/ZendAfi/View/Helper/TagListeCoches.php
index 1a6eb00e960..98b22026bd9 100644
--- a/library/ZendAfi/View/Helper/TagListeCoches.php
+++ b/library/ZendAfi/View/Helper/TagListeCoches.php
@@ -39,30 +39,13 @@ class ZendAfi_View_Helper_TagListeCoches extends ZendAfi_View_Helper_BaseHelper
 
 
   public function tagListeCoches($rubrique, $name, $valeurs='', $id_bib=0) {
-    $rubriques =
-      ['type_doc' => new ZendAfi_View_Helper_TagListeCochesSourceDocType(),
-       'section' => $this->_simpleSourceFor('Class_CodifSection'),
-       'genre' => $this->_simpleSourceFor('Class_CodifGenre'),
-       'langue' => $this->_simpleSourceFor('Class_CodifLangue'),
-       'emplacement' => $this->_simpleSourceFor('Class_CodifEmplacement'),
-       'nature_doc' => $this->_simpleSourceFor('Class_NatureDoc'),
-
-       'bibliotheque' => $this->_simpleSourceFor('Class_Bib')->setWhere(['visibilite' => '2']),
-
-       'annexe' => (new ZendAfi_View_Helper_TagListeCochesSourceAnnexe())->setWhere(array_filter(['id_bib' => $id_bib,
-                                                                                                  'invisible' => 0])),
-
-       'profile' => new ZendAfi_View_Helper_TagListeCochesSourceProfile()
-      ];
-
-    if (!array_key_exists($rubrique, $rubriques))
+    if(!$datas = $this->_getDatas($rubrique, $id_bib))
       return '';
 
     $this->_name = $name;
     $this->_type = $rubrique;
     Class_ScriptLoader::getInstance()->addAdminScript('tag_selection');
-    $this->_definition = $rubriques[$rubrique];
-    $this->_possibles = $this->_definition->getList();
+    $this->_possibles = $datas->getList();
     $this->_cant_select_all = $this->selected_all_means_nothing ? 'true' : 'false';
 
     $this->_handleValues($valeurs);
@@ -70,7 +53,34 @@ class ZendAfi_View_Helper_TagListeCoches extends ZendAfi_View_Helper_BaseHelper
     return $this->_renderValues()
       . $this->_renderSelection()
       . $this->_renderList();
+  }
+
+
+  protected function _getDatas($data, $library_id) {
+    if(is_callable($data))
+      return call_user_func($data);
+
+    $datas =
+      ['type_doc' => new ZendAfi_View_Helper_TagListeCochesSourceDocType(),
+       'section' => $this->_simpleSourceFor('Class_CodifSection'),
+       'genre' => $this->_simpleSourceFor('Class_CodifGenre'),
+       'langue' => $this->_simpleSourceFor('Class_CodifLangue'),
+       'emplacement' => $this->_simpleSourceFor('Class_CodifEmplacement'),
+       'nature_doc' => $this->_simpleSourceFor('Class_NatureDoc'),
+
+       'bibliotheque' => $this->_simpleSourceFor('Class_Bib')
+       ->setWhere(['visibilite' => '2']),
+
+       'annexe' => (new ZendAfi_View_Helper_TagListeCochesSourceAnnexe())
+       ->setWhere(array_filter(['id_bib' => $library_id,
+                                'invisible' => 0])),
+
+       'profile' => new ZendAfi_View_Helper_TagListeCochesSourceProfile()
+      ];
 
+    return array_key_exists($data, $datas)
+      ? $datas[$data]
+      : null;
   }
 
 
diff --git a/tests/application/modules/opac/controllers/AbonneControllerSettingsTest.php b/tests/application/modules/opac/controllers/AbonneControllerSettingsTest.php
index 564d61b5611..00001e51b2a 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerSettingsTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerSettingsTest.php
@@ -137,7 +137,6 @@ class AbonneControllerSettingsFormTest extends AbonneControllerSettingsTestCase
   }
 
 
-
   /** @test */
   public function libSelectorShouldBePresent() {
     $this->assertXPathContentContains('//div', 'Mes bibliothèques préférées');
-- 
GitLab