From 945f909cc9a75430c37c25d3065b986976f48065 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?ANDRE=20s=C3=A9bastien?= <sandre@afi-sa.fr>
Date: Thu, 9 Dec 2021 12:03:45 +0100
Subject: [PATCH] hotline : #141233, #139975 : delete double accent convert

---
 VERSIONS_HOTLINE/141233                       |  1 +
 .../Cosmogramme/Integration/Record/Patron.php | 55 --------------
 library/Class/NoticeUnimarc/Writer.php        | 63 +++++++++++++---
 .../Integration/141233-patrons.txt            |  7 ++
 .../Integration/PhasePatronsTest.php          | 71 ++++++++++++++++++-
 5 files changed, 131 insertions(+), 66 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/141233
 create mode 100644 tests/library/Class/Cosmogramme/Integration/141233-patrons.txt

diff --git a/VERSIONS_HOTLINE/141233 b/VERSIONS_HOTLINE/141233
new file mode 100644
index 00000000000..34d32f118df
--- /dev/null
+++ b/VERSIONS_HOTLINE/141233
@@ -0,0 +1 @@
+ - ticket #141233, #139975 : Cosmogramme : Import des abonnés quelque soit son format, les accents sont maintenant bien récupéré
\ No newline at end of file
diff --git a/library/Class/Cosmogramme/Integration/Record/Patron.php b/library/Class/Cosmogramme/Integration/Record/Patron.php
index 5b44a1846c6..ec5d2449a05 100644
--- a/library/Class/Cosmogramme/Integration/Record/Patron.php
+++ b/library/Class/Cosmogramme/Integration/Record/Patron.php
@@ -41,8 +41,6 @@ class Class_Cosmogramme_Integration_Record_Patron {
 
 
   public function importFicheArray($enreg) {
-    $enreg = array_map([$this, 'changeAccents'],
-                       $enreg);
     $enreg = $this->prepareData($enreg);
 
     if ($library_code = $this->_known_fields->libraryCodeFrom($enreg)) {
@@ -107,8 +105,6 @@ class Class_Cosmogramme_Integration_Record_Patron {
 
 
   public function importFicheXml($data){
-    $data = $this->changeAccents($data);
-
     $data = new SimpleXMLElement($data);
     foreach($this->_champs as $champ => $balise)	{
       if (!$balise)
@@ -160,55 +156,4 @@ class Class_Cosmogramme_Integration_Record_Patron {
 
     return $this;
   }
-
-
-  private function changeAccents($chaine) {
-    if (!trim($chaine))
-      return $chaine;
-
-    $profil = $this->_integration->getProfilDonnees();
-
-    if ($profil->isWindowsEncoded())
-      return utf8_encode($chaine);
-
-    if ($profil->isAsciiDosEncoded())
-      return $this->_asciiDosDecode($chaine);
-
-    return $chaine;
-  }
-
-
-  protected function _asciiDosDecode($string) {
-    $new = '';
-    for($i=0; $i < strlen($string); $i++)
-      $new.=$this->dosDecode($string[$i]);
-
-    return utf8_encode($new);
-  }
-
-
-  private function dosDecode($char) {
-    switch($char) {
-      case 0xe9: $result = 'é';	break ;
-      case 0xe8: $result = 'è';	break ;
-      case 0xeb: $result = 'ë';	break ;
-      case 0xe4: $result = 'ä';	break ;
-      case 0xe2: $result = 'â';	break ;
-      case 0xef: $result = 'ï';	break ;
-      case 0xcf: $result = 'Ï';	break ;
-      case 0xee: $result = 'î';	break ;
-      case 0xce: $result = 'ÃŽ';	break ;
-      case 0xf4: $result = 'ô';	break ;
-      case 0xf6: $result = 'ö';	break ;
-      case 0xd6: $result = 'Ö';	break ;
-      case 0xfc: $result = 'ü';	break ;
-      case 0xdc: $result = 'Ü';	break ;
-      case 0xfb: $result = 'û';	break ;
-      case 0xe7: $result = 'ç';	break ;
-      case 0xc7: $result = 'Ç';	break ;
-      default: $result = $char;	break;
-    }
-
-    return $result;
-  }
 }
diff --git a/library/Class/NoticeUnimarc/Writer.php b/library/Class/NoticeUnimarc/Writer.php
index dabfb9d0c37..e4ae8a90a8c 100644
--- a/library/Class/NoticeUnimarc/Writer.php
+++ b/library/Class/NoticeUnimarc/Writer.php
@@ -26,7 +26,10 @@ class Class_NoticeUnimarc_Writer extends Class_NoticeUnimarc {
     RECORD_STATUS_NEW = 'n',
     RECORD_STATUS_UPDATED = 'c';
 
-  protected $_pattern_subfield_cache = [];
+  protected
+    $_pattern_subfield_cache = [],
+    $_ascii_decode_keys,
+    $_ascii_decode_values;
 
   public function setNotice($string, $type_accents = 0) {
     $this->reset_notice();
@@ -313,9 +316,9 @@ class Class_NoticeUnimarc_Writer extends Class_NoticeUnimarc {
     }
 
     // mise à jour du guide
-    ## adresse de base.
+    // adresse de base.
     $this->inner_guide['ba'] = self::LABEL_LENGTH + strlen($this->directory) + 1;
-    ## longueur de l'enregistrement iso2709
+    // longueur de l'enregistrement iso2709
     $this->inner_guide['rl'] = self::LABEL_LENGTH + strlen($this->directory) + strlen($data)+1;
 
     $this->guide = sprintf('%05d', $this->inner_guide['rl'])
@@ -384,11 +387,17 @@ class Class_NoticeUnimarc_Writer extends Class_NoticeUnimarc {
 
   public function decode_accents($chaine, $type_accents) {
     switch ($type_accents) {
-    case Class_IntProfilDonnees::ENCODING_UTF8: return $chaine;
-    case Class_IntProfilDonnees::ENCODING_ISO2709: return $this->ISO_decode($chaine);
-    case Class_IntProfilDonnees::ENCODING_WINDOWS_ANSI: return $this->ansi_decode($chaine);
-    case Class_IntProfilDonnees::ENCODING_MARC21: return $this->marc21_decode($chaine);
-    default: return $chaine;
+      case Class_IntProfilDonnees::ENCODING_UTF8:
+        return $chaine;
+      case Class_IntProfilDonnees::ENCODING_ISO2709:
+        return $this->ISO_decode($chaine);
+      case Class_IntProfilDonnees::ENCODING_WINDOWS_ANSI:
+        return $this->ansi_decode($chaine);
+      case Class_IntProfilDonnees::ENCODING_ASCII_DOS:
+        return $this->_asciiDosDecode($chaine);
+      case Class_IntProfilDonnees::ENCODING_MARC21:
+        return $this->marc21_decode($chaine);
+      default: return $chaine;
     }
   }
 
@@ -770,13 +779,47 @@ class Class_NoticeUnimarc_Writer extends Class_NoticeUnimarc {
       'N'.chr(0xb0) => 'N°',
     ];
 
-
-
     $this->_marc21_decode_table_2709 = array_keys($this->_marc21_decode_table);
     $this->_marc21_decode_table_utf8 = array_values($this->_marc21_decode_table);
   }
 
 
+  protected function _asciiDosDecode($value) {
+    if (!isset($this->_ascii_decode_keys))
+      $this->_initDosDecodeTables();
+
+    return str_replace($this->_ascii_decode_keys,
+                       $this->_ascii_decode_values,
+                       $value);
+  }
+
+
+  protected function _initDosDecodeTables() {
+    $ascii_decode_table = [chr(0xe9) => 'é',
+                           chr(0xe8) => 'è',
+                           chr(0xeb) => 'ë',
+                           chr(0xe4) => 'ä',
+                           chr(0xe2) => 'â',
+                           chr(0xef) => 'ï',
+                           chr(0xcf) => 'Ï',
+                           chr(0xee) => 'î',
+                           chr(0xce) => 'ÃŽ',
+                           chr(0xf4) => 'ô',
+                           chr(0xf6) => 'ö',
+                           chr(0xd6) => 'Ö',
+                           chr(0xfc) => 'ü',
+                           chr(0xdc) => 'Ü',
+                           chr(0xfb) => 'û',
+                           chr(0xe7) => 'ç',
+                           chr(0xc7) => 'Ç'
+    ];
+
+    $this->_ascii_decode_keys = array_keys($ascii_decode_table);
+    $this->_ascii_decode_values = array_values($ascii_decode_table);
+    return $this;
+  }
+
+
   public function addSerializedFields($serialized) {
     if (!$fields = unserialize($serialized))
       return $this;
diff --git a/tests/library/Class/Cosmogramme/Integration/141233-patrons.txt b/tests/library/Class/Cosmogramme/Integration/141233-patrons.txt
new file mode 100644
index 00000000000..14b9ab84fd5
--- /dev/null
+++ b/tests/library/Class/Cosmogramme/Integration/141233-patrons.txt
@@ -0,0 +1,7 @@
+DELBECQUE	Céline	96229000021600	35 rue du Docteur Schweitzer		62640	MONTIGNY-EN-GOHELLE	16/06/1980	09/06/2009	11/03/2022	0	nehra.tyler@hotmail.fr
+DELBECQUE	Raphaëlle	96229000021642	35 rue du Docteur Schweitzer		62640	MONTIGNY-EN-GOHELLE	22/03/2010	14/12/2011	02/08/2022	0	celine.delbecque@laposte.net
+DELBECQUE	Philippe	96229000021634	35 rue du Docteur Schweitzer		62640	MONTIGNY-EN-GOHELLE	29/06/1977	14/12/2011	02/08/2022	0	nehra.tyler@hotmail.fr
+DELBECQUE	Gabriel	96229000021618	35 rue du Docteur Schweitzer		62640	MONTIGNY-EN-GOHELLE	17/10/2006	14/12/2011	02/08/2022	0	nehra.tyler@hotmail.fr
+DELBECQUE	Aurore	96229000385211	80, route de Leforest		59162	Ostricourt	29/09/1980	14/10/2016	29/09/2021	4	aurore.gorski@gmail.com
+DELBECQUE	Jennifer	96229000234054	42 rue de bretagne		62300	Lens	28/06/1992	15/11/2016	01/03/2022	0	jml.delb@hotmail.fr
+DELBECQUE	Nawal	96229000393249	11 rue Martha Desrumaux		62320	Drocourt	21/07/1985	11/02/2020	17/02/2022	0	
diff --git a/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php b/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php
index c5910e2c7ce..fac442161c3 100644
--- a/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php
+++ b/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php
@@ -775,4 +775,73 @@ class PhasePatronsCsvDosTest extends PhasePatronsTestCase {
   public function idabonShouldBe1000305148() {
     $this->assertEquals('1000305148', $this->_helene->getIdabon());
   }
-}
\ No newline at end of file
+}
+
+
+
+
+/* hotline: #141233, #139975 */
+class PhasePatronsImportOrpheeTest
+  extends Class_Cosmogramme_Integration_PhaseTestCase {
+
+  protected function _getPreviousPhase() {
+    return (new Class_Cosmogramme_Integration_Phase(7))
+      ->beCron();
+  }
+
+
+  public function _prepareFixtures() {
+    $bib_annecy = $this->fixture(Class_Bib::class,
+                                 ['id' => 1,
+                                  'libelle' => 'Annecy']);
+
+    $this->fixture(Class_CodifAnnexe::class,
+                   ['id' => 34,
+                    'id_origine' => 'ANNECY',
+                    'code' => 'ANNECY',
+                    'id_bib' => 1]);
+
+    $this->fixture(Class_IntBib::class,
+                   ['id' => 1,
+                    'comm_sigb' => Class_IntBib::COM_ORPHEE,
+                    'sigb' => Class_IntBib::SIGB_ORPHEE,
+                    'nom_court' => 'Annecy'
+                   ]);
+
+    $this->fixture(Class_IntProfilDonnees::class,
+                   ['id' => 102,
+                    'libelle' => 'Patrons',
+                    'accents' => Class_IntProfilDonnees::ENCODING_WINDOWS_ANSI,
+                    'type_fichier' => Class_IntProfilDonnees::FT_PATRONS,
+                    'format' => Class_IntProfilDonnees::FORMAT_TABBED_ASCII,
+                    'attributs' => [1 => ['champs' => 'NOM;PRENOM;IDABON;NULL;NULL;NULL;NULL;PASSWORD;DATE_DEBUT;DATE_FIN;NULL;MAIL']]]);
+
+    $this->fixture(Class_Cosmogramme_Integration::class,
+                   ['id' => 999,
+                    'bib' => Class_IntBib::find(1),
+                    'profil_donnees' => Class_IntProfilDonnees::find(102),
+                    'type_operation' => Class_Cosmogramme_Integration::TYPE_OPERATION_TOTAL,
+                    'traite' => 'non',
+                    'fichier' => '141233-patrons.txt',
+                    'pointeur_reprise' => 0]);
+  }
+
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->_phase = $this->_buildPhase('Patrons')
+                         ->setMemoryCleaner(function() {})
+                         ->setPrinter($this->_printer);
+
+    $this->_phase->run();
+    Class_Users::clearCache();
+  }
+
+
+  /** @test */
+  public function contextShouldExpectation() {
+    $user = Class_Users::findFirstBy(['idabon' => '96229000021600']);
+    $this->assertEquals('Céline DELBECQUE', $user->getNomComplet());
+  }
+}
-- 
GitLab