From 2b3c6c5a7ae8e0446c18b40a07cc0a571a7fd905 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Thu, 19 Feb 2015 11:22:34 +0100
Subject: [PATCH] rd #14085 pnb

album MARC refactoring
---
 library/Class/Album.php                       | 163 +++++++-----------
 library/Class/Album/Marc.php                  |  21 ++-
 library/Class/Indexation/PseudoNotice.php     |   5 +-
 .../ZendAfi/View/Helper/Admin/FrbrLabel.php   |   2 +-
 ...echercheControllerAlbumAudioRecordTest.php |  10 +-
 .../Class/WebService/Dilicom/ParserTest.php   |   2 +-
 6 files changed, 85 insertions(+), 118 deletions(-)

diff --git a/library/Class/Album.php b/library/Class/Album.php
index 6fdc650a9b2..104b7b2eeb9 100644
--- a/library/Class/Album.php
+++ b/library/Class/Album.php
@@ -1091,17 +1091,6 @@ class Class_Album extends Storm_Model_Abstract {
 	}
 
 
-	public function getDatasOfField($field) {
-		return array_filter(
-			array_map(
-				function($note) use ($field) {
-					return is_array($note) && $field === $note['field'] ?
-						$note['data'] : null;
-				},
-				$this->getNotesAsArray()));
-	}
-
-
 	/** @return string */
 	public function getVideoUrl() {
 		return $this->getNoteForFieldAndDatas(self::VIDEO_URL_FIELD,
@@ -1381,28 +1370,6 @@ class Class_Album extends Storm_Model_Abstract {
 	}
 
 
-	public function addZone($zone, $sous_zones) {
-		$this->getMarc()->addZone($zone, $sous_zones);
-		return $this;
-	}
-
-
-	public function getSubfield($field, $subfield) {
-		return $this->getMarc()->getSubfield($field, $subfield);
-	}
-
-
-	public function addSubField($field, $subfield, $value) {
-		$this->getMarc()->addSubfield($field, $subfield, $value);
-		return $this;
-	}
-
-
-	public function clearZones($zone) {
-		$this->getMarc()->clearZones([$zone]);
-	}
-
-
 	/** @return string */
 	public function getNotesForPseudoNotice() {
 		$pseudo_marc = clone $this->getMarc();
@@ -1456,9 +1423,11 @@ class Class_Album extends Storm_Model_Abstract {
 	 * Main author
 	 * @return string
 	 */
-	public function getMainAuthor() {
-		$authors = $this->getAuthorsNames();
-		return isset($authors[0]) ? $authors[0] : '';
+	public function getMainAuthorName() {
+		if (!$authors = $this->getAuthors())
+			return '';
+
+		return array_shift($authors)->getName();
 	}
 
 
@@ -1467,16 +1436,10 @@ class Class_Album extends Storm_Model_Abstract {
 	 * @return array
 	 */
 	public function getAuthorsNames() {
-		return
-			array_values(
-									 array_filter(
-																array_map(
-																					function($item) {
-																						return (!is_array($item) || !isset($item['a']))
-																							?	null
-																							: $item['a'];
-																					},
-																					$this->getAuthors())));
+		$names = [];
+		foreach($this->getAuthors() as $author)
+			$names []= $author->getName();
+		return $names;
 	}
 
 
@@ -1485,54 +1448,18 @@ class Class_Album extends Storm_Model_Abstract {
 	 * @return array
 	 */
 	public function getAuthors() {
-		return array_filter(
-			array_map(
-				function($item) {
-					return (!is_array($item) || self::AUTHOR_FIELD !== $item['field']
-									|| !isset($item['data']) || !isset($item['data']['a'])) ?
-						null : $item['data'];
-				},
-				$this->getNotesAsArray()));
-	}
-
-
-  /**
-	 * Editor from unimarc container
-	 * @return array
-	 */
-	public function getEditors() {
-		return $this->getSubfield(Class_Album::EDITOR_FIELD, 'c');
-	}
-
-
-	/**
-	 * Collection from unimarc container
-	 * @return array
-	 */
-	public function getCollections() {
-		return array_filter(
-			array_map(
-				function($item) {
-					return (!is_array($item) || self::COLLECTION_FIELD !== $item['field']
-									|| !isset($item['data']) || !isset($item['data']['a'])) ?
-						null : $item['data']['a'];
-				},
-				$this->getNotesAsArray()));
-	}
-
-
-	public function getDistributor() {
-		return $this->getNote(self::DISTRIBUTOR_FIELD);
-	}
-
-
-	public function setDistributor($distributor) {
-		$this->addNote(self::DISTRIBUTOR_FIELD, $distributor);
-		return $this;
+		$authors = [];
+		foreach ($this->getMarc()->getDatasOfField(self::AUTHOR_FIELD) as $item) {
+			if (!isset($item['a']))
+				continue;
+			$authors []= new Class_Notice_Author($item['a'],
+																					 isset($item['4']) ? $item['4'] : '');
+		}
+		return $authors;
 	}
 
 
-	/**
+		/**
 	 * add author in unimarc container if not present with the same function
 	 */
 	public function addAuthor($name, $function = '') {
@@ -1543,8 +1470,10 @@ class Class_Album extends Storm_Model_Abstract {
 		if (null !== $function)
 			$datas['4'] = $function;
 
-		if (!in_array((string)$name, $this->getAuthorsNames()))
-			return $this->addZone(self::AUTHOR_FIELD, $datas);
+		if (!in_array((string)$name, $this->getAuthorsNames())) {
+			$this->getMarc()->addZone(self::AUTHOR_FIELD, $datas);
+			return $this;
+		}
 
 		if (!$function)
 			return $this;
@@ -1557,7 +1486,39 @@ class Class_Album extends Storm_Model_Abstract {
 			}
 		}
 
-		return (!$found) ? $this->addZone(self::AUTHOR_FIELD, $datas) : $this;
+		if (!$found)
+		  $this->getMarc()->addZone(self::AUTHOR_FIELD, $datas);
+
+		return $this;
+	}
+
+
+  /**
+	 * Editor from unimarc container
+	 * @return array
+	 */
+	public function getEditors() {
+		return $this->getMarc()->getSubfield(Class_Album::EDITOR_FIELD, 'c');
+	}
+
+
+	/**
+	 * Collection from unimarc container
+	 * @return array
+	 */
+	public function getCollections() {
+		return $this->getMarc()->getSubfield(self::COLLECTION_FIELD, 'a');
+	}
+
+
+	public function getDistributor() {
+		return $this->getMarc()->getNote(self::DISTRIBUTOR_FIELD);
+	}
+
+
+	public function setDistributor($distributor) {
+		$this->getMarc()->addNote(self::DISTRIBUTOR_FIELD, $distributor);
+		return $this;
 	}
 
 
@@ -1565,10 +1526,9 @@ class Class_Album extends Storm_Model_Abstract {
 	 * add editor in unimarc container if not present with the same function
 	 */
 	public function addEditor($name) {
-		if (!$name)
-			return $this;
-
-		return $this->addSubField('210', 'c', $name);
+		if ($name)
+			$this->getMarc()->addSubField('210', 'c', $name);
+		return $this;
 	}
 
 
@@ -1576,10 +1536,9 @@ class Class_Album extends Storm_Model_Abstract {
 	 * add collection in unimarc container if not present with the same function
 	 */
 	public function addCollection($name) {
-		if (!$name)
-			return $this;
-
-		return $this->addZone(self::COLLECTION_FIELD,['a' => $name]);
+		if ($name)
+			$this->getMarc()->addZone(self::COLLECTION_FIELD, ['a' => $name]);
+		return $this;
 	}
 
 
diff --git a/library/Class/Album/Marc.php b/library/Class/Album/Marc.php
index f9478bb4a02..a4e139d91fe 100644
--- a/library/Class/Album/Marc.php
+++ b/library/Class/Album/Marc.php
@@ -67,6 +67,7 @@ class Class_Album_Marc {
 	public function addZone($zone, $sous_zones) {
 		$this->_datas[] = ['field' => $zone,
 											 'data' => $sous_zones];
+		return $this;
 	}
 
 
@@ -76,9 +77,14 @@ class Class_Album_Marc {
 
 		$values = [];
 		foreach($datas as $data) {
-			foreach($data as $value) {
-				if ($value[0] == $subfield)
+			foreach($data as $key => $value) {
+				if (is_array($value) && ($value[0] == $subfield)) {
 					$values[] = $value[1];
+					continue;
+				}
+
+				if ($key == $subfield)
+					$values[] = $value;
 			}
 		}
 
@@ -89,11 +95,12 @@ class Class_Album_Marc {
 	public function addSubField($field, $subfield, $value) {
 		foreach($this->_datas as $k => $v){
 			if (is_array($v) && isset($v['field']) && $v['field'] == $field) {
-				$notes[$k]['data'][] = [$subfield, $value];
+				$this->_datas[$k]['data'][] = [$subfield, $value];
+				return $this;
 			}
 		}
 
-		return $this->addZone($field, [[$subfield, $value]]);
+		return $this->addZone($field, [[ $subfield, $value ]]);
 	}
 
 
@@ -122,12 +129,12 @@ class Class_Album_Marc {
 		return array_filter(
 												array_map(
 																	function($note) use ($field) {
-																		return is_array($note) && $field === $note['field'] ?
-																			$note['data'] : null;
+																		return is_array($note) && $field === $note['field']
+																			?	$note['data']
+																			: null;
 																	},
 																	$this->_datas));
 	}
-
 }
 
 ?>
\ No newline at end of file
diff --git a/library/Class/Indexation/PseudoNotice.php b/library/Class/Indexation/PseudoNotice.php
index ad5c63a1a79..73ac8f10604 100644
--- a/library/Class/Indexation/PseudoNotice.php
+++ b/library/Class/Indexation/PseudoNotice.php
@@ -374,9 +374,8 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 
 	/** @return array */
 	public function extractAuthors() {
-		$authors = parent::extractAuthors();
-
-		$this->_addAuthorArrayAsNoticeAuthor($this->_model->getAuthors(), $authors);
+		$authors = array_merge(parent::extractAuthors(),
+													 $this->_model->getAuthors());
 
 		foreach ($this->_model->getRessources() as $ressource)
 			$this->_addAuthorArrayAsNoticeAuthor($ressource->getAuthors(), $authors);
diff --git a/library/ZendAfi/View/Helper/Admin/FrbrLabel.php b/library/ZendAfi/View/Helper/Admin/FrbrLabel.php
index eef1295fb49..594cd30ef11 100644
--- a/library/ZendAfi/View/Helper/Admin/FrbrLabel.php
+++ b/library/ZendAfi/View/Helper/Admin/FrbrLabel.php
@@ -84,7 +84,7 @@ class ZendAfi_View_Helper_Admin_FrbrLabelAlbumRenderer
 	extends ZendAfi_View_Helper_Admin_FrbrLabelRenderer{
 	public function render() {
 		return $this->_renderTitleAuthor($this->entity->getTitre(),
-																		 $this->entity->getMainAuthor());
+																		 $this->entity->getMainAuthorName());
 	}
 }
 ?>
diff --git a/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php b/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php
index c7083ed5988..8625532a085 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php
@@ -74,10 +74,12 @@ abstract class RechercheControllerAlbumAudioRecordTestCase extends AbstractContr
 																		 'fichier' => 'unknown.mp3']))
 			->addRessource($this->fixture('Class_AlbumRessource',
 																		['id' => 4,
-																		 'fichier' => '502_05_the_prophecy.mp3']))
-			->addZone('856', ['a' => 'http://mabib.net/bib-numerique/notice/ido/1'])
-			->addZone('856', ['x' => 'external_uri'])
-			->assertSave();
+																		 'fichier' => '502_05_the_prophecy.mp3']));
+		$album->getMarc()
+					->addZone('856', ['a' => 'http://mabib.net/bib-numerique/notice/ido/1'])
+					->addZone('856', ['x' => 'external_uri']);
+
+		$album->assertSave();
 
 		$album->index();
 
diff --git a/tests/library/Class/WebService/Dilicom/ParserTest.php b/tests/library/Class/WebService/Dilicom/ParserTest.php
index 85315676fd7..b8e57adf65f 100644
--- a/tests/library/Class/WebService/Dilicom/ParserTest.php
+++ b/tests/library/Class/WebService/Dilicom/ParserTest.php
@@ -106,7 +106,7 @@ class DilicomONIXParserTest extends Storm_Test_ModelTestCase {
 	 * @test
 	 */
 	public function albumMainAuthorShouldBeJulieOtsuka($album) {
-		$this->assertEquals('Julie Otsuka', $album->getMainAuthor());
+		$this->assertEquals('Julie Otsuka', $album->getMainAuthorName());
 	}
 
 }
-- 
GitLab