[ic] Foundation - auto prod_group and category fine tune.

Mike Heins interchange-users@interchange.redhat.com
Wed Oct 31 13:37:01 2001


--zYM0uCDKw75PZbzx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Quoting music@labyrinth.net.au (music@labyrinth.net.au):
> Quoting mike:
> > By default, the link type is "simple", which is a simple search.
> > You can put "prod_group=X, category=Y" in the "selector" field of
> > the cat table and get that to happen.
> >
> > In fact, I think the 4.9 devel CVS has a pages/admin/layout_auto.html
> > file which does that for you automatically. That may even work
> > in 4.8.
> 
> Thanks! It certainly does work as far as creating the different search
> queries for each link.
> 
> Unfortunately, as is also the case with the 4.8.2 version, it does not auto
> generate a complete category list if the products.txt file has a duplicate
> category name within a prod_group:
> 
> prod_group    category
> Dog    food
> Dog    collar
> Cat    food
> Cat     flea powder
> 

Thanks for pointing that out. I was not updating the %cat_done differently
based on the "narrow" cat_type.

I have attached an (untested) version of that page that might do it
automatically.

-- 
Red Hat, Inc., 3005 Nichols Rd., Hamilton, OH  45013
phone +1.513.523.7621      <mheins@redhat.com>

Fast, reliable, cheap.  Pick two and we'll talk.  -- unknown

--zYM0uCDKw75PZbzx
Content-Type: text/html; charset=us-ascii
Content-Disposition: attachment; filename="layout_auto.html"

[set page_title]Layout auto-populate[/set]
[set ui_class]Design[/set]
[set page_banner]Layout Editor: Organize your site into categories[/set]
[set page_perm]layout=e[/set]
[set help_name]layout.edit[/set]
[set icon_name]icon_pages.gif[/set]
[tmp meta_header]
	[include include/table_populator]
	<SCRIPT LANGUAGE=JavaScript>
	  function change_ta (ta_field, selector) {
	  	if(selector == 'complex') {
			ta_field.rows = 20;
			ta_field.disabled = false;
		}
		else {
			ta_field.rows = 1;
			ta_field.disabled = true;
		}
		return;
	  }
	</SCRIPT>
[/tmp]
[seti ui_body_extra][/seti]

@_UI_STD_HEAD_@
<!-- ----- BEGIN REAL STUFF ----- -->

<form action="[area __UI_BASE__/layout]" method=GET>
<input type=hidden name=mv_action value=back>

<table border=0 __UI_T_PROPERTIES__>
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>

<tr bgcolor="__UI_C_INTBLOCK__">
<td width=25%>

1. Choose a table as the source:

</td>
<td>
[if !value area_table]
	[value name=area_table set=products hide=1]
	[value name=area_column set=prod_group hide=1]
	[value name=cat_column set=category hide=1]
[/if]
<select name="area_table"
	onChange="
		populateTable(
            this.form.area_table.options,
            this.form.area_column.options,
            this.form.area_table.options[this.form.area_table.selectedIndex].value
            );
		populateTable(
            this.form.area_table.options,
            this.form.cat_column.options,
            this.form.area_table.options[this.form.area_table.selectedIndex].value
            );
	"
size=1>
[loop list="[list-databases]" option=area_table]
	<OPTION VALUE="[loop-code]">[loop-code]</OPTION>
[/loop]
</select>
</td>
</tr>
<tr>
<td>
2. Choose a column to populate areas:
</td>
<td>
<select name="area_column">
<option value=""> -- none --
[loop list="[db-columns table='[value area_table]']" option=area_column]
<option>[loop-code]
[/loop]
</select>
</td>
</tr>
<tr>
<td>
3. Choose a column to populate categories:
</td>
<td>
<select name="cat_column">
<option value=""> -- none --
[loop list="[db-columns table='[value area_table]']" option=cat_column]
<option>[loop-code]
[/loop]
</select>
</td>
</tr>
<tr>
<td>
4. Choose a search type:
</td>
<td>
<select name="cat_type" onChange="change_ta(this.form.search_code,this.form.cat_type.value)">
<option value="narrow"> Narrow search (area and category match)
<option value="simple"> Simple search (only category matches)
<option value="complex"> Complex search (template substitution on below)
</select>
</td>
</tr>
<tr>
<td valign=top>
5. Complex search code:
</td>
<td>
NOTE: Not active unless complex search selected above.<br>
<textarea rows=1 cols=50 name=search_code disabled=true>
	fi=products
	st=db
	co=yes

	sf=prod_group
	se=~PROD_GROUP~
	op=eq

	sf=category
	se=~CATEGORY~
	op=eq

	sf=inactive
	se=1
	op=ne

	rf=sku

	va=banner_text=~BANNER_TEXT~
</textarea>
</td>
</tr>
<tr bgcolor="__UI_C_INTBLOCK__">
<td>
6. Select the page class:
</td>
<td>
<INPUT NAME=which_page VALUE=all>
</td>
</tr>
<tr bgcolor="__UI_C_INTBLOCK__">
<td>
7. Select the page area:
</td>
<td>
<INPUT NAME=sel VALUE=left>
</td>
</tr>
<tr bgcolor="__UI_C_INTBLOCK__">
<td>
8. Decide whether to remove previous settings:
</td>
<td>
<SELECT NAME=nuke><OPTION VALUE=0>No <OPTION VALUE=1>Yes</SELECT>
</td>
</tr>
<tr>
<td>
9. Click this button to populate:
</td>
<td>
[button text="Populate"]
[flag type=write table="area"]
[flag type=write table="cat"]
[seti ui_failure]
[perl tables="area cat [cgi area_table]"]
	my $go_page = $CGI->{mv_nextpage};
	$CGI->{mv_nextpage} = '@@MV_PAGE@@';
	my $ptab = $CGI->{area_table}
		or return "no area source table";
	my $pdb = $Db{$ptab}
		or return "area source table bad";
	my $adb = $Db{area}
		or return "no area table";
	my $cdb = $Db{cat}
		or return "no cat table";
	my $acol = $CGI->{area_column};
	my $ccol = $CGI->{cat_column};
	if(! $acol and ! $ccol ) {
		return "no column to populate either";
	}

	my $selector = '';
	my $selcode = '';

	my %area_defaults = (
		name => undef,
		which_page	=> $CGI->{which_page} || 'all',
		sort	=> '05',
		sel	=> $CGI->{sel} || 'left',
		display_type => 'name',
	);
	my %catmap;
	my $narrow;
	my $lt;
	my $search_code;
	if($CGI->{cat_type} eq 'simple') {
		$lt = 'simple';
		$selector = 'category=~CATEGORY~';
		$search_code = '';
	}
	elsif($CGI->{cat_type} eq 'narrow') {
		$narrow = 1;
		$lt = 'simple';
		$selector = <<EOF;
category=~CATEGORY~
prod_group=~PROD_GROUP~
EOF
		$search_code = '';
	}
	else {
		$lt = 'complex';
		$selector = '';
		$search_code = $CGI->{search_code};
	}

	my %cat_defaults = (
		link_type => $lt,
		selector => $selector,
		search => $search_code,
		sort	=> '05',
		display_type => 'name'
	);

	if($CGI->{nuke}) {
		$adb->query('delete from area');
		$cdb->query('delete from cat');
	}
	if($acol) {
		$adb->config('AUTO_NUMBER', '1000') if ! $adb->config('_Auto_number');
		return "area column $acol doesn't exist"
			unless $pdb->column_exists($acol);
		my $ary = $pdb->query("select distinct $acol from $ptab order by $acol")
			or return "area column query failed";
		for(@$ary) {
			my ($area) = (@$_);
			my $key = $adb->set_row('');
			return "no autonumbering available" if ! $key;
			$catmap{$area} = $key;
			for(keys %area_defaults) {
				$adb->set_field($key, $_, $area_defaults{$_} || $area);
			}
		}
	}

	my %catdone;
	if($ccol) {
		$cdb->config('AUTO_NUMBER', '1000') if ! $cdb->config('_Auto_number');
		return "cat column $ccol doesn't exist"
			unless $pdb->column_exists($ccol);
		my ($ary, $fh) = $pdb->query(
								"select $acol, $ccol from $ptab order by $ccol",
								)
			or return "cat column query failed";
		foreach my $row (@$ary) {
			my $area = $row->[$fh->{$acol}];
			my $cat  = $row->[$fh->{$ccol}];
			my $donetag = $narrow ? "$area:$cat" : $cat;
			next if $catdone{$donetag}++;
			my $key = $cdb->set_row('');
			return "no cat autonumbering available" if ! $key;

			my %defaults = %cat_defaults;
			$defaults{name} = $cat;
			$defaults{sel} = $catmap{$area};
			$defaults{search} =~ s/~ (\w+) ~/$row->[$fh->{lc $1}]/gx;
			$defaults{selector} =~ s/~ (\w+) ~/$row->[$fh->{lc $1}]/gx;
			
			for(keys %defaults) {
				Log("setting col=$_ key=$key = $defaults{$_}");
				$cdb->set_field($key, $_, $defaults{$_});
			}
		}
	}
	$CGI->{mv_nextpage} = $go_page;
	my $out = $Tag->uneval( { ref => \%catmap } );
	$out =~ s/,/,\n/g;
	#return '<XMP>' . $Tag->uneval( { ref => \%catmap } ) . "</XMP";
	return;
[/perl]
[/seti]
[/button]
<BR>
</td>
</tr>

<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>

</table>

</form>


<!-- ----- END REAL STUFF ----- -->
@_UI_STD_FOOTER_@
<!-- page: @@MV_PAGE@@ -->







--zYM0uCDKw75PZbzx--