From 9b706727ae58f729e41be1cf91a092082ffd13f1 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Wed, 30 Mar 2016 10:58:36 +0200
Subject: [PATCH] dev #35088 implementing post

---
 .../controllers/DataProfileControllerTest.php | 59 +++++++++++++++--
 library/Class/IntProfilDonnees.php            | 30 +++++++++
 library/Class/ProfileSerializer.php           | 65 ++++++++++++-------
 3 files changed, 125 insertions(+), 29 deletions(-)

diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
index 28540fe8eab..178c9c1c541 100644
--- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
+++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
@@ -391,11 +391,7 @@ class Cosmo_DataProfileControllerPostEditUnimarcKohaFileFormatTest extends Cosmo
                                     2 => ''],
              'nouveaute_valeurs' => ['nouveau',
                                      'new'],
-             'subscribers' => 'ORDREABON; ORDREABON',
-             'loans' => 'ID_PERGAME; ID_PERGAME',
-             'holds' => 'SUPPORT; SUPPORT',
-             'light_records' => 'ean;ean',
-             'carts' => 'LIBELLE; ROLE'];
+             'subscribers' => 'ORDREABON; ORDREABON'];
 
     $this->postDispatch('cosmo/data-profile/edit/id/56', $post, true);
 
@@ -491,4 +487,55 @@ class Cosmo_DataProfileControllerPostEditUnimarcKohaFileFormatTest extends Cosmo
   public function noveltyValeursShouldBeNouveauAndNew() {
     $this->assertEquals('nouveau;new', $this->_koha->getNoveltyValues());
   }
-}
\ No newline at end of file
+
+
+  /** @test */
+  public function subscribersFieldsShouldContainsOrdreabon() {
+    $this->assertEquals('ORDREABON; ORDREABON', $this->_koha->getSubscriberFieldsValues());
+  }
+}
+
+
+
+class Cosmo_DataProfileControllerPostEditFieldsTest extends Cosmo_DataProfileControllerTestCase {
+
+  /** @test */
+  public function loansFieldsShouldContainsIdPergame() {
+    $this->postDispatch('cosmo/data-profile/edit/id/105', ['loans' => 'ID_PERGAME; ID_PERGAME'], true);
+    $this->assertEquals('ID_PERGAME; ID_PERGAME', Class_IntProfilDonnees::find(105)->getLoansFieldsValues());
+  }
+
+
+  /** @test */
+  public function lightRecordsFieldsShouldContainsEan() {
+    $this->postDispatch('cosmo/data-profile/edit/id/105', ['light_records' => 'ean;ean'], true);
+    $this->assertEquals('ean;ean', Class_IntProfilDonnees::find(105)->getLightRecordsFieldsValues());
+  }
+
+  /** @test */
+  public function holdsFieldsShouldContainsSupport() {
+    $this->postDispatch('cosmo/data-profile/edit/id/105', ['holds' => 'SUPPORT; SUPPORT'], true);
+    $this->assertEquals('SUPPORT; SUPPORT', Class_IntProfilDonnees::find(105)->getHoldsFieldsValues());
+  }
+
+
+  /** @test */
+  public function cartsFieldsShouldContainsLibelleAndRole() {
+    $this->postDispatch('cosmo/data-profile/edit/id/105', ['carts' => 'LIBELLE; ROLE'], true);
+    $this->assertEquals('LIBELLE; ROLE', Class_IntProfilDonnees::find(105)->getCartsFieldsValues());
+  }
+
+
+  /** @test */
+  public function docTypeFieldsShouldBeAsExpected() {
+    $doc_type = ['1_label' => ['am',
+                               'af',
+                               'bb'],
+                 '1_zone' => ['z',
+                              't',
+                              'u']];
+
+    $this->postDispatch('cosmo/data-profile/edit/id/105', $doc_type, true);
+    $this->assertEquals(['code' => '1', 'label' => 'am;af;bb', 'zone_995' => 'z;t;u'], Class_IntProfilDonnees::find(105)->getItemDocTypesPrefs()[1]);
+  }
+}
diff --git a/library/Class/IntProfilDonnees.php b/library/Class/IntProfilDonnees.php
index 11891ad117d..10ab0997255 100644
--- a/library/Class/IntProfilDonnees.php
+++ b/library/Class/IntProfilDonnees.php
@@ -774,6 +774,36 @@ class Class_IntProfilDonnees extends Storm_Model_Abstract {
   }
 
 
+  public function getSubscriberFieldsValues() {
+    return $this->getProfilePrefs()->getSubscriberFields();
+  }
+
+
+  public function getLoansFieldsValues() {
+    return $this->getProfilePrefs()->getUnknownFields();
+  }
+
+
+  public function getLightRecordsFieldsValues() {
+    return $this->getProfilePrefs()->getUnknownFields();
+  }
+
+
+  public function getHoldsFieldsValues() {
+    return $this->getProfilePrefs()->getUnknownFields();
+  }
+
+
+  public function getCartsFieldsValues() {
+    return $this->getProfilePrefs()->getUnknownFields();
+  }
+
+
+  public function getItemDocTypesPrefs() {
+    return $this->getProfilePrefs()->getDocTypesPrefs();
+  }
+
+
   public function getProfilePrefs() {
     if(!$this->_profile_prefs)
       $this->_profile_prefs = (new Class_ProfilePrefs())->setDatas($this->toArray());
diff --git a/library/Class/ProfileSerializer.php b/library/Class/ProfileSerializer.php
index 00e763afea7..12b29a87c41 100644
--- a/library/Class/ProfileSerializer.php
+++ b/library/Class/ProfileSerializer.php
@@ -31,11 +31,9 @@ class Class_ProfileSerializer {
 
   public function serializeDatas() {
     $datas = [$this->extractRecords(),
-            $this->extractNovelty(),
-            $this->extractSubscribers(),
-            $this->extractCarts(),
-            $this->extractLoans(),
-            $this->extractHolds()];
+              $this->extractNovelty(),
+              $this->extractProfileFields(),
+              $this->extractSubscribers()];
 
     return serialize($datas);
   }
@@ -47,7 +45,37 @@ class Class_ProfileSerializer {
 
 
   public function extractDocTypes() {
-    return [Class_IntProfilDonnees::PROFILE_DOC_TYPES => []];
+    $doc_types_prefs = [];
+    $doc_types = Class_TypeDoc::findAll();
+    foreach($doc_types as $doc_type) {
+      $doc_type_id = $doc_type->getId();
+      $doc_types_prefs[$doc_type_id] = ['code' => $doc_type_id,
+                                        'label' => $this->_extractDocTypeLabel($doc_type_id),
+                                        'zone_995' => $this->_extractDocTypeZone($doc_type_id)];
+    }
+
+    return [Class_IntProfilDonnees::PROFILE_DOC_TYPES => $doc_types_prefs];
+  }
+
+
+  protected function _extractDocTypeLabel($id) {
+    return $this->_extractDocTypeData($id . '_label');
+  }
+
+
+  protected function _extractDocTypeZone($id) {
+    return $this->_extractDocTypeData($id . '_zone');
+  }
+
+
+  protected function _extractDocTypeData($key) {
+    if(!isset($this->_datas[$key]))
+      return '';
+
+    if(!is_array($this->_datas[$key]))
+      return '';
+
+    return implode(';', $this->_datas[$key]);
   }
 
 
@@ -62,11 +90,6 @@ class Class_ProfileSerializer {
   }
 
 
-  public function extractInterestFields() {
-
-  }
-
-
   public function extractNovelty() {
     return [Class_IntProfilDonnees::PROFILE_NOVELTY => $this->_datas['nouveaute_jours'],
             Class_IntProfilDonnees::NOVELTY_ZONE => $this->_datas['nouveaute_zone'][1],
@@ -89,22 +112,18 @@ class Class_ProfileSerializer {
 
   public function extractSubscribers() {
     return [Class_IntProfilDonnees::XML_SUBSCRIBER_FIELD => $this->extractXmlSubscriber(),
-            Class_IntProfilDonnees::XML_SUBSCRIBER_FIELDS => $this->extractSubscriberFields()];
-  }
-
-
-  public function extractCarts() {
-
-  }
-
-
-  public function extractLoans() {
-
+            Class_IntProfilDonnees::XML_SUBSCRIBER_FIELDS => $this->extractSubscriberFields(),
+            Class_IntProfilDonnees::PROFILE_FIELDS => $this->_datas['subscribers']];
   }
 
 
-  public function extractHolds() {
+  public function extractProfileFields() {
+    $fields = array_filter(array_intersect_key($this->_datas, ['loans' => '',
+                                                               'holds' => '',
+                                                               'light_records' => '',
+                                                               'carts' => '']));
 
+    return [Class_IntProfilDonnees::PROFILE_FIELDS => array_shift($fields)];
   }
 
 
-- 
GitLab