1	# =====================================================================
     2	# cartLoad: W-TW shopping cart update processor.
     3	#
     4	# Copyright (c) 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	#		Local variables and functions
    23	# =====================================================================
    24	
    25	cgi.group = ()
    26	cgi.group.literal = ()
    27	cgi.page = ()
    28	cgi.page.literal = ()
    29	cgi.qty = ..DEL..					# default.
    30	
    31	# =====================================================================
    32	#			Main program
    33	# =====================================================================
    34	
    35	csaGetArgs POST
    36	
    37	#~ $REMOTE_ADDR 192.168.1.2 && csaExit.env
    38	
    39	. $CSA_ROOT/lib/group-stuff.rc
    40	
    41	. $CSA_ROOT/lib/group-editor.rc
    42	
    43	. $CSA_ROOT/lib/page-stuff.rc
    44	
    45	# Hidden/redirected items are unconditionally not eligible.
    46	if (~ $'tbl_page.p_descr' -* *^'(:redirect '^*) {
    47	   csaExit.fault 1038 $'tbl_page.p_name'
    48	}
    49	
    50	# Get individual store-related metadata for the target item.
    51	* = ``($ifs:,){echo -n $'tbl_page.p_store'}
    52	
    53	# Check the specified quantity value for consistency,
    54	# unless item is to be deleted from cart.
    55	
    56	if (!~ $'cgi.qty' ..DEL..) {
    57	
    58	   switch ($2) {
    59	     case 0
    60		  csaExit.fault 1038 $'tbl_page.p_name'
    61	     case -1
    62	          cgi.qty = -1				# always override.
    63	     case *
    64	          test $'cgi.qty' -lt $2 &&
    65				csaExit.fault 1039 $'tbl_page.p_name' $2
    66	   }
    67	}
    68	
    69	# No PLS is really necessary here. Note that cart files cannot be hashed
    70	# into a PBC-based directory tree because the only PBC that is made
    71	# available to us by csaCgi.awk is the one of the main session file.
    72	# Should this will become a performance issue in the future I'll have
    73	# to modify csaCgi.awk to set a second PBC value for those session TagURI
    74	# names that do not change across the request-response chain.
    75	
    76	# Set default, effective immediately.
    77	~ $CSA_SESSION(19) tag:* || csaSession.set $CSA_GUID 19 immediate
    78	
    79	# Touch cart into existence, just in case, and update it's mtime.
    80	touch $TMPDIR/$CSA_SESSION(19)^-crt || csaExit.fault 0003 touch
    81	
    82	# Prime the cart if needed.
    83	if (~ `{wc -l $TMPDIR/$CSA_SESSION(19)^-crt} 0 1) {
    84	   printf \001k_page\t\001cart_qty\n > $TMPDIR/$CSA_SESSION(19)^-crt
    85	   ~ $status 0 || csaExit.fault 0009 $TMPDIR/$CSA_SESSION(19)^-crt
    86	}
    87	
    88	csaOpen --fast --relaxed $TMPDIR/$CSA_SESSION(19)^-crt || csaExit.fault
    89	tmp1 = $CSA_RESULT
    90	
    91	# Replace any previous occurrence of the same item in cart.
    92	
    93	printf \001k_page\t\001cart_qty\n$'cgi.page'\t$'cgi.qty'\n |
    94	  updtable --stdin --key-columns k_page $TMPDIR/$CSA_SESSION(19)^-crt > $tmp1
    95	
    96	csaStatus || csaExit.fault 0003 printf/updtable
    97	
    98	# Now set the return URL and redirect to cartList.
    99	~ $HTTP_REFERER http://* https://* && csaSession.set $HTTP_REFERER 20
   100	
   101	# Redirection will be done only if we are in interactive mode.
   102	csaExit.ok --confirmed $CSA_RPC_URI/$CSA_LANG/$'tbl_group.g_uri'/tw-cart
   103	
   104	#EOF