From 994417b1cb279af95c3a2410297e72dbc29043bd Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Thu, 14 Jan 2021 14:44:18 +0100
Subject: [PATCH] refs #18910 [fid] provide finc/dbis-module and standard
 configuration

* moves some standard configuration from ADLR module to FID module to be reused in other FIDs
---
 fid/config/vufind/permissions.ini             |  12 +-
 module/fid/config/module.config.php           |   4 +
 .../src/Controller/MyResearchController.php   | 218 ++++++++++++++++++
 .../MyResearchControllerFactory.php           |  51 ++++
 themes/fid/languages/de.ini                   |  18 +-
 themes/fid/languages/en.ini                   |  20 +-
 .../fid/templates/myresearch/databases.phtml  |  57 +++++
 .../templates/myresearch/dbis-content.phtml   |  30 +++
 8 files changed, 407 insertions(+), 3 deletions(-)
 create mode 100644 module/fid/src/Controller/MyResearchController.php
 create mode 100644 module/fid/src/Controller/MyResearchControllerFactory.php
 create mode 100644 themes/fid/templates/myresearch/databases.phtml
 create mode 100644 themes/fid/templates/myresearch/dbis-content.phtml

diff --git a/fid/config/vufind/permissions.ini b/fid/config/vufind/permissions.ini
index 4b4053857ba..c3bee6a1bf0 100644
--- a/fid/config/vufind/permissions.ini
+++ b/fid/config/vufind/permissions.ini
@@ -49,4 +49,14 @@ permission = fid.EBooks
 [fid.Acquisitions]
 role[] = loggedin
 FidApiPermission[] = limited_access
-permission = fid.Acquisitions
\ No newline at end of file
+permission = fid.Acquisitions
+
+[default.DBIS]
+role[] = guest
+role[] = loggedin
+permission = access.DBIS
+
+[default.Licenses]
+role[] = loggedin
+FidApiPermission[] = full_access
+permission = access.Licenses
\ No newline at end of file
diff --git a/module/fid/config/module.config.php b/module/fid/config/module.config.php
index bb11f27f4b4..a39af204e50 100644
--- a/module/fid/config/module.config.php
+++ b/module/fid/config/module.config.php
@@ -19,6 +19,8 @@
  * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
  */
 
+use fid\Controller\MyResearchController;
+use fid\Controller\MyResearchControllerFactory;
 use fid\Controller\RecordController;
 use fid\Controller\RecordControllerDelegatorFactory;
 use fid\Controller\UserController;
@@ -92,9 +94,11 @@ $config = [
         'factories'  => [
             RecordController::class => AbstractBaseWithConfigFactory::class,
             UserController::class   => UserControllerFactory::class,
+            MyResearchController::class => MyResearchControllerFactory::class,
         ],
         'aliases'    => [
             \VuFind\Controller\RecordController::class => RecordController::class,
+            \VuFind\Controller\MyResearchController::class => MyResearchController::class,
         ],
         'delegators' => [
             RecordController::class => [
diff --git a/module/fid/src/Controller/MyResearchController.php b/module/fid/src/Controller/MyResearchController.php
new file mode 100644
index 00000000000..e8999fd059b
--- /dev/null
+++ b/module/fid/src/Controller/MyResearchController.php
@@ -0,0 +1,218 @@
+<?php
+/**
+ * MyResearch Controller
+ *
+ * PHP version 7
+ *
+ * Copyright (C) 2019 Leipzig University Library
+ *
+ * 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  Controller
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+namespace fid\Controller;
+
+use fid\Service\Client;
+use fid\Service\ClientException;
+use fid\Service\DataTransferObject\Library;
+use VuFind\Exception\Forbidden;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * Controller for the user account area.
+ *
+ * @category VuFind
+ * @package  Controller
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+class MyResearchController extends \VuFind\Controller\MyResearchController
+{
+    /**
+     * @var \Zend\Config\Config
+     */
+    protected $mainConfig;
+
+    /**
+     * @var \fid\Service\Client
+     */
+    protected $fidClient;
+
+    /**
+     * MyResearchController constructor.
+     *
+     * @param ServiceLocatorInterface $serviceLocator
+     * @param array  fid config       $config
+     */
+    public function __construct(
+        ServiceLocatorInterface $serviceLocator
+    ) {
+        parent::__construct($serviceLocator);
+        $this->mainConfig = $this->getConfig();
+    }
+
+    /**
+     * @param Client $fidClient
+     */
+    public function setFidClient(Client $fidClient): void
+    {
+        $this->fidClient = $fidClient;
+    }
+
+    /**
+     * Show links for Access to Specific and Press Databases, E-Books
+     *
+     * @return mixed
+     * @throws \VuFind\Exception\ILS
+     */
+    public function databasesAction()
+    {
+        if (($user = $this->getUser())) {
+            $patron = $this->catalogLogin();
+        }
+
+        $hasDbis        = $this->hasDbis();
+        $hasLicenses    = $this->hasLicenses();
+
+        if (!$hasDbis && !$hasLicenses) {
+            return $this->forwardTo('MyResearch', 'Profile');
+        }
+
+        $view = $this->createViewModel(['user' => $user ?? null]);
+
+        if ($hasDbis) {
+            $view->isDbisAvailable = true;
+
+            if ($this->fidClient->isLoggedOn() && isset($patron['user'])) {
+
+                try {
+                    $library = $this->getHomeLibrary($patron);
+                    $view->homeLibrary = $library->getDbis();
+                    $view->map_bibid_to_dbis = false;
+                } catch (ClientException $exception) {
+                    // TODO: Implement logging (in database?)
+                    $view->homeLibrary = $patron['user']->getHomeLibrary();
+                    $view->map_bibid_to_dbis = true;
+                }
+            }
+        }
+
+        if ($hasLicenses) {
+            $view->licenses = $this->getLicenses();
+        }
+
+        if (!empty($patron)) {
+            $catalog = $this->getILS();
+            $this->addAccountBlocksToFlashMessenger($catalog, $patron);
+        }
+
+        $view->setTemplate('myresearch/databases');
+        return $view;
+    }
+
+    /**
+     * Check if DBIS module is active and user has permission
+     *
+     * @return bool
+     */
+    public function hasDbis(): bool
+    {
+        // Is module activated?
+        if (!isset($this->mainConfig['ExternalDatabases']['DBIS'])
+            || $this->mainConfig['ExternalDatabases']['DBIS'] != true) {
+            return false;
+        }
+
+        // Has user rights to access?
+        try {
+            return $this->permission()->check('access.DBIS', 'exception') == null;
+        } catch (Forbidden $e) {
+            // Permission Plugin needs cumbersome exception for logged-in users
+        }
+
+        return false;
+    }
+
+    /**
+     * @param $patron
+     * @return \fid\Service\DataTransferObject\Library | null
+     * @throws \fid\Service\ClientException
+     */
+    protected function getHomeLibrary($patron): ?Library
+    {
+        if (is_a($patron['user'], 'fid\Service\DataTransferObject\User')
+            && $patron['user']->getHomeLibrary())
+        {
+            if (isset($patron['libs'][$patron['user']->getHomeLibrary()])) {
+                return $patron['libs'][$patron['user']->getHomeLibrary()];
+            }
+
+            /* no home library found in patron object? => fetch library from api directly */
+            return $this->fidClient->requestLibraryById($patron['user']->getHomeLibrary());
+        }
+
+        return null;
+    }
+
+    /**
+     * Check if licenses are specified and user has permission
+     *
+     * @return bool
+     */
+    public function hasLicenses(): bool
+    {
+        // Are licenses configured
+        if (!isset($this->mainConfig['MediaLicenses']) || count($this->mainConfig['MediaLicenses']) == 0) {
+            return false;
+        }
+
+        // Has user rights to access?
+        try {
+            return $this->permission()->check('access.Licenses', 'exception') == null;
+        } catch (Forbidden $e) {
+            // Permission Plugin needs cumbersome exception for logged-in users
+        }
+
+        return false;
+    }
+
+    /**
+     * Get links for licensed resources
+     *
+     * @return mixed
+     */
+    protected function getLicenses()
+    {
+        $licenses = false;
+        $configLicenses = $this->mainConfig['MediaLicenses'] ?? [];
+
+        if (count($configLicenses) > 0) {
+            $licenses = [];
+            $i = 0;
+            foreach ($configLicenses as $key => $value) {
+                $licenses[$i]['name'] = $key;
+                $licenses[$i]['url'] = $value;
+                $licenses[$i]['desc'] = $this->translate('licenses_' . $key . '_desc', [], '');
+                $i++;
+            }
+        }
+
+        return $licenses;
+    }
+}
diff --git a/module/fid/src/Controller/MyResearchControllerFactory.php b/module/fid/src/Controller/MyResearchControllerFactory.php
new file mode 100644
index 00000000000..ebdec5ec36f
--- /dev/null
+++ b/module/fid/src/Controller/MyResearchControllerFactory.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Service MyResearchControllerFactory
+ *
+ * PHP version 7
+ *
+ * Copyright (C) 2019 Leipzig University Library
+ *
+ * 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  Controller
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+
+namespace fid\Controller;
+
+use fid\Service\Client;
+use Psr\Container\ContainerInterface;
+use Zend\ServiceManager\ServiceManager;
+
+class MyResearchControllerFactory
+{
+    public function __construct()
+    {
+        return null;
+    }
+
+    public function __invoke(ContainerInterface $container)
+    {
+        /** @var ServiceManager $serviceManager */
+        $serviceManager = $container->get(ServiceManager::class);
+        $controller = new MyResearchController($serviceManager);
+        $controller->setFidClient($container->get(Client::class));
+
+        return $controller;
+    }
+}
diff --git a/themes/fid/languages/de.ini b/themes/fid/languages/de.ini
index 9e2428ae6d2..e7a23e50a42 100644
--- a/themes/fid/languages/de.ini
+++ b/themes/fid/languages/de.ini
@@ -3,4 +3,20 @@ Username = E-Mail-Adresse
 back_to_form = "weiter ausfüllen"
 
 #17495
-Price = "Preis"
\ No newline at end of file
+Price = "Preis"
+
+; DBIS / Licenses
+dbis_hint = "Hinweis"
+dbis_licenses_name = "Lizensierte Datenbanken"
+dbis_licenses_name_long = "Zugang zu freien und lizenzierten Fachdatenbanken"
+dbis_name = "Fachdatenbanken"
+dbis_text = "Diese Übersicht zeigt Ihnen alle frei verfügbaren Datenbanken aus DBIS zum Fachgebiet der Kommunikations- und Medienwissenschaft an. Wenn Sie mit Ihrem Nutzerkonto eingeloggt sind, werden Ihnen zusätzlich die bei Ihrer Heimatbibliothek lizenzierten Datenbanken angezeigt. Diese sind i. d. R. nur über Ihr jeweiliges Universitätsnetz bzw. per VPN-Zugang abrufbar."
+
+licenses = "Datenbanken"
+licenses_desc_1 = "Momentan haben Sie Zugriff auf:"
+licenses_forbidden_login_required = "Diese Lizenzen sind nur für angemeldete Nutzer in bestimmten Nutzergruppen verfügbar."
+licenses_forbidden_permission_denied = "Diese Lizenzen sind für Ihre Nutzergruppe leider nicht verfügbar."
+licenses_not_available = "Diese Lizenzen sind derzeit leider nicht verfügbar."
+licenses_notice_html = "Bitte nutzen Sie die Zeitungsarchive nur für Recherchen in üblichen Maßen. Die eingeräumten Lizenzen sind nicht für einen massenhaften Abruf von Zeitungsseiten ausgelegt (auch nicht für Textmining). Ein Abruf über das übliche Maß hinaus führt zu einem frühen Erschöpfen der lizenzierten Kontingente und geht zu Lasten Ihrer Kolleginnen und Kollegen.<br /><br />Bitte kontaktieren Sie uns immer vorab per E-Mail oder über unser <a data-lightbox href="%%contact_url%%">Kontaktformular</a>, wenn Sie für ein Forschungsprojekt eine größere Anzahl an Dokumenten abrufen bzw. Textmining nutzen möchten. Wir setzen uns dann mit Ihnen in Verbindung. <br /><br />Bitte beachten Sie auch unsere <a data-lightbox="" href="%%terms_url%%">Nutzungsbedingungen</a><br /><br />Herzlichen Dank für Ihr Verständnis!"
+licenses_search = "Datenbanken"
+licenses_text = "Hier haben Sie Zugang zu Datenbanken, die speziell durch uns für Sie lizenziert wurden. Die Kosten übernehmen wir für Sie."
diff --git a/themes/fid/languages/en.ini b/themes/fid/languages/en.ini
index 1fc72e4f73c..2b5ed42d0e1 100644
--- a/themes/fid/languages/en.ini
+++ b/themes/fid/languages/en.ini
@@ -1,3 +1,21 @@
+@parent_ini = ../../languages/en.ini
 Username = Email address
 
-back_to_form = "back to previous form"
\ No newline at end of file
+back_to_form = "back to previous form"
+
+; DBIS / Licenses
+dbis_hint = "Notice"
+dbis_licenses_name = "Licensed Databases"
+dbis_licenses_name_long = "Access to free and licensed databases"
+dbis_name = "subject-specific Databases"
+dbis_text = "The subject-specific databases shown below and provided by DBIS are free to access. If you are logged in with your user account, additional databases which are specifically licensed by your home library are also listed. As a rule, these can only be accessed via your respective university network or VPN access."
+
+licenses = "Databases"
+licenses_desc_1 = "Currently, you have access to:"
+licenses_desc_2 = "Further license offers are in preparation. You may also write us your suggestions, which offers should also be licensed. Please use our <a href="../Contact/ContactForm">contact form</a>.<br /><br /><br />Our <a href="../AdlrRegistration/Terms">terms of use</a> apply."
+licenses_forbidden_login_required  = "These licenses are only available for registered users in certain user groups."
+licenses_forbidden_permission_denied = "Licensed resources are not available for your user group."
+licenses_not_available = "Licensed resources temporarily not available."
+licenses_notice_html = "Please use the newspaper archives for normal searches only. The licences granted are not designed for mass retrieval of newspaper pages (e.g. for text mining). Downloads beyond the usual level will lead to premature expiry of the licensed quotas and is at the expense of your colleagues.<br /><br />Please always contact us in advance by e-mail or via our <a data-lightbox href="%%contact_url%%">contact form</a> if you would like to retrieve a larger number of documents for a research project or use text mining. We will then get in touch with you. <br /><br />Please also note our <a data-lightbox href="%%terms_url%%">Terms of Use</a>.<br /><br />Thank you for your understanding."
+licenses_search = "Databases"
+licenses_text = "Here you have access to databases that have been specially licensed for you. We cover the costs for you."
diff --git a/themes/fid/templates/myresearch/databases.phtml b/themes/fid/templates/myresearch/databases.phtml
new file mode 100644
index 00000000000..a50765777df
--- /dev/null
+++ b/themes/fid/templates/myresearch/databases.phtml
@@ -0,0 +1,57 @@
+<!-- fid: databases -->
+<?php
+
+// Set up breadcrumbs:
+$this->layout()->breadcrumbs = '<li><a href="' . $this->url('myresearch-home') . '">' . $this->transEsc('Your Account') . '</a></li> <li class="active">' . $this->transEsc('fid::dbis_licenses_name') . '</li>';
+
+// Convenience variable:
+$account = $this->auth()->getManager();
+?>
+
+<div class="<?= $this->layoutClass('mainbody') ?>">
+  <?php if ($account->isLoggedIn()): ?>
+    <?php /* finc V5: adds offcanvas-toggler missing in VF5, compare with finc/fid themes during update - CK */ ?>
+    <?=$this->render('RecordDriver/DefaultRecord/offcanvas-toggler-myresearch'); ?>
+  <?php endif; ?>
+
+  <!-- fid-adlr: licenses -->
+  <br />
+  <h3><?= $this->translate('dbis_licenses_name') ?></h3>
+  <?php if (!$account->isLoggedIn()): ?>
+    <?= $this->translate('licenses_forbidden_login_required') ?><br/><br/>
+  <?php elseif (!$this->permission()->allowDisplay('access.Licenses')) : ?>
+    <?= $this->translate('licenses_forbidden_permission_denied') ?><br/><br/>
+  <?php elseif (empty($licenses)) : ?>
+    <?= $this->translate('licenses_not_available') ?><br/><br/>
+  <?php else: ?>
+    <?= $this->translate('licenses_text') ?><br/><br/>
+    <div class="dbis_description">
+      <button class="title collapsed" data-toggle="collapse" href="#fid_terms"><?= $this->translate('dbis_hint') ?></button>
+      <div id="fid_terms" class="collapse">
+        <p>
+          <?= $this->translate('licenses_notice_html', ['%%contact_url%%' => $this->url('feedback-home'), '%%terms_url%%' => $this->url('fid/user/terms')]) ?><br/><br/>
+        </p>
+      </div>
+    </div><br/><br/>
+    <div><?= $this->transEsc('licenses_desc_1') ?></div><br/>
+    <ul class="licenses">
+      <?php foreach ($licenses as $license): ?>
+        <li>
+          <a href="<?= $license['url'] ?>" target="_blank"><?= $this->translate('licenses_' . $license['name']) ?></a><br/>
+          <span><?= $license['desc'] ?></span>
+          <br/><br/>
+        </li>
+      <?php endforeach; ?>
+    </ul>
+  <?php endif; ?>
+  <!-- fid-adlr: licenses - END -->
+
+  <?php if ($this->isDbisAvailable) : ?>
+      <?= $this->render('myresearch/dbis-content'); ?>
+  <?php endif; ?>
+</div>
+
+<div class="<?= $this->layoutClass('sidebar') ?>">
+  <?= $this->context($this)->renderInContext("myresearch/menu.phtml", ['active' => 'databases']) ?>
+</div>
+<!-- fid: databases - END -->
diff --git a/themes/fid/templates/myresearch/dbis-content.phtml b/themes/fid/templates/myresearch/dbis-content.phtml
new file mode 100644
index 00000000000..5b852e63755
--- /dev/null
+++ b/themes/fid/templates/myresearch/dbis-content.phtml
@@ -0,0 +1,30 @@
+<!-- fid: dbis -->
+<br/>
+<h3><?= $this->translate('dbis_name') ?></h3>
+<div class="dbis_description">
+  <button class="title collapsed" data-toggle="collapse" href="#dbis_description"><?= $this->translate('dbis_hint') ?></button>
+  <div id="dbis_description" class="collapse">
+    <p><?= $this->translate('dbis_text') ?></p>
+  </div>
+</div>
+<br/>
+<div class="dbis_content" id="dbis_content"><i class="fa fa-spinner fa-spin"></i> <?= $this->transEsc("Loading") ?> ...
+</div>
+<?php
+
+$dbisId = $this->homeLibrary ?? '';
+
+$dbisRequest = "$(document).ready(function () {
+    $.ajax({
+        dataType: 'json',
+        method: 'GET',
+        url: VuFind.path + '/AJAX/JSON?method=getDbis&bibId=$dbisId&map_bibid_to_dbis=$this->map_bibid_to_dbis'
+    })
+    .done(function(response) {
+        $('#dbis_content').html(response.data.html);
+    });
+});";
+
+echo $this->inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $dbisRequest, 'SET');
+?>
+<!-- fid: dbis - END -->
-- 
GitLab