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