[interchange-cvs] interchange - heins modified 2 files

interchange-cvs at icdevgroup.org interchange-cvs at icdevgroup.org
Sat Jan 29 13:30:02 EST 2005


User:      heins
Date:      2005-01-29 18:30:02 GMT
Modified:  lib/Vend Dispatch.pm Config.pm
Log:
* Add new DispatchRoutines code.

* Make the code for each DispatchRoutine in one common place, so that
  when multiple catalogs are using the code we don't have many copies
  of the identical subroutine.

* Make %Defaults, %Dispatch_priority, %Dispatch_code non-local variables
  so that AddDirective has full access to them.

* Added DispatchRoutines for:

	CookieLogin     first so Locale can be set as part of login
	Locale          next to account for settings for DiscountSpaces and Autoload
	DiscountSpaces  next
	Autoload        last so that catalog has complete environment set

* DiscountSpaces code works with latests tests.asc 000156 -- Ethan
  may wish to tweak.

* To add a new DispatchRoutine you just:

	-- Put in $Vend::Config::Defaults sub for set_defaults, with
	   line somewhere:

	   		push @Dispatches, 'YourDirective';

	-- $Vend::Config::Dispatch_code{YourDirecive} = $sub;
	-- $Vend::Config::Dispatch_priority{YourDirecive} = $n;

Revision  Changes    Path
1.48      +10 -72    interchange/lib/Vend/Dispatch.pm


rev 1.48, prev_rev 1.47
Index: Dispatch.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Dispatch.pm,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -r1.47 -r1.48
--- Dispatch.pm	25 Jan 2005 01:02:59 -0000	1.47
+++ Dispatch.pm	29 Jan 2005 18:30:01 -0000	1.48
@@ -1,6 +1,6 @@
 # Vend::Dispatch - Handle Interchange page requests
 #
-# $Id: Dispatch.pm,v 1.47 2005/01/25 01:02:59 jon Exp $
+# $Id: Dispatch.pm,v 1.48 2005/01/29 18:30:01 mheins Exp $
 #
 # Copyright (C) 2002-2003 Interchange Development Group
 # Copyright (C) 2002 Mike Heins <mike at perusion.net>
@@ -26,7 +26,7 @@
 package Vend::Dispatch;
 
 use vars qw($VERSION);
-$VERSION = substr(q$Revision: 1.47 $, 10);
+$VERSION = substr(q$Revision: 1.48 $, 10);
 
 use POSIX qw(strftime);
 use Vend::Util;
@@ -1311,57 +1311,6 @@
 		$Vend::ValuesSpace = $vspace;
 	}
 
-	if (
-		my $dspace = defined $::Variable->{MV_DISCOUNT_SPACE}
-			? $CGI::values{$::Variable->{MV_DISCOUNT_SPACE}} || $CGI::values{mv_discount_space}
-			: $CGI::values{mv_discount_space}
-	) {
-		# The 'main' is the default space; this allows us to easily tie the discount space
-		# to the cart namespace.
-#::logDebug("Dispatch: discount space is '$dspace'");
-		if ($dspace eq 'main') {
-			$::Discounts = $Vend::Session->{discount_space}{$dspace} = $Vend::Session->{discount} ||= {};
-		}
-		else {
-			$::Discounts = $Vend::Session->{discount_space}{$dspace} ||= {};
-		}
-		$Vend::DiscountSpace = $dspace;
-	}
-	else {
-		$Vend::DiscountSpace = 'main';
-		if (defined $Vend::Session->{discount}) {
-			$::Discounts = $Vend::Session->{discount_space}{main} = $Vend::Session->{discount};
-		}
-		else {
-			$::Discounts = undef;
-		}
-	}
-
-	if($Vend::Cfg->{CookieLogin} and ! $Vend::Session->{logged_in}) {
-		COOKIELOGIN: {
-			my $username;
-			my $password;
-			last COOKIELOGIN
-				if  exists  $CGI::values{mv_username}
-				and defined $CGI::values{mv_username};
-			last COOKIELOGIN
-				unless $username = Vend::Util::read_cookie('MV_USERNAME');
-			last COOKIELOGIN
-				unless $password = Vend::Util::read_cookie('MV_PASSWORD');
-			$CGI::values{mv_username} = $username;
-			$CGI::values{mv_password} = $password;
-			my $profile = Vend::Util::read_cookie('MV_USERPROFILE');
-			local(%SIG);
-			undef $SIG{__DIE__};
-			eval {
-				Vend::UserDB::userdb('login', profile => $profile );
-			};
-			if($@) {
-				$Vend::Session->{failure} .= $@;
-			}
-		}
-	}
-
 	$Vend::Session->{'arg'} = $Vend::Argument = ($CGI::values{mv_arg} || undef);
 
 	if ($CGI::values{mv_pc} =~ /\D/) {
@@ -1425,31 +1374,20 @@
 		}
 		$Vend::FinalPath =~ s{/$Vend::Cfg->{ProcessPage}/page/}{/};
 	}
-	my $locale;
-	if($locale = $::Scratch->{mv_language}) {
+
+	if(my $locale = $::Scratch->{mv_language}) {
 		$Global::Variable->{LANG}
 			= $::Variable->{LANG} = $locale;
 	}
+# END LEGACY
 
-	if ($Vend::Cfg->{Locale}								and
-		$locale = $::Scratch->{mv_locale}	and
-		defined $Vend::Cfg->{Locale_repository}->{$locale}
-		)
-	{ 
-		$Global::Variable->{LANG}
-				= $::Variable->{LANG}
-				= $::Scratch->{mv_language}
-				= $locale
-			 if ! $::Scratch->{mv_language};
-		Vend::Util::setlocale(	$locale,
-								($::Scratch->{mv_currency} || undef),
-								{ persist => 1 }
-							);
+	if(my $ary = $Vend::Cfg->{DispatchRoutines}) {
+		for(@$ary) {
+			$_->();
+		}
 	}
-# END LEGACY
 
-	run_macro($Vend::Cfg->{Autoload});
-#show_times("end global Autoload macro") if $Global::ShowTimes;
+#show_times("end dispatch routines (Autoload, etc.)") if $Global::ShowTimes;
 
 	for my $macro ( $Vend::Cfg->{Filter}, $Vend::Session->{Filter}) {
 		next unless $macro;



2.152     +111 -5    interchange/lib/Vend/Config.pm


rev 2.152, prev_rev 2.151
Index: Config.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Config.pm,v
retrieving revision 2.151
retrieving revision 2.152
diff -u -r2.151 -r2.152
--- Config.pm	4 Nov 2004 11:23:56 -0000	2.151
+++ Config.pm	29 Jan 2005 18:30:01 -0000	2.152
@@ -1,6 +1,6 @@
 # Vend::Config - Configure Interchange
 #
-# $Id: Config.pm,v 2.151 2004/11/04 11:23:56 racke Exp $
+# $Id: Config.pm,v 2.152 2005/01/29 18:30:01 mheins Exp $
 #
 # Copyright (C) 2002-2003 Interchange Development Group
 # Copyright (C) 1996-2002 Red Hat, Inc.
@@ -38,6 +38,7 @@
 			@Locale_directives_ary @Locale_directives_scalar
 			@Locale_directives_code
 			%ContainerSave %ContainerTrigger %ContainerSpecial %ContainerType
+			%Default %Dispatch_code %Dispatch_priority
 			@Locale_directives_currency @Locale_keys_currency
 			$GlobalRead  $SystemCodeDone $SystemGroupsDone $CodeDest
 			);
@@ -48,7 +49,7 @@
 use Vend::File;
 use Vend::Data;
 
-$VERSION = substr(q$Revision: 2.151 $, 10);
+$VERSION = substr(q$Revision: 2.152 $, 10);
 
 my %CDname;
 my %CPname;
@@ -551,6 +552,8 @@
 	['DescriptionField', undef,              'description'],
 	['PriceDefault',	 undef,              'price'],
 	['PriceField',		 undef,              'price'],
+	['DiscountSpaces',	 'yesno',            'no'],
+	['DiscountSpaceVar', 'word',             'no'],
 	['Jobs',		 	 'hash',     	 	 ''],
 	['Shipping',         'locale',           ''],
 	['Accounting',	 	 'locale',     	 	 ''],
@@ -1587,12 +1590,12 @@
 
 	my ($ref, $orig);
 #::logDebug("Contents of $name: " . uneval_it($C->{$name}));
-	if(ref($C->{$name}) =~ /ARRAY/) {
+	if(CORE::ref($C->{$name}) =~ /ARRAY/) {
 #::logDebug("watch ref=array");
 		$ref = $C->{$name};
 		$orig = [ @{ $C->{$name} } ];
 	}
-	elsif(ref($C->{$name}) =~ /HASH/) {
+	elsif(CORE::ref($C->{$name}) =~ /HASH/) {
 #::logDebug("watch ref=hash");
 		$ref = $C->{$name};
 		$orig = { %{ $C->{$name} } };
@@ -2034,6 +2037,15 @@
 	return 1;
 }
 
+sub parse_word {
+	my($name, $val) = @_;
+
+	return '' unless $val;
+	unless ($val =~ /^\w+$/) {
+		config_error("Illegal non-word value in '%s' for %s", $val, $name);
+	}
+	return $val;
+}
 
 # Allow addition of a new catalog directive
 sub parse_directive {
@@ -2337,6 +2349,77 @@
 						}
 );
 
+my @Dispatches;
+
+%Dispatch_priority = (
+	CookieLogin => 1,
+	Locale => 2,
+	DiscountSpaces => 5,
+	Autoload => 9,
+);
+
+%Dispatch_code = (
+	Autoload => sub {
+#::logDebug("Doing Autoload dispatch...");
+		Vend::Dispatch::run_macro($Vend::Cfg->{Autoload});
+	},
+	CookieLogin => sub {
+#::logDebug("Doing CookieLogin dispatch....");
+		if(! $Vend::Session->{logged_in}) {
+			COOKIELOGIN: {
+				my $username;
+				my $password;
+				last COOKIELOGIN
+					if  exists  $CGI::values{mv_username}
+					and defined $CGI::values{mv_username};
+				last COOKIELOGIN
+					unless $username = Vend::Util::read_cookie('MV_USERNAME');
+				last COOKIELOGIN
+					unless $password = Vend::Util::read_cookie('MV_PASSWORD');
+				$CGI::values{mv_username} = $username;
+				$CGI::values{mv_password} = $password;
+				my $profile = Vend::Util::read_cookie('MV_USERPROFILE');
+				local(%SIG);
+				undef $SIG{__DIE__};
+				eval {
+					Vend::UserDB::userdb('login', profile => $profile );
+				};
+				if($@) {
+					$Vend::Session->{failure} .= $@;
+				}
+			}
+		}
+	},
+	Locale => sub {
+#::logDebug("Doing Locale dispatch...");
+		my $locale = $::Scratch->{mv_locale}
+			or return;
+
+		if(! $::Scratch->{mv_language}) {
+			$Global::Variable->{LANG}
+					= $::Variable->{LANG}
+					= $::Scratch->{mv_language}
+					= $locale;
+		}
+
+		return unless defined $Vend::Cfg->{Locale_repository}{$locale};
+
+		Vend::Util::setlocale(  $locale,
+								($::Scratch->{mv_currency} || undef),
+								{ persist => 1 }
+							);
+	},
+	DiscountSpaces => sub {
+#::logDebug("Doing DiscountSpaces dispatch...");
+	   if ($CGI::values{$Vend::Cfg->{DiscountSpaceVar}}) {
+           $Vend::DiscountSpace = $CGI::values{$Vend::Cfg->{DiscountSpaceVar}};
+		   $::Discounts
+				= $Vend::Session->{discount}
+				= $Vend::Session->{discount_space}{$Vend::DiscountSpace}
+				||= {};
+        }
+    },
+);
 
 # Set up defaults for certain directives
 my $Have_set_global_defaults;
@@ -2398,7 +2481,7 @@
 	return 1;
 }
 
-my %Default = (
+%Default = (
 		## This rather extensive default setting is not typical for IC,
 		## but performance in pricing routines demands it
 		Options => sub {
@@ -2569,8 +2652,21 @@
 								$C->{Locale} = $repos->{$C->{LastLocale}};
 							}
 						}
+
+						push @Dispatches, 'Locale';
 						return 1;
 					},
+
+		DiscountSpaces => sub {
+					return 1 unless $C->{DiscountSpaces};
+					push @Dispatches, 'DiscountSpaces';
+					return 1;
+		},
+		CookieLogin => sub {
+					return 1 unless $C->{CookieLogin};
+					push @Dispatches, 'CookieLogin';
+					return 1;
+		},
 		ProductFiles => \&set_default_search,
 		VendRoot => sub {
 			my @paths = map { quotemeta $_ }
@@ -2581,6 +2677,11 @@
 			$C->{AllowedFileRegex} = qr{^($re)};
 			return 1;
 		},
+		Autoload => sub {
+			return 1 unless $C->{Autoload};
+			push @Dispatches, 'Autoload';
+			return 1;
+		},
 );
 
 sub set_global_defaults {
@@ -2601,6 +2702,7 @@
 }
 
 sub set_defaults {
+	@Dispatches = ();
 	for(keys %Default) {
 		my ($status, $error) = $Default{$_}->($C->{$_});
 		next if $status;
@@ -2611,6 +2713,10 @@
 					$error
 				)
 		);
+	}
+	@Dispatches = sort { $Dispatch_priority{$a} cmp $Dispatch_priority{$b} } @Dispatches;
+	for(@Dispatches) {
+		push @{ $C->{DispatchRoutines} ||= [] }, $Dispatch_code{$_};
 	}
 	$Have_set_global_defaults = 1;
 	return;








More information about the interchange-cvs mailing list