Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Relais
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchLink
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 renderButtonIfActive
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Relais view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\Config\Config;
33
34/**
35 * Relais view helper
36 *
37 * @category VuFind
38 * @package  View_Helpers
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class Relais extends \Laminas\View\Helper\AbstractHelper
44{
45    /**
46     * Relais configuration (or null if none found)
47     *
48     * @var Config
49     */
50    protected $config;
51
52    /**
53     * Login URL
54     *
55     * @var string
56     */
57    protected $loginUrl;
58
59    /**
60     * Constructor.
61     *
62     * @param Config $config   Relais configuration (or null if none found)
63     * @param string $loginUrl Login base URL
64     */
65    public function __construct($config, $loginUrl)
66    {
67        $this->config = $config;
68        $this->loginUrl = $loginUrl;
69    }
70
71    /**
72     * Create a Relais search link from a record driver.
73     *
74     * @param object $driver Record driver
75     *
76     * @return string
77     */
78    public function getSearchLink($driver)
79    {
80        // Get data elements:
81        $isbn = $driver->tryMethod('getCleanISBN');
82        $title = $driver->tryMethod('getShortTitle');
83        if (empty($title)) {
84            $title = $driver->tryMethod('getTitle');
85        }
86        $mainAuthor = $driver->tryMethod('getPrimaryAuthor');
87
88        // Assemble and return URL:
89        $separator = strstr($this->loginUrl, '?') === false ? '?' : '&';
90        $url = $this->loginUrl . $separator . 'query='
91            . ($isbn ? 'isbn%3D' . rawurlencode($isbn) : 'ti%3D'
92            . rawurlencode($title));
93        if ($mainAuthor) {
94            $url .= '%20and%20au%3D' . rawurlencode($mainAuthor);
95        }
96        return $url;
97    }
98
99    /**
100     * Render a button if Relais is active.
101     *
102     * @param object $driver Record driver
103     *
104     * @return string
105     */
106    public function renderButtonIfActive($driver = null)
107    {
108        // Case 1: API enabled:
109        if ($this->config->apikey ?? false) {
110            return $this->getView()->render('relais/button.phtml');
111        }
112        // Case 2: Search links enabled:
113        if ($this->config->loginUrl ?? false) {
114            return '<a href="' . htmlspecialchars($this->getSearchLink($driver))
115                . '" target="new">' . $this->getView()->transEsc('relais_search')
116                . '</a>';
117        }
118        // Case 3: Nothing enabled:
119        return '';
120    }
121}