]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/templates/opac/parts/misc_util.tt2
fe4d97fe023596b2cb14392ca6068b2f74cf16d2
[working/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.publishers = [];
51         FOR sub IN xml.findnodes('//*[@tag="260"]/*[@code="b"]');
52             args.publishers.push(sub.textContent);
53         END;
54         args.publisher = (args.publishers.size) ? args.publishers.0 : '';
55
56         args.pubdates = [];
57         FOR sub IN xml.findnodes('//*[@tag="260"]/*[@code="c"]');
58             args.pubdates.push(sub.textContent);
59         END;
60         args.pubdate = (args.pubdates.size) ? args.pubdates.0 : '';
61
62         args.summaries = [];
63         FOR sub IN xml.findnodes('//*[@tag="520"]/*[@code="a"]');
64             args.summaries.push(sub.textContent);
65         END;
66         args.summary = (args.summary.size) ? args.summary.0 : '';
67
68         args.editions = [];
69         ed_hunt = xml.findnodes('//*[@tag="250"]/*[@code="a"]') &&
70             xml.findnodes('//*[@tag="534"]/*[@code="b"]') &&
71             xml.findnodes('//*[@tag="775"]/*[@code="b"]');
72         FOR sub IN ed_hunt;
73             args.editions.push(sub.textContent);
74         END;
75         args.edition = (args.editions.size) ? args.editions.0 : '';
76
77         phys_content = [];
78         FOR sub IN xml.findnodes(
79             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
80         );
81             phys_content.push(sub.textContent);
82         END;
83         args.phys_desc = phys_content.join(" ");
84
85         args.contents_list = [];
86         FOR sub IN xml.findnodes('//*[@tag="505"]');
87             args.contents_list.push(sub.textContent);
88         END;
89         args.contents = args.contents_list.join(" ");
90         args.content = (args.contents.size) ? args.contents.0 : '';
91
92         # MARC Callnumber
93         args.marc_cns = [];
94         FOR sub IN xml.findnodes('//*[@tag="092" or @tag="099"]/*');
95             args.marc_cns.push(sub.textContent);
96         END;
97         args.marc_cn = (args.marc_cns.size ) ? args.marc_cns.0 : '';
98             
99
100         # clean up the ISBN
101         args.isbn_clean = args.isbns.0.replace('\ .*', '');
102
103         # Extract the 856 URLs that are not otherwise represented by asset.uri's
104         args.online_res = [];
105         FOR node IN xml.findnodes('//*[@tag="856" and @ind1="4" and (@ind2="0" or @ind2="1")]');
106             IF node.findnodes('./*[@code="9" or @code="w" or @code="n"]'); NEXT; END; # asset.uri's
107             label = node.findnodes('./*[@code="y"]');
108             notes = node.findnodes('./*[@code="z" or @code="3"]');
109             FOR href IN node.findnodes('./*[@code="u"]');
110                 NEXT UNLESS href;
111                 # it's possible for multiple $u's to exist within 1 856 tag.
112                 # in that case, honor the label/notes data for the first $u, but
113                 # leave any subsequent $u's as unadorned href's. 
114                 # use href/link/note keys to be consistent with args.uri's
115                 args.online_res.push({
116                     href => href.textContent, 
117                     link => (loop.first AND label) ? label.textContent : href.textContent,
118                     note => (loop.first) ? notes.textContent : ''
119                 });
120             END;
121         END;
122  
123         args.holdings = [];
124         args.uris = [];
125         args.issns = [];
126         args.resolver_isbns = [];
127         args.resolver_issns = [];
128
129         # we use $9 of ISBN and ISSN as a flag for e-version
130         FOR resolver_isbn IN xml.findnodes('//*[@tag="020"]/*[@code="9"]');
131             IF resolver_isbn.textContent == "SFX" || resolver_isbn.textContent == "CUFTS";
132                 my_parent = resolver_isbn.parentNode();
133                 FOR resolver_isbn_val IN my_parent.findnodes('./*[@code="a"]');
134                     args.resolver_isbns.push(
135                         resolver_isbn_val.textContent.replace('-', '').replace('\ .*', '')
136                     );
137                 END;
138             END;
139         END;
140
141         FOR resolver_issn IN xml.findnodes('//*[@tag="022"]/*[@code="9"]');
142             IF resolver_issn.textContent == "SFX" || resolver_issn.textContent == "CUFTS";
143                 my_parent = resolver_issn.parentNode();
144                 FOR resolver_issn_val IN my_parent.findnodes('./*[@code="a"]');
145                     args.resolver_issns.push(
146                         resolver_issn_val.textContent.replace('[^\d\-X]', '')
147                     );
148                 END;
149             END;
150         END;
151
152         # now snag all issns 
153         FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
154             args.issns.push(
155                 rawissn.textContent.replace('[^\d\-X]', '')
156             );
157         END;
158
159         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
160
161             # Check volume visibility - could push this into XPath
162             vol.label = volume.getAttribute('label');
163
164             # Prepend prefix, if any
165             prefix = volume.findnodes('./*[local-name()="call_number_prefix"][@ident!="-1"]');
166             IF prefix.getAttribute('label') != '';
167                 vol.label = prefix.getAttribute('label') _ " " _ vol.label;
168             END;
169
170             # Append prefix, if any
171             suffix = volume.findnodes('./*[local-name()="call_number_suffix"][@ident!="-1"]');
172             IF suffix.getAttribute('label') != '';
173                 vol.label = vol.label _ " " _ suffix.getAttribute('label');
174             END;
175
176             vol.id = volume.getAttribute('id');
177             NEXT IF volume.getAttribute('opac_visible') == 'false';
178             NEXT IF volume.getAttribute('deleted') == 'true';
179
180             IF vol.label == '##URI##';
181                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
182                     res = {};
183                     res.href = uri.getAttribute('href');
184                     res.link = uri.getAttribute('label');
185                     res.note = uri.getAttribute('use_restriction');
186                     args.uris.push(res);
187                 END;
188                 NEXT;
189             ELSE;
190                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
191                 FOR copy IN copies;
192                     parts = copy.findnodes('./*[local-name()="monograph_parts"]/*[local-name()="monograph_part"]');
193                     FOREACH part IN parts;
194                         part_label = part.getAttribute('label');
195                         LAST IF part_label != '';
196                     END;
197                     # Check copy visibility
198                     cp.deleted = copy.getAttribute('deleted');    
199                     cp.visible = copy.getAttribute('opac_visible');
200                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
201
202                     # Iterate through all of the children to determine visibility
203                     FOR node IN cp.childNodes;
204                         NEXT IF cp.visible == 'false';
205                         vis = node.getAttribute('opac_visible');
206                         del = node.getAttribute('deleted');
207                         IF vis == 'false' or del == 'true';
208                             cp.visible = 'false';
209                         END;
210                     END;
211
212                     NEXT IF cp.visible == 'false';
213                     
214                     loc = copy.findnodes('./*[local-name()="location"]');
215                     circlib = copy.findnodes('./*[local-name()="circlib"]');
216                     status = copy.findnodes('./*[local-name()="status"]');
217
218                     holding = {
219                         label => vol.label,
220                         part_label => part_label,
221                         location => loc.textContent,
222                         library => circlib.textContent,
223                         status => status.textContent
224                         barcode => copy.getAttribute('barcode')
225                     };
226                     args.holdings.push(holding);
227                     part_label = '';
228                 END;
229             END;
230         END;
231
232         # Extract the copy count summary
233         count_type = (ctx.is_staff) ? 'staff' : 'public';
234
235         # Consortial copy count summary first
236         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
237         args.copy_counts = {};
238         FOR node IN xml.findnodes(xpath);
239             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
240                 depth = node.getAttribute('depth');
241                 args.copy_counts.$depth.$attr = node.getAttribute(attr);
242             END;
243         END;
244         
245         # Get preferred library copy count
246         args.plib_copy_counts = {};
247         count_type = 'pref_lib';
248         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
249         FOR node IN xml.findnodes(xpath);
250             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
251                 depth = node.getAttribute('depth');
252                 args.plib_copy_counts.$depth.$attr = node.getAttribute(attr);
253             END;
254         END;
255
256         # "mattype" == "custom marc format specifier"
257         FOR icon_style IN ['mattype', 'item_type']; 
258             node = xml.findnodes(
259                 '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]');
260             IF node AND node.textContent;
261                 type = node.textContent;
262                 args.format_label = node.getAttribute('coded-value')
263                 args.schema.itemtype = schema_typemap.$type;
264                 args.format_icon = ctx.media_prefix _ '/images/format_icons/' _ icon_style _ '/' _ type _ '.png';
265                 LAST;
266             END;
267         END;
268
269     END;
270 %]