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

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Mon Aug 5 02:19:05 2002


User:      heins
Date:      2002-08-05 06:17:52 GMT
Modified:  lib/Vend Menu.pm
Log:
* Add the menu transforms and gates.

  ui_security       if-mm advanced on contents of a field for permission
  superuser         if field is set, user must be super
  full_interpolate  These fields get ITL interpolation before substitution,=
 slow
  expand_values     [var VARNAME] , [cgi var], [value var] work, fast
  menu_group        Entry shows if field empty or matches regex
  indicator         Sets {indicated} if variable name set or page matches
  depends_on        Needs this CGI var set to show
  exclude_on        Don't show if this CGI var set

  All take the form of:

  		gate_type=3Dmenu_field

  The contents of "gate_type" specify a menu field which is used to
  do the gating.

  These features implement the same capability as the current UI menus
  have with their custom code. So the typical entry for one of those will
  be:

  	[menu menu-type=3Dsimple
			superuser=3Dsuper
			depends_on=3Ddepends_on
			exclude_on=3Dexclude_on
			expand_values=3Dpage,form,name
			indicator=3Dindicator
	]

  This should provide very fast menuing. It is still quite possible
  to write your own custom menus with [loop ...] and such, but the
  idea is to make these adaptable enough with enough examples to
  mostly get rid of the old.

Revision  Changes    Path
2.2       +120 -7    interchange/lib/Vend/Menu.pm


rev 2.2, prev_rev 2.1
Index: Menu.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /var/cvs/interchange/lib/Vend/Menu.pm,v
retrieving revision 2.1
retrieving revision 2.2
diff -u -r2.1 -r2.2
--- Menu.pm	5 Aug 2002 04:05:40 -0000	2.1
+++ Menu.pm	5 Aug 2002 06:17:49 -0000	2.2
@@ -1,6 +1,6 @@
 # Vend::Menu - Interchange payment processing routines
 #
-# $Id: Menu.pm,v 2.1 2002/08/05 04:05:40 mheins Exp $
+# $Id: Menu.pm,v 2.2 2002/08/05 06:17:49 mheins Exp $
 #
 # Copyright (C) 2002 Mike Heins, <mike@perusion.net>
 #
@@ -21,11 +21,104 @@
=20
 package Vend::Menu;
=20
-$VERSION =3D substr(q$Revision: 2.1 $, 10);
+$VERSION =3D substr(q$Revision: 2.2 $, 10);
=20
 use Vend::Util;
 use strict;
=20
+my %transform =3D (
+	ui_security =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		my $status =3D 1;
+		for(@$fields) {
+			$status =3D $status && Vend::Tags->if_mm('advanced', $row->{$_});
+		}
+		return $status;
+	},
+	full_interpolate =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		for(@$fields) {
+			next unless $row->{$_} =3D~ /\[|__[A-Z]\w+__/;
+			$row->{$_} =3D Vend::Interpolate::interpolate_html($row->{$_});
+		}
+		return 1;
+	},
+	menu_group =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		my $status =3D 1;
+		eval {
+			for(@$fields) {
+				my($f, $c) =3D split /[=3D~]+/, $_;
+				$c ||=3D $f;
+				$status =3D $status && (
+								!  $row->{$f}
+								or $row->{$f} =3D~ /$CGI::values{$c}/i
+								);
+			}
+		};
+		return $status;
+	},
+	superuser =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		my $status =3D 1;
+		for(@$fields) {
+			$status =3D $status && (! $row->{$_} or Vend::Tags->if_mm('super'));
+		}
+		return $status;
+	},
+	depends_on =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		my $status =3D 1;
+		for(@$fields) {
+			$status =3D $status && $CGI::values{$row->{$_}};
+		}
+		return $status;
+	},
+	exclude_on =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		my $status =3D 1;
+		for(@$fields) {
+			$status =3D $status && (! $CGI::values{$row->{$_}});
+		}
+		return $status;
+	},
+	indicator =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		for(@$fields) {
+			my $indicator;
+			next unless $indicator =3D $row->{$_};
+			my $rev =3D $indicator =3D~ s/^!\s*// ? 1 : 0;
+			my $status =3D  defined $CGI::values{$indicator}
+						  ? $CGI::values{$indicator}
+						  : $::Values->{$indicator};
+			($row->{indicated} =3D 1, next)
+				if $rev xor $status;
+			$status =3D $Global::Variable->{MV_PAGE} eq $indicator;
+			($row->{indicated} =3D 1, next)
+				if $rev xor $status;
+		}
+		return 1;
+	},
+	expand_values =3D> sub {
+		my ($row, $fields) =3D @_;
+		return 1 if ref($fields) ne 'ARRAY';
+		for(@$fields) {
+			next unless $row->{$_} =3D~ /\[/;
+			$row->{$_} =3D~ s/\[cgi\s+([^\[]+)\]/$CGI::values{$1}/g;
+			$row->{$_} =3D~ s/\[var\s+([^\[]+)\]/$::Variable->{$1}/g;
+			$row->{$_} =3D~ s/\[value\s+([^\[]+)\]/$::Values->{$1}/g;
+		}
+		return 1;
+	},
+);
+
 sub old_tree {
 	my ($name, $opt, $template) =3D @_;
 	my @out;
@@ -91,18 +184,15 @@
 		if $opt->{header_template};
=20
 	my %defaults =3D (
-				lr			=3D> 1,
-				file        =3D> $opt->{file},
 				iterator    =3D> \&menu_link,
 				head_skip   =3D> 1,
-				body   =3D> $template,
 			);
=20
 	while( my ($k, $v) =3D each %defaults) {
 		next if defined $opt->{$k};
 		$opt->{$k} =3D $v;
 	}
-	push @out, Vend::Tags->loop($opt);
+	push @out, Vend::Tags->loop(undef,$opt,$template);
=20
 	push @out, Vend::Tags->uc_attr_list($opt, $opt->{footer_template})
 		if $opt->{footer_template};
@@ -334,6 +424,10 @@
 sub tree_link {
 	my ($template, $row, $opt) =3D @_;
=20
+	for(@{$opt->{_transform}}) {
+		return unless $transform{$_}->($row, $opt->{$_});
+	}
+
 	$template ||=3D qq[
 {MV_SPACER}{MV_CHILDREN?}<A href=3D"{TOGGLE_URL}" class=3D"{TOGGLE_CLASS}"=
 style=3D"{TOGGLE_STYLE}">{TOGGLE_ANCHOR}</A>{URL?}<A href=3D"{URL}" class=
=3D"{TOGGLE_CLASS}" style=3D"{TOGGLE_STYLE}">{/URL?}{NAME}{URL?}</a>{/URL?}=
{/MV_CHILDREN?}{MV_CHILDREN:}{TOGGLE_ANCHOR}{URL?}<A href=3D"{URL}" class=
=3D"{LINK_CLASS}" style=3D"{LINK_STYLE}">{/URL?}{NAME}{URL?}</a>{/URL?}{/MV=
_CHILDREN:}<br>
 ];
@@ -417,6 +511,11 @@
 	}
=20
 	$fields =3D $opt->{loopfields};
+
+	for(@{$opt->{_transform}}) {
+		return unless $transform{$_}->($row, $opt->{$_});
+	}
+
 	if($row->{page}) {
 		my $form =3D $row->{form};
 		if($form) {
@@ -462,9 +561,10 @@
 		$row =3D \%line;
 	}
=20
-	if($row->{mgroup} and $CGI::values{mgroup} !~ /\b$row->{mgroup}\b/) {
-		return;
+	for(@{$opt->{_transform}}) {
+		return unless $transform{$_}->($row, $opt->{$_});
 	}
+
 	return '<br>' unless $row->{name};
 	return $row->{name} if ! $row->{page} and $row->{name} =3D~ /^\s*</;
 	$row->{win}  =3D $::Scratch->{win};
@@ -479,6 +579,7 @@
=20=09
 	$opt->{dhtml_browser} =3D dhtml_browser()
 		unless defined $opt->{dhtml_browser};
+	$opt->{menu_type} ||=3D 'simple';
=20
 	my $prefix =3D $opt->{prefix} || 'menu';
 	$opt->{link_class} ||=3D $::Variable->{MV_DEFAULT_LINK_CLASS};
@@ -490,6 +591,15 @@
 		$opt->{footer_template} =3D $1;
 	}
=20
+	my @transform;
+	for(keys %transform) {
+		next unless $opt->{$_};
+		my @fields =3D grep /\S/, split /[\s,\0]+/, $opt->{$_};
+		$opt->{$_} =3D \@fields;
+		push @transform, $_;
+	}
+	$opt->{_transform} =3D \@transform;
+
 	if($opt->{menu_type} eq 'tree') {
 		$opt->{link_class_open}   ||=3D $opt->{link_class};
 		$opt->{link_class_closed} ||=3D $opt->{link_class};
@@ -527,7 +637,10 @@
 		return dhtml_tree(@_);
 	}
 	elsif($opt->{menu_type} eq 'simple') {
-		if(! $opt->{file}) {
+		if($opt->{search}) {
+			## Do nothing
+		}
+		elsif(! $opt->{file}) {
 			$opt->{file} =3D $::Variable->{MV_MENU_DIRECTORY} || 'include/menus';
 			if(! $opt->{name}) {
 				logError("No file or name specified for menu.");