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