diff --git a/application/modules/admin/controllers/AlbumController.php b/application/modules/admin/controllers/AlbumController.php index 2e9536237f365f38aa86faef5f0e15b27c6bbc1c..fa27e6e4db7fc82201130c16b0ba5405fdc6445c 100644 --- a/application/modules/admin/controllers/AlbumController.php +++ b/application/modules/admin/controllers/AlbumController.php @@ -520,21 +520,31 @@ class Admin_AlbumController extends ZendAfi_Controller_Action { foreach(Class_FRBR_Link::findAllRecordLinksForAlbum($album) as $link) $frbr_links[md5($link->getSource().$link->getTarget())] = $link; + $absoluteUrl = $this->view->absoluteUrl($album->getPermalink()); + $received_links = []; - $count = count($frbr_multi['frbr_url']); - for($i=0; $i < $count; $i++) - $received_links[md5($frbr_multi['frbr_url'][$i] - . $this->view->absoluteUrl($album->getPermalink()))] = ['frbr_url' => $frbr_multi['frbr_url'][$i], - 'frbr_type' => $frbr_multi['frbr_type'][$i]]; + $urls = $frbr_multi['frbr_url']; + $types = $frbr_multi['frbr_type']; + $count = count($urls); + for($i=0; $i < $count; $i++) { + if (!$types[$i] && !$urls[$i]) + continue; + + list($id, $type) = explode(':', $types[$i]); + $md5 = md5('target' == $type ? ($urls[$i] . $absoluteUrl) : ($absoluteUrl . $urls[$i])); + + $received_links[$md5] = ['frbr_url' => $urls[$i], 'frbr_type' => $id, 'type' => $type]; + } foreach(array_diff_key($frbr_links, $received_links) as $link) $link->delete(); foreach(array_diff_key($received_links, $frbr_links) as $new) { - if (!$new['frbr_type'] && !$new['frbr_url']) - continue; - $frbr_link = Class_FRBR_Link::newInstance(['target' => $this->view->absoluteUrl($album->getPermalink())]); - $frbr_link->setTypeId($new['frbr_type'])->setSource($new['frbr_url']); + $record_type = 'target' == $new['type'] ? 'source' : 'target'; + $frbr_link = Class_FRBR_Link::newInstance([$new['type'] => $absoluteUrl, + $record_type => $new['frbr_url'], + 'type_id' => $new['frbr_type']]); + if (!$frbr_link->save()) throw new Zend_Exception(json_encode($frbr_link->getErrors())); } diff --git a/application/modules/opac/controllers/NoticeajaxController.php b/application/modules/opac/controllers/NoticeajaxController.php index a10be43fd46b47656c114d67283691de351baacc..d883d624553a20393e041eeef2a331a6d8ae5836 100644 --- a/application/modules/opac/controllers/NoticeajaxController.php +++ b/application/modules/opac/controllers/NoticeajaxController.php @@ -303,13 +303,15 @@ class NoticeAjaxController extends Zend_Controller_Action { $link_sorted = []; foreach(Class_FRBR_Link::findAllAlbumsFromNotice($this->notice) as $link) { - $link_sorted[$link->getType()->getFromSource()][] = $link; + $type = $link->getSourceType() == Class_FRBR_Link::TYPE_NOTICE ? 'Source' : 'Target'; + $link_sorted[$link->getType()->{'getFrom' . $type}()][] = $link; } foreach ($link_sorted as $from_source => $links) { $html .= '<div class="notice_info_titre">' . $from_source . '</div>'; foreach ($links as $link) { - $html .= $this->view->renderAlbum($link->getTargetEntity()); + $type = $link->getSourceType() == Class_FRBR_Link::TYPE_NOTICE ? 'Target' : 'Source'; + $html .= $this->view->renderAlbum($link->{'get' . $type . 'Entity'}()); } } diff --git a/library/Class/FRBR/Link.php b/library/Class/FRBR/Link.php index c6278840a30546a499c0012b2d446c8ee866bf39..73a6be020137a137211375df2cbfcbf2f92e17ff 100644 --- a/library/Class/FRBR/Link.php +++ b/library/Class/FRBR/Link.php @@ -53,12 +53,18 @@ class FRBR_LinkLoader extends Storm_Model_Loader { public function findAllAlbumsFromNotice($notice) { - return Class_FRBR_Link::findAllBy($this->_albumsFromNoticeFilter($notice)); + $links = []; + foreach(Class_FRBR_Link::findAllBy($this->_albumsFromNoticeFilter($notice)) as $link) + $links[] = $link; + foreach(Class_FRBR_Link::findAllBy($this->_recordsFromNoticeFilter($notice)) as $link) + $links[] = $link; + return $links; } public function countAlbumsFromNotice($notice) { - return Class_FRBR_Link::countBy($this->_albumsFromNoticeFilter($notice)); + return Class_FRBR_Link::countBy($this->_albumsFromNoticeFilter($notice)) + + Class_FRBR_Link::countBy($this->_recordsFromNoticeFilter($notice)); } @@ -69,6 +75,13 @@ class FRBR_LinkLoader extends Storm_Model_Loader { } + protected function _recordsFromNoticeFilter($notice) { + return ['target_type' => Class_FRBR_Link::TYPE_NOTICE, + 'source_type' => Class_FRBR_Link::TYPE_ALBUM, + 'target_key' => $notice->getClefAlpha()]; + } + + /** * @param $album Class_Album * @return Class_FRBR_Link or null @@ -83,7 +96,12 @@ class FRBR_LinkLoader extends Storm_Model_Loader { * @return Array Class_FRBR_Link */ public function findAllRecordLinksForAlbum($album) { - return Class_FRBR_Link::findAllBy($this->_recordLinkFilterFromAlbum($album)); + $links = []; + foreach(Class_FRBR_Link::findAllBy($this->_recordLinkFilterFromAlbum($album)) as $link) + $links[] = $link; + foreach(class_FRBR_Link::findAllBy($this->_albumLinkFilterFromRecord($album)) as $link) + $links[] = $link; + return $links; } @@ -96,6 +114,17 @@ class FRBR_LinkLoader extends Storm_Model_Loader { 'target_type' => Class_FRBR_Link::TYPE_ALBUM, 'target_key' => $album->getId()]; } + + + /** + * @param $album Class_Album + * @return Array + */ + public function _albumLinkFilterFromRecord($album) { + return ['target_type' => Class_FRBR_Link::TYPE_NOTICE, + 'source_type' => Class_FRBR_Link::TYPE_ALBUM, + 'source_key' => $album->getId()]; + } } diff --git a/library/Class/FRBR/LinkType.php b/library/Class/FRBR/LinkType.php index 5a05a832d2776e327ca5990977e41948f3a88a48..58a5871f72eab48326e305830037adac3374344a 100644 --- a/library/Class/FRBR/LinkType.php +++ b/library/Class/FRBR/LinkType.php @@ -30,6 +30,17 @@ class Class_FRBR_LinkTypeLoader extends Storm_Model_Loader { return $list; } + + /** @return array */ + public function getSourceTargetComboList() { + $list = []; + foreach ($this->findAllBy(['order' => 'libelle']) as $model) { + $list[$model->getId() . ':source'] = $model->getLibelle() . ' - ' . $model->getFromSource(); + $list[$model->getId() . ':target'] = $model->getLibelle() . ' - ' . $model->getFromTarget(); + } + return $list; + } + } diff --git a/library/ZendAfi/Form/Album.php b/library/ZendAfi/Form/Album.php index fc936f8400417b2882ce9c9cfc3ef9dd34102344..1b961ba6ce97bc8ec5e41caabbfa6d5cddec364f 100644 --- a/library/ZendAfi/Form/Album.php +++ b/library/ZendAfi/Form/Album.php @@ -223,7 +223,7 @@ class ZendAfi_Form_Album extends ZendAfi_Form { 'label' => $this->_('Type de lien'), 'attribs' => ['style' => 'width:180px;'], 'type' => 'select', - 'options' => ['' => $this->_('Aucun')] + Class_FRBR_LinkType::getComboList()], + 'options' => ['' => $this->_('Aucun')] + Class_FRBR_LinkType::getSourceTargetComboList()], ['name' => 'frbr_url', 'label' => $this->_('Permalien de la notice'), 'attribs' => ['style' => 'width:450px;']]], @@ -322,12 +322,15 @@ class ZendAfi_Form_Album extends ZendAfi_Form { public function addFrbrLinkFor($album) { $descs = $values = ['frbr_type' => [], 'frbr_url' => []]; foreach (Class_FRBR_Link::findAllRecordLinksForAlbum($album) as $link) { - $values['frbr_url'][] = $link->getSource(); - $values['frbr_type'][] = $link->getType()->getId(); + $record_type = (Class_FRBR_Link::TYPE_NOTICE == $link->getSourceType()) ? 'Source' : 'Target'; + $album_type = 'Source' == $record_type ? 'target' : 'source'; + + $values['frbr_url'][] = $link->{'get' . $record_type}(); + $values['frbr_type'][] = $link->getType()->getId() . ':' . $album_type; $descs['frbr_type'][] = ''; $label = ''; - if ($entity = $link->getSourceEntity()) { - $label = '<br><a href="' . $link->getSource() + if ($entity = $link->{'get' . $record_type . 'Entity'}()) { + $label = '<br><a href="' . $link->{'get' . $record_type}() . '" onclick="window.open(this.href);return false;">' . $entity->getTitrePrincipal() . '</a>' . '<br>' . $entity->getAuteurPrincipal(); } diff --git a/tests/application/modules/admin/controllers/AlbumControllerTest.php b/tests/application/modules/admin/controllers/AlbumControllerTest.php index 5d6d7b82838c60b4253acadd49c61f7c970ae791..6f04c9c3036c03d14344b760cddd1e3b3b846610 100644 --- a/tests/application/modules/admin/controllers/AlbumControllerTest.php +++ b/tests/application/modules/admin/controllers/AlbumControllerTest.php @@ -73,6 +73,11 @@ abstract class Admin_AlbumControllerTestCase extends Admin_AbstractControllerTes 'from_source' => 'A pour ebook', 'from_target' => 'Est l\'ebook de']); + $this->fixture('Class_FRBR_LinkType', ['id' => 2, + 'libelle' => 'Diaporama', + 'from_source' => 'Est le diaporama de', + 'from_target' => 'A pour diaporama']); + $this->fixture('Class_Album', ['id' => 43, 'pcdm4' => 'Musique;Dessin', 'notes' => 'a:3:{s:5:"305$a";s:12:"XIIe siècle";s:5:"200$b";s:9:"Parchemin";s:5:"316$a";s:12:"Reliure bois";}']) @@ -115,6 +120,10 @@ abstract class Admin_AlbumControllerTestCase extends Admin_AbstractControllerTes $this->fixture('Class_Notice', ['id' => 8, 'clef_alpha' => 'DIAMANTSEMPOISONNES--HENRYA--PAYOT-2007-1', 'type_doc' => 1]); + + $this->fixture('Class_Notice', ['id' => 9, + 'clef_alpha' => 'CEQUETUVEUXMAISENMAJUSCULE--ALEX--PATBATOR-2015-1', + 'type_doc' => 1]); } @@ -693,7 +702,9 @@ class Admin_AlbumControllerAddAlbumToPatrimoineTest extends Admin_AlbumControlle /** @test */ public function frbrMultiShouldContainFrbrType() { - $this->assertXPathContentContains('//script', '{"name":"frbr_type","label":"Type de lien","attribs":{"style":"width:180px;"},"type":"select","options":{"":"Aucun","1":"Ebook"}}', $this->_response->getBody()); + $this->assertXPathContentContains('//script', + '{"name":"frbr_type","label":"Type de lien","attribs":{"style":"width:180px;"},"type":"select","options":{"":"Aucun","2:source":"Diaporama - Est le diaporama de","2:target":"Diaporama - A pour diaporama","1:source":"Ebook - A pour ebook","1:target":"Ebook - Est l\'ebook de"}}', + $this->_response->getBody()); } } @@ -795,7 +806,7 @@ class Admin_AlbumControllerPostAlbumRenaissanceToPatrimoineFrbrNoLinkTest 'duration' => '00:04:08', 'distributor' => 'galoum', 'frbr_url' => [''], - 'frbr_type' => ['1']]); + 'frbr_type' => ['1:target']]); } @@ -834,8 +845,9 @@ class Admin_AlbumControllerPostAlbumRenaissanceToPatrimoineTest 'collection' => ['Ratm', 'Soad'], 'duration' => '00:04:08', 'distributor' => 'galoum', - 'frbr_url' => [ROOT_URL . BASE_URL . '/recherche/viewnotice/id/8/id_notice/8/type_doc/1/clef/DIAMANTSEMPOISONNES--HENRYA--PAYOT-2007-1?id_profil=7'], - 'frbr_type' => [1]]); + 'frbr_url' => [ROOT_URL . BASE_URL . '/recherche/viewnotice/id/8/id_notice/8/type_doc/1/clef/DIAMANTSEMPOISONNES--HENRYA--PAYOT-2007-1?id_profil=7', + ROOT_URL . BASE_URL . '/recherche/viewnotice/id/9/id_notice/9/type_doc/1/clef/CEQUETUVEUXMAISENMAJUSCULE--ALEX--PATBATOR-2015-1?id_profil=7'], + 'frbr_type' => ['1:target', '2:source']]); $this->new_album = Class_Album::findFirstBy(['order'=>'id desc']); $this->new_notice = $this->new_album->getNotice(); } @@ -982,12 +994,23 @@ class Admin_AlbumControllerPostAlbumRenaissanceToPatrimoineTest /** @test */ - public function shouldHaveCreatedFrbrLink() { + public function shouldHaveCreatedFrbrLinkForDiamantsEmpoisonnes() { $this->assertNotNull(Class_FRBR_Link::findFirstBy(['source' => ROOT_URL . BASE_URL . '/recherche/viewnotice/id/8/id_notice/8/type_doc/1/clef/DIAMANTSEMPOISONNES--HENRYA--PAYOT-2007-1?id_profil=7', + 'type_id' => 1, 'source_type' => Class_FRBR_Link::TYPE_NOTICE, 'target' => ROOT_URL . BASE_URL . '/bib-numerique/notice/id/'.$this->new_album->getId(), 'target_type' => Class_FRBR_Link::TYPE_ALBUM])); } + + + /** @test */ + public function shouldHaveCreatedFrbrLinkForCeQueTuVeuxMaisEnMajuscule() { + $this->assertNotNull(Class_FRBR_Link::findFirstBy(['target' => ROOT_URL . BASE_URL . '/recherche/viewnotice/id/9/id_notice/9/type_doc/1/clef/CEQUETUVEUXMAISENMAJUSCULE--ALEX--PATBATOR-2015-1?id_profil=7', + 'type_id' => 2, + 'target_type' => Class_FRBR_Link::TYPE_NOTICE, + 'source' => ROOT_URL . BASE_URL . '/bib-numerique/notice/id/'.$this->new_album->getId(), + 'source_type' => Class_FRBR_Link::TYPE_ALBUM])); + } } @@ -1168,7 +1191,7 @@ class Admin_AlbumControllerEditAlbumMesBDPostFrbrModifyTest extends Admin_AlbumC 'duration' => '00:04:08', 'distributor' => 'galoum', 'frbr_url' => [ROOT_URL . BASE_URL . '/recherche/viewnotice/id/12/id_notice/12/type_doc/1/clef/ACESHIGH--IRONM---1984-1?id_profil=7'], - 'frbr_type' => ['1']]); + 'frbr_type' => ['1:target']]); } @@ -1464,7 +1487,7 @@ class Admin_AlbumControllerEditAlbumMesBDTest extends Admin_AlbumControllerEditA /** @test */ public function frbrLinkTypeShouldBeEbook() { - $this->assertXPathContentContains('//script', 'values:{"frbr_type":[1]'); + $this->assertXPathContentContains('//script', 'values:{"frbr_type":["1:target"]'); } diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php index 8584d1da3f4c740f22eef45c754bfba579beaed0..aa446a91034cc9ae4e6e2747b8f1feea37b45248 100644 --- a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php +++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php @@ -320,12 +320,25 @@ class NoticeAjaxControllerResNumeriquesFromFrbrLinkTest extends AbstractControll 'libelle' => 'CD MP3', 'from_source' => 'A pour pistes MP3', 'from_target' => 'Pistes MP3 de']); + + $this->fixture('Class_FRBR_LinkType', + ['id' => 2, + 'libelle' => 'Pistes audio', + 'from_target' => 'A pour pistes audio', + 'from_source' => 'Pistes audio de']); + $this->fixture('Class_FRBR_Link', ['id' => 1, 'type_id' => 1, 'source' => ROOT_URL . BASE_URL . '/recherche/viewnotice/clef/ACESHIGH--IRONM---1984-1', 'target' => ROOT_URL . BASE_URL . '/bib-numerique/notice/id/42']); + $this->fixture('Class_FRBR_Link', + ['id' => 2, + 'type_id' => 2, + 'target' => ROOT_URL . BASE_URL . '/recherche/viewnotice/clef/ACESHIGH--IRONM---1984-1', + 'source' => ROOT_URL . BASE_URL . '/bib-numerique/notice/id/42']); + $this->dispatch('noticeajax/resnumeriques?id_notice=123', true); } @@ -340,6 +353,12 @@ class NoticeAjaxControllerResNumeriquesFromFrbrLinkTest extends AbstractControll public function frbrLinkSourceLabelShouldBeDisplayed() { $this->assertXPathContentContains('//div[@class="notice_info_titre"]', 'A pour pistes MP3'); } + + + /** @test */ + public function frbrLinkTargetLabelShouldBeDisplayed() { + $this->assertXPathContentContains('//div[@class="notice_info_titre"]', 'A pour pistes audio', $this->_response->getBody()); + } }