[interchange-cvs] interchange - heins modified lib/Vend/Form.pm

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sat Feb 8 15:09:01 2003


User:      heins
Date:      2003-02-08 20:08:11 GMT
Modified:  lib/Vend Form.pm
Log:
* Allow templated options in select, radio, checkbox, etc. Enabled
  by passing option_template="ATTR Template".

  Uses same attr_list methodology as in most IC secondary templating.

  Active keys:

  	  LABEL           The normal label value
  	  VALUE           The normal value
	  PRICE           The formatted price, converted for locale
	  PRICE_NOFORMAT  The raw price data
	  ABSOLUTE        The formatted price, absolute (no negative)
	  NEGATIVE        Set true if price data is negative

  The default is equivalent to (though it doesn't use formatting):

  		{LABEL} {PRICE?}({PRICE}){/PRICE?}

  This example:

	{LABEL}
		{PRICE?}
			{NEGATIVE?}(subtract {ABSOLUTE}){/NEGATIVE?}
			{NEGATIVE:}(add {PRICE}){/NEGATIVE:}
		{/PRICE?}

	Would turn:

		Ebony handle ($20.00)
		Wood handle
		Plastic handle ($-5.00)

    into:

		Ebony handle (add $20.00)
		Wood handle
		Plastic handle (subtract $5.00)

Revision  Changes    Path
2.25      +34 -13    interchange/lib/Vend/Form.pm


rev 2.25, prev_rev 2.24
Index: Form.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Form.pm,v
retrieving revision 2.24
retrieving revision 2.25
diff -u -r2.24 -r2.25
--- Form.pm	1 Feb 2003 21:45:16 -0000	2.24
+++ Form.pm	8 Feb 2003 20:08:11 -0000	2.25
@@ -1,6 +1,6 @@
 # Vend::Form - Generate Form widgets
 # 
-# $Id: Form.pm,v 2.24 2003/02/01 21:45:16 mheins Exp $
+# $Id: Form.pm,v 2.25 2003/02/08 20:08:11 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -37,7 +37,7 @@
 require Exporter;
 @ISA = qw(Exporter);
 
-$VERSION = substr(q$Revision: 2.24 $, 10);
+$VERSION = substr(q$Revision: 2.25 $, 10);
 
 @EXPORT = qw (
 	display
@@ -199,8 +199,8 @@
 	$body =~ s!\{([A-Z_]+)\}!$hash->{lc $1}!g;
 	$body =~ s!\{([A-Z_]+)\|($Some)\}!$hash->{lc $1} || $2!eg;
 	$body =~ s!\{([A-Z_]+)\s+($Some)\}! $hash->{lc $1} ? $2 : ''!eg;
-	$body =~ s!\{([A-Z_]+)\?\}($Some){/\1\?\}! $hash->{lc $1} ? $2 : ''!eg;
-	$body =~ s!\{([A-Z_]+)\:\}($Some){/\1\:\}! $hash->{lc $1} ? '' : $2!eg;
+	1 while $body =~ s!\{([A-Z_]+)\?\}($Some){/\1\?\}! $hash->{lc $1} ? $2 : ''!eg;
+	1 while $body =~ s!\{([A-Z_]+)\:\}($Some){/\1\:\}! $hash->{lc $1} ? '' : $2!eg;
 	return $body;
 }
 
@@ -665,9 +665,13 @@
 			$select = '';
 		}
 
-		my $extra;
-		if($price->{$value}) {
-			$extra = currency($price->{$value}, undef, 1);
+		my $extra = '';
+		my $attr = {};
+		if(my $p = $price->{$value}) {
+			$attr->{negative} = $p < 0 ? 1 : 0;
+			$attr->{price_noformat} = $p;
+			$attr->{absolute} = currency(abs($p), undef, 1);
+			$attr->{price} = $extra = currency($p, undef, 1);
 			$extra = " ($extra)";
 		}
 
@@ -680,13 +684,19 @@
 		}
 		$run .= ' SELECTED' if $select;
 		$run .= '>';
-		if($label) {
+		if($opt->{option_template}) {
+			$attr->{label} = $label || $value;
+			$attr->{value} = $value;
+			$run .= attr_list($opt->{option_template}, $attr);
+		}
+		elsif($label) {
 			$run .= $limit->($label);
+			$run .= $extra;
 		}
 		else {
 			$run .= $limit->($value);
+			$run .= $extra;
 		}
-		$run .= $extra if $extra;
 	}
 	$run .= "</optgroup>" if $optgroup_one++;
 	$run .= attr_list($Template{selecttail}, $opt);
@@ -820,8 +830,13 @@
 		$opt->{selected} = '' if defined $opt->{value};
 
 		my $extra;
-		if($price->{$value}) {
-			$label .= "&nbsp;(" . currency($price->{$value}, undef, 1) . ")";
+		my $attr = { label => $label };
+		if(my $p = $price->{$value}) {
+			$attr->{negative} = $p < 0 ? 1 : 0;
+			$attr->{price_noformat} = $p;
+			$attr->{absolute} = currency(abs($p), undef, 1);
+			$attr->{price} = $extra = currency($p, undef, 1);
+			$label .= "&nbsp;($attr->{price})";
 		}
 
 		$value eq ''
@@ -838,8 +853,14 @@
 
 		$opt->{tvalue} = encode($value, $ESCAPE_CHARS::std);
 
-		$label =~ s/ /&nbsp;/g if $xlt;
-		$opt->{tlabel} = $label;
+		if($opt->{option_template}) {
+			$opt->{tlabel} = attr_list($opt->{option_template}, $attr);
+			$opt->{tlabel} =~ s/ /&nbsp;/g if $xlt;
+		}
+		else {
+			$label =~ s/ /&nbsp;/g if $xlt;
+			$opt->{tlabel} = $label;
+		}
 
 		$run .= attr_list($template, $opt);
 		$run .= '</TR>' if $inc && ! ($i % $inc);