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