From 6ea7655ee66c234a17aac9dfd030c7aab292bbf3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathias=20Maa=C3=9F?= <mathias.maass@uni-leipzig.de>
Date: Wed, 27 Apr 2022 09:13:28 +0200
Subject: [PATCH] refs #16476 [finc] add interlibrary loan link

* adds a interlibrary loan link to the list of recommendations if search
  result was empty.
* add isil parameter to interlib loan url
* refactor and add docblocks
* add descriptive text to interlibrary loan link
* use a configurable url template
---
 local/config/vufind/searches.ini              | 11 +++
 local/languages/de.ini                        |  1 +
 local/languages/en.ini                        |  1 +
 module/finc/config/module.config.php          |  2 +
 module/finc/src/finc/Recommend/Factory.php    | 12 +++
 .../src/finc/Recommend/InterlibraryLoan.php   | 97 +++++++++++++++++++
 .../Recommend/InterlibraryLoan.phtml          | 12 +++
 7 files changed, 136 insertions(+)
 create mode 100644 module/finc/src/finc/Recommend/InterlibraryLoan.php
 create mode 100644 themes/finc/templates/Recommend/InterlibraryLoan.phtml

diff --git a/local/config/vufind/searches.ini b/local/config/vufind/searches.ini
index d55eda57d75..b46a0108273 100644
--- a/local/config/vufind/searches.ini
+++ b/local/config/vufind/searches.ini
@@ -62,6 +62,8 @@ default_noresults_recommend[] = SwitchQuery
 ;default_noresults_recommend[] = SwitchQuery:::fuzzy
 ;default_noresults_recommend[] = SpellingSuggestions
 default_noresults_recommend[] = RemoveFilters
+; InterlibraryLoan:
+default_noresults_recommend[] = InterlibraryLoan:"https://fernleihe.boss.bsz-bw.de/Search/Results?lookfor=<QUERY>"
 
 ; Set this to true in order to highlight keywords from the search query when they
 ; appear in fields displayed in search results.
@@ -405,6 +407,15 @@ CallNumber = callnumber-sort
 ;       suggesting that the user try switching to [field].  [field description]
 ;       is the human-readable description of [field].  Default values are
 ;       'AllFields' and 'All Fields' respectively.
+; InterlibraryLoan:[URL template]
+;       Shows a link to an interlibrary loan site. The [URL template] is an URL
+;       with a placeholder <QUERY> (case sensitive) that gets replaced by the
+;       user's search query. E.g: 
+;
+;           http://example.com/interlibraryloan?q=<QUERY>&foo=bar
+;       
+;       The search query will be encoded via `rawurlencode` (RFC 3986) before
+;       inserted into the url template.
 ;
 ; You can build your own recommendations modules if you wish.  See the developer's
 ; guide here:
diff --git a/local/languages/de.ini b/local/languages/de.ini
index fdd1f1f1949..3c2577c9337 100644
--- a/local/languages/de.ini
+++ b/local/languages/de.ini
@@ -338,6 +338,7 @@ Interlibrary loan = "Fernleihe"
 Interlibraryloans = "Fernleihe"
 Interlibrary Loans = "Fernleihe"
 Interlibrary loan information = "Hinweise zur Fernleihe"
+Interlibrary loan recommendation = "Sie können mit Ihrer Suchanfrage direkt nach Fernleihen suchen. Folgen Sie hierfür bitte folgendem Link:"
 Invalid Recipient Email Address = "Ungültige E-Mail-Adresse des Empfängers"
 Invalid Sender Email Address = "Ungültige E-Mail-Adresse zum Versenden"
 ISBD Citation = "ISBD Zitierstil"
diff --git a/local/languages/en.ini b/local/languages/en.ini
index 540e6182b18..c1fcba84f3b 100644
--- a/local/languages/en.ini
+++ b/local/languages/en.ini
@@ -442,6 +442,7 @@ Interlibrary loan = "Interlibrary Loan"
 Interlibrary loan information = "Interlibrary Loan Information"
 Interlibrary loan - Medicine = "Interlibrary Loan - Medicine"
 Interlibraryloans = "Interlibrary Loans"
+Interlibrary loan recommendation = "You can search for interlibrary loans directly with your search query. Please follow the link:"
 Invalid Recipient Email Address = "Invalid recipient e-mail address"
 Invalid Sender Email Address = "Invalid sender e-mail address"
 # Irish = Gaeilge
diff --git a/module/finc/config/module.config.php b/module/finc/config/module.config.php
index d126c482735..640b46118ec 100644
--- a/module/finc/config/module.config.php
+++ b/module/finc/config/module.config.php
@@ -137,9 +137,11 @@ $config = [
             'recommend' => [
                 'factories' => [
                     'finc\Recommend\EbscoResults' => 'finc\Recommend\Factory::getEbscoResults',
+                    'finc\Recommend\InterlibraryLoan' => 'finc\Recommend\Factory::getInterlibraryLoan',
                 ],
                 'aliases' => [
                     'ebscoresults' => 'finc\Recommend\EbscoResults',
+                    'interlibraryloan' => 'finc\Recommend\InterlibraryLoan',
                 ]
             ],
             'recorddriver' => [
diff --git a/module/finc/src/finc/Recommend/Factory.php b/module/finc/src/finc/Recommend/Factory.php
index cfd2e61ca3d..0ac7101fb80 100644
--- a/module/finc/src/finc/Recommend/Factory.php
+++ b/module/finc/src/finc/Recommend/Factory.php
@@ -55,4 +55,16 @@ class Factory extends \Vufind\Recommend\Factory
     {
         return new EbscoResults();
     }
+
+    /**
+     * Creates InterlibraryLoan
+     *
+     * @param ServiceManager $sm Service manager
+     * 
+     * @return InterlibraryLoan
+     */
+    public static function getInterlibraryLoan(ServiceManager $sm)
+    {
+        return new InterlibraryLoan($sm);
+    }
 }
diff --git a/module/finc/src/finc/Recommend/InterlibraryLoan.php b/module/finc/src/finc/Recommend/InterlibraryLoan.php
new file mode 100644
index 00000000000..55e29021407
--- /dev/null
+++ b/module/finc/src/finc/Recommend/InterlibraryLoan.php
@@ -0,0 +1,97 @@
+<?php
+/**
+ * Recommendation Module Interlibrary Loan
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2014.
+ * Copyright (C) Leipzig University Library 2022.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * @category VuFind
+ * @package  Recommendations
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
+ */
+namespace finc\Recommend;
+
+use VuFind\Recommend\RecommendInterface;
+
+/**
+ * Controller for an interlibary loan link to fernleihe.boss.bsz-bw.de
+ *
+ * @category VuFind
+ * @package  Recommendations
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
+ */
+class InterlibraryLoan implements RecommendInterface
+{
+    /**
+     * Template URL with a "<QUERY>" slot to be replaced by the search query.
+     *
+     * @var string
+     */
+    protected $templateUrl;
+
+    /**
+     * Store the configuration of the recommendation module.
+     *
+     * @param string $settings Settings from searches.ini.
+     *
+     * @return void
+     */
+    public function setConfig($settings)
+    {
+        $this->templateUrl = $settings;
+    }
+
+    /**
+     * Called at the end of the Search Params objects' initFromRequest() method.
+     * This method is responsible for setting search parameters needed by the
+     * recommendation module and for reading any existing search parameters that may
+     * be needed.
+     *
+     * @param \VuFind\Search\Base\Params $params  Search parameter object
+     * @param \Zend\StdLib\Parameters    $request Parameter object representing user
+     *                                            request.
+     *
+     * @return void
+     */
+    public function init($params, $request)
+    {
+    }
+
+    /**
+     * Create the interlibrary loan href.
+     *
+     * The href is accessible in the corresponding template via:
+     *   `$this->recommend->href`
+     *
+     * @param \VuFind\Search\Base\Results $results Search results object
+     *
+     * @return void
+     */
+    public function process($results)
+    {
+        $this->href = str_replace(
+            '<QUERY>',
+            rawurlencode($results->getParams()->getDisplayQuery()),
+            $this->templateUrl
+        );
+    }
+}
diff --git a/themes/finc/templates/Recommend/InterlibraryLoan.phtml b/themes/finc/templates/Recommend/InterlibraryLoan.phtml
new file mode 100644
index 00000000000..ceaff644395
--- /dev/null
+++ b/themes/finc/templates/Recommend/InterlibraryLoan.phtml
@@ -0,0 +1,12 @@
+<?php
+    $href = $this->recommend->href;
+    $text = $this->transEsc('Interlibrary loan recommendation');
+?>
+<div class="alert alert-info">
+  <p>
+    <?= $text ?>
+  </p>
+  <a href=<?= $href ?>target="_blank">
+    <?= $href ?>
+  </a>
+</div>
-- 
GitLab