diff --git a/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php b/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php index 2be5139c83b899e79c715b06332783a3d02adde4..37969af773be115130d74d5a4184387394395e15 100644 --- a/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php +++ b/cosmogramme/cosmozend/application/modules/cosmo/controllers/DataProfileController.php @@ -40,13 +40,14 @@ class Cosmo_DataProfileController extends ZendAfi_Controller_Action { protected function _setupFormAndSave($model) { + $model = $this->_autoUpdateFormatInModel($model); $form = $this->_getForm($model); $this->view->form = $form; if (!$this->_request->isPost()) return false; - $values = $this->_getPost(); + $values = $this->_autoUpdateFormat($this->_getPost()); $attributes_values = $this->_extractAttributesFrom($values); $model->updateAttributes($attributes_values); @@ -54,7 +55,7 @@ class Cosmo_DataProfileController extends ZendAfi_Controller_Action { $profile_prefs = $this->_extractProfilePrefsFrom($values); $model->setAttributs($profile_prefs); - if ((!$form->isValidModelAndArray($model, $this->_getPost()))) + if ((!$form->isValidModelAndArray($model, $values))) return false; if (!$model->save()) @@ -64,6 +65,27 @@ class Cosmo_DataProfileController extends ZendAfi_Controller_Action { } + protected function _autoUpdateFormatInModel($model) { + $attributes = $this->_autoUpdateFormat($model->toArray(), $model); + $model->updateAttributes($attributes) + ->save(); + return $model; + } + + + protected function _autoUpdateFormat($attributes) { + $old_format = $attributes['format']; + $default_formats = Class_IntProfilDonnees::getFormatsForType($attributes['type_fichier']); + + if(!key_exists($old_format, $default_formats)) { + $keys = array_keys($default_formats); + $attributes['format'] = array_shift($keys); + } + + return $attributes; + } + + protected function _extractAttributesFrom($values) { $default_values = Class_IntProfilDonnees::getClassVar('_default_attribute_values'); return array_intersect_key($values, $default_values); diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php index 86a56731513eeabf310fac12c80f0ba959d7b19e..fdae3475f619d7a57ecf8e33bd2c4a66f1f7e16c 100644 --- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php +++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DataProfileControllerTest.php @@ -939,4 +939,75 @@ class Cosmo_DataProfileControllerDispatchCSVItemsProfileTest extends Cosmo_DataP public function fieldIdLightRecordsShouldBeEmpty() { $this->assertXPath('//form//input[@id="csv_item_fields"][@value="PRENOM; NAISSANCE; PRENOM"]'); } +} + + + +class Cosmo_DataProfileControllerAutoFormatTest extends Cosmo_DataProfileControllerTestCase { + + public function setUp() { + parent::setUp(); + + $this->fixture('Class_IntProfilDonnees', + ['id' => 654, + 'libelle' => 'Nanook loans', + 'type_fichier' => Class_IntProfilDonnees::FT_LOANS, + 'format' => Class_IntProfilDonnees::FORMAT_UNIMARC, + 'attributs' => + [0 => [], + 1 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 2 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 3 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 4 => [], + 5 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 6 => []]]); + + $this->dispatch('cosmo/data-profile/edit/id/654', true); + } + + + /** @test */ + public function formatShouldBeSetToCSV() { + $this->assertXPath('//form//select[@id="format"]/option[@value="5"][@selected]', $this->_response->getBody()); + } + + + /** @test */ + public function formatShouldHaveBeenUpdatedToCSV() { + $this->assertEquals(Class_IntProfilDonnees::FORMAT_CSV, Class_IntProfilDonnees::find(654)->getFormat()); + } +} + + + +class Cosmo_DataProfileControllerAutoFormatPostTest extends Cosmo_DataProfileControllerTestCase { + + public function setUp() { + parent::setUp(); + + $this->fixture('Class_IntProfilDonnees', + ['id' => 654, + 'libelle' => 'Nanook loans', + 'type_fichier' => Class_IntProfilDonnees::FT_RECORDS, + 'format' => Class_IntProfilDonnees::FORMAT_UNIMARC, + 'attributs' => + [0 => [], + 1 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 2 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 3 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 4 => [], + 5 => ['champs' => 'PRENOM; NAISSANCE; PRENOM'], + 6 => []]]); + + $this->postDispatch('cosmo/data-profile/edit/id/654', + ['type_fichier' => Class_IntProfilDonnees::FT_LOANS, + 'format' => Class_IntProfilDonnees::FORMAT_UNIMARC], + true); + } + + + /** @test */ + public function formatShouldHaveBeenUpdatedToCSV() { + $this->assertEquals(Class_IntProfilDonnees::FORMAT_CSV, Class_IntProfilDonnees::find(654)->getFormat()); + } } \ No newline at end of file diff --git a/library/Class/IntProfilDonnees.php b/library/Class/IntProfilDonnees.php index eb5bb50b8f6ec795ba7d317f143befb814d39e3b..1523b9458050e6b2d219676bebe12888710bced8 100644 --- a/library/Class/IntProfilDonnees.php +++ b/library/Class/IntProfilDonnees.php @@ -67,6 +67,57 @@ class IntProfilDonneesLoader extends Storm_Model_Loader { } + public function getFormatsForType($type) { + if($this->isTypeCSV($type)) + return $this->getCSVFormats(); + + if($type == Class_IntProfilDonnees::FT_RECORDS) + return $this->getRecordsFormats(); + + if($type == Class_IntProfilDonnees::FT_PATRONS) + return $this->getPatronsFormats(); + + return $this->getFormats(); + } + + + public function isTypeCSV($type) { + return ($type == Class_IntProfilDonnees::FT_LOANS) + || ($type == Class_IntProfilDonnees::FT_HOLDS) + || ($type == Class_IntProfilDonnees::FT_BASKETS); + } + + + public function getRecordsFormats() { + return array_intersect_key($this->getFormats(), + [Class_IntProfilDonnees::FORMAT_UNIMARC => '', + Class_IntProfilDonnees::FORMAT_CSV => '', + Class_IntProfilDonnees::FORMAT_TABBED_ASCII => '', + Class_IntProfilDonnees::FORMAT_SEMI_COLON_ASCII => '', + Class_IntProfilDonnees::FORMAT_PIPED_ASCII => '', + Class_IntProfilDonnees::FORMAT_MARC21 => '', + Class_IntProfilDonnees::FORMAT_UNIMARC_XML => '', + Class_IntProfilDonnees::FORMAT_AVENIO => '', + Class_IntProfilDonnees::FORMAT_DUBLIN_CORE => '']); + } + + + public function getCSVFormats() { + return array_intersect_key($this->getFormats(), + [Class_IntProfilDonnees::FORMAT_CSV => '', + Class_IntProfilDonnees::FORMAT_TABBED_ASCII => '', + Class_IntProfilDonnees::FORMAT_SEMI_COLON_ASCII => '', + Class_IntProfilDonnees::FORMAT_PIPED_ASCII => '', + Class_IntProfilDonnees::FORMAT_AVENIO => '']); + } + + + public function getPatronsFormats() { + return array_intersect_key($this->getFormats(), + [Class_IntProfilDonnees::FORMAT_XML => '']) + $this->getCSVFormats(); + } + + public function isFormatCSV($format) { return in_array($format, [Class_IntProfilDonnees::FORMAT_CSV, Class_IntProfilDonnees::FORMAT_TABBED_ASCII, diff --git a/library/Class/ProfileSerializer/Abstract.php b/library/Class/ProfileSerializer/Abstract.php index ada42be8d6be9b580ef1c024352952dc71d0a070..4724e24ec4e922b05a899419d09aeb90524edfdd 100644 --- a/library/Class/ProfileSerializer/Abstract.php +++ b/library/Class/ProfileSerializer/Abstract.php @@ -30,6 +30,11 @@ abstract class Class_ProfileSerializer_Abstract { return $this; } + public function getFormats() { + return Class_IntProfilDonnees::getFormatsForType($this->_datas[Class_IntProfilDonnees::PROFILE_FILE_TYPE ]); + } + + abstract public function buildForm($form); abstract public function populateForm($form); } diff --git a/library/ZendAfi/Form/Cosmo/DataProfile.php b/library/ZendAfi/Form/Cosmo/DataProfile.php index 7057b4d43f26cd91eac57cb79d91cc91119f2432..cf4f75dad2bd2fed49ca5b82e2249b3a53e9dd0a 100644 --- a/library/ZendAfi/Form/Cosmo/DataProfile.php +++ b/library/ZendAfi/Form/Cosmo/DataProfile.php @@ -73,7 +73,7 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form { ->addElement('select', 'format', - ['multiOptions' => Class_IntProfilDonnees::getFormats(), + ['multiOptions' => $this->_strategy->getFormats(), 'label' => $this->_('Format du fichier')]) ->addDisplayGroup(['libelle', @@ -459,28 +459,14 @@ class ZendAfi_Form_Cosmo_DataProfile extends ZendAfi_Form { } + public function getDefaultFormat() { + return $this->_strategy->getDefaultFormat(); + } + + public function populate($datas) { parent::populate($datas); return $this->_strategy->populateForm($this); - - return $this - ->populateItemBarCode() - ->populateItemCote() - ->populateItemDocType() - ->populateItemGenre() - ->populateItemSection() - ->populateItemEmplacement() - ->populateItemAnnexe() - ->populateItemAvailability() - ->populateItemNoveltyZoneAndField() - ->populateItemNoveltyFormat() - ->populateItemNoveltyDays() - ->populateItemNoveltyValeurs() - ->populateItemDocTypes() - ->populateItemUrl() - ->populateXmlSubribers() - ->populateFields() - ->populateInterest(); } } ?> \ No newline at end of file