From 26879038d3edc5aebcee8ff508e8abad27c1dcba Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Wed, 30 Mar 2016 17:20:15 +0200
Subject: [PATCH] dev #35088 conditioning fieldset display

---
 .../controllers/DataProfileController.php     |   8 +-
 .../controllers/DataProfileControllerTest.php |  15 +-
 library/Class/ProfileSerializer.php           |   2 +-
 library/ZendAfi/Form/Cosmo/DataProfile.php    | 135 ++++++++++++++----
 .../View/Helper/FormSortableConnectLists.php  |  12 +-
 5 files changed, 128 insertions(+), 44 deletions(-)

diff --git a/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php b/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php
index 3a4d37a1dcf..2a8e9df5da5 100644
--- a/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php
+++ b/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php
@@ -80,16 +80,16 @@ class Cosmo_DataProfileController extends ZendAfi_Controller_Action {
   protected function _cleansFieldsValues($values, $model) {
     $file_type = $model->getTypeFichier();
 
-    if(!Class_IntProfilDonnees::FT_RECORDS == $file_type)
+    if(Class_IntProfilDonnees::FT_RECORDS != $file_type)
       unset($values['light_records']);
 
-    if(!Class_IntProfilDonnees::FT_HOLDS == $file_type)
+    if(Class_IntProfilDonnees::FT_HOLDS != $file_type)
       unset($values['holds']);
 
-    if(!Class_IntProfilDonnees::FT_BASKETS == $file_type)
+    if(Class_IntProfilDonnees::FT_BASKETS != $file_type)
       unset($values['carts']);
 
-    if(!Class_IntProfilDonnees::FT_LOANS == $file_type)
+    if(Class_IntProfilDonnees::FT_LOANS != $file_type)
       unset($values['loans']);
 
     return $values;
diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
index 3cf3ef81669..4a1f6418fc2 100644
--- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
+++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
@@ -184,7 +184,7 @@ class Cosmo_DataProfileControllerEditUnimarcKohaFileFormatTest extends Cosmo_Dat
 
   /** @test */
   public function subscribersShouldBePrenomAndNaissance() {
-    $this->assertXPath('//form//input[@name="subscribers"][@value="PRENOM; NAISSANCE"]');
+    $this->assertXPath('//form//input[@name="subscribers_light"][@value="PRENOM; NAISSANCE"]');
   }
 
 
@@ -390,7 +390,11 @@ class Cosmo_DataProfileControllerPostEditUnimarcKohaFileFormatTest extends Cosmo
                                      'new'],
              'interest_zone' => [0 => '932'],
              'interest_champ' =>  [0 => '7'],
-             'subscribers' => 'ORDREABON; ORDREABON'];
+             'holds' => 'SUPPORT; SUPPORT',
+             'carts' => 'LIBELLE; ROLE',
+             'light_records' => 'ean;ean',
+             'loans' => 'ID_PERGAME; ID_PERGAME',
+             'subscribers_light' => 'ORDREABON; ORDREABON'];
 
     $this->postDispatch('cosmo/data-profile/edit/id/56', $post, true);
 
@@ -500,11 +504,16 @@ class Cosmo_DataProfileControllerPostEditUnimarcKohaFileFormatTest extends Cosmo
   }
 
 
-
   /** @test */
   public function subscribersFieldsShouldContainsOrdreabon() {
     $this->assertEquals('ORDREABON; ORDREABON', $this->_koha->getSubscriberFieldsValues());
   }
+
+
+  /** @test */
+  public function loansFieldsShouldBeNull() {
+    $this->assertNull($this->_koha->getLoansFieldsValues());
+  }
 }
 
 
diff --git a/library/Class/ProfileSerializer.php b/library/Class/ProfileSerializer.php
index d30201b4720..7b7e0848d9b 100644
--- a/library/Class/ProfileSerializer.php
+++ b/library/Class/ProfileSerializer.php
@@ -120,7 +120,7 @@ class Class_ProfileSerializer {
   protected function _extractSubscribers() {
     return [Class_IntProfilDonnees::XML_SUBSCRIBER_FIELD => $this->_extractXmlSubscriber(),
             Class_IntProfilDonnees::XML_SUBSCRIBER_FIELDS => $this->_extractSubscriberFields(),
-            Class_IntProfilDonnees::PROFILE_FIELDS => $this->_datas['subscribers']];
+            Class_IntProfilDonnees::PROFILE_FIELDS => $this->_datas['subscribers_light']];
   }
 
 
diff --git a/library/ZendAfi/Form/Cosmo/DataProfile.php b/library/ZendAfi/Form/Cosmo/DataProfile.php
index e48bb963f21..c70e2c27753 100644
--- a/library/ZendAfi/Form/Cosmo/DataProfile.php
+++ b/library/ZendAfi/Form/Cosmo/DataProfile.php
@@ -26,24 +26,9 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
   public static function newWith($datas = [], $custom_form = null) {
     $form = parent::newWith($datas, $custom_form);
     $form->setProfilePrefs((new Class_ProfilePrefs())->setDatas($datas));
-    $form
-      ->populateItemBarCode()
-      ->populateItemCote()
-      ->populateItemDocType()
-      ->populateItemGenre()
-      ->populateItemSection()
-      ->populateItemEmplacement()
-      ->populateItemAnnexe()
-      ->populateItemAvailability()
-      ->populateItemNoveltyZoneAndField()
-      ->populateItemNoveltyFormat()
-      ->populateItemNoveltyDays()
-      ->populateItemNoveltyValeurs()
-      ->populateItemDocTypes()
-      ->populateItemUrl()
-      ->populateXmlSubribers()
-      ->populateFields()
-      ->populateInterest();
+    $form->populateForm();
+
+    static::addJavascripts($form);
 
     return $form;
   }
@@ -104,7 +89,8 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
     $this->_addDraggable($id, $values);
     return $this->addDisplayGroup([$id],
                                   $id . '_group',
-                                  ['legend' => $this->_('Configuration des champs pour le fichier %s', $label)]);
+                                  ['legend' => $this->_('Configuration des champs pour le fichier %s', $label),
+                                   'style' => 'display: none']);
   }
 
 
@@ -126,13 +112,13 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
                    'xml_balise_abonne',
                    ['label' => $this->_('Balise abonné (sans les crochets)')])
 
-      ->_addDraggable('subscribers', Class_IntProfilDonnees::getSubscriberFields())
+      ->_draggableGroup('subscribers_light', Class_IntProfilDonnees::getSubscriberFields(), $this->_('des abonnés'))
 
       ->addDisplayGroup(array_merge(['xml_balise_abonne'],
-                                    $xml_fields,
-                                    ['subscribers']),
-                        'subscribers_group',
-                        ['legend' => $this->_('Configuration des champs pour le fichier des abonnés')]);
+                                    $xml_fields),
+                        'subscribers_xml_group',
+                        ['legend' => $this->_('Configuration des champs pour le fichier des abonnés'),
+                         'style' => 'display: none']);
   }
 
 
@@ -174,7 +160,8 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
 
     return $this->addDisplayGroup($doc_types_ids,
                                   'item_doc_type_group',
-                                  ['legend' => $this->_('Type de documents')]);
+                                  ['legend' => $this->_('Type de documents'),
+                                   'style' => 'display: none']);
   }
 
 
@@ -201,7 +188,8 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
                 ->addDisplayGroup(['rejet_periodiques',
                                    'id_article_periodique'],
                                   'item_serial_group',
-                                  ['legend' => $this->_('Périodiques')]);
+                                  ['legend' => $this->_('Périodiques'),
+                                   'style' => 'display: none']);
   }
 
 
@@ -217,7 +205,8 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
 
                 ->addDisplayGroup(['interet_zone'],
                                   'item_interest_group',
-                                  ['legend' => $this->_('Indexation')]);
+                                  ['legend' => $this->_('Indexation'),
+                                   'style' => 'display: none']);
   }
 
 
@@ -253,7 +242,8 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
                                               array_keys($elements_with_all_fields),
                                               ['item_url']),
                                   'item_group',
-                                  ['legend' => $this->_('Champs exemplaires')]);
+                                  ['legend' => $this->_('Champs exemplaires'),
+                                   'style' => 'display: none']);
   }
 
 
@@ -293,8 +283,9 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
                                    'nouveaute_format',
                                    'nouveaute_valeurs',
                                    'nouveaute_jours'],
-                                  'records_news_date',
-                                  ['legend' => $this->_('Date de nouveauté')]);
+                                  'item_novelty',
+                                  ['legend' => $this->_('Date de nouveauté'),
+                                   'style' => 'display: none']);
   }
 
 
@@ -394,7 +385,7 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
 
   public function populateXmlSubribers() {
     $this->xml_balise_abonne->setValue($this->_profile_prefs->getSubscriberXmlField());
-    $this->subscribers->setValue($this->_profile_prefs->getSubscriberFields());
+    $this->subscribers_light->setValue($this->_profile_prefs->getSubscriberFields());
 
     $xml_fields = Class_IntProfilDonnees::getSubscriberFields();
 
@@ -433,5 +424,87 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
   public function setProfilePrefs($preferences) {
     $this->_profile_prefs = $preferences;
   }
+
+
+  public function populateForm() {
+    return $this
+      ->populateItemBarCode()
+      ->populateItemCote()
+      ->populateItemDocType()
+      ->populateItemGenre()
+      ->populateItemSection()
+      ->populateItemEmplacement()
+      ->populateItemAnnexe()
+      ->populateItemAvailability()
+      ->populateItemNoveltyZoneAndField()
+      ->populateItemNoveltyFormat()
+      ->populateItemNoveltyDays()
+      ->populateItemNoveltyValeurs()
+      ->populateItemDocTypes()
+      ->populateItemUrl()
+      ->populateXmlSubribers()
+      ->populateFields()
+      ->populateInterest();
+  }
+
+
+  public static function addJavascripts($form) {
+    $instance = Class_ScriptLoader::getInstance();
+
+    $javascript = '$("#fieldset-general select").on("change", function() {$(this).closest("form").submit()});';
+
+    return $instance->addJQueryReady($javascript . static::filterGroupsToDisplay($form));
+  }
+
+
+  public static function filterGroupsToDisplay($form) {
+    $show = '$("fieldset[id*=%s]").show();';
+
+    $current_data_type = $form->type_fichier->getValue();
+    $current_format = $form->format->getValue();
+
+    if(Class_IntProfilDonnees::FT_RECORDS === $current_data_type
+       && (Class_IntProfilDonnees::FORMAT_UNIMARC === $current_format
+           || Class_IntProfilDonnees::FORMAT_MARC21 === $current_format
+           || Class_IntProfilDonnees::FORMAT_UNIMARC_XML === $current_format))
+      return sprintf($show, 'item');
+
+    if(Class_IntProfilDonnees::FT_RECORDS === $current_data_type
+       && Class_IntProfilDonnees::FORMAT_XML !== $current_format)
+      return sprintf($show, 'light_records');
+
+    if(Class_IntProfilDonnees::FT_PATRONS === $current_data_type
+       && Class_IntProfilDonnees::FORMAT_XML === $current_format)
+      return sprintf($show, 'subscribers_xml');
+
+    if(Class_IntProfilDonnees::FT_PATRONS === $current_data_type
+       && (Class_IntProfilDonnees::FORMAT_UNIMARC !== $current_format
+           && Class_IntProfilDonnees::FORMAT_MARC21 !== $current_format
+           && Class_IntProfilDonnees::FORMAT_UNIMARC_XML !== $current_format))
+      return sprintf($show, 'subscribers_light');
+
+    if(Class_IntProfilDonnees::FT_LOANS === $current_data_type
+       && (Class_IntProfilDonnees::FORMAT_UNIMARC !== $current_format
+           && Class_IntProfilDonnees::FORMAT_MARC21 !== $current_format
+           && Class_IntProfilDonnees::FORMAT_XML !== $current_format
+           && Class_IntProfilDonnees::FORMAT_UNIMARC_XML !== $current_format))
+      return sprintf($show, 'loans');
+
+    if(Class_IntProfilDonnees::FT_HOLDS === $current_data_type
+       && (Class_IntProfilDonnees::FORMAT_UNIMARC !== $current_format
+           && Class_IntProfilDonnees::FORMAT_MARC21 !== $current_format
+           && Class_IntProfilDonnees::FORMAT_XML !== $current_format
+           && Class_IntProfilDonnees::FORMAT_UNIMARC_XML !== $current_format))
+      return sprintf($show, 'holds');
+
+    if(Class_IntProfilDonnees::FT_BASKETS === $current_data_type
+       && (Class_IntProfilDonnees::FORMAT_UNIMARC !== $current_format
+           && Class_IntProfilDonnees::FORMAT_MARC21 !== $current_format
+           && Class_IntProfilDonnees::FORMAT_XML !== $current_format
+           && Class_IntProfilDonnees::FORMAT_UNIMARC_XML !== $current_format))
+      return sprintf($show, 'carts');
+
+    return '';
+  }
 }
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/FormSortableConnectLists.php b/library/ZendAfi/View/Helper/FormSortableConnectLists.php
index 28bb7763007..3e509dc8f7e 100644
--- a/library/ZendAfi/View/Helper/FormSortableConnectLists.php
+++ b/library/ZendAfi/View/Helper/FormSortableConnectLists.php
@@ -24,7 +24,7 @@ class ZendAfi_View_Helper_FormSortableConnectLists extends ZendAfi_View_Helper_B
     $output = '<div><h2>' . $title . '</h2>';
     $output .= '<ul>';
     foreach ($values as $v => $label) {
-      $output .= '<li data-value="' . $v . '">' . $label . '</li>';
+      $output .= '<li data-value="' . preg_replace('/[0-9]/', '', $v) . '">' . $label . '</li>';
     }
     $output .= '</ul>';
     $output .= '</div>';
@@ -36,11 +36,13 @@ class ZendAfi_View_Helper_FormSortableConnectLists extends ZendAfi_View_Helper_B
   public function formSortableConnectLists($name, $value = null, $available_values = [], $options = []) {
     $values = explode(';', $value);
     $enabled_values = [];
-    foreach ($values as $v) {
-      if (isset($available_values[$v])) {
-        $enabled_values[$v] = $available_values[$v];
+
+    foreach ($values as $key => $v) {
+      if (isset($available_values[$v]))
+        $enabled_values[$v . $key] = $available_values[$v];
+
+      if(!$options['sticky_available'])
         unset($available_values[$v]);
-      }
     }
 
     $options = array_merge(['selected_label' => $this->view->_('Filtres activés'),
-- 
GitLab