]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/templates/opac/parts/misc_util.tt2
c188e97f5743ca7428d1dce68437ab12376586ff
[Evergreen.git] / Open-ILS / src / templates / opac / parts / misc_util.tt2
1 [% 
2     # Extract MARC fields from XML
3     #   get_marc_attrs( { marc_xml => doc } )
4     BLOCK get_marc_attrs;
5         xml = args.marc_xml;
6
7         # Map item types to schema.org types; impedance mismatch :(
8         args.schema.itemtype = {};
9         schema_typemap = {};
10         schema_typemap.a = 'http://schema.org/Book';
11         schema_typemap.j = 'http://schema.org/MusicRecording';
12
13         args.isbns = [];
14         FOR isbn IN xml.findnodes('//*[@tag="020"]/*[@code="a"]');
15             args.isbns.push(isbn.textContent);
16         END;
17
18         args.upcs = [];
19         FOR upc IN xml.findnodes('//*[@tag="024"]/*[@code="a"]');
20             args.upcs.push(upc.textContent);
21         END;
22         args.upc = args.upcs.0; # use first UPC as the default
23
24         args.issns = [];
25         FOR sub IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
26             args.issns.push(sub.textContent);
27         END;
28         args.issn = (args.issns.size) ? args.issn.0 : '';
29
30         args.authors = [];
31         FOR sub IN xml.findnodes('//*[@tag="100"]/*[@code="a"]');
32             args.authors.push(sub.textContent);
33         END;
34         args.author = (args.authors.size) ? args.authors.0 : '';
35
36         # Include subfields 'abnp' to generate a more comprehensive title display in search results
37         titresults = xml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b" or @code="n" or @code="p"]');
38         titresults_content = [];
39             FOR sub IN titresults; titresults_content.push(sub.textContent); END;
40         args.title = titresults_content.join(" ");
41         # Avoid ugly trailing syntax on brief titles
42         args.title = args.title | replace('[:;/]$', '');
43
44         # Provide correct spacing between the subfields
45         titsubs = xml.findnodes('//*[@tag="245"]/*[@code]');
46         titsubs_content = [];
47             FOR sub IN titsubs; titsubs_content.push(sub.textContent); END;
48         args.title_extended = titsubs_content.join(" ");
49
50         args.pubplaces = [];
51         FOR sub IN xml.findnodes('//*[@tag="260"]/*[@code="a"]');
52             args.pubplaces.push(sub.textContent);
53         END;
54         args.pubplace = (args.pubplaces.size) ? args.pubplaces.0 : '';
55
56         args.publishers = [];
57         FOR sub IN xml.findnodes('//*[@tag="260"]/*[@code="b"]');
58             args.publishers.push(sub.textContent);
59         END;
60         args.publisher = (args.publishers.size) ? args.publishers.0 : '';
61
62         args.pubdates = [];
63         FOR sub IN xml.findnodes('//*[@tag="260"]/*[@code="c"]');
64             args.pubdates.push(sub.textContent);
65         END;
66         args.pubdate = (args.pubdates.size) ? args.pubdates.0 : '';
67
68         # Full publisher info
69         args.pubinfo = "$args.pubplace $args.publisher $args.pubdate";
70
71         args.summaries = [];
72         FOR sub IN xml.findnodes('//*[@tag="520"]/*[@code="a"]');
73             args.summaries.push(sub.textContent);
74         END;
75         args.summary = (args.summaries.size) ? args.summaries.0 : '';
76
77         args.editions = [];
78         ed_hunt = xml.findnodes('//*[@tag="250"]/*[@code="a"]') ||
79             xml.findnodes('//*[@tag="534"]/*[@code="b"]') ||
80             xml.findnodes('//*[@tag="775"]/*[@code="b"]');
81         FOR sub IN ed_hunt;
82             args.editions.push(sub.textContent);
83         END;
84         args.edition = (args.editions.size) ? args.editions.0 : '';
85
86         phys_content = [];
87         FOR sub IN xml.findnodes(
88             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
89         );
90             phys_content.push(sub.textContent);
91         END;
92         args.phys_desc = phys_content.join(" ");
93
94         args.contents_list = [];
95         FOR sub IN xml.findnodes('//*[@tag="505"]');
96             args.contents_list.push(sub.textContent);
97         END;
98         args.contents = args.contents_list.join(" ");
99         args.content = (args.contents.size) ? args.contents.0 : '';
100
101         # MARC Callnumber
102         args.marc_cns = [];
103         FOR sub IN xml.findnodes('//*[@tag="092" or @tag="099"]/*');
104             args.marc_cns.push(sub.textContent);
105         END;
106         args.marc_cn = (args.marc_cns.size ) ? args.marc_cns.0 : '';
107             
108
109         # clean up the ISBN
110         args.isbn_clean = args.isbns.0.replace('\ .*', '');
111
112         # Extract the 856 URLs that are not otherwise represented by asset.uri's
113         args.online_res = [];
114         FOR node IN xml.findnodes('//*[@tag="856" and @ind1="4" and (@ind2="0" or @ind2="1")]');
115             IF node.findnodes('./*[@code="9" or @code="w" or @code="n"]'); NEXT; END; # asset.uri's
116             label = node.findnodes('./*[@code="y"]');
117             notes = node.findnodes('./*[@code="z" or @code="3"]');
118             FOR href IN node.findnodes('./*[@code="u"]');
119                 NEXT UNLESS href;
120                 # it's possible for multiple $u's to exist within 1 856 tag.
121                 # in that case, honor the label/notes data for the first $u, but
122                 # leave any subsequent $u's as unadorned href's. 
123                 # use href/link/note keys to be consistent with args.uri's
124                 args.online_res.push({
125                     href => href.textContent, 
126                     link => (loop.first AND label) ? label.textContent : href.textContent,
127                     note => (loop.first) ? notes.textContent : ''
128                 });
129             END;
130         END;
131  
132         args.holdings = [];
133         args.uris = [];
134         args.issns = [];
135         args.resolver_isbns = [];
136         args.resolver_issns = [];
137
138         # we use $9 of ISBN and ISSN as a flag for e-version
139         FOR resolver_isbn IN xml.findnodes('//*[@tag="020"]/*[@code="9"]');
140             IF resolver_isbn.textContent == "SFX" || resolver_isbn.textContent == "CUFTS";
141                 my_parent = resolver_isbn.parentNode();
142                 FOR resolver_isbn_val IN my_parent.findnodes('./*[@code="a"]');
143                     args.resolver_isbns.push(
144                         resolver_isbn_val.textContent.replace('-', '').replace('\ .*', '')
145                     );
146                 END;
147             END;
148         END;
149
150         FOR resolver_issn IN xml.findnodes('//*[@tag="022"]/*[@code="9"]');
151             IF resolver_issn.textContent == "SFX" || resolver_issn.textContent == "CUFTS";
152                 my_parent = resolver_issn.parentNode();
153                 FOR resolver_issn_val IN my_parent.findnodes('./*[@code="a"]');
154                     args.resolver_issns.push(
155                         resolver_issn_val.textContent.replace('[^\d\-X]', '')
156                     );
157                 END;
158             END;
159         END;
160
161         # now snag all issns 
162         FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
163             args.issns.push(
164                 rawissn.textContent.replace('[^\d\-X]', '')
165             );
166         END;
167
168         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
169
170             # Check volume visibility - could push this into XPath
171             vol.label = volume.getAttribute('label');
172
173             # Prepend prefix, if any
174             prefix = volume.findnodes('./*[local-name()="call_number_prefix"][@ident!="-1"]');
175             IF prefix.getAttribute('label') != '';
176                 vol.label = prefix.getAttribute('label') _ " " _ vol.label;
177             END;
178
179             # Append prefix, if any
180             suffix = volume.findnodes('./*[local-name()="call_number_suffix"][@ident!="-1"]');
181             IF suffix.getAttribute('label') != '';
182                 vol.label = vol.label _ " " _ suffix.getAttribute('label');
183             END;
184
185             vol.id = volume.getAttribute('id');
186             NEXT IF volume.getAttribute('opac_visible') == 'false';
187             NEXT IF volume.getAttribute('deleted') == 'true';
188
189             IF vol.label == '##URI##';
190                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
191                     res = {};
192                     res.href = uri.getAttribute('href');
193                     res.link = uri.getAttribute('label');
194                     res.note = uri.getAttribute('use_restriction');
195                     args.uris.push(res);
196                 END;
197                 NEXT;
198             ELSE;
199                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
200                 FOR copy IN copies;
201                     parts = copy.findnodes('./*[local-name()="monograph_parts"]/*[local-name()="monograph_part"]');
202                     FOREACH part IN parts;
203                         part_label = part.getAttribute('label');
204                         LAST IF part_label != '';
205                     END;
206                     # Check copy visibility
207                     cp.deleted = copy.getAttribute('deleted');    
208                     cp.visible = copy.getAttribute('opac_visible');
209                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
210
211                     # Iterate through all of the children to determine visibility
212                     FOR node IN cp.childNodes;
213                         NEXT IF cp.visible == 'false';
214                         vis = node.getAttribute('opac_visible');
215                         del = node.getAttribute('deleted');
216                         IF vis == 'false' or del == 'true';
217                             cp.visible = 'false';
218                         END;
219                     END;
220
221                     NEXT IF cp.visible == 'false';
222                     
223                     loc = copy.findnodes('./*[local-name()="location"]');
224                     NEXT IF loc.getAttribute('opac_visible') == 'false';
225
226                     circlib = copy.findnodes('./*[local-name()="circlib"]');
227                     NEXT IF circlib.getAttribute('opac_visible') == 'false';
228
229                     status = copy.findnodes('./*[local-name()="status"]');
230                     NEXT IF status.getAttribute('opac_visible') == 'false';
231
232                     holding = {
233                         label => vol.label,
234                         part_label => part_label,
235                         location => loc.textContent,
236                         library => circlib.textContent,
237                         status => status.textContent
238                         barcode => copy.getAttribute('barcode')
239                     };
240                     args.holdings.push(holding);
241                     part_label = '';
242                 END;
243             END;
244         END;
245
246         # Extract the copy count summary
247         count_type = (ctx.is_staff) ? 'staff' : 'public';
248
249         # Consortial copy count summary first
250         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
251         args.copy_counts = {};
252         FOR node IN xml.findnodes(xpath);
253             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
254                 depth = node.getAttribute('depth');
255                 org_unit = node.getAttribute('org_unit');
256                 args.copy_counts.$depth.$attr = node.getAttribute(attr);
257                 args.org_copy_counts.$org_unit.$attr = node.getAttribute(attr);
258             END;
259         END;
260
261         # Get preferred library copy count
262         args.plib_copy_counts = {};
263         count_type = 'pref_lib';
264         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
265         FOR node IN xml.findnodes(xpath);
266             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
267                 depth = node.getAttribute('depth');
268                 args.plib_copy_counts.$depth.$attr = node.getAttribute(attr);
269             END;
270         END;
271
272         # "mattype" == "custom marc format specifier"
273         FOR icon_style IN ['mattype', 'item_type']; 
274             node = xml.findnodes(
275                 '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]');
276             IF node AND node.textContent;
277                 type = node.textContent;
278                 args.format_label = node.getAttribute('coded-value')
279                 args.schema.itemtype = schema_typemap.$type;
280                 args.format_icon = ctx.media_prefix _ '/images/format_icons/' _ icon_style _ '/' _ type _ '.png';
281                 LAST;
282             END;
283         END;
284
285     END;
286
287     # Get the library or location group
288     # get_library()
289     # magically upgrades any use of 'loc' to 'locg', 
290     # which is a superset of 'loc'.
291     BLOCK get_library;
292         loc_name = 'locg';
293         loc_value = CGI.param(loc_name) || CGI.param('loc') || ctx.search_ou;
294     END;
295 %]