1 # Copyright 2002-2007 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: menu_load.coretag,v 1.9 2007-03-30 23:40:54 pajamian Exp $
10 UserTag menu-load Order type
11 UserTag menu-load addAttr
12 UserTag menu-load Version $Revision: 1.9 $
13 UserTag menu-load Routine <<EOR
15 my ($row, $nrow) = @_;
16 #Debug("row link_type='$row->{link_type}'");
17 if($row->{link_type} eq 'external') {
22 $nrow->{page} = $first;
24 elsif ($row->{link_type} eq 'internal') {
25 my ($page, $form) = split /\s+/, $row->{url}, 2;
26 $nrow->{page} = $page;
27 $nrow->{form} = $form;
29 elsif ($row->{link_type} eq 'simple') {
30 my (@items) = split /\s*[\n,]\s*/, $row->{selector};
33 my $sp = $row->{page};
35 $nrow->{page} = 'search';
36 push @out, "fi=$fi" if $fi;
37 push @out, "sp=$sp" if $sp;
42 $nrow->{form} = join "&", @out;
47 my ($col, $string) = split /\s*=\s*/, $_, 2;
49 push @out, "se=$string";
51 push @out, $row->{search}
52 if $row->{search} =~ /^\s*\w\w=/;
54 push @out, qq{va=banner_image=$row->{banner_image}}
55 if $row->{banner_image};
56 push @out, qq{va=banner_text=$row->{banner_text}}
57 if $row->{banner_text};
59 s/(.*?=)(.*)/$1 . Vend::Util::hexify($2)/ges;
61 $arg = join $Global::UrlJoiner, @out;
65 elsif ($row->{link_type} eq 'complex') {
66 $nrow->{page} = 'search';
67 $row->{search} =~ s/[\r\n+]/\n/g;
68 $row->{search} .= qq{\nva=banner_text=$row->{banner_text}}
69 if $row->{banner_text};
70 $row->{search} .= qq{\nva=banner_image=$row->{banner_image}}
71 if $row->{banner_image};
72 my @items = grep /\S/, split /[\r\n]+/, $row->{search};
74 s/(.*?=)(.*)/$1 . Vend::Util::hexify($2)/ges;
76 $nrow->{form} = join $Global::UrlJoiner, @items;
77 $nrow->{form} =~ s/[\r\n]+/&/g;
83 my ($type, $opt) = @_;
84 #::logDebug("Called menu_load");
85 $type ||= $opt->{type} || 'tree';
88 if($opt->{menu_fields}) {
89 @menufields = grep /\S/, split /[\s,\0]+/, $opt->{menu_fields};
93 code mgroup msort next_line indicator exclude_on depends_on page
94 form name super inactive description help_name img_dn img_up
95 img_sel img_icon url member
107 if ($type eq 'tree') {
108 $opt->{table} ||= 'products';
109 $opt->{first_field} ||= 'prod_group';
110 $opt->{second_field} ||= 'category';
111 $opt->{desc_field} ||= $opt->{description_field} || 'description';
112 #::logDebug("menu_load options=" . uneval($opt));
114 my $tab = $opt->{table};
115 my $db = database_exists_ref($tab)
117 Vend::Tags->error({ set => errmsg(
118 "Failed to open %s table %s.",
125 my $tname = $db->name();
126 #::logDebug("LARGE=" . $db->config('LARGE'));
127 $opt->{key_field} ||= $db->config('KEY');
128 if(! $opt->{even_large} and $db->config('LARGE')) {
129 Vend::Tags->error({ set => errmsg(
130 "%s database %s for tree write: %s",
133 'too large, must override',
138 my @somefields = qw/mgroup page name description/;
142 $opt->{second_field},
145 my $sfields = join ",", @fields;
146 my $tfields = $opt->{sort_fields} || join ",", @fields[1..$#fields];
147 my $q = qq{SELECT $sfields FROM $tname ORDER BY $tfields};
148 my $ary = $db->query($q)
152 "No results from %s table %s.",
161 @out = join "\t", @menufields;
163 my $base_search = "scan/co=yes/fi=$tab";
166 my($sku, $area, $cat, $desc) = @$_;
167 for( \$sku, \$area, \$cat, \$desc) {
170 if($area ne $prev_area) {
175 "sf=$opt->{first_field}",
178 "tf=$opt->{second_field},$opt->{desc_field}",
188 if($cat ne $prev_cat) {
192 "sf=$opt->{first_field}",
195 "sf=$opt->{second_field}",
198 "tf=$opt->{desc_field}",
215 } unless $opt->{no_leaves};
219 #::logDebug("pushing out --> " . $_->{name});
220 push @out, join "\t", @{$_}{@menufields};
224 elsif ($type eq 'category_file') {
225 $opt->{table} ||= 'category';
226 $opt->{first_field} ||= 'prod_group';
227 $opt->{second_field} ||= 'category';
228 #::logDebug("menu_load options=" . uneval($opt));
230 my $tab = $opt->{table};
231 my $db = database_exists_ref($tab)
233 Vend::Tags->error({ set => errmsg(
234 "Failed to open %s table %s.",
241 my $tname = $db->name();
242 #::logDebug("LARGE=" . $db->config('LARGE'));
243 $opt->{key_field} ||= $db->config('KEY');
244 $opt->{sku_field} ||= 'sku';
246 unless ( $db->column_exists($opt->{sku_field}) ) {
247 Vend::Tags->error({ set => errmsg(
248 "%s database %s for tree write: %s",
251 "sku field $opt->{key_field} does not exist",
258 my @somefields = qw/mgroup page name description/;
262 $opt->{second_field},
264 push @fields, $opt->{desc_field} if $opt->{desc_field};
266 my $sfields = join ",", @fields;
267 my $tfields = $opt->{sort_fields};
269 $tfields = "$opt->{first_field},$opt->{second_field}";
270 $tfields .= ",$opt->{desc_field}" if $opt->{desc_field};
273 my $q = qq{SELECT $sfields FROM $tname ORDER BY $tfields};
274 #::logDebug("category_file menu_load query=$q");
275 my $ary = $db->query($q)
279 "No results from %s table %s.",
288 @out = join "\t", @menufields;
290 my $base_search = "scan/co=yes/fi=$tab/rf=$opt->{sku_field}";
291 $base_search .= "/tf=$opt->{desc_field}" if $opt->{desc_field};
294 my($sku, $area, $cat, $desc) = @$_;
298 if($area ne $prev_area) {
303 "sf=$opt->{first_field}",
306 "tf=$opt->{second_field}",
316 if($cat ne $prev_cat) {
320 "sf=$opt->{first_field}",
323 "sf=$opt->{second_field}",
339 #::logDebug("pushing out --> " . $_->{name});
340 push @out, join "\t", @{$_}{@menufields};
344 elsif ($type eq 'comb_category') {
345 $opt->{table} ||= 'products';
346 $opt->{comb_field} ||= 'comb_category';
347 $opt->{sort_string} ||= "tf=$opt->{comb_field},$Vend::Cfg->{DescriptionField}";
348 $opt->{sort_order} ||= $opt->{comb_field};
352 my $tab = $opt->{table};
353 my $comb_field = $opt->{comb_field};
356 $Tag->error({ set => errmsg(
357 "Failed to open %s table %s.",
365 #Debug("LARGE=" . $db->config('LARGE'));
366 if(! $opt->{even_large} and $db->config('LARGE')) {
367 $Tag->error({ set => errmsg(
368 "%s database %s for tree write: %s",
371 'too large, must override',
376 my @somefields = qw/mgroup page name description/;
382 my $ary = $db->query($q)
386 "No results from %s table %s.",
393 @out = join "\t", @menufields;
395 my @base_search = ( "bs=1",
404 $seen{$_->[0]}++ for @$ary;
405 for(sort keys %seen) {
406 my $comb_category = $_;
407 $comb_category =~ s/\s+$//;
409 my @parts = split /:/, $comb_category;
411 for( my $i = 0; $i < @parts; $i++) {
412 my $level = $levels[$i] ||= {};
413 my $name = $parts[$i];
414 my $comb = join ":", @parts[0 .. $i];
415 if(! $level->{$name}) {
418 my $searchterm = "se=";
419 $searchterm .= $Tag->filter('urlencode',$comb);
440 #Debug("pushing out --> " . $_->{name});
441 push @out, join "\t", @{$_}{@menufields};
443 #return join("<br>",@out);
446 elsif ($type eq 'cat_menu') {
448 my $tab = $opt->{table} || 'area';
449 my $ctab = $opt->{cat_table} || 'cat';
450 my $db = database_exists_ref($tab)
452 Vend::Tags->error({ set => errmsg(
453 "Failed to open %s table %s.",
460 #Debug("LARGE=" . $db->config('LARGE'));
461 my $q = qq{ SELECT * FROM $tab};
462 $q .= qq{ WHERE sel = '$opt->{sel}'}
464 $q .= qq{ ORDER BY sort };
465 my $ary = $db->query({ sql => $q, hashref => 1 } )
469 "No results from %s table %s.",
477 @out = join "\t", @menufields;
481 my $cdb = database_exists_ref($ctab)
485 "No results from %s table %s.",
492 my $ctabname = $cdb->name();
493 foreach my $row (@$ary) {
494 my $code = $row->{code};
497 name => $row->{name},
498 img_icon => $row->{image},
500 mgroup => $row->{set_selector},
502 old_link($row, $nrow);
504 SELECT * FROM $ctabname
506 OR sel like '$code %'
507 OR sel like '% $code'
508 OR sel like '% $code %'
511 #Debug("subquery=$sq");
513 my $sary = $cdb->query({ sql => $sq, hashref => 1 });
514 #Debug("subquery returned: " . uneval($sary));
515 for my $crow (@$sary) {
518 name => $crow->{name},
519 img_icon => $crow->{image},
521 mgroup => $crow->{sel},
523 old_link($crow, $nsub);
528 #Debug("pushing out --> " . $_->{name});
529 push @out, join "\t", @{$_}{@menufields};
530 #Debug("pushing out --> row=" . uneval($_));
534 elsif($type eq 'html') {
536 my $text = $opt->{html};
538 @out = join "\t", @menufields;
539 while($text =~ s{<a(\s+.*?)</a>}{}is) {
542 $blob =~ m{^[^>]*\s+title=(['"]?)(.*?)\1}
544 $blob =~ s{^.*?\shref\s*=\s*(["'])?(.*?)\1}{}is
548 1 while $blob =~ s{<.*?>}{};
552 my($href, $parms) = split /\?/, $link, 2;
559 description => $desc,
562 push @out, join "\t", @record{@menufields};
566 return '' unless @out;
567 return join "\n", @out, '';