]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/templates/opac/parts/misc_util.tt2
TPAC: Make corner image link to server root
[working/Evergreen.git] / Open-ILS / src / templates / opac / parts / misc_util.tt2
1 [% 
2     # Support multiscript records via alternate graphic 880 fields
3     # get_graphic_880s(target_field='100')
4     # See "Model A" in http://www.loc.gov/marc/bibliographic/ecbdmulti.html
5     # and $6 description in http://www.loc.gov/marc/bibliographic/ecbdcntf.html
6     MACRO get_graphic_880s BLOCK;
7         FOR node IN xml.findnodes('//*[@tag="' _ target_field _ '"]');
8             raw_vals = [];
9             core_val = '';
10             FOR subnode IN node.findnodes('./*[not(contains("w 0 4 5 6 8 9", @code))]');
11                 raw_vals.push(subnode.textContent());
12             END;
13             core_val = raw_vals.join(" ");
14             raw_vals = [];
15
16             linked_fields = [];
17             FOR sub IN node.findnodes('./*[@code="6"]');
18                 linked_fields.push(sub.textContent);
19             END;
20             graphics = [];
21             get_linked_880s;
22             graphic_880s.push({
23                 primary => {"occur" => occurrence, "value" => core_val},
24                 graphic => graphics
25             });
26         END;
27     END;
28
29     MACRO get_linked_880s BLOCK;
30         FOR link_field IN linked_fields;
31             target = target_field _ link_field.substr(3);
32             # Get the linked 880 value
33             raw_val = '';
34             dir = '';
35             occurrence = '';
36             script = '';
37             FOR node IN xml.findnodes('//*[@tag="880"]');
38                 # Operate only on the target linked fields
39                 FOR linknode IN node.findnodes('./*[@code="6"]');
40                     lf = linknode.textContent();
41                     IF lf.substr(0, target.length) == target;
42                         occurrence = lf.substr(4, 2);
43                         rawscript = lf.substr(7, 2);
44                         SWITCH rawscript;
45                         CASE '(3';
46                             script = 'Arabic';
47                         CASE '(B';
48                             script = 'Latin';
49                         CASE '$1';
50                             script = 'CJK';
51                         CASE '(N';
52                             script = 'Cyrillic';
53                         CASE '(S';
54                             script = 'Greek';
55                         CASE '(2';
56                             script = 'Hebrew';
57                         END;
58
59                         rawdir = lf.substr(9, 1);
60                         SWITCH rawdir;
61                         CASE 'r';
62                             dir = 'rtl';
63                         END;
64
65                         raw_vals = [];
66                         FOR subnode IN node.findnodes('./*[not(contains("w 0 5 6 8 9", @code))]');
67                             raw_vals.push(subnode.textContent());
68                         END;
69                         raw_val = raw_vals.join(" ");
70                     END;
71                 END;
72             END;
73             graphics.push({
74                 occur => occurrence,
75                 value => raw_val,
76                 script => script,
77                 dir => dir
78             });
79         END;
80     END;
81
82     # Extract MARC fields from XML
83     #   get_marc_attrs( { marc_xml => doc } )
84     BLOCK get_marc_attrs;
85         xml = args.marc_xml;
86
87         # Map item types to schema.org types; impedance mismatch :(
88         args.schema.itemtype = {};
89         schema_typemap = {};
90         schema_typemap.a = 'http://schema.org/Book';
91         schema_typemap.e = 'http://schema.org/Map';
92         schema_typemap.j = 'http://schema.org/MusicAlbum';
93
94         # Hard-coded to match defaults in config.copy_status for all OPAC-visible statuses
95         schema_copy_status = {};
96         schema_copy_status.0 = '<link property="availability" href="http://schema.org/InStock" />'; # Available
97         schema_copy_status.1 = '<link property="availability" href="http://schema.org/OutOfStock" />'; # Checked out
98         schema_copy_status.5 = '<link property="availability" href="http://schema.org/PreOrder" />'; # In process
99         schema_copy_status.6 = '<link property="availability" href="http://schema.org/PreOrder" />'; # In transit
100         schema_copy_status.7 = '<link property="availability" href="http://schema.org/InStock" />'; # Reshelving
101         schema_copy_status.8 = '<link property="availability" href="http://schema.org/OutOfStock" />'; # On holds shelf
102         schema_copy_status.9 = '<link property="availability" href="http://schema.org/PreOrder" />'; # On order
103         schema_copy_status.12 = '<link property="availability" href="http://schema.org/InStoreOnly" />'; # Reserves
104
105         args.isbns = [];
106         FOR isbn IN xml.findnodes('//*[@tag="020"]/*[@code="a"]');
107             args.isbns.push(isbn.textContent);
108         END;
109
110         args.upcs = [];
111         FOR upc IN xml.findnodes('//*[@tag="024"]/*[@code="a"]');
112             args.upcs.push(upc.textContent);
113         END;
114         args.upc = args.upcs.0; # use first UPC as the default
115
116         args.issns = [];
117         FOR sub IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
118             args.issns.push(sub.textContent);
119         END;
120         args.issn = (args.issns.size) ? args.issn.0 : '';
121
122         graphic_880s = [];
123         get_graphic_880s(target_field='100');
124         get_graphic_880s(target_field='110');
125         get_graphic_880s(target_field='111');
126         args.graphic_authors = graphic_880s;
127         args.authors = [];
128         FOR author IN args.graphic_authors;
129             args.authors.push(author.primary.value);
130         END;
131         args.author = (args.authors.size) ? args.authors.0 : '';
132
133         # Include subfields 'abnp' to generate a more comprehensive title display in search results
134         titresults = xml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b" or @code="n" or @code="p"]');
135         titresults_content = [];
136             FOR sub IN titresults; titresults_content.push(sub.textContent); END;
137         args.title = titresults_content.join(" ");
138         # Avoid ugly trailing syntax on brief titles
139         args.title = args.title | replace('[:;/]$', '');
140
141         graphic_880s = [];
142         get_graphic_880s(target_field='245');
143         args.graphic_titles = graphic_880s;
144         args.titles = [];
145         FOR title IN args.graphic_titles;
146             args.titles.push(title.primary.value);
147         END;
148         args.title_extended = (args.titles.size) ? args.titles.0 : '';
149
150         args.pubplaces = [];
151         pubplace_hunt = xml.findnodes('//*[@tag="260"]/*[@code="a"]') ||
152             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="a"]');
153         FOR sub IN pubplace_hunt;
154             args.pubplaces.push(sub.textContent);
155         END;
156         args.pubplace = (args.pubplaces.size) ? args.pubplaces.0 : '';
157
158         args.publishers = [];
159         publishers_hunt = xml.findnodes('//*[@tag="260"]/*[@code="b"]') ||
160             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="b"]');
161         FOR sub IN publishers_hunt;
162             args.publishers.push(sub.textContent);
163         END;
164         args.publisher = (args.publishers.size) ? args.publishers.0 : '';
165
166         args.pubdates = [];
167         pubdates_hunt = xml.findnodes('//*[@tag="260"]/*[@code="c"]') || 
168             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="c"]');
169         FOR sub IN pubdates_hunt;
170             args.pubdates.push(sub.textContent);
171         END;
172         args.pubdate = (args.pubdates.size) ? args.pubdates.0 : '';
173
174         # Full publisher info
175         args.pubinfo = "$args.pubplace $args.publisher $args.pubdate";
176
177         graphic_880s = [];
178         get_graphic_880s(target_field='260');
179         args.graphic_pubinfos = graphic_880s;
180         args.pubinfos = [];
181         FOR pubinfo IN args.graphic_pubinfos;
182             args.pubinfos.push(pubinfo.primary.value);
183         END;
184         args.pubinfo = (args.pubinfos.size) ? args.pubinfos.0 : '';
185
186         args.summaries = [];
187         FOR sub IN xml.findnodes('//*[@tag="520"]/*[@code="a"]');
188             args.summaries.push(sub.textContent);
189         END;
190         args.summary = (args.summaries.size) ? args.summaries.0 : '';
191
192         # 250 gets pride of place for edition statement, and is the
193         # only logical choice for 880 graphic fields
194         graphic_880s = [];
195         get_graphic_880s(target_field='250');
196         args.graphic_editions = graphic_880s;
197         args.editions = [];
198         FOR edition IN args.graphic_editions;
199             args.editions.push(edition.primary.value);
200         END;
201
202         ed_hunt = xml.findnodes('//*[@tag="250"]/*[@code="a"]') ||
203             xml.findnodes('//*[@tag="534"]/*[@code="b"]') ||
204             xml.findnodes('//*[@tag="775"]/*[@code="b"]');
205         FOR sub IN ed_hunt;
206             args.editions.push(sub.textContent);
207         END;
208         args.edition = (args.editions.size) ? args.editions.0 : '';
209
210         phys_content = [];
211         FOR sub IN xml.findnodes(
212             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
213         );
214             phys_content.push(sub.textContent);
215         END;
216         args.phys_desc = phys_content.join(" ");
217
218         graphic_880s = [];
219         get_graphic_880s(target_field='505');
220         args.graphic_contents = graphic_880s;
221         FOR content IN args.graphic_contents;
222             args.contents.push(content.primary.value);
223         END;
224         args.content = (args.contents.size) ? args.contents.0 : '';
225
226         # Maintain contents_list in case any custom use was made of it
227         args.contents_list = [];
228         FOR sub IN xml.findnodes('//*[@tag="505"]');
229             args.contents_list.push(sub.textContent);
230         END;
231
232         # MARC Callnumber
233         args.marc_cns = [];
234         FOR sub IN xml.findnodes('//*[@tag="092" or @tag="099"]/*');
235             args.marc_cns.push(sub.textContent);
236         END;
237         args.marc_cn = (args.marc_cns.size ) ? args.marc_cns.0 : '';
238             
239
240         # clean up the ISBN
241         args.isbn_clean = args.isbns.0.replace('\ .*', '');
242         FOR isbn IN args.isbns;
243             clean_isbn = isbn.replace('\ .*', '');
244             clean_isbn = clean_isbn.replace('-', '');
245             IF clean_isbn.length == 13;
246                 args.gtin13 = clean_isbn;
247                 LAST;
248             END;
249         END;
250
251         # Extract the 856 URLs that are not otherwise represented by asset.uri's
252         args.online_res = [];
253         FOR node IN xml.findnodes('//*[@tag="856" and @ind1="4" and (@ind2="0" or @ind2="1")]');
254             IF node.findnodes('./*[@code="9" or @code="w" or @code="n"]'); NEXT; END; # asset.uri's
255             label = node.findnodes('./*[@code="y"]');
256             notes = node.findnodes('./*[@code="z" or @code="3"]');
257             FOR href IN node.findnodes('./*[@code="u"]');
258                 NEXT UNLESS href;
259                 # it's possible for multiple $u's to exist within 1 856 tag.
260                 # in that case, honor the label/notes data for the first $u, but
261                 # leave any subsequent $u's as unadorned href's. 
262                 # use href/link/note keys to be consistent with args.uri's
263                 args.online_res.push({
264                     href => href.textContent, 
265                     link => (loop.first AND label) ? label.textContent : href.textContent,
266                     note => (loop.first) ? notes.textContent : ''
267                 });
268             END;
269         END;
270  
271         args.holdings = [];
272         args.uris = [];
273         args.issns = [];
274         args.resolver_isbns = [];
275         args.resolver_issns = [];
276
277         # we use $9 of ISBN and ISSN as a flag for e-version
278         FOR resolver_isbn IN xml.findnodes('//*[@tag="020"]/*[@code="9"]');
279             IF resolver_isbn.textContent == "SFX" || resolver_isbn.textContent == "CUFTS";
280                 my_parent = resolver_isbn.parentNode();
281                 FOR resolver_isbn_val IN my_parent.findnodes('./*[@code="a"]');
282                     args.resolver_isbns.push(
283                         resolver_isbn_val.textContent.replace('-', '').replace('\ .*', '')
284                     );
285                 END;
286             END;
287         END;
288
289         FOR resolver_issn IN xml.findnodes('//*[@tag="022"]/*[@code="9"]');
290             IF resolver_issn.textContent == "SFX" || resolver_issn.textContent == "CUFTS";
291                 my_parent = resolver_issn.parentNode();
292                 FOR resolver_issn_val IN my_parent.findnodes('./*[@code="a"]');
293                     args.resolver_issns.push(
294                         resolver_issn_val.textContent.replace('[^\d\-X]', '')
295                     );
296                 END;
297             END;
298         END;
299
300         # now snag all issns 
301         FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
302             args.issns.push(
303                 rawissn.textContent.replace('[^\d\-X]', '')
304             );
305         END;
306
307         ou_hiding_disabled = ctx.org_hiding_disabled();
308
309         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
310
311             # Check volume visibility - could push this into XPath
312             vol.label = volume.getAttribute('label');
313
314             # Prepend prefix, if any
315             prefix = volume.findnodes('./*[local-name()="call_number_prefix"][@ident!="-1"]');
316             IF prefix.getAttribute('label') != '';
317                 vol.label = prefix.getAttribute('label') _ " " _ vol.label;
318             END;
319
320             # Append prefix, if any
321             suffix = volume.findnodes('./*[local-name()="call_number_suffix"][@ident!="-1"]');
322             IF suffix.getAttribute('label') != '';
323                 vol.label = vol.label _ " " _ suffix.getAttribute('label');
324             END;
325
326             vol.id = volume.getAttribute('id');
327             NEXT IF volume.getAttribute('opac_visible') == 'false';
328             NEXT IF volume.getAttribute('deleted') == 'true';
329
330             IF vol.label == '##URI##';
331                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
332                     res = {};
333                     res.href = uri.getAttribute('href');
334                     res.link = uri.getAttribute('label');
335                     res.note = uri.getAttribute('use_restriction');
336                     args.uris.push(res);
337                 END;
338                 NEXT;
339             ELSE;
340                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
341                 FOR copy IN copies;
342                     parts = copy.findnodes('./*[local-name()="monograph_parts"]/*[local-name()="monograph_part"]');
343                     FOREACH part IN parts;
344                         part_label = part.getAttribute('label');
345                         LAST IF part_label != '';
346                     END;
347                     # Check copy visibility
348                     cp.deleted = copy.getAttribute('deleted');    
349                     cp.visible = copy.getAttribute('opac_visible');
350                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
351
352                     # Iterate through all of the children to determine visibility
353                     FOR node IN cp.childNodes;
354                         NEXT IF cp.visible == 'false';
355                         vis = node.getAttribute('opac_visible');
356                         del = node.getAttribute('deleted');
357                         IF vis == 'false' or del == 'true';
358                             cp.visible = 'false';
359                         END;
360                     END;
361
362                     NEXT IF cp.visible == 'false';
363                     
364                     loc = copy.findnodes('./*[local-name()="location"]');
365                     NEXT IF loc.getAttribute('opac_visible') == 'false';
366
367                     circlib = copy.findnodes('./*[local-name()="circlib"]');
368                     NEXT IF circlib.getAttribute('opac_visible') == 'false';
369
370                     status = copy.findnodes('./*[local-name()="status"]');
371                     NEXT IF status.getAttribute('opac_visible') == 'false';
372
373                     # extract the circ_lib id from the circ_lib node
374                     circ_lib = copy.findnodes('./*[local-name()="circ_lib"]');
375                     circ_lib_id = circ_lib.getAttribute('id').replace('.*/', '');
376
377                     UNLESS ou_hiding_disabled;
378                         NEXT UNLESS ctx.org_within_hiding_scope(circ_lib_id);
379                     END;
380
381                     holding = {
382                         circ_lib => circ_lib_id,
383                         label => vol.label,
384                         part_label => part_label,
385                         location => loc.textContent,
386                         library => circlib.textContent,
387                         status => status.textContent,
388                         status_code => status.getAttribute('ident'),
389                         barcode => copy.getAttribute('barcode'),
390                         owner => volume.getAttribute('lib')
391                     };
392                     args.holdings.push(holding);
393                     part_label = '';
394                 END;
395             END;
396         END;
397
398         # Extract the copy count summary
399         count_type = (ctx.is_staff) ? 'staff' : 'public';
400
401         # Consortial copy count summary first
402         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
403         args.copy_counts = {};
404         FOR node IN xml.findnodes(xpath);
405             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
406                 depth = node.getAttribute('depth');
407                 count_org_unit = node.getAttribute('org_unit');
408                 args.copy_counts.$depth.$attr = node.getAttribute(attr);
409                 args.org_copy_counts.$count_org_unit.$attr = node.getAttribute(attr);
410             END;
411         END;
412
413         # Get preferred library copy count
414         args.plib_copy_counts = {};
415         count_type = 'pref_lib';
416         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
417         FOR node IN xml.findnodes(xpath);
418             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
419                 depth = node.getAttribute('depth');
420                 args.plib_copy_counts.$depth.$attr = node.getAttribute(attr);
421             END;
422         END;
423
424         # "mattype" == "custom marc format specifier"
425         FOR icon_style IN ['mattype', 'item_type']; 
426             node = xml.findnodes(
427                 '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]');
428             IF node AND node.textContent;
429                 type = node.textContent;
430                 args.format_label = node.getAttribute('coded-value')
431                 args.schema.itemtype = schema_typemap.$type || 'CreativeWork';
432                 args.format_icon = ctx.media_prefix _ '/images/format_icons/' _ icon_style _ '/' _ type _ '.png';
433                 LAST;
434             END;
435         END;
436         
437         args.bibid = [];
438         FOR bibid IN xml.findnodes('//*[@tag="901"]/*[@code="c"]');
439             args.bibid.push(bibid.textContent);
440         END;
441         args.bibid = args.bibid.0; 
442
443     END;
444
445     # Get the library or location group
446     # get_library()
447     # magically upgrades any use of 'loc' to 'locg', 
448     # which is a superset of 'loc'.
449     BLOCK get_library;
450         loc_name = 'locg';
451         loc_value = ctx.copy_location_group_org ||  # resolved locg
452             CGI.param(loc_name) || CGI.param('loc') || ctx.search_ou;
453     END;
454
455 %]