[interchange-cvs] interchange - jon modified 7 files

interchange-cvs at icdevgroup.org interchange-cvs at icdevgroup.org
Thu Apr 7 18:51:34 EDT 2005


User:      jon
Date:      2005-04-07 22:51:33 GMT
Modified:  code/SystemTag discount.coretag
Modified:  dist/test/products tests.asc
Modified:  lib/Vend Config.pm Interpolate.pm Session.pm
Added:     code/UserTag discount_space.tag
Removed:   code/SystemTag discount_space.coretag
Log:
Patch from Ethan Rowe <ethan at endpoint.com>, which I've been sitting on
for far too long. His comments:

Discount-space rework according to agreements reached between involved
core team members; modified initialization logic and configuration such
that discount spaces do not make an impact on page processing for catalogs
that do not explicitly make use of the feature. Changes include:

DiscountSpacesOn -- boolean catalog configuration directive. This is
FALSE by default. When set to true, a dispatch routine is added
to the (relatively new) dispatch routine stack, to be evaluated
per page process prior to autoloads; this routine initializes the
current discount space to 'main', and then checks all CGI variables
specified in the DiscountSpaceVar configuration array in the event
that an alternate discount space has been specified via CGI variables.
When DiscountSpacesOn is FALSE, *all* discount space functions become
a no-op; attempts to specify an alternate discount space will have no
effect, and will result in an error message in the catalog's error log.

DiscountSpaceVar -- array catalog configuration directive, with a default
entry of 'mv_discount_space'; the members of this array are treated as
names of CGI variables to check per page process, such that a CGI variable
can be used to specify the discount space to use for that page process
in a manner akin to values space ('mv_values_space') or named shopping
cart ('mv_cartname'). The CGI variables will *not* be checked unless
DiscountSpacesOn is true. While default behavior of 'mv_discount_space'
is likely to suffice for most purposes, people can tie the discount space
to other things, such as the cart, by including other variable names in
this array (for instance, 'mv_cartname' would tie the discount space to
the cart name, which could be convenient in some situations).

A new global object has been introduced: $::Discounts. This will always
point to the current discount hash; changing the discount space via
CGI variables or the [discount-space] tag will update $::Discounts.
Furthermore, a new Safe object has been introduced for accessing the
current discount space in [perl] and [calc] blocks: $Discounts.

Any code that works directly with the $Vend::Session->{discount} hash
should be updated to work with the $::Discounts variable (in global
space) and the $Discounts variable (in Safe space such as [perl] and
[calc]). Care has been taken to maintain backwards compatibility,
and working directly with the {discount} session member will generally
not cause problems (changing the discount space will in fact change the
hash to which $Vend::Session->{discount} points), except in the case of
reassigning the hash (i.e., $Session->{discount} = { 'SOMEITEM' => ' %s *
0.80 ' }); reassigning the hash will break the $::Discounts reference and
result in unpredictable behavior for the remainder of the page process.
Therefore, it is strongly advised that all such code be updated at some
point to use the new objects. Furthermore, any actions such as clearing
the discounts should be done by dereferencing rather than assigning a
new hashref.

Thus:
%$Discounts = ();
and not:
$Discounts = {};

Furthermore, you can clear the discount space via:
[discount-space name=some_spacename clear=1]

The current discount space name can be checked via:
[discount-space current=1]

Sessions: to maintain backwards compatibility, sessions continue to use a
{discount} member. However, at session initialization, all sessions will
receive a {discount_space}{main} member, and {discount} will point to it.
The session read process is essentially unchanged, with the exception of
a minor assignment in which $::Discounts is initialized to point to the
{discount} member. The modification to discount-spaces was driven in
part to reduce the number of assignments necessary at session read time,
and this new implementation is a reflection of that.

For the future, it may be worthwhile to revise the logic to eliminate
the {discount} session member entirely, and simply expect all code to
work directly through the $::Discounts/$Discounts variables. This would
allow some slight adjustments to the implementation of the discount space
initialization and switching logic, making it slightly more efficient
and certainly cleaner.

Revision  Changes    Path
1.6       +10 -10    interchange/code/SystemTag/discount.coretag


rev 1.6, prev_rev 1.5
Index: discount.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/discount.coretag,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -u -r1.5 -r1.6
--- discount.coretag	9 Feb 2005 13:39:42 -0000	1.5
+++ discount.coretag	7 Apr 2005 22:51:32 -0000	1.6
@@ -1,13 +1,13 @@
 # Copyright 2002-2005 Interchange Development Group (http://www.icdevgroup.org/)
 # Licensed under the GNU GPL v2. See file LICENSE for details.
-# $Id: discount.coretag,v 1.5 2005/02/09 13:39:42 docelic Exp $
+# $Id: discount.coretag,v 1.6 2005/04/07 22:51:32 jon Exp $
 
 UserTag discount            Order        code
 UserTag discount            AddAttr
 UserTag discount            attrAlias    space discount_space
 UserTag discount            hasEndTag
 UserTag discount            PosNumber    1
-UserTag discount            Version      $Revision: 1.5 $
+UserTag discount            Version      $Revision: 1.6 $
 UserTag discount            Routine      <<EOR
 
 # Sets the value of a discount field
@@ -20,18 +20,18 @@
 		$opt = {};
 	}
 
-	unless (
+	if (! ($::Discounts
+			and $Vend::Session->{discount_space}
+			and $Vend::Session->{discount}
+			and $Vend::DiscountSpaceName)) {
 		$::Discounts
-		and $Vend::Session->{discount_space}
-		and $Vend::Session->{discount}
-	) {
-		$::Discounts = 
-			$Vend::Session->{discount_space}{ $Vend::DiscountSpace = 'main' } =
-			$Vend::Session->{discount} ||= {};
+		 	= $Vend::Session->{discount}
+			= $Vend::Session->{discount_space}{ $Vend::DiscountSpaceName = 'main' }
+			||= ($Vend::Session->{discount} || {});
 	}
 
 	my $dspace;
-	if ($dspace = $opt->{discount_space}) {
+	if ($Vend::Cfg->{DiscountSpacesOn} and $dspace = $opt->{discount_space}) {
 		$dspace = $Vend::Session->{discount_space}{$dspace} ||= {};
 	}
 	else {



1.3       +36 -24    interchange/code/UserTag/discount_space.tag


rev 1.3, prev_rev 1.2



2.21      +1 -1      interchange/dist/test/products/tests.asc


rev 2.21, prev_rev 2.20
Index: tests.asc
===================================================================
RCS file: /var/cvs/interchange/dist/test/products/tests.asc,v
retrieving revision 2.20
retrieving revision 2.21
diff -u -u -r2.20 -r2.21
--- tests.asc	29 Mar 2005 17:40:56 -0000	2.20
+++ tests.asc	7 Apr 2005 22:51:33 -0000	2.21
@@ -2630,7 +2630,7 @@
 000156
 %%
 [calcn]
-
+$Config->{DiscountSpacesOn} = 1;
 @{$Scratch->{shipping_line} = []} = @{$Config->{Shipping_line}};
 
 @{$Config->{Shipping_line}} = grep { ! $_->[6] } @{$Scratch->{shipping_line}};



2.158     +14 -8     interchange/lib/Vend/Config.pm


rev 2.158, prev_rev 2.157
Index: Config.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Config.pm,v
retrieving revision 2.157
retrieving revision 2.158
diff -u -u -r2.157 -r2.158
--- Config.pm	6 Mar 2005 04:14:07 -0000	2.157
+++ Config.pm	7 Apr 2005 22:51:33 -0000	2.158
@@ -1,6 +1,6 @@
 # Vend::Config - Configure Interchange
 #
-# $Id: Config.pm,v 2.157 2005/03/06 04:14:07 mheins Exp $
+# $Id: Config.pm,v 2.158 2005/04/07 22:51:33 jon Exp $
 #
 # Copyright (C) 2002-2003 Interchange Development Group
 # Copyright (C) 1996-2002 Red Hat, Inc.
@@ -49,7 +49,7 @@
 use Vend::File;
 use Vend::Data;
 
-$VERSION = substr(q$Revision: 2.157 $, 10);
+$VERSION = substr(q$Revision: 2.158 $, 10);
 
 my %CDname;
 my %CPname;
@@ -553,7 +553,7 @@
 	['DescriptionField', undef,              'description'],
 	['PriceDefault',	 undef,              'price'],
 	['PriceField',		 undef,              'price'],
-	['DiscountSpaces',	 'yesno',            'no'],
+	['DiscountSpacesOn', 'yesno',            'no'],
 	['DiscountSpaceVar', 'array',            'mv_discount_space'],
 	['Jobs',		 	 'hash',     	 	 ''],
 	['Shipping',         'locale',           ''],
@@ -2417,6 +2417,12 @@
 
 	DiscountSpaces => sub {
 #::logDebug("Doing DiscountSpaces dispatch...");
+		$::Discounts
+			= $Vend::Session->{discount}
+			= $Vend::Session->{discount_space}{
+					$Vend::DiscountSpaceName = 'main'
+				}
+			||= {};
 		my $dspace;
 		for (@{$Vend::Cfg->{DiscountSpaceVar}}) {
 			next unless $dspace = $CGI::values{$_};
@@ -2424,11 +2430,11 @@
 			last;
 		}
 		return unless $dspace;
-		$Vend::DiscountSpace = $dspace;
-#::logDebug("Discount space is set=$Vend::DiscountSpace...");
+		$Vend::DiscountSpaceName = $dspace;
+#::logDebug("Discount space is set=$Vend::DiscountSpaceName...");
 		$::Discounts
 				= $Vend::Session->{discount}
-				= $Vend::Session->{discount_space}{$Vend::DiscountSpace}
+				= $Vend::Session->{discount_space}{$Vend::DiscountSpaceName}
 				||= {};
     },
 
@@ -2670,8 +2676,8 @@
 						return 1;
 					},
 
-		DiscountSpaces => sub {
-					return 1 unless $C->{DiscountSpaces};
+		DiscountSpacesOn => sub {
+					return 1 unless $C->{DiscountSpacesOn};
 					push @Dispatches, 'DiscountSpaces';
 					return 1;
 		},



2.237     +40 -34    interchange/lib/Vend/Interpolate.pm


rev 2.237, prev_rev 2.236
Index: Interpolate.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Interpolate.pm,v
retrieving revision 2.236
retrieving revision 2.237
diff -u -u -r2.236 -r2.237
--- Interpolate.pm	29 Mar 2005 17:40:56 -0000	2.236
+++ Interpolate.pm	7 Apr 2005 22:51:33 -0000	2.237
@@ -1,6 +1,6 @@
 # Vend::Interpolate - Interpret Interchange tags
 # 
-# $Id: Interpolate.pm,v 2.236 2005/03/29 17:40:56 jon Exp $
+# $Id: Interpolate.pm,v 2.237 2005/04/07 22:51:33 jon Exp $
 #
 # Copyright (C) 2002-2005 Interchange Development Group
 # Copyright (C) 1996-2002 Red Hat, Inc.
@@ -28,7 +28,7 @@
 require Exporter;
 @ISA = qw(Exporter);
 
-$VERSION = substr(q$Revision: 2.236 $, 10);
+$VERSION = substr(q$Revision: 2.237 $, 10);
 
 @EXPORT = qw (
 
@@ -40,7 +40,7 @@
 $CGI
 $Session
 $Values
-
+$Discounts
 );
 
 =head1 NAME
@@ -205,6 +205,7 @@
 	$CGI_array  = \%CGI::values_array;
 	$CGI        = \%CGI::values;
 	$Carts      = $::Carts;
+	$Discounts	= $::Discounts;
 	$Items      = $Vend::Items;
 	$Config     = $Vend::Cfg;
 	$Scratch    = $::Scratch;
@@ -2665,18 +2666,24 @@
 # Sets the discount namespace.
 sub switch_discount_space {
 	my $dspace = shift || 'main';
-	my $oldspace;
-#::logDebug("switch_discount_space: called for space '$dspace'; current space is $Vend::DiscountSpace.");
+	my $oldspace = $Vend::DiscountSpaceName || 'main';
+	logError('Attempt to set discount space in catalog with discount spaces set to OFF.'), return $oldspace
+		unless $Vend::Cfg->{DiscountSpacesOn};
+#::logDebug("switch_discount_space: called for space '$dspace'; current space is $oldspace.");
 	unless ($Vend::Session->{discount} and $Vend::Session->{discount_space}) {
-		$::Discounts = $Vend::Session->{discount_space}{main} = $Vend::Session->{discount} ||= {};
-		$Vend::DiscountSpace = 'main';
+		$::Discounts
+			= $Vend::Session->{discount}
+			= $Vend::Session->{discount_space}{main}
+			||= ($Vend::Session->{discount} || {});
+		$Vend::DiscountSpaceName = 'main';
 #::logDebug('switch_discount_space: initialized discount space hash.');
 	}
-	if ($dspace ne ($oldspace = $Vend::DiscountSpace)) {
-		$::Discounts = $Vend::Session->{discount}
-			= $Vend::Session->{discount_space}{$Vend::DiscountSpace = $dspace}
+	if ($dspace ne $oldspace) {
+		$::Discounts
+			= $Vend::Session->{discount}
+			= $Vend::Session->{discount_space}{$Vend::DiscountSpaceName = $dspace}
 			||= {};
-#::logDebug("switch_discount_space: changed discount space from '$oldspace' to $Vend::DiscountSpace");
+#::logDebug("switch_discount_space: changed discount space from '$oldspace' to '$Vend::DiscountSpaceName'");
 	}
 	return $oldspace;
 }
@@ -4911,13 +4918,17 @@
 	($code, $extra) = ($item->{code}, $item->{mv_discount});
 
 	if ($extra and ! $::Discounts) {
-		my $dspace = $Vend::DiscountSpace ||= 'main';
-		$Vend::Session->{discount_space}{main} = $Vend::Session->{discount} ||= {}
-			unless $Vend::Session->{discount_space}{main};
-		$::Discounts = $Vend::Session->{discount} = $Vend::Session->{discount_space}{$dspace} ||= {};
+		my $dspace = $Vend::DiscountSpaceName ||= 'main';
+		$Vend::Session->{discount_space}{main}
+			= $Vend::Session->{discount}
+			||= {} unless $Vend::Session->{discount_space}{main};
+		$::Discounts
+			= $Vend::Session->{discount}
+			= $Vend::Session->{discount_space}{$dspace}
+			||= {} if $Vend::Cfg->{DiscountSpacesOn};
 	}
 
-	return $price unless $::Discounts;
+	return $price unless %$::Discounts;
 
 	$quantity = $item->{quantity};
 
@@ -5186,9 +5197,8 @@
         tag_cart($cart);
     }
 
-	if (defined $dspace and $dspace ne $Vend::DiscountSpace) {
-		$oldspace = switch_discount_space($dspace);
-	}
+	# Support for discount namespaces.
+	$oldspace = switch_discount_space($dspace) if $dspace;
 
     $taxable = 0;
 
@@ -5197,7 +5207,7 @@
 		next if is_yes( $item->{mv_nontaxable} );
 		next if is_yes( item_field($item, $Vend::Cfg->{NonTaxableField}) );
 		$tmp = item_subtotal($item);
-		unless ($::Discounts) {
+		unless (%$::Discounts) {
 			$taxable += $tmp;
 		}
 		else {
@@ -5222,8 +5232,7 @@
 	$Vend::Items = $save if defined $save;
 
 	# Restore initial discount namespace if appropriate.
-	switch_discount_space($oldspace)
-		if $dspace and $oldspace ne $Vend::DiscountSpace;
+	switch_discount_space($oldspace) if defined $oldspace;
 
 	return $taxable;
 }
@@ -5447,9 +5456,7 @@
         tag_cart($cart);
     }
 
-	if (defined $opt->{discount_space} and $opt->{discount_space} ne $Vend::DiscountSpace) {
-		$oldspace = switch_discount_space( $opt->{discount_space} );
-	}
+	$oldspace = switch_discount_space( $opt->{discount_space} ) if $opt->{discount_space};
 
 #::logDebug("salestax entered, cart=$cart");
 	my $tax_hash;
@@ -5472,7 +5479,7 @@
 # if we have a cost from previous routines, return it
 	if(defined $cost) {
 		$Vend::Items = $save if $save;
-		switch_discount_space($oldspace) if $opt->{discount_space} and $oldspace ne $Vend::DiscountSpace;
+		switch_discount_space($oldspace) if defined $oldspace;
 		return Vend::Util::round_to_frac_digits($cost);
 	}
 
@@ -5483,7 +5490,7 @@
 #::logDebug("got to tax function: " . uneval($tax_hash));
 	my $amount = taxable_amount();
 	# Restore the original discount namespace if appropriate; no other routines need the discount info.
-	switch_discount_space($oldspace) if $opt->{discount_space} and $oldspace ne $Vend::DiscountSpace;
+	switch_discount_space($oldspace) if defined $oldspace;
 
 	my($r, $code);
 	# Make it upper case for state and overseas postal
@@ -5547,7 +5554,7 @@
 			&& length( $Vend::Session->{assigned}{subtotal});
 	}
 
-    my($save,$subtotal, $i, $item, $tmp, $cost, $formula);
+    my ($save, $subtotal, $i, $item, $tmp, $cost, $formula, $oldspace);
 	if ($cart) {
 		$save = $Vend::Items;
 		tag_cart($cart);
@@ -5555,9 +5562,9 @@
 
 	levies() unless $Vend::Levying;
 	
-	my $oldspace = switch_discount_space($dspace) if $dspace;
+	$oldspace = switch_discount_space($dspace) if $dspace;
 	
-	my $discount = defined $::Discounts;
+	my $discount = (defined $::Discounts and %$::Discounts);
 
     $subtotal = 0;
 	$tmp = 0;
@@ -5588,7 +5595,7 @@
 	$Vend::Session->{latest_subtotal} = $subtotal;
 
 	# Switch to original discount space if one exists.
-	switch_discount_space($oldspace) if $dspace and $oldspace ne $Vend::DiscountSpace;
+	switch_discount_space($oldspace) if defined $oldspace;
 
     return $subtotal;
 }
@@ -5599,10 +5606,9 @@
 
 sub total_cost {
 	my ($cart, $dspace) = @_;
-	my ($total, $i, $save, $oldspace);
+    my ($total, $i, $save, $oldspace);
 
-	$oldspace = switch_discount_space($dspace)
-		if defined $dspace and $dspace ne $Vend::DiscountSpace;
+	$oldspace = switch_discount_space($dspace) if $dspace;
 
 	if ($cart) {
 		$save = $Vend::Items;



2.22      +7 -4      interchange/lib/Vend/Session.pm


rev 2.22, prev_rev 2.21
Index: Session.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Session.pm,v
retrieving revision 2.21
retrieving revision 2.22
diff -u -u -r2.21 -r2.22
--- Session.pm	1 Feb 2005 02:07:15 -0000	2.21
+++ Session.pm	7 Apr 2005 22:51:33 -0000	2.22
@@ -1,6 +1,6 @@
 # Vend::Session - Interchange session routines
 #
-# $Id: Session.pm,v 2.21 2005/02/01 02:07:15 jon Exp $
+# $Id: Session.pm,v 2.22 2005/04/07 22:51:33 jon Exp $
 # 
 # Copyright (C) 2002-2003 Interchange Development Group
 # Copyright (C) 1996-2002 Red Hat, Inc.
@@ -27,7 +27,7 @@
 require Exporter;
 
 use vars qw($VERSION);
-$VERSION = substr(q$Revision: 2.21 $, 10);
+$VERSION = substr(q$Revision: 2.22 $, 10);
 
 @ISA = qw(Exporter);
 
@@ -433,8 +433,7 @@
     $::Values	= $Vend::Session->{values};
     $::Scratch	= $Vend::Session->{scratch};
     $::Carts	= $Vend::Session->{carts};
-	$::Discounts = $Vend::Session->{discount}
-		= $Vend::Session->{discount_space}{main} ||= {};
+    $::Discounts = $Vend::Session->{discount};
     $Vend::Interpolate::Tmp ||= {};
     $::Control	= $Vend::Interpolate::Tmp->{control} = [];
 	tie $Vend::Items, 'Vend::Cart';
@@ -489,6 +488,7 @@
 		'values'	=> { %{$Vend::Cfg->{ValuesDefault}} },
 		'carts'		=> {main => []},
 		'levies'	=> {main => []},
+		'discount_space'	=> {main => {}},
     };
 	$Vend::Session->{shost} = $CGI::remote_addr
 		if $CGI::secure;
@@ -499,6 +499,9 @@
 	tie $Vend::Items, 'Vend::Cart';
 	$::Values->{mv_shipmode} = $Vend::Cfg->{DefaultShipping}
 		if ! defined $::Values->{mv_shipmode};
+	$::Discounts
+		= $Vend::Session->{discount}
+		= $Vend::Session->{discount_space}{main};
 	if(my $macro = $Vend::Cfg->{SpecialSub}{init_session}) {
 		Vend::Dispatch::run_macro(
 				$macro,








More information about the interchange-cvs mailing list