[interchange-cvs] interchange - heins modified 2 files

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Mon May 5 10:55:01 2003


User:      heins
Date:      2003-05-05 14:54:30 GMT
Modified:  code/SystemTag tree.coretag
Modified:  lib/Vend Menu.pm
Log:
* Add ability for [tree ...] tag to use a file such as that generated
  by the UI's menu editor for its tree data. It uses the "code" field
  for the subordinate if not otherwise passed, and uses the "msort"
  field for the level determination.

  Basically scans the lines starting after the one in question and
  returns all until level is != the next highest level.

  A minimal tree file would be:

  msort	description
  0	    Level 0
  1		Level 1 subordinate of line=0
  2		Level 2 subordinate of line=1
  2		Level 2 subordinate of line=1
  1		Level 1 subordinate of line=0
  1		Level 1 subordinate of line=0
  0		Level 0, another entry
  1		Level 1 subordinate of line=6
  2		Level 2 subordinate of line=7
  1		Level 1 subordinate of line=6

* Add support in Menu.pm for file-based trees.

  -- If use-file=1 is passed, the file will be found if possible and
     used in preference to the database tree.

  -- If file=filename.ext is passed, will be used regardless.

* Remove spurious open= parameters from flyout menus.

Revision  Changes    Path
1.6       +70 -16    interchange/code/SystemTag/tree.coretag


rev 1.6, prev_rev 1.5
Index: tree.coretag
===================================================================
RCS file: /var/cvs/interchange/code/SystemTag/tree.coretag,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- tree.coretag	28 Mar 2003 19:39:33 -0000	1.5
+++ tree.coretag	5 May 2003 14:54:29 -0000	1.6
@@ -2,19 +2,44 @@
 UserTag tree                addAttr
 UserTag tree                attrAlias    sub subordinate
 UserTag tree                hasEndTag
-UserTag tree                Version   $Revision: 1.5 $
+UserTag tree                Version   $Revision: 1.6 $
 UserTag tree                Routine <<EOR
 sub {
 	my($table, $parent, $sub, $start_item, $opt, $text) = @_;
 
 #::logDebug("tree-list: received parent=$parent sub=$sub start=$start_item");
 
-	my $db = ::database_exists_ref($table)
-		or return error_opt($opt, "Database %s doesn't exist", $table);
-	$db->column_exists($parent)
-		or return error_opt($opt, "Parent column %s doesn't exist", $parent);
-	$db->column_exists($sub)
-		or return error_opt($opt, "Subordinate column %s doesn't exist", $sub);
+	my $nodb;
+	my @passed;
+	my @start;
+	if($opt->{file}) {
+		my $delim = $opt->{delimiter} || "\t";
+		my $s = $opt->{subordinate} || 'code';
+		my $l = $opt->{level_field} || 'msort';
+		$delim = qr/$delim/;
+		my @lines = split /\n/, readfile($opt->{file});
+		my $hdr = shift @lines;
+		my @fields = split $delim, $hdr;
+		my $i = 1;
+		for(@lines) {
+			my $ref = {};
+			@{$ref}{@fields} = split $delim, $_;
+			$ref->{$s} = $i++;
+			push @passed, $ref;
+			push @start, $ref if $ref->{$l} == 0;
+		}
+		$nodb = 1;
+	}
+	my $db;
+	
+	unless($nodb) {
+		$db = ::database_exists_ref($table)
+			or return error_opt($opt, "Database %s doesn't exist", $table);
+		$db->column_exists($parent)
+			or return error_opt($opt, "Parent column %s doesn't exist", $parent);
+		$db->column_exists($sub)
+			or return error_opt($opt, "Subordinate column %s doesn't exist", $sub);
+	}
 
 	my $basewhere;
 
@@ -28,8 +53,10 @@
 			@keys = $start_item;
 		}
 
-		for(@keys) {
-			push @things, "$parent = " . $db->quote($_, $parent);
+		unless($nodb) {
+			for(@keys) {
+				push @things, "$parent = " . $db->quote($_, $parent);
+			}
 		}
 		$basewhere = join " OR ", @things;
 	}
@@ -42,7 +69,8 @@
 	}
 
 	my $mult = ( int($opt->{spacing}) || 10 );
-	my $keyfield = $db->config('KEY');
+	my $keyfield;
+	$keyfield = $db->config('KEY') unless $nodb;
 	$opt->{code_field} = $keyfield if ! $opt->{code_field};
 
 	my $sort = '';
@@ -63,8 +91,10 @@
 	}
 
 	my $where = '';
-	if( my $f = $db->config('HIDE_FIELD')) {
-		$where .= " AND $f != 1";
+	unless($nodb) {
+		if( my $f = $db->config('HIDE_FIELD')) {
+			$where .= " AND $f != 1";
+		}
 	}
 
 	if($opt->{where}) {
@@ -74,10 +104,16 @@
 	my $qb = "SELECT * FROM $table WHERE $basewhere$where$sort";
 #::logDebug("tree tag initial query=$qb");
 
-	my $ary = $db->query( {
+	my $ary;
+	if($nodb) {
+		$ary = \@start;
+	}
+	else {
+		$ary = $db->query( {
 							hashref => 1,
 							sql => $qb,
 							});
+	}
 	
 	my $memo;
 	if( $opt->{memo} ) {
@@ -103,9 +139,27 @@
 	my $qsub;
 
 	my $donemsg;
-	my $dbh = $db->dbh();
+	my $dbh;
+	$dbh = $db->dbh() unless $nodb;
+
 	my $qs_query = "SELECT * FROM $table WHERE $parent = ?$where$sort";
-	if($dbh and $db->config('Class') eq 'DBI') {
+	if($nodb) {
+		my $l = $opt->{level_field} || 'msort';
+#::logDebug("setting up nodb qsub level=$l");
+		$qsub = sub {
+			my $key = shift;
+#::logDebug("Looking for key=$key");
+			return if $key < 1;
+			my $base = $passed[$key - 1]->{$l} + 1;
+#::logDebug("Base level=$base, firstone = $passed[$key]{$l}");
+			my @out;
+			for(my $i = $key; $passed[$i]{$l} == $base ; $i++ ) {
+				push @out, $passed[$i];
+			}
+			return \@out;
+		};
+	}
+	elsif($dbh and $db->config('Class') eq 'DBI') {
 		my $sth = $dbh->prepare($qs_query)
 				or die errmsg(
 						"tree failed to prepare query: %s\nError was: %s",
@@ -204,7 +258,7 @@
 
 			my $a;
 			if ($opt->{autodetect} or ! $stop) {
-#::logDebug("next row query=$q");
+#::logDebug("next=$next row query=$q");
 				$a = $qsub->($next);
 				$above->{$next} = 1 if $a and scalar @{$a};
 			}



2.27      +31 -7     interchange/lib/Vend/Menu.pm


rev 2.27, prev_rev 2.26
Index: Menu.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Menu.pm,v
retrieving revision 2.26
retrieving revision 2.27
diff -u -r2.26 -r2.27
--- Menu.pm	29 Apr 2003 15:49:22 -0000	2.26
+++ Menu.pm	5 May 2003 14:54:29 -0000	2.27
@@ -1,6 +1,6 @@
 # Vend::Menu - Interchange menu processing routines
 #
-# $Id: Menu.pm,v 2.26 2003/04/29 15:49:22 mheins Exp $
+# $Id: Menu.pm,v 2.27 2003/05/05 14:54:29 mheins Exp $
 #
 # Copyright (C) 2002 Mike Heins, <mike@perusion.net>
 #
@@ -21,7 +21,7 @@
 
 package Vend::Menu;
 
-$VERSION = substr(q$Revision: 2.26 $, 10);
+$VERSION = substr(q$Revision: 2.27 $, 10);
 
 use Vend::Util;
 use strict;
@@ -543,10 +543,12 @@
 
 	my %o = (
 			start       => $opt->{tree_selector} || $opt->{name},
+			file		=> $opt->{file},
 			table       => $opt->{table} || $::Variable->{MV_TREE_TABLE} || 'tree',
 			master      => 'parent_fld',
 			subordinate => 'code',
 			autodetect  => '1',
+			no_open		=> 1,
 			js_prefix	=> $vpf,
 			sort        => $opt->{sort} || 'code',
 			full        => '1',
@@ -896,6 +898,7 @@
 			start       => $opt->{tree_selector} || 'Products',
 			table       => $opt->{table} || $::Variable->{MV_TREE_TABLE} || 'tree',
 			master      => 'parent_fld',
+			file		=> $opt->{file},
 			subordinate => 'code',
 			autodetect  => '1',
 			sort        => $opt->{sort} || 'code',
@@ -1355,11 +1358,13 @@
 								no_session_id => $opt->{timed}
 							});
 
-		if($row->{page} =~ m{\?.+=}) {
-			$row->{page} .= $Global::UrlJoiner . 'open=';
-		}
-		else {
-			$row->{page} .= '?open=';
+		unless($opt->{no_open}) {
+			if($row->{page} =~ m{\?.+=}) {
+				$row->{page} .= $Global::UrlJoiner . 'open=';
+			}
+			else {
+				$row->{page} .= '?open=';
+			}
 		}
 	}
 
@@ -1567,6 +1572,16 @@
 							});
 		}
 
+		if($opt->{use_file}) {
+			$opt->{file} = $::Variable->{MV_MENU_DIRECTORY} || 'include/menus';
+			if(! $opt->{name}) {
+				logError("No file or name specified for menu.");
+			}
+			my $nm = escape_chars($opt->{name});
+			$opt->{file} .= "/$nm.txt";
+			undef $opt->{file} unless -f $opt->{file};
+		}
+
 		return old_tree($name,$opt,$template) unless $opt->{dhtml_browser};
 		return dhtml_tree($name,$opt,$template);
 	}
@@ -1601,6 +1616,15 @@
 							border => 0,
 							extra => $opt->{img_clear_extra},
 							});
+		}
+		if($opt->{use_file}) {
+			$opt->{file} = $::Variable->{MV_MENU_DIRECTORY} || 'include/menus';
+			if(! $opt->{name}) {
+				logError("No file or name specified for menu.");
+			}
+			my $nm = escape_chars($opt->{name});
+			$opt->{file} .= "/$nm.txt";
+			undef $opt->{file} unless -f $opt->{file};
 		}
 
 		return old_flyout($name,$opt,$template) unless $opt->{dhtml_browser};