Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Translator
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 9
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 parsetable
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
56
 tab40Translate
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 tabSubLibraryTranslate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 tab15Translate
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 tab15Callback
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 tab40Callback
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 tabSubLibraryCallback
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 regexp
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Translator support class for Aleph ILS driver
5 *
6 * PHP version 8
7 *
8 * Copyright (C) UB/FU Berlin
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  ILS_Drivers
25 * @author   Vaclav Rosecky <vufind-tech@lists.sourceforge.net>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
28 */
29
30namespace VuFind\ILS\Driver\Aleph;
31
32use function call_user_func_array;
33use function get_class;
34
35/**
36 * Aleph Translator Class
37 *
38 * @category VuFind
39 * @package  ILS_Drivers
40 * @author   Vaclav Rosecky <vufind-tech@lists.sourceforge.net>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
43 */
44class Translator
45{
46    /**
47     * Character set
48     *
49     * @var string
50     */
51    protected $charset;
52
53    /**
54     * Table 15 configuration
55     *
56     * @var array
57     */
58    protected $table15;
59
60    /**
61     * Table 40 configuration
62     *
63     * @var array
64     */
65    protected $table40;
66
67    /**
68     * Sub library configuration table
69     *
70     * @var array
71     */
72    protected $table_sub_library;
73
74    /**
75     * Constructor
76     *
77     * @param array $configArray Aleph configuration
78     */
79    public function __construct($configArray)
80    {
81        $this->charset = $configArray['util']['charset'];
82        $this->table15 = $this->parsetable(
83            $configArray['util']['tab15'],
84            get_class($this) . '::tab15Callback'
85        );
86        $this->table40 = $this->parsetable(
87            $configArray['util']['tab40'],
88            get_class($this) . '::tab40Callback'
89        );
90        $this->table_sub_library = $this->parsetable(
91            $configArray['util']['tab_sub_library'],
92            get_class($this) . '::tabSubLibraryCallback'
93        );
94    }
95
96    /**
97     * Parse a table
98     *
99     * @param string $file     Input file
100     * @param string $callback Callback routine for parsing
101     *
102     * @return array
103     */
104    public function parsetable($file, $callback)
105    {
106        $result = [];
107        $file_handle = fopen($file, 'r, ccs=UTF-8');
108        $rgxp = '';
109        while (!feof($file_handle)) {
110            $line = fgets($file_handle);
111            $line = rtrim($line);
112            if (preg_match('/!!/', $line)) {
113                $line = rtrim($line);
114                $rgxp = static::regexp($line);
115            }
116            if (preg_match('/!.*/', $line) || $rgxp == '' || $line == '') {
117            } else {
118                $line = str_pad($line, 80);
119                $matches = '';
120                if (preg_match($rgxp, $line, $matches)) {
121                    call_user_func_array(
122                        $callback,
123                        [$matches, &$result, $this->charset]
124                    );
125                }
126            }
127        }
128        fclose($file_handle);
129        return $result;
130    }
131
132    /**
133     * Get a tab40 collection description
134     *
135     * @param string $collection Collection
136     * @param string $sublib     Sub-library
137     *
138     * @return string
139     */
140    public function tab40Translate($collection, $sublib)
141    {
142        $findme = $collection . '|' . $sublib;
143        $desc = $this->table40[$findme];
144        if ($desc == null) {
145            $findme = $collection . '|';
146            $desc = $this->table40[$findme];
147        }
148        return $desc;
149    }
150
151    /**
152     * Support method for tab15Translate -- translate a sub-library name
153     *
154     * @param string $sl Text to translate
155     *
156     * @return string
157     */
158    public function tabSubLibraryTranslate($sl)
159    {
160        return $this->table_sub_library[$sl];
161    }
162
163    /**
164     * Get a tab15 item status
165     *
166     * @param string $slc  Sub-library
167     * @param string $isc  Item status code
168     * @param string $ipsc Item process status code
169     *
170     * @return string
171     */
172    public function tab15Translate($slc, $isc, $ipsc)
173    {
174        $tab15 = $this->tabSubLibraryTranslate($slc);
175        if ($tab15 == null) {
176            echo 'tab15 is null!<br>';
177        }
178        $findme = $tab15['tab15'] . '|' . $isc . '|' . $ipsc;
179        $result = $this->table15[$findme] ?? null;
180        if ($result == null) {
181            $findme = $tab15['tab15'] . '||' . $ipsc;
182            $result = $this->table15[$findme];
183        }
184        $result['sub_lib_desc'] = $tab15['desc'];
185        return $result;
186    }
187
188    /**
189     * Callback for tab15 (modify $tab15 by reference)
190     *
191     * @param array  $matches preg_match() return array
192     * @param array  $tab15   result array to generate
193     * @param string $charset character set
194     *
195     * @return void
196     */
197    public static function tab15Callback($matches, &$tab15, $charset)
198    {
199        $lib = $matches[1];
200        $no1 = $matches[2];
201        if ($no1 == '##') {
202            $no1 = '';
203        }
204        $no2 = $matches[3];
205        if ($no2 == '##') {
206            $no2 = '';
207        }
208        $desc = iconv($charset, 'UTF-8', $matches[5]);
209        $key = trim($lib) . '|' . trim($no1) . '|' . trim($no2);
210        $tab15[trim($key)] = [
211            'desc' => trim($desc), 'loan' => $matches[6], 'request' => $matches[8],
212            'opac' => $matches[10],
213        ];
214    }
215
216    /**
217     * Callback for tab40 (modify $tab40 by reference)
218     *
219     * @param array  $matches preg_match() return array
220     * @param array  $tab40   result array to generate
221     * @param string $charset character set
222     *
223     * @return void
224     */
225    public static function tab40Callback($matches, &$tab40, $charset)
226    {
227        $code = trim($matches[1]);
228        $sub = trim($matches[2]);
229        $sub = trim(preg_replace('/#/', '', $sub));
230        $desc = trim(iconv($charset, 'UTF-8', $matches[4]));
231        $key = $code . '|' . $sub;
232        $tab40[trim($key)] = [ 'desc' => $desc ];
233    }
234
235    /**
236     * Sub-library callback (modify $tab_sub_library by reference)
237     *
238     * @param array  $matches         preg_match() return array
239     * @param array  $tab_sub_library result array to generate
240     * @param string $charset         character set
241     *
242     * @return void
243     */
244    public static function tabSubLibraryCallback(
245        $matches,
246        &$tab_sub_library,
247        $charset
248    ) {
249        $sublib = trim($matches[1]);
250        $desc = trim(iconv($charset, 'UTF-8', $matches[5]));
251        $tab = trim($matches[6]);
252        $tab_sub_library[$sublib] = [ 'desc' => $desc, 'tab15' => $tab ];
253    }
254
255    /**
256     * Apply standard regular expression cleanup to a string.
257     *
258     * @param string $string String to clean up.
259     *
260     * @return string
261     */
262    public static function regexp($string)
263    {
264        $string = preg_replace('/\\-/', ')\\s(', $string);
265        $string = preg_replace('/!/', '.', $string);
266        $string = preg_replace('/[<>]/', '', $string);
267        $string = '/(' . $string . ')/';
268        return $string;
269    }
270}