1 # =====================================================================
2 # cartView.awk: RPC I/O function for rpclib/cartView.
3 #
4 # Copyright (c) 2007,2008,2009,2010 Carlo Strozzi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 dated June, 1991.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # =====================================================================
20
21 # =====================================================================
22 # void _userproc(int mode)
23 # =====================================================================
24
25 function _userproc(mode, value,outfile,i,j,e,tot,\
26 url,a,b,f,fmt,tmp,stem) {
27
28 if (mode == _O_REQUEST) { # request.
29
30 # target group.
31 value = substr(_request("1",1),1,ENVIRON["TNS_GROUP_MAXLEN"])
32
33 # group must not be null and it may not contain the
34 # unescaped ``.'' character.
35
36 if (value != _NULL && value !~ /\./) {
37 _rcset("cgi.group",unixify(value))
38 _rcset("cgi.group.literal",value)
39 }
40
41 # The following test is necessary since the address could,
42 # at least in theory, have been set to any string by the
43 # remote user, due to how it is handled to cope with stunnel(8)
44 # and the lack of transproxy support in kernel 2.4.x.
45
46 if (_isipaddr(ENVIRON["REMOTE_ADDR"]) == _TRUE)
47 value = ENVIRON["REMOTE_ADDR"]
48 else value = "0.0.0.0"
49
50 _rcset("REMOTE_ADDR",value)
51 }
52
53 else { # response
54
55 outfile = _rcget("tpl.include.tw.page")
56 if (outfile !~ /^\/\.*[a-zA-Z0-9]/)
57 return(_sys("csaExit.fault 0041 outfile"))
58
59 # Set cart list format string.
60
61 fmt = readfmt("tw-shop-cartview")
62 gsub(/%/,"%%",fmt) # turn plain '%' into '%%'.
63 gsub(/\\/,"\\\\&",fmt) # turn '\' into '\\'.
64 gsub(/[\n\r]+/,"",fmt) # just in case.
65
66 # Handle custom positioning of output tokens.
67 sub(/.*\[:/,_NULL,tmp); sub(/:].*/,_NULL,tmp)
68 tmp = _strip(tmp,_O_MIDDLE)
69 if (tmp !~ /^[0-9 ]+$/) tmp = "1 2 3 4 5 6 7 8 9"
70
71 # pad missing arg specs with "0".
72 if ((i=split(tmp,f," ")) < 9) {
73 while (i++ <= 9) tmp = tmp " 0"
74 i = split(tmp,f," ")
75 }
76
77 tmp = _NULL
78
79 for (j=1; j<=i; j++) {
80 if (j > 9) break # ignore excess arg specs.
81 if (!sub(//,"%s",fmt)) fmt = fmt ""
82 tmp = tmp " " f[j]
83 }
84
85 # encode any extra markers.
86 gsub(//,"\\<tw:s/\\>",fmt)
87
88 fmt = fmt "\n"
89
90 split(_strip(tmp),f," "); f[0] = 0
91
92 stem = ENVIRON["CSA_RPC_URI"] "/" \
93 ENVIRON["CSA_LANG"] "/" _rcget("tbl_group.g_uri")
94
95 # Make sure output file is cleared, in case we are re-using
96 # an old temporary file and we have nothing to print to it.
97 _creat(outfile,_O_TRUNC)
98
99 # read input table.
100 # k_page, cart_qty, p_name, p_uri, p_descr, p_link, p_store
101
102 e[0] = _NULL
103 i=j=1
104 while (split(_TBLS[1,i++],a,"\t")) {
105
106 if (a[5] == _NULL) a[5] = a[3] # default descr.
107
108 url = stem "/" a[1]
109
110 # Work-out store data, filling missing defaults along the way.
111 if (a[7] ~ /^[^:]+:[^:]+ /)
112 sub (/ /,":" _nlsmap(_NULL,"units") " ",a[7])
113
114 split(a[7],b,/[ :]/)
115
116 tmp = b[4]
117 sub(/,/,".",tmp) # just in case.
118 tot += (tmp * a[2]) # compute subtotal.
119
120 e[1] = _xmlencode(url)
121 e[2] = _xmlencode(b[1])
122 e[3] = _xmlencode(a[5])
123 e[4] = _xmlencode(a[6])
124 e[5] = _xmlencode(b[4])
125 e[6] = stem "/tw-cart"
126 e[7] = a[1]
127 e[8] = _xmlencode(a[2])
128 e[9] = _xmlencode(b[3])
129
130 printf(fmt,e[f[1]],e[f[2]],e[f[3]],e[f[4]],\
131 e[f[5]],e[f[6]],e[f[7]],e[f[8]],e[f[9]]) > outfile
132
133 # Provide also a machine-readable format.
134 _wsresponse(1,j,e[1])
135 _wsresponse(2,j,e[2])
136 _wsresponse(3,j,e[3])
137 _wsresponse(4,j,e[4])
138 _wsresponse(5,j,e[5])
139 _wsresponse(6,j,e[6])
140 _wsresponse(7,j,e[7])
141 _wsresponse(8,j,e[8])
142 _wsresponse(9,j++,e[9])
143 }
144
145 close(outfile)
146
147 # Handle decimal separator, add more languages if needed.
148 tmp = sprintf("%.2f",tot)
149 if (ENVIRON["CSA_LANG"] ~ /^it/) gsub(/\./,",",tmp)
150
151 _response("tpl.var.tw.cart.total",tmp)
152
153 # generic template conditionals.
154
155 ifsections()
156 }
157 }
158
159 # EOF