]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/templates/opac/parts/misc_util.tt2
Tpac - add additional subfields for title in search results
[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         args.isbns = [];
7         FOR isbn IN xml.findnodes('//*[@tag="020"]/*[@code="a"]');
8             args.isbns.push(isbn.textContent);
9         END;
10         args.upcs = [];
11         FOR upc IN xml.findnodes('//*[@tag="024"]/*[@code="a"]');
12             args.upcs.push(upc.textContent);
13         END;
14         args.upc = args.upcs.0; # use first UPC as the default
15         args.issn = xml.findnodes('//*[@tag="022"]/*[@code="a"]').textContent;
16         args.author = xml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent;
17
18         # Include subfields 'abnp' to generate a more comprehensive title display in search results
19         titresults = xml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b" or @code="n" or @code="p"]');
20         titresults_content = [];
21             FOR sub IN titresults; titresults_content.push(sub.textContent); END;
22         args.title = titresults_content.join(" ");
23         # Avoid ugly trailing syntax on brief titles
24         args.title = args.title | replace('[:;/]$', '');
25
26         # Provide correct spacing between the subfields
27         titsubs = xml.findnodes('//*[@tag="245"]/*[@code]');
28         titsubs_content = [];
29             FOR sub IN titsubs; titsubs_content.push(sub.textContent); END;
30         args.title_extended = titsubs_content.join(" ");
31
32         args.publisher = xml.findnodes('//*[@tag="260"]/*[@code="b"]').textContent;
33         args.pubdate = xml.findnodes('//*[@tag="260"]/*[@code="c"]').textContent;
34         args.summary = xml.findnodes('//*[@tag="520"]/*[@code="a"]').textContent;
35         args.edition = xml.findnodes('//*[@tag="250"]/*[@code="a"]').textContent ||
36             xml.findnodes('//*[@tag="534"]/*[@code="b"]').textContent ||
37             xml.findnodes('//*[@tag="775"]/*[@code="b"]').textContent;
38         phys = xml.findnodes(
39             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
40         );
41         phys_content = [];
42         FOR p IN phys; phys_content.push(p.textContent); END;
43         args.phys_desc = phys_content.join("");
44
45         args.contents = xml.findnodes('//*[@tag="505"]').textContent;
46
47         # MARC Callnumber
48         args.marc_cn = xml.findnodes('//*[@tag="092" or @tag="099"]/*').textContent;
49
50         # clean up the ISBN
51         args.isbn_clean = args.isbns.0.replace('\ .*', '');
52
53         # Extract the 856 URLs that are not otherwise represented by asset.uri's
54         args.online_res = [];
55         FOR node IN xml.findnodes('//*[@tag="856" and @ind1="4" and (@ind2="0" or @ind2="1")]');
56             IF node.findnodes('./*[@code="9" or @code="w" or @code="n"]'); NEXT; END; # asset.uri's
57             label = node.findnodes('./*[@code="y"]');
58             notes = node.findnodes('./*[@code="z" or @code="3"]');
59             FOR href IN node.findnodes('./*[@code="u"]');
60                 NEXT UNLESS href;
61                 # it's possible for multiple $u's to exist within 1 856 tag.
62                 # in that case, honor the label/notes data for the first $u, but
63                 # leave any subsequent $u's as unadorned href's. 
64                 # use href/link/note keys to be consistent with args.uri's
65                 args.online_res.push({
66                     href => href.textContent, 
67                     link => (loop.first AND label) ? label.textContent : href.textContent,
68                     note => (loop.first) ? notes.textContent : ''
69                 });
70             END;
71         END;
72  
73         args.holdings = [];
74         args.uris = [];
75         args.issns = [];
76         args.resolver_isbns = [];
77         args.resolver_issns = [];
78
79         # we use $9 of ISBN and ISSN as a flag for e-version
80         FOR resolver_isbn IN xml.findnodes('//*[@tag="020"]/*[@code="9"]');
81             IF resolver_isbn.textContent == "SFX" || resolver_isbn.textContent == "CUFTS";
82                 my_parent = resolver_isbn.parentNode();
83                 FOR resolver_isbn_val IN my_parent.findnodes('./*[@code="a"]');
84                     args.resolver_isbns.push(
85                         resolver_isbn_val.textContent.replace('-', '').replace('\ .*', '')
86                     );
87                 END;
88             END;
89         END;
90
91         FOR resolver_issn IN xml.findnodes('//*[@tag="022"]/*[@code="9"]');
92             IF resolver_issn.textContent == "SFX" || resolver_issn.textContent == "CUFTS";
93                 my_parent = resolver_issn.parentNode();
94                 FOR resolver_issn_val IN my_parent.findnodes('./*[@code="a"]');
95                     args.resolver_issns.push(
96                         resolver_issn_val.textContent.replace('[^\d\-X]', '')
97                     );
98                 END;
99             END;
100         END;
101
102         # now snag all issns 
103         FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
104             args.issns.push(
105                 rawissn.textContent.replace('[^\d\-X]', '')
106             );
107         END;
108
109         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
110
111             # Check volume visibility - could push this into XPath
112             vol.label = volume.getAttribute('label');
113
114             # Prepend prefix, if any
115             prefix = volume.findnodes('./*[local-name()="call_number_prefix"][@ident!="-1"]');
116             IF prefix.getAttribute('label') != '';
117                 vol.label = prefix.getAttribute('label') _ " " _ vol.label;
118             END;
119
120             # Append prefix, if any
121             suffix = volume.findnodes('./*[local-name()="call_number_suffix"][@ident!="-1"]');
122             IF suffix.getAttribute('label') != '';
123                 vol.label = vol.label _ " " _ suffix.getAttribute('label');
124             END;
125
126             vol.id = volume.getAttribute('id');
127             NEXT IF volume.getAttribute('opac_visible') == 'false';
128             NEXT IF volume.getAttribute('deleted') == 'true';
129
130             IF vol.label == '##URI##';
131                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
132                     res.href = uri.getAttribute('href');
133                     res.link = uri.getAttribute('label');
134                     res.note = uri.getAttribute('use_restriction');
135                     args.uris.push(res);
136                 END;
137                 NEXT;
138             ELSE;
139                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
140                 FOR copy IN copies;
141                     parts = copy.findnodes('./*[local-name()="monograph_parts"]/*[local-name()="monograph_part"]');
142                     FOREACH part IN parts;
143                         part_label = part.getAttribute('label');
144                         LAST IF part_label != '';
145                     END;
146                     # Check copy visibility
147                     cp.deleted = copy.getAttribute('deleted');    
148                     cp.visible = copy.getAttribute('opac_visible');
149                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
150
151                     # Iterate through all of the children to determine visibility
152                     FOR node IN cp.childNodes;
153                         NEXT IF cp.visible == 'false';
154                         vis = node.getAttribute('opac_visible');
155                         del = node.getAttribute('deleted');
156                         IF vis == 'false' or del == 'true';
157                             cp.visible = 'false';
158                         END;
159                     END;
160
161                     NEXT IF cp.visible == 'false';
162                     
163                     loc = copy.findnodes('./*[local-name()="location"]');
164                     circlib = copy.findnodes('./*[local-name()="circlib"]');
165                     status = copy.findnodes('./*[local-name()="status"]');
166
167                     holding = {
168                         label => vol.label,
169                         part_label => part_label,
170                         location => loc.textContent,
171                         library => circlib.textContent,
172                         status => status.textContent
173                         barcode => copy.getAttribute('barcode')
174                     };
175                     args.holdings.push(holding);
176                     part_label = '';
177                 END;
178             END;
179         END;
180
181         # Extract the copy count summary
182         count_type = (ctx.is_staff) ? 'staff' : 'public';
183
184         # Consortial copy count summary first
185         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
186         args.copy_counts = {};
187         FOR node IN xml.findnodes(xpath);
188             FOR attr IN ['count', 'available', 'unshadow', 'transcendant', 'org_unit']; 
189                 depth = node.getAttribute('depth');
190                 args.copy_counts.$depth.$attr = node.getAttribute(attr);
191             END;
192         END;
193
194         # "mattype" == "custom marc format specifier"
195         FOR icon_style IN ['mattype', 'item_type']; 
196             node = xml.findnodes(
197                 '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]');
198             IF node AND node.textContent;
199                 args.format_label = node.getAttribute('coded-value')
200                 args.format_icon = ctx.media_prefix _ '/images/format_icons/' _ icon_style _ '/' _ node.textContent _ '.png';
201                 LAST;
202             END;
203         END;
204     END;
205 %]