]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/templates/default/opac/parts/header.tt2
Force-encode CGI param values to prevent decode errors
[working/Evergreen.git] / Open-ILS / web / templates / default / opac / parts / header.tt2
1 [%- USE money = format(l('$%.2f'));
2     USE date;
3     USE CGI = CGI_utf8;
4     USE POSIX;
5     SET DATE_FORMAT = l('%m/%d/%Y');
6
7     # Don't wrap in l() here; do that where this format string is actually used.
8     SET HUMAN_NAME_FORMAT = '[_1] [_2] [_3] [_4] [_5]';
9
10     # x and y are artifacts of using <input type="image" /> tags instead of
11     # true submit buttons, and their values are never used. page is used, but
12     # currently none of the use cases for rendering the query_string back
13     # into page output call for propagating the value of the page variable.
14
15     query_string = CGI.query_string |
16         replace(';x=\d+','') | replace(';y=\d+','') | replace(';page=\d*', '') |
17         replace(';', '&') | replace('&', '&amp;');
18
19     propagator = '?' _ query_string;
20
21     is_advanced = CGI.param("_adv").size;
22     is_special = CGI.param("_special").size;
23
24     # -----------------------------------------------------------------------------
25     # mkurl()
26     #
27     # Current page, updated params:
28     # mkurl('', {foo => 'bar', boo => 'baz'});
29     #
30     # New page, one param is a list:
31     # mkurl('http://flarg.baz/squz', {foo => 'bar', boo => ['baz', 'faz']});
32     #
33     # New page, clear all existing params before applying new ones:
34     # mkurl('/fuz/buster', {foo => 'bar', boo => 'baz'}, 1); 
35     #
36     # Current page, clear the 'some_param' from the existing params:
37     # mkurl('', {foo => 'bar', boo => 'baz'}, ['some_param']);
38     MACRO mkurl(page, params, clear_params) BLOCK;
39
40         # clone the query string to avoid clobberation
41         cgi = CGI.new(CGI.query_string);
42
43         # remove requested params
44         IF clear_params.0; # array
45             FOR p IN clear_params; cgi.delete(p); END;
46         ELSIF clear_params;
47             cgi.delete_all();
48         END;
49
50         # always remove these pesky artifacts
51         cgi.delete('x', 'y'); 
52
53         # apply user params
54         FOR k IN params.keys;
55             encoded = [];
56             list = params.$k.0 ? params.$k : [params.$k];
57             # CGI croaks on already-decoded strings.  force-encode to be safe
58             FOR p IN list; encoded.push(ctx.encode_utf8(p)); END;
59             foo = cgi.param("-name", k, "-values", encoded);
60         END;
61
62         # for url brevity, remove any params that have no value
63         FOR p IN cgi.param;
64             val = cgi.param(p);
65             IF val == ''; cgi.delete(p); END;
66         END;
67
68         IF page;
69             IF cgi.query_string;
70                 page _ '?' _ cgi.query_string;
71             ELSE;
72                 page;
73             END;
74         ELSE;
75             cgi.url("-path" => 1, "-query" => 1);
76         END;
77     END;
78 %]