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