]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/templates/opac/parts/misc_util.tt2
LP#1744385: Allow highlight disabling globally via config.tt2
[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("e w 0 4 5 6 7 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     BLOCK get_ccvm_icon;
83         ctx.media_prefix _ '/images/format_icons/' _ ccvm.ctype _ '/' _ ccvm.code _ '.png' _ ctx.cache_key;
84     END;
85
86     # Extract MARC fields from XML
87     #   get_marc_attrs( args = { marc_xml => doc } )
88     BLOCK get_marc_attrs;
89         USE Dumper;
90         xml = args.marc_xml;
91
92         args.bibid = [];
93         FOR bibid IN xml.findnodes('//*[@tag="901"]/*[@code="c"]');
94             args.bibid.push(bibid.textContent);
95         END;
96
97         args.df_bib_list = args.bibid;
98         args.bibid = args.bibid.0; 
99
100         IF args.mr_constituent_ids.size && !args.df_bib_list.size;
101             args.df_bib_list = args.mr_constituent_ids;
102         END;
103
104
105         # Gather display field data for this record and map it
106         # to a display field map.  Hopefully, one day, this can
107         # replace the XPath below entirely.
108
109         args.display_fields = {};
110         args.hl = {};
111         IF !CGI.param('no_highlight') && !search.no_highlight;
112             args.display_field_list = ctx.fetch_display_fields(args.df_bib_list.list);
113
114             FOR df IN args.display_field_list;
115                 df_map = ctx.search_cdfm('field', df.field).0;
116                 df_name = df_map.name();
117                 IF df_map.multi() == 't';
118                     IF NOT args.hl_display_fields.$df_name;
119                         args.hl_display_fields.$df_name = [];
120                         args.hl.$df_name = [];
121                     END;
122                     args.hl_display_fields.$df_name.push(df);
123                     args.hl.$df_name.push(df.highlight || df.value);
124                 ELSIF !args.hl_display_fields.$df_name.defined;
125                     args.hl_display_fields.$df_name = df;
126                     args.hl.$df_name = df.highlight || df.value;
127                 END;
128             END;
129         END;
130
131         # Map item types to schema.org types; impedance mismatch :(
132         args.schema.itemtype = {};
133         schema_typemap = {};
134
135         schema_typemap.bluray = 'Movie'; # Movie could also be TVSeries
136         schema_typemap.book = 'Book';
137         schema_typemap.braille = 'Book';
138         schema_typemap.casaudiobook = 'Book AudioObject';
139         schema_typemap.casmusic = 'MusicAlbum';
140         schema_typemap.cdaudiobook = 'Book AudioObject';
141         schema_typemap.cdmusic = 'MusicAlbum';
142         schema_typemap.dvd = 'Movie';
143         schema_typemap.eaudio = 'AudioObject';
144         schema_typemap.ebook = 'Book';
145         # schema_typemap.equip = '';
146         schema_typemap.evideo = 'Movie';
147         # schema_typemap.kit = '';
148         schema_typemap.lpbook = 'Book';
149         schema_typemap.map = 'Map';
150         # schema_typemap.microform = '';
151         schema_typemap.music = 'MusicAlbum';
152         schema_typemap.phonomusic = 'MusicAlbum';
153         # schema_typemap.phonospoken = '';
154         # schema_typemap.picture = ''; Painting or Photograph?
155         schema_typemap.score = 'Book'; # schema.org has no generic Music type
156         schema_typemap.serial = 'Periodical';
157         schema_typemap.software = 'SoftwareApplication';
158         schema_typemap.vhs = 'Movie';
159
160         schema_typemap.a = 'Book';
161         schema_typemap.e = 'Map';
162         schema_typemap.j = 'MusicAlbum';
163
164         # Hard-coded to match defaults in config.copy_status for all OPAC-visible statuses
165         schema_copy_status = {};
166         schema_copy_status.0 = '<link property="availability" href="http://schema.org/InStock" />'; # Available
167         schema_copy_status.1 = '<link property="availability" href="http://schema.org/OutOfStock" />'; # Checked out
168         schema_copy_status.5 = '<link property="availability" href="http://schema.org/PreOrder" />'; # In process
169         schema_copy_status.6 = '<link property="availability" href="http://schema.org/PreOrder" />'; # In transit
170         schema_copy_status.7 = '<link property="availability" href="http://schema.org/InStock" />'; # Reshelving
171         schema_copy_status.8 = '<link property="availability" href="http://schema.org/OutOfStock" />'; # On holds shelf
172         schema_copy_status.9 = '<link property="availability" href="http://schema.org/PreOrder" />'; # On order
173         schema_copy_status.12 = '<link property="availability" href="http://schema.org/InStoreOnly" />'; # Reserves
174
175         args.isbns = [];
176         FOR isbn IN xml.findnodes('//*[@tag="020"]/*[@code="a"]');
177             args.isbns.push(isbn.textContent);
178         END;
179
180         # UPCs can be identified either by ind1="1" or subfield 2 of "upc"
181         args.upcs = [];
182         FOR upc IN xml.findnodes('//*[@tag="024" and @ind="1"]/*[@code="a"]');
183             args.upcs.push(upc.textContent);
184         END;
185         FOR upcfield IN xml.findnodes('//*[@tag="024"]/*[@code="2" and text()="upc"]');
186             my_parent = upcfield.parentNode();
187             FOR upc IN my_parent.findnodes('./*[@code="a"]');
188                 args.upcs.push(upc.textContent);
189             END;
190         END;
191         args.upc = args.upcs.0; # use first UPC as the default
192
193         # These are URIs that link out to related works for linked data purposes,
194         # such as OCLC Work Entities like http://worldcat.org/entity/work/id/415491
195
196         # We differentiate between http://schema.org/sameAs relations, which
197         # are roughly the same as the work we're describing here, and
198         # http://schema.org/exampleOfWork which point to a broader work, of which
199         # this is a more specific edition or manifestation.
200         args.links.sameAs = [];
201         args.links.exampleOfWork = [];
202         FOR link_node IN xml.findnodes('//*[@tag="024"]/*[@code="2" and text()="uri"]');
203             my_parent = link_node.parentNode();
204             FOR link IN my_parent.findnodes('./*[@code="a"]');
205                 link_uri = link.textContent | trim;
206                 IF link_uri.search('worldcat.org/entity/work');
207                     args.links.exampleOfWork.push(link_uri);
208                 ELSE;
209                     args.links.sameAs.push(link_uri);
210                 END;
211             END;
212         END;
213         FOR lccn IN xml.findnodes('//*[@tag="010"]/*[@code="a"]');
214             lccn_num = lccn.textContent | trim;
215             args.links.sameAs.push('http://lccn.loc.gov/' _ lccn_num);
216         END;
217         FOR oclcnum IN xml.findnodes('//*[@tag="035"]/*[@code="a"]');
218             oclcnum = oclcnum.textContent | trim;
219             NEXT UNLESS oclcnum.search('^\(OCoLC\)');
220             oclcnum = oclcnum | remove('\(OCoLC\)');
221             # These prefixes are often included in the 035, even though they should not be
222             oclcnum = oclcnum | remove('ocm');
223             oclcnum = oclcnum | remove('ocn');
224             oclcnum = oclcnum | remove('on') | trim;
225             args.links.sameAs.push('http://www.worldcat.org/oclc/' _ oclcnum);
226         END;
227         args.links.sameAs = args.links.sameAs.unique;
228
229         args.issns = [];
230         FOR sub IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
231             args.issns.push(sub.textContent);
232         END;
233         args.issn = (args.issns.size) ? args.issn.0 : '';
234
235         graphic_880s = [];
236         get_graphic_880s(target_field='100');
237         get_graphic_880s(target_field='110');
238         get_graphic_880s(target_field='111');
239         args.graphic_authors = graphic_880s;
240         args.authors = [];
241         FOR author IN args.graphic_authors;
242             args.authors.push(author.primary.value);
243         END;
244         args.author = (args.authors.size) ? args.authors.0 : '';
245
246         # Include subfields 'abnp' to generate a more comprehensive title display in search results
247         titresults = xml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b" or @code="n" or @code="p"]');
248         titresults_content = [];
249             FOR sub IN titresults; titresults_content.push(sub.textContent); END;
250
251         args.title = titresults_content.join(" ");
252         # Avoid ugly trailing syntax on brief titles
253         args.title = args.title | replace('[:;/]$', '');
254
255         graphic_880s = [];
256         get_graphic_880s(target_field='245');
257         args.graphic_titles = graphic_880s;
258         args.titles = [];
259         FOR title IN args.graphic_titles;
260             args.titles.push(title.primary.value);
261         END;
262         args.title_extended = (args.titles.size) ? args.titles.0 : '';
263
264         # Create a version of the title designed for sorted displays.
265         args.sort_title = args.title | upper;
266
267         # If the title has a "non-filing chaaracters" 
268         # (to logically remove leading "The " for example)
269         # chop the title. Otherwise, chop until the first alphanumeric.
270         # BTW: Template Toolkit folds 1-element arrays to scalars!
271         title_node = xml.findnodes('//*[@tag="245"]');
272
273         args.nonfiling_characters = title_node.findvalue('@ind2');
274       
275         IF (args.nonfiling_characters > 0);
276              args.sort_title = args.sort_title.substr(args.nonfiling_characters); 
277         ELSE;
278              args.sort_title = args.sort_title.replace('^[^A-Z0-9]*','');
279         END;
280       
281         args.pubplaces = [];
282         pubplace_hunt = xml.findnodes('//*[@tag="260"]/*[@code="a"]') ||
283             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="a"]');
284         FOR sub IN pubplace_hunt;
285             args.pubplaces.push(sub.textContent);
286         END;
287         args.pubplace = (args.pubplaces.size) ? args.pubplaces.0 : '';
288
289         args.publishers = [];
290         publishers_hunt = xml.findnodes('//*[@tag="260"]/*[@code="b"]') ||
291             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="b"]');
292         FOR sub IN publishers_hunt;
293             args.publishers.push(sub.textContent);
294         END;
295         args.publisher = (args.publishers.size) ? args.publishers.0 : '';
296
297         args.pubdates = [];
298         pubdates_hunt = xml.findnodes('//*[@tag="260"]/*[@code="c"]') || 
299             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="c"]');
300         FOR sub IN pubdates_hunt;
301             args.pubdates.push(sub.textContent);
302         END;
303         args.pubdate = (args.pubdates.size) ? args.pubdates.0 : '';
304
305         # Get RDA Copyright Info.
306         args.copyrights = [];
307         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="4"]/*[@code="c"]');
308             args.copyrights.push(sub.textContent);
309         END;
310         args.copyright = (args.copyrights.size) ? args.copyrights.0 : '';
311
312         IF args.copyright.length >= 4;
313             args.copyrightYear = args.copyright.match('(\d{4})');
314             IF args.copyrightYear;
315                 args.copyrightYear = args.copyrightYear.0;
316             END;
317         END;
318
319         # Get the RDA Production info.
320         args.producers = [];
321         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="0"]/*[@code="b"]');
322             args.producers.push(sub.textContent);
323         END;
324         args.producer = (args.producers.size) ? args.producers.0 : '';
325
326         args.prodplaces = [];
327         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="0"]/*[@code="a"]');
328             args.prodplaces.push(sub.textContent);
329         END;
330         args.prodplace = (args.prodplaces.size) ? args.prodplaces.0 : '';
331
332         args.proddates = [];
333         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="0"]/*[@code="c"]');
334             args.proddates.push(sub.textContent);
335         END;
336         args.proddate = (args.proddates.size) ? args.proddates.0 : '';
337
338         # Get the RDA Distribution args.
339         args.distributors = [];
340         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="2"]/*[@code="b"]');
341             args.distributors.push(sub.textContent);
342         END;
343         args.distributor = (args.distributors.size) ? args.distributors.0 : '';
344
345         args.distplaces = [];
346         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="2"]/*[@code="a"]');
347             args.distplaces.push(sub.textContent);
348         END;
349         args.distplace = (args.distplaces.size) ? args.distplaces.0 : '';
350
351         args.distdates = [];
352         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="2"]/*[@code="c"]');
353             args.distdates.push(sub.textContent);
354         END;
355         args.distdate = (args.distdates.size) ? args.distdates.0 : '';
356
357         # Get the RDA Manufacture args.
358         args.manufacturers = [];
359         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="3"]/*[@code="b"]');
360             args.manufacturers.push(sub.textContent);
361         END;
362         args.manufacturer = (args.manufacturers.size) ? args.manufacturers.0 : '';
363
364         args.manplaces = [];
365         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="3"]/*[@code="a"]');
366             args.manplaces.push(sub.textContent);
367         END;
368         args.manplace = (args.manplaces.size) ? args.manplaces.0 : '';
369
370         args.mandates = [];
371         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="3"]/*[@code="c"]');
372             args.mandates.push(sub.textContent);
373         END;
374         args.mandate = (args.mandates.size) ? args.mandates.0 : '';
375
376         # RDA adds 264 to the pubinfo 880 consideration mix
377         graphic_880s = [];
378         get_graphic_880s(target_field='260');
379         get_graphic_880s(target_field='264');
380         args.graphic_pubinfos = graphic_880s;
381         args.pubinfos = [];
382         FOR pubinfo IN args.graphic_pubinfos;
383             args.pubinfos.push(pubinfo.primary.value);
384         END;
385         args.pubinfo = (args.pubinfos.size) ? args.pubinfos.0 : '';
386
387         args.summaries = [];
388         FOR sub IN xml.findnodes('//*[@tag="520"]/*[@code="a"]');
389             args.summaries.push(sub.textContent);
390         END;
391         args.summary = (args.summaries.size) ? args.summaries.0 : '';
392
393         # 250 gets pride of place for edition statement, and is the
394         # only logical choice for 880 graphic fields
395         graphic_880s = [];
396         get_graphic_880s(target_field='250');
397         args.graphic_editions = graphic_880s;
398         args.editions = [];
399         FOR edition IN args.graphic_editions;
400             args.editions.push(edition.primary.value);
401         END;
402
403         ed_hunt = xml.findnodes('//*[@tag="250"]/*[@code="a"]');
404         FOR sub IN ed_hunt;
405             args.editions.push(sub.textContent);
406         END;
407         args.edition = (args.editions.size) ? args.editions.0 : '';
408
409         phys_content = [];
410         FOR sub IN xml.findnodes(
411             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
412         );
413             phys_content.push(sub.textContent);
414         END;
415         args.phys_desc = phys_content.join(" ");
416
417         graphic_880s = [];
418         get_graphic_880s(target_field='505');
419         args.graphic_contents = graphic_880s;
420         FOR content IN args.graphic_contents;
421             args.contents.push(content.primary.value);
422         END;
423         args.content = (args.contents.size) ? args.contents.0 : '';
424
425         # Maintain contents_list in case any custom use was made of it
426         args.contents_list = [];
427         FOR sub IN xml.findnodes('//*[@tag="505"]');
428             args.contents_list.push(sub.textContent);
429         END;
430
431         # MARC Callnumber
432         args.marc_cns = [];
433         FOR sub IN xml.findnodes('//*[@tag="092" or @tag="099"]/*');
434             args.marc_cns.push(sub.textContent);
435         END;
436         args.marc_cn = (args.marc_cns.size ) ? args.marc_cns.0 : '';
437             
438
439         # clean up the ISBN
440         args.isbn_clean = args.isbns.0.replace('\ .*', '');
441         FOR isbn IN args.isbns;
442             clean_isbn = isbn.replace('\ .*', '');
443             clean_isbn = clean_isbn.replace('-', '');
444             IF clean_isbn.length == 13;
445                 args.gtin13 = clean_isbn;
446                 LAST;
447             END;
448         END;
449
450         IF ebook_api.overdrive.enabled == 'true';
451             FOR marc037 IN xml.findnodes('//*[@tag="037"]');
452                 marc037_id = marc037.findnodes('./*[@code="a"]').textContent;
453                 marc037_source = marc037.findnodes('./*[@code="b"]').textContent;
454                 IF marc037_source.match('OverDrive') AND marc037_id;
455                     args.overdrive_id = marc037_id;
456                     LAST;
457                 END;
458             END;
459         END;
460
461         # Extract the 856 URLs that are not otherwise represented by asset.uri's
462         args.online_res = [];
463         FOR node IN xml.findnodes('//*[@tag="856" and @ind1="4" and (@ind2="0" or @ind2="1")]');
464             IF node.findnodes('./*[@code="9" or @code="w" or @code="n"]'); NEXT; END; # asset.uri's
465             label = node.findnodes('./*[@code="y"]');
466             notes = node.findnodes('./*[@code="z" or @code="3"]');
467             FOR href IN node.findnodes('./*[@code="u"]');
468                 NEXT UNLESS href;
469                 # it's possible for multiple $u's to exist within 1 856 tag.
470                 # in that case, honor the label/notes data for the first $u, but
471                 # leave any subsequent $u's as unadorned href's. 
472                 # use href/link/note keys to be consistent with args.uri's
473                 args.online_res.push({
474                     href => href.textContent, 
475                     link => (loop.first AND label) ? label.textContent : href.textContent,
476                     note => (loop.first) ? notes.textContent : ''
477                 });
478             END;
479         END;
480  
481         args.holdings = [];
482         args.uris = [];
483         args.issns = [];
484         args.resolver_isbns = [];
485         args.resolver_issns = [];
486
487         # we use $9 of ISBN and ISSN as a flag for e-version
488         FOR resolver_isbn IN xml.findnodes('//*[@tag="020"]/*[@code="9"]');
489             IF resolver_isbn.textContent == "SFX" || resolver_isbn.textContent == "CUFTS";
490                 my_parent = resolver_isbn.parentNode();
491                 FOR resolver_isbn_val IN my_parent.findnodes('./*[@code="a"]');
492                     args.resolver_isbns.push(
493                         resolver_isbn_val.textContent.replace('-', '').replace('\ .*', '')
494                     );
495                 END;
496             END;
497         END;
498
499         FOR resolver_issn IN xml.findnodes('//*[@tag="022"]/*[@code="9"]');
500             IF resolver_issn.textContent == "SFX" || resolver_issn.textContent == "CUFTS";
501                 my_parent = resolver_issn.parentNode();
502                 FOR resolver_issn_val IN my_parent.findnodes('./*[@code="a"]');
503                     args.resolver_issns.push(
504                         resolver_issn_val.textContent.replace('[^\d\-X]', '')
505                     );
506                 END;
507             END;
508         END;
509
510         # now snag all issns 
511         FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
512             args.issns.push(
513                 rawissn.textContent.replace('[^\d\-X]', '')
514             );
515         END;
516
517         ou_hiding_disabled = ctx.org_hiding_disabled();
518
519         # explicitly grabbing the first bib_source node, though there should be only one anyway
520         bib_source = xml.findnodes('//*[local-name()="bib_source"][1]');
521         args.bib_source.id = bib_source.getAttribute('ident');
522         args.bib_source.source = bib_source.textContent;
523
524         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
525
526             # Check volume visibility - could push this into XPath
527             vol.label = volume.getAttribute('label');
528
529             # Prepend prefix, if any
530             prefix = volume.findnodes('./*[local-name()="call_number_prefix"][@ident!="-1"]');
531             IF prefix.getAttribute('label') != '';
532                 vol.label = prefix.getAttribute('label') _ " " _ vol.label;
533             END;
534
535             # Append prefix, if any
536             suffix = volume.findnodes('./*[local-name()="call_number_suffix"][@ident!="-1"]');
537             IF suffix.getAttribute('label') != '';
538                 vol.label = vol.label _ " " _ suffix.getAttribute('label');
539             END;
540
541             vol.id = volume.getAttribute('id');
542             NEXT IF volume.getAttribute('opac_visible') == 'false';
543             NEXT IF volume.getAttribute('deleted') == 'true';
544
545             IF vol.label == '##URI##';
546                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
547                     res = {};
548                     res.href = uri.getAttribute('href');
549                     res.link = uri.getAttribute('label');
550                     res.note = uri.getAttribute('use_restriction');
551                     # Avoid displaying the href as the link text if we can display the note instead
552                     IF res.link == res.href AND res.note;
553                         res.link = res.note;
554                         res.note = '';
555                     END;
556                     args.uris.push(res);
557
558                     IF ebook_api.ebook_test.enabled == 'true';
559                         IF !args.ebook_test_id;
560                             FOR base_uri IN ebook_api.ebook_test.base_uris;
561                                 IF res.href.match(base_uri);
562                                     args.ebook_test_id = res.href.remove(base_uri);
563                                     LAST;
564                                 END;
565                             END;
566                         END;
567                     END;
568
569                     IF ebook_api.oneclickdigital.enabled == 'true';
570                         # A record might conceivably have multiple OneClickdigital URIs,
571                         # but we use (the same) ISBN as the ebook ID in each case.
572                         IF !args.oneclickdigital_id;
573                             FOR base_uri IN ebook_api.oneclickdigital.base_uris;
574                                 IF res.href.match(base_uri);
575                                     # found a OneClickdigital URI, let's grab our ID and move on
576                                     args.oneclickdigital_id = clean_isbn;
577                                     LAST;
578                                 END;
579                             END;
580                         END;
581                     END;
582
583                     IF ebook_api.overdrive.enabled == 'true';
584                         # Ideally we already have an OverDrive record ID from MARC 037 (see above).
585                         # But for older records, it will be embedded in the URI in MARC 856.
586                         IF !args.overdrive_id;
587                             FOR base_uri IN ebook_api.overdrive.base_uris;
588                                 IF res.href.match(base_uri);
589                                     args.overdrive_id = res.href.remove('^.*/ContentDetails.htm\?ID=');
590                                     LAST;
591                                 END;
592                             END;
593                         END;
594                     END;
595                 END;
596
597                 NEXT;
598             ELSE;
599                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
600                 FOR copy IN copies;
601                     parts = copy.findnodes('./*[local-name()="monograph_parts"]/*[local-name()="monograph_part"]');
602                     part_label = '';
603                     FOREACH part IN parts;
604                         part_label = part.getAttribute('label');
605                         LAST IF part_label != '';
606                     END;
607                     # Check copy visibility
608                     cp.deleted = copy.getAttribute('deleted');    
609                     cp.visible = copy.getAttribute('opac_visible');
610                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
611
612                     # Iterate through all of the children to determine visibility
613                     FOR node IN cp.childNodes;
614                         NEXT IF cp.visible == 'false';
615                         vis = node.getAttribute('opac_visible');
616                         del = node.getAttribute('deleted');
617                         IF vis == 'false' or del == 'true';
618                             cp.visible = 'false';
619                         END;
620                     END;
621
622                     NEXT IF cp.visible == 'false';
623                     
624                     loc = copy.findnodes('./*[local-name()="location"]');
625                     NEXT IF loc.getAttribute('opac_visible') == 'false';
626
627                     circlib = copy.findnodes('./*[local-name()="circlib"]');
628                     NEXT IF circlib.getAttribute('opac_visible') == 'false';
629
630                     status = copy.findnodes('./*[local-name()="status"]');
631                     NEXT IF status.getAttribute('opac_visible') == 'false';
632
633                     # extract the circ_lib id from the circ_lib node
634                     circ_lib = copy.findnodes('./*[local-name()="circ_lib"]');
635                     circ_lib_id = circ_lib.getAttribute('id').replace('.*/', '');
636
637                     UNLESS ou_hiding_disabled;
638                         NEXT UNLESS ctx.org_within_hiding_scope(circ_lib_id);
639                     END;
640
641                     holding = {
642                         circ_lib => circ_lib_id,
643                         label => vol.label,
644                         part_label => part_label,
645                         location => loc.textContent,
646                         library => circlib.textContent,
647                         status => status.textContent,
648                         status_code => status.getAttribute('ident'),
649                         barcode => copy.getAttribute('barcode'),
650                         owner => volume.getAttribute('lib')
651                     };
652                     args.holdings.push(holding);
653                     part_label = '';
654                 END;
655             END;
656         END;
657
658         # Extract the copy count summary
659         count_type = (ctx.is_staff) ? 'staff' : 'public';
660
661         # Consortial copy count summary first
662         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
663         args.copy_counts = {};
664         FOR node IN xml.findnodes(xpath);
665             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
666                 depth = node.getAttribute('depth');
667                 count_org_unit = node.getAttribute('org_unit');
668                 args.copy_counts.$depth.$attr = node.getAttribute(attr);
669                 args.org_copy_counts.$count_org_unit.$attr = node.getAttribute(attr);
670             END;
671         END;
672
673         # Get preferred library copy count
674         args.plib_copy_counts = {};
675         count_type = 'pref_lib';
676         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
677         FOR node IN xml.findnodes(xpath);
678             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
679                 depth = node.getAttribute('depth');
680                 args.plib_copy_counts.$depth.$attr = node.getAttribute(attr);
681             END;
682         END;
683
684         mmr_unique_bib = [];
685         # "mattype" == "custom marc format specifier"
686         icon_style = ctx.get_cgf('opac.icon_attr').value || 'item_type';
687         formats_xpath = '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]';
688
689         args.all_formats = [];
690         FOR node IN xml.findnodes(formats_xpath);
691             IF node AND node.textContent;
692                 ccvm = ctx.get_ccvm(node.getAttribute('cvmid'));
693                 NEXT IF ccvm.opac_visible == 'f';
694
695                 format = {};
696                 this_icon_source = node.getAttribute('source_list');
697                 including = 'F';
698                 # Just display everything if we don't have the data
699                 IF NOT args.mr_constituent_ids OR NOT this_icon_source;
700                     including = 'T';
701                 # We have an array of search-included bib IDs and we have the bib ID that this icon belongs to
702                 ELSE;
703                     FOR mr_constituent_id IN args.mr_constituent_ids;
704                         IF this_icon_source.split(',').grep('^' _ mr_constituent_id _ '$' ).size;
705                             # This bib appears to be in the array of filtered bibs
706                             including = 'T';
707                         END;
708                     END;
709                 END;
710                 IF including == 'T';
711                     type = ccvm.code.remove('-'); # blu-ray to bluray
712                     format.label = ccvm.search_label || ccvm.value;
713                     format.icon = PROCESS get_ccvm_icon ccvm=ccvm;
714                     format.itemtype = schema_typemap.$type || 'CreativeWork';
715                     format.search_format = ccvm.code;
716                     format.source_bibs = this_icon_source.split(',');
717                     FOR bib_source IN format.source_bibs;
718                         IF NOT mmr_unique_bib.grep(bib_source).size; mmr_unique_bib.push(bib_source); END;
719                     END;
720                     args.all_formats.push(format); # metarecords want all formats
721
722                     IF !args.format_label;
723                         # use the first format as the default
724                         args.format_label = format.label;
725                         args.schema.itemtype = format.itemtype;
726                         args.format_icon = format.icon;
727                     END;
728                 END;
729             END;
730         END;
731
732         formats_xpath = '//*[local-name()="attributes"]/*[local-name()="field"][@name="item_lang"]';
733         args.all_lang = [];
734         FOR node IN xml.findnodes(formats_xpath);
735             IF node AND node.textContent;
736                 ccvm = ctx.get_ccvm(node.getAttribute('cvmid'));
737                 NEXT IF ccvm.opac_visible == 'f';
738
739                 lang = {};
740                 this_source = node.getAttribute('source_list');
741                 including = 'F';
742                 # Just display everything if we don't have the data
743                 IF NOT args.mr_constituent_ids OR NOT this_source;
744                     including = 'T';
745                 # We have an array of search-included bib IDs and we have the bib ID that this lang belongs to
746                 ELSE;
747                     FOR mr_constituent_id IN args.mr_constituent_ids;
748                         IF this_source.split(',').grep('^' _ mr_constituent_id _ '$' ).size;
749                             # This bib appears to be in the array of filtered bibs
750                             including = 'T';
751                         END;
752                     END;
753                 END;
754                 IF including == 'T';
755                     lang.label = ccvm.search_label || ccvm.value;
756                     lang.itemtype = schema_typemap.$type || 'CreativeWork';
757                     lang.search_format = node.textContent;
758                     lang.source_bibs = this_source.split(',');
759                     FOR bib_source IN lang.source_bibs;
760                         IF NOT mmr_unique_bib.grep(bib_source).size; mmr_unique_bib.push(bib_source); END;
761                     END;
762
763                     args.all_lang.push(lang); # metarecords want all
764                 END;
765             END;
766         END;
767         args.mmr_unique_bib = mmr_unique_bib;
768
769         IF args.ebook_test_id;
770             args.ebook.ebook_id = args.ebook_test_id;
771             args.ebook.vendor = 'ebook_test';
772         ELSIF args.oneclickdigital_id;
773             args.ebook.ebook_id = args.oneclickdigital_id;
774             args.ebook.vendor = 'oneclickdigital';
775         ELSIF args.overdrive_id;
776             args.ebook.ebook_id = args.overdrive_id;
777             args.ebook.vendor = 'overdrive';
778         END;
779
780     END;
781
782     # Get the library or location group
783     # get_library()
784     # magically upgrades any use of 'loc' to 'locg', 
785     # which is a superset of 'loc'.
786     BLOCK get_library;
787         loc_name = 'locg';
788
789         # Location groups don't have shortnames, so they'll take priority
790         # (_org doesn't contain the group and breaks the selector).
791         loc_value = ctx.copy_location_group ? CGI.param(loc_name)
792                   : ctx.copy_location_group_org # resolved locg
793                     || CGI.param(loc_name) || CGI.param('loc') || ctx.search_ou;
794     END;
795
796 -%]