From eef423849bb6a2e003da099cf7a03073a823061b Mon Sep 17 00:00:00 2001
From: lbrun <leo@sandbox.pergame.net>
Date: Fri, 26 Feb 2016 16:04:06 +0100
Subject: [PATCH] dev#37281 : change bibnum config form to zend form

---
 .../admin/controllers/AccueilController.php   |  10 +-
 .../accueil/bibliotheque-numerique.phtml      | 140 +------------
 .../Form/Configuration/BibNumerique.php       | 189 ++++++++++++++++++
 .../Decorator/AlbumsCategoriesSelector.php    |  38 ++++
 .../ZendAfi/Form/Decorator/ImageViewer.php    |  32 +++
 .../Form/Element/AlbumsCategoriesSelector.php |  50 +++++
 library/ZendAfi/Form/Element/ImagesViewer.php |  50 +++++
 .../controllers/AccueilControllerTest.php     |   7 +
 8 files changed, 373 insertions(+), 143 deletions(-)
 create mode 100644 library/ZendAfi/Form/Configuration/BibNumerique.php
 create mode 100644 library/ZendAfi/Form/Decorator/AlbumsCategoriesSelector.php
 create mode 100644 library/ZendAfi/Form/Decorator/ImageViewer.php
 create mode 100644 library/ZendAfi/Form/Element/AlbumsCategoriesSelector.php
 create mode 100644 library/ZendAfi/Form/Element/ImagesViewer.php

diff --git a/application/modules/admin/controllers/AccueilController.php b/application/modules/admin/controllers/AccueilController.php
index 3ec3f38cdec..66df802a796 100644
--- a/application/modules/admin/controllers/AccueilController.php
+++ b/application/modules/admin/controllers/AccueilController.php
@@ -239,16 +239,10 @@ class Admin_AccueilController extends Zend_Controller_Action {
   public function bibliothequeNumeriqueAction() {
     if (1 == $this->_getParam('styles_reload')) {
       $this->view->preferences = $this->_request->getPost();
-    } else {
-      $this->_simpleAction('BIB_NUMERIQUE');
+      return ;
     }
 
-    $this->view->categories = Class_AlbumCategorie::getLoader()->getCollections();
-    $moduleBibNumerique = $this->_systemeModulesAccueil->getModuleByCode('BIB_NUMERIQUE');
-    $this->view->displayModes = $moduleBibNumerique->getDisplayModes();
-    $this->view->orderModes = $moduleBibNumerique->getOrderModes();
-
-    $this->view->albums = Class_AlbumCategorie::getLoader()->findAlbumsRecursively();
+    $this->_simpleAction('BIB_NUMERIQUE');
   }
 
 
diff --git a/application/modules/admin/views/scripts/accueil/bibliotheque-numerique.phtml b/application/modules/admin/views/scripts/accueil/bibliotheque-numerique.phtml
index 0b7942afae9..9cdc9d93699 100644
--- a/application/modules/admin/views/scripts/accueil/bibliotheque-numerique.phtml
+++ b/application/modules/admin/views/scripts/accueil/bibliotheque-numerique.phtml
@@ -1,141 +1,11 @@
-<script type="text/javascript">
- $(document).ready(function() {
-   $('select[name="type_aff"]').change(function (event) {
-     $('.displayOptions').hide();
-     $('.' + $(this).val() + 'Options').show();
-   });
-
-   $('select[name="type_aff"]').change();
- });
-</script>
-
 <center>
   <h1><?php echo $this->_('Propriétés de la bibliothèque numérique'); ?></h1><br>
   <div class="formTable">
-    <form method="post" action="<?php echo $this->url ?>">
-
-      <fieldset>
-        <legend><?php echo $this->_('Général'); ?></legend>
-        <table cellspacing="2">
-          <tr>
-            <td class="droite"><?php echo $this->_('Style de boite'); ?>&nbsp;</td>
-            <td class="gauche"><?php echo $this->combo_templates ?></td>
-          </tr>
-
-          <tr>
-            <td class="droite"><?php echo $this->_('Titre de la boite'); ?>&nbsp;</td>
-            <td class="gauche">
-              <input type="text" name="titre" size="52" value="<?php echo $this->escape($this->preferences["titre"]); ?>">
-            </td>
-          </tr>
-        </table>
-      </fieldset>
-
-      <br>
-
-      <fieldset>
-        <legend><?php echo $this->_('Affichage'); ?></legend>
-        <table cellspacing="2" width="100%">
-          <tr>
-            <td class="droite"><?php echo $this->_('Type d\'affichage'); ?> </td>
-            <td class="gauche"><?php echo $this->formSelect('type_aff',
-                                                           $this->preferences['type_aff'],
-                                                           [],
-                                                           $this->displayModes);  ?></td>
-          </tr>
-        </table>
-
-        <div class="displayPaginatedOptions displayOptions" id="table_selection" style="display:none">
-          <?php
-          echo $this->treeSelect($this->preferences['id_items'],
-                                 $this->preferences['id_categorie'],
-                                 true,
-                                 $this->url(['modules' => 'admin',
-                                             'controller' => 'bibnum',
-                                             'action' => 'collections']),
-                                 "form");
-          ?>
-        </div>
-
-        <table>
-          <tr class="displayTreeOptions displayOptions" style="display:none">
-            <td class="droite"><?php echo $this->_('Collection'); ?> </td>
-            <td class="gauche">
-              <?php
-              $multiOptions = ['' => 'Toutes les collections'];
-
-              foreach ($this->categories as $categorie)
-                $multiOptions[$categorie->getId()] = $categorie->getLibelle();
-
-              echo $this->formSelect('id_categories',
-                                     $this->preferences['id_categories'],
-                                     [],
-                                     $multiOptions);
-              ?>
-            </td>
-          </tr>
-
-          <tr class="displayAlbumTeaserOptions displayOptions" style="display:none">
-            <td class="droite"><?php echo $this->_('Album'); ?> </td>
-            <td class="gauche">
-              <?php
-              $multiOptions = [];
-
-              foreach ($this->albums as $album) {
-                $hierarchy = $album->getHierarchy();
-                $hierarchy = array_reverse($hierarchy);
-                $parents = [];
-                foreach ($hierarchy as $parent) {
-                  $parents[] = $parent->getLibelle();
-                }
-
-                $multiOptions[$album->getId()] = implode(' > ', $parents) . ' > ' . $album->getTitre();
-              }
-
-              echo $this->formSelect('id_albums',
-                                     $this->preferences['id_albums'],
-                                     [],
-                                     $multiOptions);
-              ?>
-            </td>
-          </tr>
-
-          <tr class="displayAlbumTeaserOptions displayOptions" style="display:none">
-            <td class="droite" valign="top"><?php $this->_('Style de représentation');?> </td>
-            <td class="gauche"><?php echo $this->imageViewersOptions($this->preferences)?></td>
-          </tr>
-
-          <tr class="displayAlbumTeaserOptions displayOptions" style="display:none">
-            <td class="droite"><?php echo $this->_('Nb d\'images à afficher'); ?> </td>
-            <td class="gauche">
-              <?php
-              echo $this->formText('nb_aff', $this->preferences['nb_aff'], ['size' => '4']);
-              ?>
-            </td>
-          </tr>
-
-          <tr class="displayPaginatedOptions displayOptions" style="display:none">
-            <td class="droite"><?php echo $this->_('Orde d\'affichage'); ?> </td>
-            <td class="gauche">
-              <?php
-              echo $this->formRadio('display_order',
-                                    $this->preferences['display_order'],
-                                    [],
-                                    $this->orderModes);
-              ?>
-            </td>
-          </tr>
-
-          <tr class="displayPaginatedOptions displayOptions" style="display:none">
-            <td class="droite"><?php echo $this->_('Nbre de sites par page'); ?> </td>
-            <td class="gauche">
-              <input type="text" name="nb_aff_by_page" size="3" maxlength="3" value="<?php print($this->preferences['nb_aff_by_page']); ?>">
-            </td>
-          </tr>
-        </table>
-      </fieldset>
+    <?php
+    $form = ZendAfi_Form_Configuration_BibNumerique::newWith($this->preferences);
+    $form->setAction($this->url);
 
-      <?php echo $this->formSubmit('Valider', $this->_('Valider'), ['class' => 'bouton']) ?>
-    </form>
+    echo $this->renderForm($form);
+    ?>
   </div>
 </center>
diff --git a/library/ZendAfi/Form/Configuration/BibNumerique.php b/library/ZendAfi/Form/Configuration/BibNumerique.php
new file mode 100644
index 00000000000..b191f91c064
--- /dev/null
+++ b/library/ZendAfi/Form/Configuration/BibNumerique.php
@@ -0,0 +1,189 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+
+class ZendAfi_Form_Configuration_BibNumerique extends ZendAfi_Form {
+
+  public static function newWith($conf) {
+    $form = new self();
+
+    $selected_categories = array_key_exists('id_categorie', $conf) ? $conf['id_categorie'] : '';
+
+    $form
+      ->populate($conf)
+
+      ->populateAlbumsCategories('id_categorie', $selected_categories)
+      ->populateImageViewer('style_liste', $conf)
+
+      ->addDisplayGroup(['titre',
+                         'boite'],
+                        'base',
+                        ['legend' => $form->_('Général')])
+
+      ->addDisplayGroup(['type_aff',
+                         'id_categories',
+                         'id_albums',
+                         'style_liste',
+                         'nb_aff',
+                         'id_categorie',
+                         'display_order',
+                         'nb_aff_by_page'],
+                        'display',
+                        ['legend' => $form->_('Affichage')])
+
+      ->addDisplayGroup(['submit'],
+                        'submit',
+                        ['legend' => '']);
+
+    return $form;
+  }
+
+
+  public function init() {
+    parent::init();
+
+    Class_ScriptLoader::getInstance()->addJQueryReady($this->_getChangeDisplayScript());
+
+    $module = (new Class_Systeme_ModulesAccueil())->getModuleByCode('BIB_NUMERIQUE');
+
+    $this
+      ->setAttrib('id', 'configuration_bibnumerique')
+
+      ->addElement('text',
+                   'titre',
+                   ['label' => $this->_('Titre de la boite'),
+                    'size' => '52'])
+
+      ->addElement('select',
+                   'boite',
+                   ['label' => $this->_('Style de boite'),
+                    'multiOptions' => (
+                      new Class_Profil_Templates(
+                        Class_Profil::getCurrentProfil()))->toArray()])
+
+      ->addElement('select',
+                   'type_aff',
+                   ['label' => $this->_('Type d\'affichage'),
+                    'multiOptions' => $module->getDisplayModes()])
+
+      ->addElement('select',
+                   'id_categories',
+                   ['label' => $this->_('Collection'),
+                    'multiOptions' => $this->_getCollections()])
+
+      ->addElement('select',
+                   'id_albums',
+                   ['label' => $this->_('Album'),
+                    'multiOptions' => $this->_getAlbums()])
+
+      ->addElement('imagesViewer',
+                   'style_liste',
+                   ['label' => $this->_('Style de représentation'),
+                    'preferences' => ''])
+
+      ->addElement('text',
+                   'nb_aff',
+                   ['label' => $this->_('Nb d\'images à afficher'),
+                    'size' => '4'])
+
+      ->addElement('albumsCategoriesSelector',
+                   'id_categorie',
+                   ['label' => $this->_('Choisissez les dossiers à afficher'),
+                    'selected_categories' => ''])
+
+      ->addElement('radio',
+                   'display_order',
+                   ['label' => $this->_('Ordre d\'affichage'),
+                    'multiOptions' => $module->getOrderModes()])
+
+      ->addElement('text',
+                   'nb_aff_by_page',
+                   ['label' => $this->_('Nbre de sites par page'),
+                    'size' => '3',
+                   'maxlenght' =>  '3'])
+
+      ->addElement('submit',
+                   'submit',
+                   ['label' => $this->_('Valider'),
+                    'class' => 'bouton']);
+  }
+
+
+  public function populateAlbumsCategories($element, $selected_categories) {
+    $this->$element->setSelectedCategories($selected_categories);
+    return $this;
+  }
+
+
+  public function populateImageViewer($element, $preferences) {
+    $this->$element->setPreferences($preferences);
+    return $this;
+  }
+
+
+  protected function _getChangeDisplayScript() {
+    return
+      "$('select[name=\"type_aff\"]').change(function (event) {
+     if ($('select[name=\"type_aff\"]').val() == 'displayTree')
+       displayOnly($('#id_categories'));
+
+     if ($('select[name=\"type_aff\"]').val() == 'displayAlbumTeaser')
+       displayOnly($('#id_albums, #nb_aff, [name=\"style_liste\"]'));
+
+     if ($('select[name=\"type_aff\"]').val() == 'displayPaginated')
+       displayOnly($('#id_categorie, [name=\"display_order\"], #nb_aff_by_page'));
+   });
+
+   function displayOnly(selector) {
+     $('#fieldset-display tr').not(':first').hide();
+     selector.closest('tr').show();
+   }
+
+   $('select[name=\"type_aff\"]').change();";
+  }
+
+
+  protected function _getCollections() {
+    $collections = ['' => 'Toutes les collections'];
+
+    foreach (Class_AlbumCategorie::getLoader()->getCollections() as $categorie)
+      $collections[$categorie->getId()] = $categorie->getLibelle();
+
+    return $collections;
+  }
+
+
+  protected function _getAlbums() {
+    $albums = [];
+
+    foreach (Class_AlbumCategorie::getLoader()->findAlbumsRecursively() as $album) {
+      $hierarchy = array_reverse($album->getHierarchy());
+      $parents = [];
+      foreach ($hierarchy as $parent)
+        $parents[] = $parent->getLibelle();
+      $albums[$album->getId()] = implode(' > ', $parents) . ' > ' . $album->getTitre();
+    }
+
+    return $albums;
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Decorator/AlbumsCategoriesSelector.php b/library/ZendAfi/Form/Decorator/AlbumsCategoriesSelector.php
new file mode 100644
index 00000000000..fd5d56e6dfe
--- /dev/null
+++ b/library/ZendAfi/Form/Decorator/AlbumsCategoriesSelector.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+class ZendAfi_Form_Decorator_AlbumsCategoriesSelector extends Zend_Form_Decorator_Abstract {
+  public function render($content) {
+    $helper = $this->_element
+      ->getView()
+      ->getHelper('TreeSelect');
+
+    return $helper->treeSelect('',
+                               $this->_element->getSelectedCategories(),
+                               true,
+                               $this->_element->getView()->url(['module' => 'admin',
+                                                                'controller' => 'bibnum',
+                                                                'action' => 'collections']),
+                               'form');
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Decorator/ImageViewer.php b/library/ZendAfi/Form/Decorator/ImageViewer.php
new file mode 100644
index 00000000000..23e3538b353
--- /dev/null
+++ b/library/ZendAfi/Form/Decorator/ImageViewer.php
@@ -0,0 +1,32 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+class ZendAfi_Form_Decorator_ImageViewer extends Zend_Form_Decorator_Abstract {
+  public function render($content) {
+    $helper = $this->_element
+      ->getView()
+      ->getHelper('ImageViewersOptions');
+
+    return $helper->imageViewersOptions($this->_element->getPreferences());
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Element/AlbumsCategoriesSelector.php b/library/ZendAfi/Form/Element/AlbumsCategoriesSelector.php
new file mode 100644
index 00000000000..ac0a61ee820
--- /dev/null
+++ b/library/ZendAfi/Form/Element/AlbumsCategoriesSelector.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+class ZendAfi_Form_Element_AlbumsCategoriesSelector extends Zend_Form_Element {
+  protected
+    $_selected_categories;
+
+  public function __construct($spec, $options = null) {
+    parent::__construct($spec, $options);
+
+    $decorators = $this->_decorators;
+    $this->_decorators = ['AlbumsCategoriesSelector' => new ZendAfi_Form_Decorator_AlbumsCategoriesSelector()];
+
+    foreach ($decorators as $name => $value)
+      $this->_decorators[$name] = $value;
+    $this->removeDecorator('ViewHelper');
+
+  }
+
+
+  public function setSelectedCategories($selected_categories) {
+    $this->_selected_categories = $selected_categories;
+    return $this;
+  }
+
+
+  public function getSelectedCategories() {
+    return $this->_selected_categories;
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Element/ImagesViewer.php b/library/ZendAfi/Form/Element/ImagesViewer.php
new file mode 100644
index 00000000000..ee9261828aa
--- /dev/null
+++ b/library/ZendAfi/Form/Element/ImagesViewer.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+class ZendAfi_Form_Element_ImagesViewer extends Zend_Form_Element {
+  protected
+    $_preferences;
+
+  public function __construct($spec, $options = null) {
+    parent::__construct($spec, $options);
+
+    $decorators = $this->_decorators;
+    $this->_decorators = ['ImageViewer' => new ZendAfi_Form_Decorator_ImageViewer()];
+
+    foreach ($decorators as $name => $value)
+      $this->_decorators[$name] = $value;
+    $this->removeDecorator('ViewHelper');
+
+  }
+
+
+  public function setPreferences($preferences) {
+    $this->_preferences = $preferences;
+    return $this;
+  }
+
+
+  public function getPreferences() {
+    return $this->_preferences;
+  }
+}
+?>
\ No newline at end of file
diff --git a/tests/application/modules/admin/controllers/AccueilControllerTest.php b/tests/application/modules/admin/controllers/AccueilControllerTest.php
index b519d0a1999..430e3281891 100644
--- a/tests/application/modules/admin/controllers/AccueilControllerTest.php
+++ b/tests/application/modules/admin/controllers/AccueilControllerTest.php
@@ -132,6 +132,13 @@ class AccueilControllerBibliothequeNumeriqueTest extends Admin_AbstractControlle
   public function selectionOfDisplayStyleShouldBePresent() {
     $this->assertXPath('//select[@name="style_liste"]');
   }
+
+
+  /** @test */
+  public function selectionOfAlbumsCategoriesShouldBePresent() {
+    $this->assertXPath('//div[contains(@class, "treeselect")]');
+    $this->assertXPath('//input[@name="id_categorie"]');
+  }
 }
 
 
-- 
GitLab