From 86460d3b1e8511038872c9f845874c63f643f95f Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Thu, 14 Jun 2018 13:29:27 +0200
Subject: [PATCH] dev #75882 refacto thesauri list view mode to
 treeListViewMode

---
 .../ListViewModeDescription/Thesauri.php}     | 72 +++++++++++++------
 .../Action/Helper/AbstractListViewMode.php    | 10 +++
 .../Helper/TreeCodificationListViewMode.php   | 16 +++++
 .../Plugin/ResourceDefinition/Abstract.php    | 11 ++-
 .../Plugin/ResourceDefinition/Thesauri.php    | 10 +--
 tests/scenarios/Thesauri/ThesauriTest.php     |  3 +
 6 files changed, 89 insertions(+), 33 deletions(-)
 rename library/{ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php => Class/ListViewModeDescription/Thesauri.php} (54%)

diff --git a/library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php b/library/Class/ListViewModeDescription/Thesauri.php
similarity index 54%
rename from library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php
rename to library/Class/ListViewModeDescription/Thesauri.php
index e047bea56dd..c1e814b96b3 100644
--- a/library/ZendAfi/Controller/Action/Helper/ThesauriListViewMode.php
+++ b/library/Class/ListViewModeDescription/Thesauri.php
@@ -1,4 +1,4 @@
-<?php
+2<?php
 /**
  * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
  *
@@ -20,25 +20,62 @@
  */
 
 
-class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Controller_Action_Helper_TreeCodificationListViewMode {
+class Class_ListViewModeDescription_Thesauri {
+  use Trait_Translator;
 
-  public function thesauriListViewMode($params) {
-    $this->_initParams($params);
-    return $this;
+  protected $_context;
+
+  public function __construct($context) {
+    $this->_context = $context;
+  }
+
+
+  public function render() {
+    $helper = $this->_context->getHelper();
+    $params = ['order' => $this->_context->getOrder(),
+               'page' => $this->_context->getPage(),
+               'search_value' => $this->_context->getSearchValue(),
+
+               'controller' => 'thesauri',
+               'label' => $this->_('Thesaurus'),
+
+               'model' => $this->_getModel(),
+               'search_columns' => $this->_getSearchColumns(),
+               'categories_description' => function($description, $list_view_mode)
+      {
+        return $this->_describeCategoriesIn($description, $list_view_mode);
+      },
+               'items_description' => function($description, $list_view_mode)
+      {
+        return $this->_describeItemsIn($description, $list_view_mode);
+      }];
+
+    return $helper->treeCodificationListViewMode($params);
   }
 
 
-  public function direct($params) {
-    return $this->thesauriListViewMode($params);
+  protected function _getModel() {
+    return $this->_context->getParentId()
+      ? Class_CodifThesaurus::find($this->_context->getParentId())
+      : Class_CodifThesaurus::root();
   }
 
 
-  protected function _describeCategoriesIn($description) {
+  protected function _getSearchColumns() {
+    return ['libelle',
+            'libelle_facette',
+            'code',
+            'id_thesaurus',
+            'rules'];
+  }
+
+
+  protected function _describeCategoriesIn($description, $list_view_mode) {
     return $description
       ->addColumn($this->_('Thesaurus'), ['attribute' => 'libelle',
-                                          'callback' => function($model, $attrib)
+                                          'callback' => function($model, $attrib) use ($list_view_mode)
                                           {
-                                            return $this->_renderCategory($model, $attrib);
+                                            return $list_view_mode->renderCategory($model, $attrib);
                                           }])
       ->addColumn($this->_('Libellé facette'), 'libelle_facette')
       ->addColumn($this->_('Code facette'), 'facette_index')
@@ -46,12 +83,12 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
   }
 
 
-  protected function _describeItemsIn($description) {
+  protected function _describeItemsIn($description, $list_view_mode) {
     return $description
       ->addColumn($this->_('Thesaurus'), ['attribute' => 'libelle',
-                                          'callback' => function($model, $attrib)
+                                          'callback' => function($model, $attrib) use ($list_view_mode)
                                           {
-                                            return $this->_renderItem($model, $attrib);
+                                            return $list_view_mode->renderItem($model, $attrib);
                                           }])
       ->addColumn($this->_('Libellé facette'), 'libelle_facette')
       ->addColumn($this->_('Code facette'), ['attribute' => 'facette_index',
@@ -60,13 +97,4 @@ class ZendAfi_Controller_Action_Helper_ThesauriListViewMode extends ZendAfi_Cont
       ->setSorterServer()
       ;
   }
-
-
-  public function getSearchColumns() {
-    return ['libelle',
-            'libelle_facette',
-            'code',
-            'id_thesaurus',
-            'rules'];
-  }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php b/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php
index 5bd37742017..a20d7db7c87 100644
--- a/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php
+++ b/library/ZendAfi/Controller/Action/Helper/AbstractListViewMode.php
@@ -104,6 +104,16 @@ abstract class ZendAfi_Controller_Action_Helper_AbstractListViewMode extends Zen
   }
 
 
+  public function renderItem($model, $attrib) {
+    return $this->_renderItem($model, $attrib);
+  }
+
+
+  public function renderCategory($model, $attrib) {
+    return $this->_renderCategory($model, $attrib);
+  }
+
+
   protected function _getBreadcrumbHtmlFor($breadcrumb) {
     $breadcrumb_html = [];
 
diff --git a/library/ZendAfi/Controller/Action/Helper/TreeCodificationListViewMode.php b/library/ZendAfi/Controller/Action/Helper/TreeCodificationListViewMode.php
index d7061a571e8..84dc6e88768 100644
--- a/library/ZendAfi/Controller/Action/Helper/TreeCodificationListViewMode.php
+++ b/library/ZendAfi/Controller/Action/Helper/TreeCodificationListViewMode.php
@@ -73,4 +73,20 @@ class ZendAfi_Controller_Action_Helper_TreeCodificationListViewMode extends Zend
     return $model->recursiveNumberOfChildren();
   }
 
+
+  public function getSearchColumns() {
+    return $this->getParam('search_columns', parent::getSearchColumns());
+  }
+
+
+  protected function _describeCategoriesIn($description) {
+    $callback = $this->getParam('categories_description');
+    return $callback($description, $this);
+  }
+
+
+  protected function _describeItemsIn($description) {
+    $callback = $this->getParam('items_description');
+    return $callback($description, $this);
+  }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php
index ec7b6961b84..2fe9c1a8d4b 100644
--- a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php
+++ b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php
@@ -296,8 +296,15 @@ abstract class ZendAfi_Controller_Plugin_ResourceDefinition_Abstract extends Zen
 
 
   protected function _listViewMode() {
-    if(!$this->_isListViewModeEnabled())
-      return;
+    if(is_string($this->_attribs['listViewMode'])) {
+      $definition = $this->_attribs['listViewMode'];
+      $context = new Class_Entity(['Helper' => $this->_helper,
+                                   'ParentId' => $this->_getParam('parent_id'),
+                                   'Order' => $this->_getParam('order'),
+                                   'Page' => $this->_getParam('page', 0),
+                                   'SearchValue' => $this->_getParam('title_search', '')]);
+      return (new $definition($context))->render();
+    }
 
     $listViewMode = $this->_attribs['listViewMode']['helper_method'];
     $model_class = $this->getModelClass();
diff --git a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Thesauri.php b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Thesauri.php
index 7fdf32964c5..e7bfa16d326 100644
--- a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Thesauri.php
+++ b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Thesauri.php
@@ -27,15 +27,7 @@ class ZendAfi_Controller_Plugin_ResourceDefinition_Thesauri extends ZendAfi_Cont
                    'name' => 'thesauri',
                    'order' => 'libelle'],
 
-       'listViewMode' => ['helper_method' => 'ThesauriListViewMode',
-                          'model' => function()
-         {
-           return $this->_getParam('parent_id')
-           ? Class_CodifThesaurus::find($this->_getParam('parent_id'))
-           : Class_CodifThesaurus::root();
-         },
-                          'label' => $this->_('Thesaurus'),
-                          'controller' => 'thesauri'],
+       'listViewMode' => 'Class_ListViewModeDescription_Thesauri',
 
        'messages' => ['successful_save' => $this->_('Thesaurus "%s" sauvegardé')],
 
diff --git a/tests/scenarios/Thesauri/ThesauriTest.php b/tests/scenarios/Thesauri/ThesauriTest.php
index 79c5ec20a37..26daaa01b6b 100644
--- a/tests/scenarios/Thesauri/ThesauriTest.php
+++ b/tests/scenarios/Thesauri/ThesauriTest.php
@@ -180,6 +180,9 @@ class ThesauriIndexChildrenSearchTest extends ThesauriTestCase {
          ->whenCalled('findParent')->with('DOCU')
          ->answers(null)
 
+         ->whenCalled('getIdField')
+         ->answers('id_thesaurus')
+
          ->beStrict();
 
     $this->dispatch('/admin/thesauri/index/title_search/Docu/page/1/order/libelle', true);
-- 
GitLab