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