From 9eb1138eca2ece1d27c2b094ca6df23d0564fa5b Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Thu, 31 May 2018 14:57:44 +0200
Subject: [PATCH] dev #75742 fix rt comments

---
 .../admin/controllers/ThesauriController.php  |  1 -
 library/Class/AdminVar.php                    |  7 ++-
 library/Class/CodifThesaurus.php              | 35 ++++++++++++--
 library/ZendAfi/Acl/AdminControllerGroup.php  |  3 +-
 library/ZendAfi/Acl/AdminControllerRoles.php  |  2 +
 .../Action/Helper/AbstractListViewMode.php    | 35 ++++++++++----
 .../Action/Helper/ThesauriListViewMode.php    | 47 ++++++++-----------
 .../Controller/Plugin/Manager/Thesauri.php    |  9 ++--
 library/ZendAfi/Form/Admin/Thesauri.php       | 47 ++++++++++---------
 .../ZendAfi/View/Helper/Admin/ContentNav.php  |  3 +-
 tests/scenarios/Thesauri/ThesauriTest.php     | 21 +++++++--
 11 files changed, 132 insertions(+), 78 deletions(-)

diff --git a/application/modules/admin/controllers/ThesauriController.php b/application/modules/admin/controllers/ThesauriController.php
index c9ae9edb79a..f4355791dac 100644
--- a/application/modules/admin/controllers/ThesauriController.php
+++ b/application/modules/admin/controllers/ThesauriController.php
@@ -35,7 +35,6 @@ class Admin_ThesauriController extends ZendAfi_Controller_Action {
                                                      'page' => $this->_getParam('page', 1),
                                                      'order' => $this->_getParam('order', 'libelle')]);
 
-
     $thesauri = $this->_getParam('parent_id')
       ? Class_CodifThesaurus::find($this->_getParam('parent_id'))
       : Class_CodifThesaurus::root();
diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php
index 5a140ef5f73..50df1d024bf 100644
--- a/library/Class/AdminVar.php
+++ b/library/Class/AdminVar.php
@@ -363,7 +363,7 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
             'FEATURES_TRACKING_ENABLE' => Class_AdminVar_Meta::newOnOff('Affiche les dernières modifications apportés au logiciel Bokeh', ['value' => 1])->bePrivate(),
             'INSPECTOR_GADGET_MARC_XML' => Class_AdminVar_Meta::newOnOff('Affiche le MARC XML de la notice dans Inspector Gadget')->bePrivate(),
             'BUSINESS_EMAIL' => Class_AdminVar_Meta::newDefault($this->_('Email utilisé pour les demandes d\'accompagnement de mise en place ou d\'utilisation des fonctionnalités du logiciel'), ['value' => 'cial-bib@afi-sa.fr'])->bePrivate(),
-            'THESAURI_BROWSER' => Class_AdminVar_Meta::newOnOff($this->_('Activer l\'outil de parcourt des Thesaurus'))
+            'THESAURI_BROWSER' => Class_AdminVar_Meta::newOnOff($this->_('Activer l\'outil de parcours des Thesaurus'))
             ->bePrivate()
 ];
   }
@@ -900,6 +900,11 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
   }
 
 
+  public static function isThesauriBrowserEnabled() {
+    return Class_AdminVar::isModuleEnabled('THESAURI_BROWSER');
+  }
+
+
   public function getBabelthequeId() {
     $mathes = [];
     if (preg_match('/bw_([^\.]+)\.js/', (string)Class_AdminVar::get('BABELTHEQUE_JS'), $matches))
diff --git a/library/Class/CodifThesaurus.php b/library/Class/CodifThesaurus.php
index 2cf69d3a00f..54a39f794fc 100644
--- a/library/Class/CodifThesaurus.php
+++ b/library/Class/CodifThesaurus.php
@@ -132,9 +132,23 @@ class CodifThesaurusLoader extends Storm_Model_Loader {
 
 
   public function countChildrenOf($id_thesaurus) {
-    return  Class_CodifThesaurus::countBy(['where' => 'id_thesaurus like "'.$id_thesaurus.'%" and LENGTH(id_thesaurus)>'.strlen($id_thesaurus)
-                                           ]);
+    $where = sprintf('id_thesaurus like "%s%%" and LENGTH(id_thesaurus) > %s',
+                     $id_thesaurus,
+                     strlen($id_thesaurus));
 
+    return  Class_CodifThesaurus::countBy(['where' => $where]);
+  }
+
+
+  public function findChildrenOfWith($id_thesaurus, $page, $items_by_page, $order) {
+    $where = sprintf('id_thesaurus like "%s%%" and LENGTH(id_thesaurus) = %s',
+                     $id_thesaurus,
+                     (strlen($id_thesaurus) + 4));
+
+    return Class_CodifThesaurus::findAllBy(['where' => $where,
+                                            'limitPage' => [$page,
+                                                            $items_by_page],
+                                            'order' => $order]);
   }
 
 
@@ -515,9 +529,12 @@ class Class_CodifThesaurus extends Storm_Model_Abstract {
 
 
   public function getRulesLabel() {
-    return ($json = $this->getRules())
-      ? json_decode($json)->label
-      : '';
+    if(!$rules = $this->getRules())
+      return '';
+
+    $json = json_decode($rules);
+
+    return $json ? $json->label : '';
   }
 
 
@@ -555,6 +572,7 @@ class Class_CodifThesaurus extends Storm_Model_Abstract {
 
   public function cleanRecords() {
     $facets = [];
+
     foreach($this->getLoader()->findChildrenOf($this) as $child)
       $facets[] = $child->getFacetteIndex();
 
@@ -604,6 +622,7 @@ class Class_CodifThesaurus extends Storm_Model_Abstract {
                         });
   }
 
+
   public function getParent() {
     return $this->getLoader()->findParent($this->getIdThesaurus());
   }
@@ -620,7 +639,13 @@ class Class_CodifThesaurus extends Storm_Model_Abstract {
     return $this->getLibelleFacette();
   }
 
+
   public function numberOfChildrens() {
     return $this->getLoader()->countChildrenOf($this->getIdThesaurus());
   }
+
+
+  public function isNotRoot() {
+    return strlen($this->getIdThesaurus()) > Class_CodifThesaurus::ID_KEY_LENGTH;
+  }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/Acl/AdminControllerGroup.php b/library/ZendAfi/Acl/AdminControllerGroup.php
index ded4dadb281..362ec3df56e 100644
--- a/library/ZendAfi/Acl/AdminControllerGroup.php
+++ b/library/ZendAfi/Acl/AdminControllerGroup.php
@@ -66,7 +66,8 @@ class ZendAfi_Acl_AdminControllerGroup {
                           'i18n' => Class_AdminVar::isTranslationEnabled(),
                           'stat/piwik' => (new Class_AdminVar_Piwik())->isEnabled(),
                           'multimedia' => Class_AdminVar::isMultimediaEnabled(),
-                          'search-form' => Class_AdminVar::isCustomSearchFormEnabled()];
+                          'search-form' => Class_AdminVar::isCustomSearchFormEnabled(),
+                          'thesauri' => Class_AdminVar::isThesauriBrowserEnabled()];
   }
 
 
diff --git a/library/ZendAfi/Acl/AdminControllerRoles.php b/library/ZendAfi/Acl/AdminControllerRoles.php
index 35cb1f24f8f..7bd00f57c00 100644
--- a/library/ZendAfi/Acl/AdminControllerRoles.php
+++ b/library/ZendAfi/Acl/AdminControllerRoles.php
@@ -94,6 +94,7 @@ class ZendAfi_Acl_AdminControllerRoles extends Zend_Acl {
     $this->add(new Zend_Acl_Resource('batch'));
     $this->add(new Zend_Acl_Resource('file-manager'));
     $this->add(new Zend_Acl_Resource('search-form'));
+    $this->add(new Zend_Acl_Resource('thesauri'));
 
     //Roles
     $this->addRole(new Zend_Acl_Role('invite'));
@@ -148,6 +149,7 @@ class ZendAfi_Acl_AdminControllerRoles extends Zend_Acl {
     $this->allow('admin_bib','menus');
     $this->allow('admin_bib','external-agendas');
     $this->allow('admin_bib','search-form');
+    $this->allow('admin_bib','thesauri');
 
     $this->deny('modo_portail','catalogue');
     $this->deny('modo_portail','rss');
diff --git a/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php b/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php
index 42f44d399af..d75e21ead8c 100644
--- a/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php
+++ b/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php
@@ -42,13 +42,13 @@ abstract class ZendAfi_Controller_Action_Helper_AbstractListViewMode extends Zen
   public function render($view, $callback) {
     return $view->tagModelTable($this->getItems(),
                                 $this->getItemsCols($view),
-                            $this->getItemsAttribs(),
+                                $this->getItemsAttribs(),
                                 [function($model) use ($view)
-                             {
-                               return $view->renderPluginsActions($model);
-                             }],
-                            $this->getItemsId(),
-                            $this->getItemsGroupBy(),
+                                 {
+                                   return $view->renderPluginsActions($model);
+                                 }],
+                                $this->getItemsId(),
+                                $this->getItemsGroupBy(),
                                 $this->isSearching() ? [$this->getItemsLabelAttrib() => $callback] : [], $this->enabledPager(), $this->enabledSorter());
   }
 
@@ -157,9 +157,21 @@ abstract class ZendAfi_Controller_Action_Helper_AbstractListViewMode extends Zen
   }
 
 
+  public function getSearchColumns() {
+    return ['titre'];
+  }
+
+
   public function getSearchParams() {
-    return ['where' => 'titre like ' . Zend_Db_Table::getDefaultAdapter()->quote('%' . $this->getSearch() . '%') ,
-            'order' => 'titre'];
+    $search_param = Zend_Db_Table::getDefaultAdapter()->quote('%' . $this->getSearch() . '%');
+    $columns = $this->getSearchColumns();
+
+    $where = [];
+    foreach ($columns as $col)
+      $where [] = '('.$col.' like '.$search_param.')';
+
+    return ['where' => implode(' or ', $where),
+            'order' => $this->getOrder()];
   }
 
 
@@ -318,7 +330,7 @@ abstract class ZendAfi_Controller_Action_Helper_AbstractListViewMode extends Zen
                                              ->setText($this->_form_settings->getSearchText())
                                              ->setImage($view->tagImg(Class_Admin_Skin::current()
                                                                       ->getIconUrl('actions',
-                                                                                        'loupe'),
+                                                                                   'loupe'),
                                                                       ['style' => 'filter: invert();']))
                                              ->setAttribs(['onclick' => "var form=$(this).parents('form'); if (!form.length) form=$(this).parents('.boutons, .admin-buttons').prevAll('form');if (!form.length) form=$(this).parents('.boutons, .admin-buttons').nextAll('form');form.submit(); return false;",
                                                            'type' => 'submit',
@@ -341,6 +353,11 @@ abstract class ZendAfi_Controller_Action_Helper_AbstractListViewMode extends Zen
   }
 
 
+  public function getOrder() {
+    return $this->getParam('order', 'titre');
+  }
+
+
   protected function _getSearchForm($view) {
     return (new ZendAfi_Form())
       ->setAction($view->url(array_filter($this->getSearchUrl()), null, true))
diff --git a/library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php b/library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php
index 7e19b20191b..e675f2c253c 100644
--- a/library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php
+++ b/library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php
@@ -22,6 +22,7 @@
 
 class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Controller_Action_Helper_AbstractListViewMode {
 
+
   public function thesauriListViewMode($params) {
     $this->_params = $params;
     return $this;
@@ -32,10 +33,12 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
     return $this->thesauriListViewMode($params);
   }
 
+
   protected function enabledSorter() {
     return false;
   }
 
+
   public function getBaseUrl() {
     return ['module' => 'admin',
             'controller' => 'thesauri'];
@@ -74,19 +77,17 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
 
   public function getItems() {
     $model = $this->getModel();
+
     if ($this->isSearching())
       return Class_CodifThesaurus::findAllBy($this->getItemsParams());
+
     if($model->isNew())
       return [];
 
-    $where = sprintf('id_thesaurus like "%s%%" and LENGTH(id_thesaurus) = %s',
-                     $model->getIdThesaurus(), strlen($model->getIdThesaurus()) + 4);
-
-    return Class_CodifThesaurus::findAllBy(['where' => $where,
-                                            'limitPage' => [$this->getPage(),
-                                                            $this->_items_by_page],
-                                            'order' => $this->getParam('order','libelle')]);
-
+    return Class_CodifThesaurus::findChildrenOfWith($model->getIdThesaurus(),
+                                                    $this->getPage(),
+                                                    $this->_items_by_page,
+                                                    $this->getOrder());
   }
 
 
@@ -96,17 +97,15 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
 
 
   public function countItemsInTreeFrom($model) {
-    if ($model->isNew())
-      return 0;
-    return $model->numberOfChildrens();
-
+    return $model->isNew()
+      ? 0
+      : $model->numberOfChildrens();
   }
 
 
   public function getItemsCols($view) {
     $order = $this->getOrder();
     $anchor_wrapper = function($key, $label) use ($view, $order) {
-      xdebug_break();
       $order_param = $key;
       if((0 === strpos($order, $key)) && (false === strpos($order, 'desc')))
         $order_param .= ' desc';
@@ -127,7 +126,6 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
              $anchor_wrapper('id_thesaurus', $this->_('ID Thesaurus')),
              $anchor_wrapper('rules_label', $this->_('Règle'))];
 
-    xdebug_break();
     return $cols;
   }
 
@@ -154,20 +152,13 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
             'rules_label'];
   }
 
-  public function getSearchParams() {
-    $columns = ['libelle',
-                'libelle_facette',
-                'code',
-                'id_thesaurus',
-                'rules'
-    ];
-    $where = [];
-    $search_param = Zend_Db_Table::getDefaultAdapter()->quote('%' . $this->getSearch() . '%');
-    foreach ($columns as $col) {
-      $where [] = '('.$col.' like '.$search_param.')';
-    }
-    return ['where' => implode(' or ',$where),
-            'order' => $this->getOrder()];
+
+  public function getSearchColumns() {
+    return ['libelle',
+            'libelle_facette',
+            'code',
+            'id_thesaurus',
+            'rules'];
   }
 
 
diff --git a/library/ZendAfi/Controller/Plugin/Manager/Thesauri.php b/library/ZendAfi/Controller/Plugin/Manager/Thesauri.php
index bcf0f855671..30e22dcefda 100644
--- a/library/ZendAfi/Controller/Plugin/Manager/Thesauri.php
+++ b/library/ZendAfi/Controller/Plugin/Manager/Thesauri.php
@@ -47,6 +47,7 @@ class ZendAfi_Controller_Plugin_Manager_Thesauri extends ZendAfi_Controller_Plug
                        'id' =>  '%s'],
              'icon' => 'edit',
              'label' => $this->_('Modifier le thesaurus')],
+
             ['url' => $this->_view->url(['module' => 'opac',
                                          'controller' => 'recherche',
                                          'action' => 'simple',
@@ -56,10 +57,8 @@ class ZendAfi_Controller_Plugin_Manager_Thesauri extends ZendAfi_Controller_Plug
              'label' => $this->_('Voir les documents qui ont la facette "%s"',$model->getFacetteIndex()),
              'anchorOptions' => ['target' => '_blank'],
              'condition' => function ($model) {
-                return (strlen($model->getIdThesaurus()) > Class_CodifThesaurus::ID_KEY_LENGTH);
+                return $model->isNotRoot();
               }
-            ],
-    ];
+            ]];
   }
-
-}
+}
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Admin/Thesauri.php b/library/ZendAfi/Form/Admin/Thesauri.php
index c9d93fc3f05..93bf3755ae7 100644
--- a/library/ZendAfi/Form/Admin/Thesauri.php
+++ b/library/ZendAfi/Form/Admin/Thesauri.php
@@ -24,31 +24,32 @@ class ZendAfi_Form_Admin_Thesauri extends ZendAfi_Form {
 
   public function init() {
     parent::init();
-    $this->addElement('text',
-                      'libelle',
-                      ['label' => $this->_('Libellé'),
-                      'disabled' => 'disabled'])
-         ->addElement('text',
-                      'libelle_facette',
-                      ['label' => $this->_('Libellé facette')])
-         ->addElement('text',
-                      'code',
-                      ['label' => $this->_('Code'),
-                      'disabled' => 'disabled'])
-         ->addElement('text',
-                      'id_thesaurus',
-                      ['label' => $this->_('Identifiant thésaurus'),
-                       'disabled' => 'disabled'])
-         ->addElement('text',
-                      'rules',
-                      ['label' => $this->_('Règle'),
-                       'disabled' => 'disabled'])
-         ->addUniqDisplayGroup('thesauri');
 
+    $this
+      ->addElement('text',
+                   'libelle',
+                   ['label' => $this->_('Libellé'),
+                    'disabled' => 'disabled'])
 
+      ->addElement('text',
+                      'libelle_facette',
+                   ['label' => $this->_('Libellé facette')])
 
-  }
+      ->addElement('text',
+                   'code',
+                   ['label' => $this->_('Code'),
+                    'disabled' => 'disabled'])
 
+      ->addElement('text',
+                   'id_thesaurus',
+                   ['label' => $this->_('Identifiant thésaurus'),
+                    'disabled' => 'disabled'])
 
-}
-?>
\ No newline at end of file
+      ->addElement('text',
+                   'rules',
+                   ['label' => $this->_('Règle'),
+                    'disabled' => 'disabled'])
+
+      ->addUniqDisplayGroup('thesauri');
+  }
+}
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Admin/ContentNav.php b/library/ZendAfi/View/Helper/Admin/ContentNav.php
index ff04f5c957b..77c3e74bdd6 100644
--- a/library/ZendAfi/View/Helper/Admin/ContentNav.php
+++ b/library/ZendAfi/View/Helper/Admin/ContentNav.php
@@ -170,8 +170,7 @@ class ZendAfi_View_Helper_Admin_ContentNav extends ZendAfi_View_Helper_BaseHelpe
 
                     ["thesaurus_edit",
                      $this->_("Parcourir les Thesaurus"),    "/admin/thesauri/index",
-                     [],
-                     function($user) { return $user->isAdmin() && Class_AdminVar::getValueOrDefault('THESAURI_BROWSER');}]
+                     []]
                    ]);
   }
 
diff --git a/tests/scenarios/Thesauri/ThesauriTest.php b/tests/scenarios/Thesauri/ThesauriTest.php
index e284c66e05f..d5504b53c84 100644
--- a/tests/scenarios/Thesauri/ThesauriTest.php
+++ b/tests/scenarios/Thesauri/ThesauriTest.php
@@ -26,6 +26,8 @@ abstract class Thesauri_ThesauriTestCase extends Admin_AbstractControllerTestCas
   public function setUp() {
     parent::setUp();
 
+    Class_AdminVar::set('THESAURI_BROWSER', 1);
+
     $this->fixture('Class_CodifThesaurus',
                    ['id' => 3,
                     'libelle' => 'Document',
@@ -163,14 +165,28 @@ class Thesauri_ThesauriEditTest extends Thesauri_ThesauriTestCase {
 
   public function setUp() {
     parent::setUp();
-    $this->dispatch('/admin/thesauri/edit/id/3', true);
   }
 
 
   /** @test */
   public function inputLibelleShouldContainsDocument() {
+    $this->dispatch('/admin/thesauri/edit/id/3', true);
     $this->assertXpath('//form//input[@value="Document"][@type="text"][@name="libelle"]');
   }
+
+
+  /** @test */
+  public function thesauriShouldHaveBeenUpdated() {
+    $this->postDispatch('/admin/thesauri/edit/id/3', ['libelle_facette' => 'brave']);
+    $this->assertEquals('brave', Class_CodifThesaurus::find(3)->getLibelleFacette());
+  }
+
+
+  /** @test */
+  public function thesauriShouldNotHaveBeenUpdated() {
+    $this->postDispatch('/admin/thesauri/edit/id/3', ['rules' => 'brave']);
+    $this->assertEmpty(Class_CodifThesaurus::find(3)->getRuleslabel());
+  }
 }
 
 
@@ -180,13 +196,12 @@ class Thesauri_ThesauriAdminIndexDispatchTest extends Thesauri_ThesauriTestCase
 
   public function setUp() {
     parent::setUp();
-    Class_AdminVar::set('THESAURI_BROWSER', 1);
     $this->dispatch('/admin/index', true);
   }
 
 
   /** @test */
   public function thesauriIndexLinkShouldBePresent() {
-    $this->assertXpath('//a[contains(@href, "/thesauri/index")]');
+    $this->assertXpathContentContains('//a[contains(@href, "/thesauri/index")]', 'Parcourir les Thesaurus');
   }
 }
-- 
GitLab