]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/templates/default/opac/parts/misc_util.tt2
Merge branch 'dbs/ttopac-move-templates' into dbs/template-toolkit-integration-on...
[Evergreen.git] / Open-ILS / src / templates / default / 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.isbn = xml.findnodes('//*[@tag="020"]/*[@code="a"]').shift.textContent;
7         args.upc = xml.findnodes('//*[@tag="024"]/*[@code="a"]').textContent;
8         args.issn = xml.findnodes('//*[@tag="022"]/*[@code="a"]').textContent;
9         args.title = xml.findnodes('//*[@tag="245"]/*[@code="a"]').textContent;
10         args.author = xml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent;
11         args.publisher = xml.findnodes('//*[@tag="260"]/*[@code="b"]').textContent;
12         args.pubdate = xml.findnodes('//*[@tag="260"]/*[@code="c"]').textContent;
13         args.edition = xml.findnodes('//*[@tag="250"]/*[@code="a"]').textContent ||
14             xml.findnodes('//*[@tag="534"]/*[@code="b"]').textContent ||
15             xml.findnodes('//*[@tag="775"]/*[@code="b"]').textContent;
16         phys = xml.findnodes(
17             '//*[@tag="300"]/*[@code="a" or @code="b" or @code="c" or @code="e"]'
18         );
19         phys_content = [];
20         FOR p IN phys; phys_content.push(p.textContent); END;
21         args.phys_desc = phys_content.join("");
22
23         # MARC Callnumber
24         args.marc_cn = xml.findnodes('//*[@tag="092" or @tag="099"]/*').textContent;
25
26         # clean up the ISBN
27         args.isbn_clean = args.isbn.replace('\ .*', '');
28
29         args.holdings = [];
30         args.uris = [];
31         args.issns = [];
32
33         # we use $9 of ISBN and ISSN as a flag for e-version
34         sfx_isbn = xml.findnodes('//*[@tag="020"]/*[@code="9"]');
35         IF sfx_isbn;
36             IF sfx_isbn.textContent == "SFX";
37                 my_parent = sfx_isbn.parentNode();
38                 sfx_isbn = my_parent.findnodes('./*[@code="a"]');
39                 sfx_isbn.replace('-', '');
40                 args.resolver_isbn = sfx_isbn.replace('\ .*', '');
41             END;
42         END;
43
44         sfx_issn = xml.findnodes('//*[@tag="022"]/*[@code="9"]');
45         IF sfx_issn;
46             IF sfx_issn.textContent == "SFX";
47                 my_parent = sfx_issn.parentNode();
48                 sfx_issn = my_parent.findnodes('./*[@code="a"]');
49                 args.issns.push(
50                     sfx_issn.textContent.replace('[^\d\-X]', '')
51                 );
52             END;
53         END;
54
55         # we snag all issns if no SFX available
56         IF args.issns.size == 0;
57             FOR rawissn IN xml.findnodes('//*[@tag="022"]/*[@code="a"]');
58                 args.issns.push(
59                     rawissn.textContent.replace('[^\d\-X]', '')
60                 );
61             END;
62         END;
63
64         FOR volume IN xml.findnodes('//*[local-name()="volumes"]/*[local-name()="volume"]');
65
66             # Check volume visibility - could push this into XPath
67             vol.label = volume.getAttribute('label');
68             vol.id = volume.getAttribute('id');
69             NEXT IF volume.getAttribute('opac_visible') == 'false';
70             NEXT IF volume.getAttribute('deleted') == 'true';
71
72             IF vol.label == '##URI##';
73                 FOR uri IN volume.findnodes('./*[local-name()="uris"]/*[local-name()="uri"]');
74                     res.href = uri.getAttribute('href');
75                     res.link = uri.getAttribute('label');
76                     res.note = uri.getAttribute('use_restriction');
77                     args.uris.push(res);
78                 END;
79                 NEXT;
80             ELSE;
81                 copies = volume.findnodes('./*[local-name()="copies"]/*[local-name()="copy"]');
82                 FOR copy IN copies;
83                     # Check copy visibility
84                     cp.deleted = copy.getAttribute('deleted');    
85                     cp.visible = copy.getAttribute('opac_visible');
86                     NEXT IF (cp.deleted == 'true' OR cp.visible == 'false');
87
88                     # Iterate through all of the children to determine visibility
89                     FOR node IN cp.childNodes;
90                         NEXT IF cp.visible == 'false';
91                         vis = node.getAttribute('opac_visible');
92                         del = node.getAttribute('deleted');
93                         IF vis == 'false' or del == 'true';
94                             cp.visible = 'false';
95                         END;
96                     END;
97
98                     NEXT IF cp.visible == 'false';
99                     
100                     loc = copy.findnodes('./*[local-name()="location"]');
101                     circlib = copy.findnodes('./*[local-name()="circlib"]');
102                     status = copy.findnodes('./*[local-name()="status"]');
103
104                     holding = {
105                         label => vol.label,
106                         location => loc.textContent,
107                         library => circlib.textContent,
108                         status => status.textContent
109                         barcode => copy.getAttribute('barcode')
110                     };
111                     args.holdings.push(holding);
112                 END;
113             END;
114         END;
115
116         # Extract the copy count summary
117         count_type = (ctx.is_staff) ? 'staff' : 'public';
118         xpath = '//*[local-name()="counts"]/*[local-name()="count"][@type="' _ count_type _ '"]';
119         FOR node IN xml.findnodes(xpath);
120             args.copy_counts = {};
121             FOR attr IN ['count', 'available', 'unshadow', 'transcendant']; 
122                 args.copy_counts.$attr = node.getAttribute(attr);
123             END;
124         END;
125
126         # "mattype" == "custom marc format specifier"
127         FOR icon_style IN ['mattype', 'item_type']; 
128             node = xml.findnodes(
129                 '//*[local-name()="attributes"]/*[local-name()="field"][@name="' _ icon_style _ '"]');
130             IF node;
131                 args.format_label = node.getAttribute('coded-value')
132                 args.format_icon = ctx.media_prefix _ '/images/format_icons/' _ icon_style _ '/' _ node.textContent _ '.png';
133                 LAST;
134             END;
135         END;
136     END;
137
138     BLOCK get_hold_status;
139         IF hold.hold.status == 4;
140             l("Available");
141             IF ahr.shelf_expire_time;
142                 l('<br/>Expires [_1]', 
143                     date.format(ctx.parse_datetime(ahr.shelf_expire_time), DATE_FORMAT));
144             END;
145         ELSIF hold.hold.estimated_wait AND hold.hold.estimated_wait > 0;
146             # estimated wait is delivered as seconds.
147             SET hwait = POSIX.ceil(hold.hold.estimated_wait / 86400);
148             l("Estimated wait: [quant,_1,day,days]", hwait);
149         ELSIF hold.hold.status == 3;
150             l("In Transit");
151         ELSIF hold.hold.status < 3;
152             l("Waiting for copy");
153         END;
154     END;
155 %]