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