From 3d3402e5a756ee3697767cc0cdfd40a427b53a6e Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Fri, 25 Mar 2016 12:07:23 +0100
Subject: [PATCH] dev #35088 start implementing POST

---
 .../controllers/DataProfileController.php     | 43 +++++++++++++
 .../controllers/DataProfileControllerTest.php | 61 ++++++++++++++++++-
 library/Class/IntProfilDonnees.php            | 13 +++-
 library/Class/ProfilePrefs.php                | 32 ++++++++--
 library/ZendAfi/Form/Cosmo/DataProfile.php    | 20 ++++--
 5 files changed, 157 insertions(+), 12 deletions(-)

diff --git a/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php b/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php
index 9a349512ec4..f1a0a3c136e 100644
--- a/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php
+++ b/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php
@@ -36,5 +36,48 @@ class Cosmo_DataProfileController extends ZendAfi_Controller_Action {
 
             'form_class_name' => 'ZendAfi_Form_Cosmo_DataProfile'];
   }
+
+
+  protected function _setupFormAndSave($model) {
+    $form = $this->_getForm($model);
+
+    $this->view->form = $form;
+    if (!$this->_request->isPost())
+      return false;
+
+    $values = $form->getValues();
+    $attributes_values = $this->_extractAttributesFrom($values);
+    $model->updateAttributes($attributes_values);
+
+    $profile_prefs = $this->_extractProfilePrefsFrom($values);
+    $model->setAttributs($profile_prefs);
+
+    if ((!$form->isValidModelAndArray($model, $this->_getPost())))
+      return false;
+
+    if  (!$model->save())
+      return false;
+
+    return true;
+  }
+
+
+  protected function _extractAttributesFrom($values) {
+    $default_values = Class_IntProfilDonnees::getClassVar('_default_attribute_values');
+    return array_intersect_key($values, $default_values);
+  }
+
+
+  protected function _extractProfilePrefsFrom($values) {
+    $prefs = array_diff_key(Class_IntProfilDonnees::getClassVar('_default_attribute_values'), $values);
+    return 'a:3:{i:0;a:1:{s:8:"type_doc";a:0:{}}i:1;a:0:{}i:2;a:0:{}}';
+  }
+
+
+  protected function _getEditUrl($model) {
+    return sprintf('/cosmo/%s/edit/id/%s',
+                   $this->_request->getControllerName(), $model->getId());
+  }
+
 }
 ?>
\ No newline at end of file
diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
index 4f7bac9b8b0..822f86c9c3c 100644
--- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
+++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php
@@ -316,5 +316,64 @@ class Cosmo_DataProfileControllerEditUnimarcKohaFileFormatTest extends Cosmo_Dat
   public function multiInputDocTypeOneShouldBePresent() {
     $this->assertXPath('//form//div[@id="multi_inputs_item_doc_type_1"]');
   }
+
+
+  /** @test */
+  public function docTypeBookShouldBeAmNaLivLivjLivaDocManuel() {
+    $this->assertXPathContentContains('//script', 'values:{"item_doc_type_label":["am","na"],"item_doc_type_zone":["LIV","LIVJ","LIVA","DOC","MANUEL"]}');
+  }
+}
+
+
+
+class Cosmo_DataProfileControllerPostEditUnimarcKohaFileFormatTest extends Cosmo_DataProfileControllerTestCase {
+  protected $_storm_default_to_volatile = false;
+
+  public function setUp() {
+    parent::setUp();
+
+    $post = ['libelle' => 'New Koha',
+             'accents' => Class_IntProfilDonnees::ENCODING_ISO2709,
+             'type_fichier' => Class_IntProfilDonnees::FT_PATRONS,
+             'format' => Class_IntProfilDonnees::FORMAT_CSV,
+             'xml_balise_abonne' => 'subscribers',
+             'xml_abonne_IDABON' => 'id',
+             'subscribers' => 'ORDREABON; ORDREABON',
+             'loans' => 'ID_PERGAME; ID_PERGAME',
+             'holds' => 'SUPPORT; SUPPORT',
+             'light_records' => 'ean;ean',
+             'champ_code_barres'=> 'f',
+             'champ_cote' => 'k',
+             'champ_type_doc' => 'x',
+             'champ_section' => 'q',
+             'champ_genre' => 'z',
+             'champ_emplacement' => 'u',
+             'champ_annexe' => 'a',
+             'champ_availability' => 't',
+             'rejet_periodiques' => 1,
+             'id_article_periodique' => 1,
+             'nouveaute_format' => 4,
+             'nouveaute_jours' => 80,
+             'carts' => 'LIBELLE; ROLE'];
+
+    $this->postDispatch('cosmo/data-profile/edit/id/1', $post, true);
+  }
+
+
+  /** @test */
+  public function shouldRedirectToCosmoSlashDataProfileSlashEditSlashIdSlashOne() {
+    $this->assertRedirectTo('/cosmo/data-profile/edit/id/1');
+  }
+
+
+  /** @test */
+  public function libelleShouldBeNewKoha() {
+    $this->assertEquals('New Koha', Class_IntProfilDonnees::find(1)->getLibelle());
+  }
+
+
+  /** @test */
+  public function accentsShouldBeISO2709() {
+    $this->assertEquals(Class_IntProfilDonnees::ENCODING_ISO2709, Class_IntProfilDonnees::find(1)->getAccents());
+  }
 }
-?>
\ No newline at end of file
diff --git a/library/Class/IntProfilDonnees.php b/library/Class/IntProfilDonnees.php
index 488be00eff8..d0cf79384d7 100644
--- a/library/Class/IntProfilDonnees.php
+++ b/library/Class/IntProfilDonnees.php
@@ -164,6 +164,7 @@ class Class_IntProfilDonnees extends Storm_Model_Abstract {
     FIELD_ITEM_URL = 'champ_url',
     PROFILE_PREFS_ITEMS = 0,
     PROFILE_PREFS_ITEMS_NEWS_DATE = 1,
+    PROFILE_DOC_TYPES = 'type_doc',
     NEWS_ZONE = 'zone',
     NEWS_FIELD = 'champ',
     NEWS_FORMAT = 'format',
@@ -172,7 +173,15 @@ class Class_IntProfilDonnees extends Storm_Model_Abstract {
   protected
     $_table_name = 'profil_donnees',
     $_table_primary = 'id_profil',
-    $_loader_class = 'IntProfilDonneesLoader';
+    $_loader_class = 'IntProfilDonneesLoader',
+    $_default_attribute_values = ['libelle' => '',
+                                  'accents' => 0,
+                                  'rejet_periodiques' => 0,
+                                  'id_article_periodique' => 0,
+                                  'type_fichier' => 0,
+                                  'format' => 0,
+                                  'attributs' => 'a:3:{i:0;a:1:{s:8:"type_doc";a:0:{}}i:1;a:0:{}i:2;a:0:{}}'];
+
 
   public static function forNanook() {
     return self::
@@ -357,7 +366,7 @@ class Class_IntProfilDonnees extends Storm_Model_Abstract {
                               'type_fichier' => self::FT_RECORDS,
                               'format' => self::FORMAT_UNIMARC,
                               'attributs' => [
-                                              ['type_doc' => $type_doc,
+                                              [self::PROFILE_DOC_TYPES => $type_doc,
                                                self::FIELD_ITEM_BARCODE => 'f',
                                                self::FIELD_ITEM_COTE => 'k',
                                                self::FIELD_ITEM_TYPE_DOC => 'r',
diff --git a/library/Class/ProfilePrefs.php b/library/Class/ProfilePrefs.php
index 81063e45780..0d4b729dda5 100644
--- a/library/Class/ProfilePrefs.php
+++ b/library/Class/ProfilePrefs.php
@@ -80,11 +80,6 @@ class Class_ProfilePrefs extends Class_Entity {
   }
 
 
-  public function getItemNewsPrefs() {
-    return $this->getPrefs()[Class_IntProfilDonnees::PROFILE_PREFS_ITEMS_NEWS_DATE];
-  }
-
-
   public function getItemNewsFormat() {
     return $this->getItemNewsPrefs()[Class_IntProfilDonnees::NEWS_FORMAT];
   }
@@ -97,11 +92,38 @@ class Class_ProfilePrefs extends Class_Entity {
   }
 
 
+  public function getItemDocTypeSettings($id) {
+    $all_doc_types = $this->getDocTypesPrefs();
+    foreach($all_doc_types as $doc_type_pref) {
+      if($id == $doc_type_pref['code'])
+        return $this->_buildDocTypeForMultiInput($doc_type_pref);
+    }
+    return ['item_doc_type_label' => [],
+            'item_doc_type_zone' => []];
+  }
+
+
+  protected function _buildDocTypeForMultiInput($pref) {
+    return ['item_doc_type_label' => array_filter(explode(';', $pref['label'])),
+            'item_doc_type_zone' => array_filter(explode(';', $pref['zone_995']))];
+  }
+
+
+  public function getDocTypesPrefs() {
+    return $this->getItemPrefs()[Class_IntProfilDonnees::PROFILE_DOC_TYPES];
+  }
+
+
   public function getItemPrefs() {
     return $this->getPrefs()[Class_IntProfilDonnees::PROFILE_PREFS_ITEMS];
   }
 
 
+  public function getItemNewsPrefs() {
+    return $this->getPrefs()[Class_IntProfilDonnees::PROFILE_PREFS_ITEMS_NEWS_DATE];
+  }
+
+
   public function getPrefs() {
     return unserialize($this->getDatas()['attributs']);
   }
diff --git a/library/ZendAfi/Form/Cosmo/DataProfile.php b/library/ZendAfi/Form/Cosmo/DataProfile.php
index cc044c3582e..f25cb70f64f 100644
--- a/library/ZendAfi/Form/Cosmo/DataProfile.php
+++ b/library/ZendAfi/Form/Cosmo/DataProfile.php
@@ -37,7 +37,8 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
       ->populateItemAvailability()
       ->populateItemNewsZoneAndField()
       ->populateItemNewsFormat()
-      ->populateItemNewsValeurs();
+      ->populateItemNewsValeurs()
+      ->populateItemDocTypes();
 
     return $form;
   }
@@ -95,7 +96,7 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
 
 
   protected function _draggableGroup($id, $values, $label) {
-    $this->_addDraggable($id, Class_IntProfilDonnees::getLoanFields());
+    $this->_addDraggable($id, $values);
     return $this->addDisplayGroup([$id],
                                   $id . '_group',
                                   ['legend' => $this->_('Configuration des champs pour le fichier %s', $label)]);
@@ -177,8 +178,7 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
                              $id,
                              ['label' => $doc_type->getLabel(),
                               'fields' => [['name' => 'item_doc_type_label', 'label' => $this->_('label')],
-                                           ['name' => 'item_doc_type_zone', 'label' => $this->_('Zone')]],
-                              'fixed' => true]);
+                                           ['name' => 'item_doc_type_zone', 'label' => $this->_('Zone')]]]);
   }
 
 
@@ -369,6 +369,18 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form {
   }
 
 
+  public function populateItemDocTypes() {
+    $doc_types = Class_TypeDoc::findAll();
+    foreach($doc_types as $doc_type) {
+      $doc_type_id = $doc_type->getId();
+      $element_id = 'item_doc_type_' . $doc_type_id;
+      $this->$element_id->setValues($this->_profile_prefs->getItemDocTypeSettings($doc_type_id));
+    }
+
+    return $this;
+  }
+
+
   public function setProfilePrefs($preferences) {
     $this->_profile_prefs = $preferences;
   }
-- 
GitLab