From 2060a7fc2fb389fc59d3b7572f0b29538a77782d Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Thu, 6 Jul 2023 14:45:24 +0000
Subject: [PATCH] hotline#176160 : fix FRBR module to use any type of records
 URL.

---
 VERSIONS_HOTLINE/176160                       |  1 +
 library/Class/FRBR/Link.php                   | 31 +++++++++---
 scripts/validate_frbr_link.php                |  3 ++
 .../controllers/FrbrLinkControllerTest.php    | 50 ++++++++++++++++++-
 4 files changed, 77 insertions(+), 8 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/176160
 create mode 100644 scripts/validate_frbr_link.php

diff --git a/VERSIONS_HOTLINE/176160 b/VERSIONS_HOTLINE/176160
new file mode 100644
index 00000000000..1d1e7db35d4
--- /dev/null
+++ b/VERSIONS_HOTLINE/176160
@@ -0,0 +1 @@
+ - correctif #176160 : Module FRBR : Il est dorénavant possible de renseigner une notice via plusieurs schémas d'URL (url pérenne via l'id_sigb ou via la clef alpha de manière indifferente).
\ No newline at end of file
diff --git a/library/Class/FRBR/Link.php b/library/Class/FRBR/Link.php
index 06a18660dcb..0c07458dc4b 100644
--- a/library/Class/FRBR/Link.php
+++ b/library/Class/FRBR/Link.php
@@ -200,17 +200,19 @@ class Class_FRBR_Link extends Storm_Model_Abstract {
       return;
 
     if ('opac' == $request->getModuleName()
-        && 'recherche' == $request->getControllerName()
-        && 'viewnotice' == $request->getActionName()) {
-      $callback(self::TYPE_NOTICE, $request->getParam('clef'));
-      ;
+        && 'bib-numerique' == $request->getControllerName()
+        && 'notice' == $request->getActionName()) {
+      $callback(self::TYPE_ALBUM, $request->getParam('id'));
       return;
     }
 
+    $clef = $this->_detectRecordKeyFromUrl($request);
+
     if ('opac' == $request->getModuleName()
-        && 'bib-numerique' == $request->getControllerName()
-        && 'notice' == $request->getActionName()) {
-      $callback(self::TYPE_ALBUM, $request->getParam('id'));
+        && 'recherche' == $request->getControllerName()
+        && 'viewnotice' == $request->getActionName()) {
+      $callback(self::TYPE_NOTICE, $this->_detectRecordKeyFromUrl($request));
+      ;
       return;
     }
 
@@ -218,6 +220,21 @@ class Class_FRBR_Link extends Storm_Model_Abstract {
   }
 
 
+  protected function _detectRecordKeyFromUrl($request) : string {
+    if ($clef = $request->getParam('clef'))
+      return $clef;
+
+    $id = $request->getParam('id') ?? Class_MoteurRecherche_RecordRequest::answer($request)
+                                     ->recordId();
+
+    return ($notice = Class_Notice::find($id))
+      ? $notice->getClefAlpha()
+      : '';
+
+    return '';
+  }
+
+
   protected function _detectSourceType() {
     $this->_detectEntityTypeFromUrl(
                                     $this->getSource(),
diff --git a/scripts/validate_frbr_link.php b/scripts/validate_frbr_link.php
new file mode 100644
index 00000000000..6005886e21d
--- /dev/null
+++ b/scripts/validate_frbr_link.php
@@ -0,0 +1,3 @@
+<?php
+require(__DIR__.'/../console.php');
+foreach(Class_FRBR_Link::findAll() as $link) $link->save();
diff --git a/tests/application/modules/admin/controllers/FrbrLinkControllerTest.php b/tests/application/modules/admin/controllers/FrbrLinkControllerTest.php
index 34beeddb3f3..fc8807cfe0d 100644
--- a/tests/application/modules/admin/controllers/FrbrLinkControllerTest.php
+++ b/tests/application/modules/admin/controllers/FrbrLinkControllerTest.php
@@ -247,4 +247,52 @@ class Admin_FrbrLinkControllerEditSuiteValidWithLocalUrlsPostTest extends Admin_
     $this->assertEquals(Class_FRBR_Link::TYPE_NOTICE,
                         $this->_link->getTargetType());
   }
-}
\ No newline at end of file
+}
+
+
+
+
+class Admin_FrbrLinkControllerIndexWithIdSigbUrlsTest extends Admin_FrbrLinkControllerTestCase {
+  protected $_link;
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->fixture(Class_Exemplaire::class,
+                   ['id' => 1,
+                    'id_notice' => 33,
+                    'id_origine' => 1,
+                    'id_bib' => 1,
+                    'id_int_bib' => 1,
+                    'type' => 1]);
+
+    $this->fixture(Class_Exemplaire::class,
+                   ['id' => 2,
+                    'id_notice' => 34,
+                    'id_origine' => 2,
+                    'id_bib' => 1,
+                    'id_int_bib' => 1,
+                    'type' => 1]);
+
+    $this->_link = Class_FRBR_Link::find(2);
+    $this->_link->setSource(Class_Url::absolute('/recherche/viewnotice/id_site/*/id_sigb/1'))
+                ->setTarget(Class_Url::absolute('/recherche/viewnotice/id_site/*/id_sigb/2'))
+                ->save();
+
+    $this->dispatch('/admin/frbr-link');
+  }
+
+
+  /** @test */
+  public function sourceKeyShouldBeLESGRANDSTEXTES_1() {
+    $this->assertEquals('LESGRANDSTEXTESDEDROITINTERNATIONALPUBLIC--DUPUYP--DALLOZ-2010-1',
+                        $this->_link->getSourceKey());
+  }
+
+
+  /** @test */
+  public function sourceKeyShouldBeLESGRANDSTEXTES_2() {
+    $this->assertEquals('LESGRANDSTEXTESDEDROITINTERNATIONALPUBLIC--DUPUYP--DALLOZ-2010-2',
+                        $this->_link->getTargetKey());
+  }
+}
-- 
GitLab