Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.62% covered (danger)
34.62%
9 / 26
37.50% covered (danger)
37.50%
3 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
IlsAwareTrait
34.62% covered (danger)
34.62%
9 / 26
37.50% covered (danger)
37.50%
3 / 8
108.57
0.00% covered (danger)
0.00%
0 / 1
 attachILS
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 hasILS
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getRealTimeHoldings
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getRealTimeHistory
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getRealTimeTitleHold
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 getURLs
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 setIlsBackends
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 supportsAjaxStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * ILS support for MARC and other types of records.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2015.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  RecordDrivers
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @author   Ere Maijala <ere.maijala@helsinki.fi>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
30 */
31
32namespace VuFind\RecordDriver\Feature;
33
34use VuFind\Exception\ILS as ILSException;
35
36use function in_array;
37
38/**
39 * ILS support for MARC and other types of records.
40 *
41 * @category VuFind
42 * @package  RecordDrivers
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @author   Ere Maijala <ere.maijala@helsinki.fi>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
47 */
48trait IlsAwareTrait
49{
50    /**
51     * ILS connection
52     *
53     * @var \VuFind\ILS\Connection
54     */
55    protected $ils = null;
56
57    /**
58     * Backends with ILS integration.
59     *
60     * @var string[]
61     */
62    protected $ilsBackends = [];
63
64    /**
65     * Hold logic
66     *
67     * @var \VuFind\ILS\Logic\Holds
68     */
69    protected $holdLogic = null;
70
71    /**
72     * Title hold logic
73     *
74     * @var \VuFind\ILS\Logic\TitleHolds
75     */
76    protected $titleHoldLogic = null;
77
78    /**
79     * Attach an ILS connection and related logic to the driver
80     *
81     * @param \VuFind\ILS\Connection       $ils            ILS connection
82     * @param \VuFind\ILS\Logic\Holds      $holdLogic      Hold logic handler
83     * @param \VuFind\ILS\Logic\TitleHolds $titleHoldLogic Title hold logic handler
84     *
85     * @return void
86     */
87    public function attachILS(
88        \VuFind\ILS\Connection $ils,
89        \VuFind\ILS\Logic\Holds $holdLogic,
90        \VuFind\ILS\Logic\TitleHolds $titleHoldLogic
91    ) {
92        $this->ils = $ils;
93        $this->holdLogic = $holdLogic;
94        $this->titleHoldLogic = $titleHoldLogic;
95    }
96
97    /**
98     * Do we have an attached ILS connection?
99     *
100     * @return bool
101     */
102    protected function hasILS()
103    {
104        return null !== $this->ils
105            && in_array($this->getSourceIdentifier(), $this->ilsBackends);
106    }
107
108    /**
109     * Get an array of information about record holdings, obtained in real-time
110     * from the ILS.
111     *
112     * @return array
113     */
114    public function getRealTimeHoldings()
115    {
116        return $this->hasILS() ? $this->holdLogic->getHoldings(
117            $this->getUniqueID(),
118            $this->tryMethod('getConsortialIDs')
119        ) : [];
120    }
121
122    /**
123     * Get an array of information about record history, obtained in real-time
124     * from the ILS.
125     *
126     * @return array
127     */
128    public function getRealTimeHistory()
129    {
130        // Get Acquisitions Data
131        if (!$this->hasILS()) {
132            return [];
133        }
134        try {
135            return $this->ils->getPurchaseHistory($this->getUniqueID());
136        } catch (ILSException $e) {
137            return [];
138        }
139    }
140
141    /**
142     * Get a link for placing a title level hold.
143     *
144     * @return mixed A url if a hold is possible, boolean false if not
145     */
146    public function getRealTimeTitleHold()
147    {
148        if ($this->hasILS()) {
149            $biblioLevel = strtolower($this->tryMethod('getBibliographicLevel'));
150            if ('monograph' == $biblioLevel || strstr($biblioLevel, 'part')) {
151                if ($this->ils->getTitleHoldsMode() != 'disabled') {
152                    return $this->titleHoldLogic->getHold($this->getUniqueID());
153                }
154            }
155        }
156
157        return false;
158    }
159
160    /**
161     * Return an array of associative URL arrays with one or more of the following
162     * keys:
163     *
164     * <li>
165     *   <ul>desc: URL description text to display (optional)</ul>
166     *   <ul>url: fully-formed URL (required if 'route' is absent)</ul>
167     *   <ul>route: VuFind route to build URL with (required if 'url' is absent)</ul>
168     *   <ul>routeParams: Parameters for route (optional)</ul>
169     *   <ul>queryString: Query params to append after building route (optional)</ul>
170     * </li>
171     *
172     * @return array
173     */
174    public function getURLs()
175    {
176        $params = [$this->getUniqueId()];
177        return $this->hasILS() && $this->ils->checkCapability('getUrlsForRecord', $params)
178            ? $this->ils->getUrlsForRecord($this->getUniqueId())
179            : [];
180    }
181
182    /**
183     * Set the list of backends that support ILS integration.
184     *
185     * @param array $backends List of backends that support ILS integration
186     *
187     * @return void
188     */
189    public function setIlsBackends($backends)
190    {
191        $this->ilsBackends = $backends;
192    }
193
194    /**
195     * Returns true if the record supports real-time AJAX status lookups.
196     *
197     * @return bool
198     */
199    public function supportsAjaxStatus()
200    {
201        // as AJAX status lookups are done via the ILS AJAX status lookup support is
202        // only given if the ILS is available for this record
203        return $this->hasILS();
204    }
205}