Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Bookplate
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
10 / 10
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
 getBookplateData
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getBookplateFullUrlTemplate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBookplateThumbUrlTemplate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 displayBookplateTitles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBookplateTitlesField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBookplateFullImagesField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBookplateThumbnailsField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBookplateDetails
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Related Records: Bookplates
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2021.
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  Related_Records
25 * @author   Brad Busenius <bbusenius@uchicago.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:related_records_modules Wiki
28 */
29
30namespace VuFind\Related;
31
32/**
33 * Related Records: Bookplates
34 *
35 * @category VuFind
36 * @package  Related_Records
37 * @author   Brad Busenius <bbusenius@uchicago.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development:plugins:related_records_modules Wiki
40 */
41class Bookplate implements RelatedInterface
42{
43    /**
44     * Bookplate config
45     */
46    protected $config;
47
48    /**
49     * Data fields (usually Solr)
50     */
51    protected $fields;
52
53    /**
54     * Bookplate strings
55     */
56    protected $bookplateStrs;
57
58    /**
59     * Bookplate image names or full URLs
60     */
61    protected $bookplateImages;
62
63    /**
64     * Bookplate thumbnail image names or thumbnail URLs
65     */
66    protected $bookplateThumbnails;
67
68    /**
69     * URL template for full bookplate
70     */
71    protected $fullUrlTemplate;
72
73    /**
74     * URL template for thumbnail
75     */
76    protected $thumbUrlTemplate;
77
78    /**
79     * Display bookplate titles?
80     */
81    protected $displayTitles;
82
83    /**
84     * Configuration loader
85     *
86     * @var \VuFind\Config\PluginManager
87     */
88    protected $configLoader;
89
90    /**
91     * Constructor
92     *
93     * @param \VuFind\Config\PluginManager $configLoader PluginManager
94     */
95    public function __construct(\VuFind\Config\PluginManager $configLoader)
96    {
97        $this->configLoader = $configLoader;
98    }
99
100    /**
101     * Establishes base settings for bookplates.
102     *
103     * @param string                            $settings Settings from config.ini
104     * @param \VuFind\RecordDriver\AbstractBase $driver   Record driver object
105     *
106     * @return void
107     */
108    public function init($settings, $driver)
109    {
110        $config = array_map('trim', explode(':', $settings));
111        $configFile = !empty($config[0]) ? $config[0] : 'config';
112        $configSection = !empty($config[1]) ? $config[1] : 'Record';
113        $this->config = $this->configLoader->get($configFile)->$configSection;
114        $this->fields = $driver->getRawData();
115        $this->bookplateStrs = $this->getBookplateData(
116            $this->getBookplateTitlesField()
117        );
118        $this->bookplateImages = $this->getBookplateData(
119            $this->getBookplateFullImagesField()
120        );
121        $this->bookplateThumbnails = $this->getBookplateData(
122            $this->getBookplateThumbnailsField()
123        );
124        $this->fullUrlTemplate = $this->getBookplateFullUrlTemplate();
125        $this->thumbUrlTemplate = $this->getBookplateThumbUrlTemplate();
126        $this->displayTitles = $this->displayBookplateTitles();
127    }
128
129    /**
130     * Get an array of data representing bookplates.
131     *
132     * @param $field string name of data to retrieve.
133     *
134     * @return array
135     */
136    protected function getBookplateData($field)
137    {
138        if (!empty($field) && isset($this->fields[$field])) {
139            return (array)$this->fields[$field];
140        }
141        return [];
142    }
143
144    /**
145     * Get the full bookplate URL string template.
146     *
147     * @return string
148     */
149    protected function getBookplateFullUrlTemplate()
150    {
151        return $this->config->bookplate_full ?? '';
152    }
153
154    /**
155     * Get the bookplate URL thumbnail string template.
156     *
157     * @return string
158     */
159    protected function getBookplateThumbUrlTemplate()
160    {
161        return $this->config->bookplate_thumb ?? '';
162    }
163
164    /**
165     * Display titles under bookplates.
166     *
167     * @return boolean
168     */
169    protected function displayBookplateTitles()
170    {
171        return $this->config->bookplate_display_title ?? true;
172    }
173
174    /**
175     * Get a data field with an array of bookplate image titles.
176     *
177     * @return string
178     */
179    protected function getBookplateTitlesField()
180    {
181        return $this->config->bookplate_titles_field ?? '';
182    }
183
184    /**
185     * Get a data field with an array of strings that represent full images.
186     * These could be the unique parts of image names (e.g. donor code) or
187     * full paths to image files.
188     *
189     * @return string
190     */
191    protected function getBookplateFullImagesField()
192    {
193        return $this->config->bookplate_images_field ?? '';
194    }
195
196    /**
197     * Get a data field with an array of strings that represent thumbnails.
198     * These could be the unique parts of thumbnail names (e.g. donor code)
199     * or full paths to thumbnail image files.
200     *
201     * @return string
202     */
203    protected function getBookplateThumbnailsField()
204    {
205        return $this->config->bookplate_thumbnails_field ?? '';
206    }
207
208    /**
209     * Get bookplate details for display.
210     *
211     * @return array
212     */
213    public function getBookplateDetails()
214    {
215        $hasBookplates = !empty($this->bookplateStrs);
216        if ($hasBookplates) {
217            $data = [];
218            foreach ($this->bookplateStrs as $i => $bookplate) {
219                $tokens = [
220                    '%%img%%',
221                    '%%thumb%%',
222                ];
223                $tokenValues = [
224                    $this->bookplateImages[$i] ?? '',
225                    $this->bookplateThumbnails[$i] ?? '',
226                ];
227                $imgUrl = str_replace(
228                    $tokens,
229                    $tokenValues,
230                    $this->fullUrlTemplate
231                );
232                $imgThumb = str_replace(
233                    $tokens,
234                    $tokenValues,
235                    $this->thumbUrlTemplate
236                );
237                $data[$i] = ['title' => $bookplate,
238                             'fullUrl' => $imgUrl,
239                             'thumbUrl' => $imgThumb,
240                             'displayTitle' => $this->displayTitles];
241            }
242            return $data;
243        }
244        return [];
245    }
246}