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