]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/templates/opac/parts/misc_util.tt2
LP#1304462: Add schema.org copyrightYear, manufacturer for RDA 264 fields
[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     BLOCK get_ccvm_icon;
83         ctx.media_prefix _ '/images/format_icons/' _ ccvm.ctype _ '/' _ ccvm.code _ '.png';
84     END;
85
86     # Extract MARC fields from XML
87     #   get_marc_attrs( { marc_xml => doc } )
88     BLOCK get_marc_attrs;
89         xml = args.marc_xml;
90
91         # Map item types to schema.org types; impedance mismatch :(
92         args.schema.itemtype = {};
93         schema_typemap = {};
94
95         schema_typemap.bluray = 'Movie'; # Movie could also be TVSeries
96         schema_typemap.book = 'Book';
97         schema_typemap.braille = 'Book';
98         schema_typemap.casaudiobook = 'Book AudioObject';
99         schema_typemap.casmusic = 'MusicAlbum';
100         schema_typemap.cdaudiobook = 'Book AudioObject';
101         schema_typemap.cdmusic = 'MusicAlbum';
102         schema_typemap.dvd = 'Movie';
103         schema_typemap.eaudio = 'AudioObject';
104         schema_typemap.ebook = 'Book';
105         # schema_typemap.equip = '';
106         schema_typemap.evideo = 'Movie';
107         # schema_typemap.kit = '';
108         schema_typemap.lpbook = 'Book';
109         schema_typemap.map = 'Map';
110         # schema_typemap.microform = '';
111         schema_typemap.music = 'MusicAlbum';
112         schema_typemap.phonomusic = 'MusicAlbum';
113         # schema_typemap.phonospoken = '';
114         # schema_typemap.picture = ''; Painting or Photograph?
115         schema_typemap.score = 'Book'; # schema.org has no generic Music type
116         schema_typemap.serial = 'Periodical';
117         schema_typemap.software = 'SoftwareApplication';
118         schema_typemap.vhs = 'Movie';
119
120         schema_typemap.a = 'Book';
121         schema_typemap.e = 'Map';
122         schema_typemap.j = 'MusicAlbum';
123
124         # Hard-coded to match defaults in config.copy_status for all OPAC-visible statuses
125         schema_copy_status = {};
126         schema_copy_status.0 = '<link property="availability" href="http://schema.org/InStock" />'; # Available
127         schema_copy_status.1 = '<link property="availability" href="http://schema.org/OutOfStock" />'; # Checked out
128         schema_copy_status.5 = '<link property="availability" href="http://schema.org/PreOrder" />'; # In process
129         schema_copy_status.6 = '<link property="availability" href="http://schema.org/PreOrder" />'; # In transit
130         schema_copy_status.7 = '<link property="availability" href="http://schema.org/InStock" />'; # Reshelving
131         schema_copy_status.8 = '<link property="availability" href="http://schema.org/OutOfStock" />'; # On holds shelf
132         schema_copy_status.9 = '<link property="availability" href="http://schema.org/PreOrder" />'; # On order
133         schema_copy_status.12 = '<link property="availability" href="http://schema.org/InStoreOnly" />'; # Reserves
134
135         args.isbns = [];
136         FOR isbn IN xml.findnodes('//*[@tag="020"]/*[@code="a"]');
137             args.isbns.push(isbn.textContent);
138         END;
139
140         args.upcs = [];
141         FOR upc IN xml.findnodes('//*[@tag="024"]/*[@code="a"]');
142             args.upcs.push(upc.textContent);
143         END;
144         args.upc = args.upcs.0; # use first UPC as the default
145
146         args.issns = [];
147         FOR sub IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
148             args.issns.push(sub.textContent);
149         END;
150         args.issn = (args.issns.size) ? args.issn.0 : '';
151
152         graphic_880s = [];
153         get_graphic_880s(target_field='100');
154         get_graphic_880s(target_field='110');
155         get_graphic_880s(target_field='111');
156         args.graphic_authors = graphic_880s;
157         args.authors = [];
158         FOR author IN args.graphic_authors;
159             args.authors.push(author.primary.value);
160         END;
161         args.author = (args.authors.size) ? args.authors.0 : '';
162
163         # Include subfields 'abnp' to generate a more comprehensive title display in search results
164         titresults = xml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b" or @code="n" or @code="p"]');
165         titresults_content = [];
166             FOR sub IN titresults; titresults_content.push(sub.textContent); END;
167         args.title = titresults_content.join(" ");
168         # Avoid ugly trailing syntax on brief titles
169         args.title = args.title | replace('[:;/]$', '');
170
171         graphic_880s = [];
172         get_graphic_880s(target_field='245');
173         args.graphic_titles = graphic_880s;
174         args.titles = [];
175         FOR title IN args.graphic_titles;
176             args.titles.push(title.primary.value);
177         END;
178         args.title_extended = (args.titles.size) ? args.titles.0 : '';
179
180         args.pubplaces = [];
181         pubplace_hunt = xml.findnodes('//*[@tag="260"]/*[@code="a"]') ||
182             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="a"]');
183         FOR sub IN pubplace_hunt;
184             args.pubplaces.push(sub.textContent);
185         END;
186         args.pubplace = (args.pubplaces.size) ? args.pubplaces.0 : '';
187
188         args.publishers = [];
189         publishers_hunt = xml.findnodes('//*[@tag="260"]/*[@code="b"]') ||
190             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="b"]');
191         FOR sub IN publishers_hunt;
192             args.publishers.push(sub.textContent);
193         END;
194         args.publisher = (args.publishers.size) ? args.publishers.0 : '';
195
196         args.pubdates = [];
197         pubdates_hunt = xml.findnodes('//*[@tag="260"]/*[@code="c"]') || 
198             xml.findnodes('//*[@tag="264" and @ind2="1"]/*[@code="c"]');
199         FOR sub IN pubdates_hunt;
200             args.pubdates.push(sub.textContent);
201         END;
202         args.pubdate = (args.pubdates.size) ? args.pubdates.0 : '';
203
204         # Full publisher info
205         args.pubinfo = "$args.pubplace $args.publisher $args.pubdate";
206
207         # Get RDA Copyright Info.
208         args.copyrights = [];
209         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="4"]/*[@code="c"]');
210             args.copyrights.push(sub.textContent);
211         END;
212         args.copyright = (args.copyrights.size) ? args.copyrights.0 : '';
213
214         IF args.copyright.length >= 4;
215             args.copyrightYear = args.copyright.match('(\d{4})');
216             IF args.copyrightYear;
217                 args.copyrightYear = args.copyrightYear.0;
218             END;
219         END;
220
221         # Get the RDA Production info.
222         args.producers = [];
223         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="0"]/*[@code="b"]');
224             args.producers.push(sub.textContent);
225         END;
226         args.producer = (args.producers.size) ? args.producers.0 : '';
227
228         args.prodplaces = [];
229         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="0"]/*[@code="a"]');
230             args.prodplaces.push(sub.textContent);
231         END;
232         args.prodplace = (args.prodplaces.size) ? args.prodplaces.0 : '';
233
234         args.proddates = [];
235         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="0"]/*[@code="c"]');
236             args.proddates.push(sub.textContent);
237         END;
238         args.proddate = (args.proddates.size) ? args.proddates.0 : '';
239
240         # Get the RDA Distribution args.
241         args.distributors = [];
242         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="2"]/*[@code="b"]');
243             args.distributors.push(sub.textContent);
244         END;
245         args.distributor = (args.distributors.size) ? args.distributors.0 : '';
246
247         args.distplaces = [];
248         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="2"]/*[@code="a"]');
249             args.distplaces.push(sub.textContent);
250         END;
251         args.distplace = (args.distplaces.size) ? args.distplaces.0 : '';
252
253         args.distdates = [];
254         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="2"]/*[@code="c"]');
255             args.distdates.push(sub.textContent);
256         END;
257         args.distdate = (args.distdates.size) ? args.distdates.0 : '';
258
259         # Get the RDA Manufacture args.
260         args.manufacturers = [];
261         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="3"]/*[@code="b"]');
262             args.manufacturers.push(sub.textContent);
263         END;
264         args.manufacturer = (args.manufacturers.size) ? args.manufacturers.0 : '';
265
266         args.manplaces = [];
267         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="3"]/*[@code="a"]');
268             args.manplaces.push(sub.textContent);
269         END;
270         args.manplace = (args.manplaces.size) ? args.manplaces.0 : '';
271
272         args.mandates = [];
273         FOR sub IN xml.findnodes('//*[@tag="264" and @ind2="3"]/*[@code="c"]');
274             args.mandates.push(sub.textContent);
275         END;
276         args.mandate = (args.mandates.size) ? args.mandates.0 : '';
277
278         # RDA adds 264 to the pubinfo 880 consideration mix
279         graphic_880s = [];
280         get_graphic_880s(target_field='260');
281         get_graphic_880s(target_field='264');
282         args.graphic_pubinfos = graphic_880s;
283         args.pubinfos = [];
284         FOR pubinfo IN args.graphic_pubinfos;
285             args.pubinfos.push(pubinfo.primary.value);
286         END;
287         args.pubinfo = (args.pubinfos.size) ? args.pubinfos.0 : '';
288
289         args.summaries = [];
290         FOR sub IN xml.findnodes('//*[@tag="520"]/*[@code="a"]');
291             args.summaries.push(sub.textContent);
292         END;
293         args.summary = (args.summaries.size) ? args.summaries.0 : '';
294
295         # 250 gets pride of place for edition statement, and is the
296         # only logical choice for 880 graphic fields
297         graphic_880s = [];
298         get_graphic_880s(target_field='250');
299         args.graphic_editions = graphic_880s;
300         args.editions = [];
301         FOR edition IN args.graphic_editions;
302             args.editions.push(edition.primary.value);
303         END;
304
305         ed_hunt = xml.findnodes('//*[@tag="250"]/*[@code="a"]') ||
306             xml.findnodes('//*[@tag="534"]/*[@code="b"]') ||
307             xml.findnodes('//*[@tag="775"]/*[@code="b"]');
308         FOR sub IN ed_hunt;
309             args.editions.push(sub.textContent);
310         END;
311         args.edition = (args.editions.size) ? args.editions.0 : '';
312
313         phys_content = [];
314         FOR sub IN xml.findnodes(
315             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
316         );
317             phys_content.push(sub.textContent);
318         END;
319         args.phys_desc = phys_content.join(" ");
320
321         graphic_880s = [];
322         get_graphic_880s(target_field='505');
323         args.graphic_contents = graphic_880s;
324         FOR content IN args.graphic_contents;
325             args.contents.push(content.primary.value);
326         END;
327         args.content = (args.contents.size) ? args.contents.0 : '';
328
329         # Maintain contents_list in case any custom use was made of it
330         args.contents_list = [];
331         FOR sub IN xml.findnodes('//*[@tag="505"]');
332             args.contents_list.push(sub.textContent);
333         END;
334
335         # MARC Callnumber
336         args.marc_cns = [];
337         FOR sub IN xml.findnodes('//*[@tag="092" or @tag="099"]/*');
338             args.marc_cns.push(sub.textContent);
339         END;
340         args.marc_cn = (args.marc_cns.size ) ? args.marc_cns.0 : '';
341             
342
343         # clean up the ISBN
344         args.isbn_clean = args.isbns.0.replace('\ .*', '');
345         FOR isbn IN args.isbns;
346             clean_isbn = isbn.replace('\ .*', '');
347             clean_isbn = clean_isbn.replace('-', '');
348             IF clean_isbn.length == 13;
349                 args.gtin13 = clean_isbn;
350                 LAST;
351             END;
352         END;
353
354         # Extract the 856 URLs that are not otherwise represented by asset.uri's
355         args.online_res = [];
356         FOR node IN xml.findnodes('//*[@tag="856" and @ind1="4" and (@ind2="0" or @ind2="1")]');
357             IF node.findnodes('./*[@code="9" or @code="w" or @code="n"]'); NEXT; END; # asset.uri's
358             label = node.findnodes('./*[@code="y"]');
359             notes = node.findnodes('./*[@code="z" or @code="3"]');
360             FOR href IN node.findnodes('./*[@code="u"]');
361                 NEXT UNLESS href;
362                 # it's possible for multiple $u's to exist within 1 856 tag.
363                 # in that case, honor the label/notes data for the first $u, but
364                 # leave any subsequent $u's as unadorned href's. 
365                 # use href/link/note keys to be consistent with args.uri's
366                 args.online_res.push({
367                     href => href.textContent, 
368                     link => (loop.first AND label) ? label.textContent : href.textContent,
369                     note => (loop.first) ? notes.textContent : ''
370                 });
371             END;
372         END;
373  
374         args.holdings = [];
375         args.uris = [];
376         args.issns = [];
377         args.resolver_isbns = [];
378         args.resolver_issns = [];
379
380         # we use $9 of ISBN and ISSN as a flag for e-version
381         FOR resolver_isbn IN xml.findnodes('//*[@tag="020"]/*[@code="9"]');
382             IF resolver_isbn.textContent == "SFX" || resolver_isbn.textContent == "CUFTS";
383                 my_parent = resolver_isbn.parentNode();
384                 FOR resolver_isbn_val IN my_parent.findnodes('./*[@code="a"]');
385                     args.resolver_isbns.push(
386                         resolver_isbn_val.textContent.replace('-', '').replace('\ .*', '')
387                     );
388                 END;
389             END;
390         END;
391
392         FOR resolver_issn IN xml.findnodes('//*[@tag="022"]/*[@code="9"]');
393             IF resolver_issn.textContent == "SFX" || resolver_issn.textContent == "CUFTS";
394                 my_parent = resolver_issn.parentNode();
395                 FOR resolver_issn_val IN my_parent.findnodes('./*[@code="a"]');
396                     args.resolver_issns.push(
397                         resolver_issn_val.textContent.replace('[^\d\-X]', '')
398                     );
399                 END;
400             END;
401         END;
402
403         # now snag all issns 
404         FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
405             args.issns.push(
406                 rawissn.textContent.replace('[^\d\-X]', '')
407             );
408         END;
409
410         ou_hiding_disabled = ctx.org_hiding_disabled();
411
412         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
413
414             # Check volume visibility - could push this into XPath
415             vol.label = volume.getAttribute('label');
416
417             # Prepend prefix, if any
418             prefix = volume.findnodes('./*[local-name()="call_number_prefix"][@ident!="-1"]');
419             IF prefix.getAttribute('label') != '';
420                 vol.label = prefix.getAttribute('label') _ " " _ vol.label;
421             END;
422
423             # Append prefix, if any
424             suffix = volume.findnodes('./*[local-name()="call_number_suffix"][@ident!="-1"]');
425             IF suffix.getAttribute('label') != '';
426                 vol.label = vol.label _ " " _ suffix.getAttribute('label');
427             END;
428
429             vol.id = volume.getAttribute('id');
430             NEXT IF volume.getAttribute('opac_visible') == 'false';
431             NEXT IF volume.getAttribute('deleted') == 'true';
432
433             IF vol.label == '##URI##';
434                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
435                     res = {};
436                     res.href = uri.getAttribute('href');
437                     res.link = uri.getAttribute('label');
438                     res.note = uri.getAttribute('use_restriction');
439                     args.uris.push(res);
440                 END;
441                 NEXT;
442             ELSE;
443                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
444                 FOR copy IN copies;
445                     parts = copy.findnodes('./*[local-name()="monograph_parts"]/*[local-name()="monograph_part"]');
446                     FOREACH part IN parts;
447                         part_label = part.getAttribute('label');
448                         LAST IF part_label != '';
449                     END;
450                     # Check copy visibility
451                     cp.deleted = copy.getAttribute('deleted');    
452                     cp.visible = copy.getAttribute('opac_visible');
453                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
454
455                     # Iterate through all of the children to determine visibility
456                     FOR node IN cp.childNodes;
457                         NEXT IF cp.visible == 'false';
458                         vis = node.getAttribute('opac_visible');
459                         del = node.getAttribute('deleted');
460                         IF vis == 'false' or del == 'true';
461                             cp.visible = 'false';
462                         END;
463                     END;
464
465                     NEXT IF cp.visible == 'false';
466                     
467                     loc = copy.findnodes('./*[local-name()="location"]');
468                     NEXT IF loc.getAttribute('opac_visible') == 'false';
469
470                     circlib = copy.findnodes('./*[local-name()="circlib"]');
471                     NEXT IF circlib.getAttribute('opac_visible') == 'false';
472
473                     status = copy.findnodes('./*[local-name()="status"]');
474                     NEXT IF status.getAttribute('opac_visible') == 'false';
475
476                     # extract the circ_lib id from the circ_lib node
477                     circ_lib = copy.findnodes('./*[local-name()="circ_lib"]');
478                     circ_lib_id = circ_lib.getAttribute('id').replace('.*/', '');
479
480                     UNLESS ou_hiding_disabled;
481                         NEXT UNLESS ctx.org_within_hiding_scope(circ_lib_id);
482                     END;
483
484                     holding = {
485                         circ_lib => circ_lib_id,
486                         label => vol.label,
487                         part_label => part_label,
488                         location => loc.textContent,
489                         library => circlib.textContent,
490                         status => status.textContent,
491                         status_code => status.getAttribute('ident'),
492                         barcode => copy.getAttribute('barcode'),
493                         owner => volume.getAttribute('lib')
494                     };
495                     args.holdings.push(holding);
496                     part_label = '';
497                 END;
498             END;
499         END;
500
501         # Extract the copy count summary
502         count_type = (ctx.is_staff) ? 'staff' : 'public';
503
504         # Consortial copy count summary first
505         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
506         args.copy_counts = {};
507         FOR node IN xml.findnodes(xpath);
508             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
509                 depth = node.getAttribute('depth');
510                 count_org_unit = node.getAttribute('org_unit');
511                 args.copy_counts.$depth.$attr = node.getAttribute(attr);
512                 args.org_copy_counts.$count_org_unit.$attr = node.getAttribute(attr);
513             END;
514         END;
515
516         # Get preferred library copy count
517         args.plib_copy_counts = {};
518         count_type = 'pref_lib';
519         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
520         FOR node IN xml.findnodes(xpath);
521             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
522                 depth = node.getAttribute('depth');
523                 args.plib_copy_counts.$depth.$attr = node.getAttribute(attr);
524             END;
525         END;
526
527         # "mattype" == "custom marc format specifier"
528         icon_style = ctx.get_cgf('opac.icon_attr').value || 'item_type';
529         formats_xpath = '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]';
530
531         args.all_formats = [];
532         FOR node IN xml.findnodes(formats_xpath);
533             IF node AND node.textContent;
534                 ccvm = ctx.get_ccvm(node.getAttribute('cvmid'));
535                 NEXT IF ccvm.opac_visible == 'f';
536
537                 format = {};
538                 type = ccvm.code.remove('-'); # blu-ray to bluray
539                 format.label = ccvm.search_label || ccvm.value;
540                 format.icon = PROCESS get_ccvm_icon ccvm=ccvm;
541                 format.itemtype = schema_typemap.$type || 'CreativeWork';
542
543                 args.all_formats.push(format); # metarecords want all formats
544
545                 IF !args.format_label;
546                     # use the first format as the default
547                     args.format_label = format.label; 
548                     args.schema.itemtype = format.itemtype;
549                     args.format_icon = format.icon;
550                 END;
551             END;
552         END;
553         
554         args.bibid = [];
555         FOR bibid IN xml.findnodes('//*[@tag="901"]/*[@code="c"]');
556             args.bibid.push(bibid.textContent);
557         END;
558         args.bibid = args.bibid.0; 
559
560     END;
561
562     # Get the library or location group
563     # get_library()
564     # magically upgrades any use of 'loc' to 'locg', 
565     # which is a superset of 'loc'.
566     BLOCK get_library;
567         loc_name = 'locg';
568         loc_value = ctx.copy_location_group_org ||  # resolved locg
569             CGI.param(loc_name) || CGI.param('loc') || ctx.search_ou;
570     END;
571
572 %]