From ac202a6165c4d09a747013e70b3159b3a23849d5 Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr>
Date: Fri, 5 Jun 2020 19:43:46 +0200
Subject: [PATCH] hotline#109561 : DILICOM : Get Subjects from ONIX Files

---
 VERSIONS_HOTLINE/109561                       |  1 +
 .../BibNumerique/Dilicom/ONIXFile.php         | 36 ++++++++++++++++++-
 tests/scenarios/PnbDilicom/PnbDilicomTest.php | 25 +++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 VERSIONS_HOTLINE/109561

diff --git a/VERSIONS_HOTLINE/109561 b/VERSIONS_HOTLINE/109561
new file mode 100644
index 00000000000..5f10d19053c
--- /dev/null
+++ b/VERSIONS_HOTLINE/109561
@@ -0,0 +1 @@
+ - ticket #109561 : Moissonnage Dilicom : Intégrer les Balises Subject dans les mots matières
\ No newline at end of file
diff --git a/library/Class/WebService/BibNumerique/Dilicom/ONIXFile.php b/library/Class/WebService/BibNumerique/Dilicom/ONIXFile.php
index 61d7ba385c4..fb4d8659fad 100644
--- a/library/Class/WebService/BibNumerique/Dilicom/ONIXFile.php
+++ b/library/Class/WebService/BibNumerique/Dilicom/ONIXFile.php
@@ -32,7 +32,8 @@ class Class_WebService_BibNumerique_Dilicom_ONIXFile {
     RESOURCE_CONTENT_TYPE_WIDGET = '16',
     USAGE_UNIT_DAYS = '09',
     USAGE_UNIT_MONTHS = '14',
-    USAGE_UNIT_USERS = '07';
+    USAGE_UNIT_USERS = '07',
+    SUBJECT_SCHEME_IDENTIFIER_CLIL = '29';
 
   protected
     $_parser,
@@ -46,6 +47,9 @@ class Class_WebService_BibNumerique_Dilicom_ONIXFile {
     $_current_usage_constraint,
     $_current_quantity,
     $_current_usage_unit,
+    $_current_subject_label,
+    $_current_subject_scheme_identifier,
+    $_current_subject_scheme_version,
     $_tag_mapping = ['B203' => 'TitleText',
                      'B221' => 'ProductIDType',
                      'B244' => 'IDValue',
@@ -111,6 +115,36 @@ class Class_WebService_BibNumerique_Dilicom_ONIXFile {
   }
 
 
+  public function endSubjectHeadingText($content) {
+    $this->_current_subject_label = $content;
+  }
+
+
+  public function endSubjectSchemeIdentifier($content) {
+    $this->_current_subject_scheme_identifier = $content;
+  }
+
+
+  public function endSubjectSchemeVersion($content) {
+    $this->_current_subject_scheme_version = $content;
+  }
+
+
+  public function endSubject($content) {
+    if ($this->_current_subject_scheme_identifier == self::SUBJECT_SCHEME_IDENTIFIER_CLIL)
+      $this->_book->addMatiere($this->_current_subject_label);
+    $this->_resetSubject();
+  }
+
+
+  protected function _resetSubject(){
+    $this->_current_subject_label = null;
+    $this->_current_subject_scheme_identifier = null;
+    $this->_current_subject_scheme_version = null;
+    return $this;
+  }
+
+
   public function endIDValue($content) {
     $this->_current_product_id_value = $content;
   }
diff --git a/tests/scenarios/PnbDilicom/PnbDilicomTest.php b/tests/scenarios/PnbDilicom/PnbDilicomTest.php
index 50aec920e80..fad70b72fd3 100644
--- a/tests/scenarios/PnbDilicom/PnbDilicomTest.php
+++ b/tests/scenarios/PnbDilicom/PnbDilicomTest.php
@@ -622,6 +622,7 @@ class PnbDilicomRiviereEsperanceOffersParserTest extends PnbDilicomOfferParserTe
     $books = Class_WebService_BibNumerique_Dilicom_PNBOffersFile::booksFromXML($this->_xml);
     $books[0]->import();
     $this->_item = Class_Album_Item::find(1);
+    $this->_album = Class_Album::find(1);
   }
 
 
@@ -637,6 +638,17 @@ class PnbDilicomRiviereEsperanceOffersParserTest extends PnbDilicomOfferParserTe
     $this->assertEquals(50,
                         $this->_item->getUsageConstraints()->getLoanQuantity());
   }
+
+  /** @test */
+  public function albumMatiereShouldContains1() {
+    $this->assertContains('1',$this->_album->getMatiere());
+  }
+
+
+  /** @test */
+  public function codifMatiereOneShouldBeContesEtLegendesTerroir() {
+    $this->assertEquals('Contes et légendes / Terroir', Class_CodifMatiere::find(1)->getLibelle());
+  }
 }
 
 
@@ -1116,6 +1128,7 @@ class PnbDilicomBatchJobOnixWithFullTest extends PnbDilicomJobOnixTestCase {
     Class_Album::find(1)->index();
     $this->assertNotNull(Class_Exemplaire::findFirstBy(['id_origine' => 'Dilicom-9782290123409']));
     $this->assertNotNull(Class_Album::find(1)->getNotice());
+
     $this->assertEquals('Dilicom-9782290123409', Class_Notice::find(1)->getAlbum()->getIdOrigine());
   }
 
@@ -1125,6 +1138,18 @@ class PnbDilicomBatchJobOnixWithFullTest extends PnbDilicomJobOnixTestCase {
     Class_Album::find(1)->index();
     $this->assertEquals('2015-07-10 13:12:43', Class_Album::find(1)->getNotice()->getDateCreation());
   }
+
+
+  /** @test */
+  public function NoticeUnimarcMatieresLabelShouldBeRomansdeScienceFictionRomansClassiques() {
+    Class_Album::find(1)->index();
+    Class_CosmoVar::set('unimarc_zone_matiere',"610a");
+    $this->assertEquals(['Romans de science-fiction, terreur, épouvante',
+                         'Romans classiques',
+                         'Oeuvres classiques',
+                         'Fantasy, Merveilleux',
+                         'LITTÉRATURE GÉNÉRALE'], Class_Album::find(1)->getNotice()->getNoticeUnimarc()->getMatieresLabels());
+  }
 }
 
 
-- 
GitLab