[if cgi ui_clone_options]
[and cgi ui_clone_id]
[perl interpolate=1 tables="[cgi mv_data_table]"]
	my $db = $Db{[cgi mv_data_table]}
		or return;
	my ($k,$v);
	$db->clone_row($CGI->{ui_clone_id}, $CGI->{sku});
	$db->clone_set('o_master', $CGI->{ui_clone_id}, $CGI->{sku});
	return;
[/perl]
[/if]

[if cgi diagnose]
[tmp testing][tree 
				table="[cgi mv_data_table]"
				pedantic=1
				show_error=1
				full=1
				master=o_master
				sub=sku
				start="[cgi item_id]"][/tree][/tmp]
	[if scratch testing]
	<BLOCKQUOTE class=cmessage>
		[scratch testing]
	</BLOCKQUOTE>
	[else]
	<BLOCKQUOTE class=cmessage>
		Tree appears OK.
	</BLOCKQUOTE>
	[/else]
	[/if]
[/if]

[page href="@@MV_PAGE@@"
		form="
			collapse=1
			sku=[cgi item_id]
	"]Collapse all</A>&nbsp;&nbsp;&nbsp;[page href="@@MV_PAGE@@"
		form="
			explode=1
			sku=[cgi item_id]
	"]Explode all</A>&nbsp;&nbsp;&nbsp;[page href="@@MV_PAGE@@"
		form="
			diagnose=1
			sku=[cgi item_id]
	"]Check integrity</A>

<TABLE CELLSPACING=0 CELLPADDING=2 CELLMARGIN=3 WIDTH="100%">
<TR class=rmarq>
	<TH ALIGN=LEFT>SKU</TH>
	<TH ALIGN=LEFT>Master</TH>
	<TH ALIGN=LEFT>Group</TH>
	<TH ALIGN=LEFT>Description</TH>
	<TH ALIGN=RIGHTPrice</TH>
</TR>
[seti mod_template]
<TR>
<TD>&nbsp;{ui_spacer}{ui_action?}[page href="@@MV_PAGE@@"
							form="
								mv_data_table=[cgi mv_data_table]
								sku=[cgi item_id]
								UI_ACTION=
							"]{/ui_action?}{ui_action_img}{ui_action </A>}&nbsp;[page
				href=__UI_BASE__/flex_editor
				form=|
					mv_data_table=[cgi mv_data_table]
					ui_class=Items
					ui_hide_key=1
					ui_meta_specific=[cgi ui_meta_specific]
					ui_meta_view=modular
					{phantom?}
					ui_te_widget:o_default=text
					ui_te_width:o_default=20
					ui_te_help:o_default=Override the option defaults, select SKU to use instead
					{/phantom?}
					ui_page_banner=[cgi ui_page_banner]
					ui_page_title=[cgi ui_page_title]
					ui_return_to=@@MV_PAGE@@
					ui_return_to=item_id=[cgi item_id]
					ui_return_to=ignore_sku=1
					item_id={code}
				|]{sku|no sku}</A>{o_default *}</TD>
<TD>{o_master}</td>
<TD>{o_group} {phantom?}[page href=__UI_BASE__/flex_editor
			form=|
				mv_data_table=[cgi mv_data_table]
				ui_meta_view=modular_create
				ui_te_default:o_master={sku}
				ui_te_default:o_group={o_group}
				ui_class=Items
				ui_return_to=@@MV_PAGE@@
				ui_return_to=item_id=[cgi item_id]
				ui_return_to=ignore_sku=1
			|]add</A>{/phantom?}</TD></td>
<TD>{description|{products.description}}</td>
<TD ALIGN=RIGHT>{price|{products.price}}</td>
</tr>
[/seti]
[comment]
[set mod_template]master={o_master}|sku={sku}|action={ui_action}|leval={level}|enable={o_enable}|{q}
[/set]
[/comment]
[perl tables="products [cgi mv_data_table]"]
	for(qw/ ui_mod_expand ui_mod_explode /) {
		$Session->{$_} = {} 
			if ! $Session->{$_};
	}
	if($CGI->{expand}) {
		$Session->{ui_mod_expand}{$CGI->{item_id}}{$CGI->{expand}} = 1;
	}
	elsif($CGI->{contract}) {
		$Session->{ui_mod_expand}{$CGI->{item_id}}{$CGI->{contract}} = 0;
	}
	elsif($CGI->{collapse}) {
		delete $Session->{ui_mod_explode}{$CGI->{item_id}};
		delete $Session->{ui_mod_expand}{$CGI->{item_id}};
	}
	elsif($CGI->{explode}) {
		delete $Session->{ui_mod_expand}{$CGI->{item_id}};
		$Session->{ui_mod_explode}{$CGI->{item_id}} = 1;
	}
	$Session->{ui_mod_expand}{$CGI->{item_id}} = {}
		unless $Session->{ui_mod_expand}{$CGI->{item_id}};
	$expand = $Session->{ui_mod_expand}{$CGI->{item_id}};
	$explode = $Session->{ui_mod_explode}{$CGI->{item_id}};
	$otab = $CGI->{mv_data_table};
	$ptab = '__ProductFiles_0__';
	$Scratch->{mod_template} =~ s/\%7b/{/g;
	$Scratch->{mod_template} =~ s/\%7d/}/g;
	$Scratch->{mod_template} =~ s/UI_ACTION=/{ui_action}/g;
	$odb = $Db{$otab}
		or return "bad options database";
	$pdb = $Db{$ptab}
		or return "bad products database";
	my $price_fld = $pdb->column_exists('__PriceField__')
						? '__PriceField__'
						: 'price';
	delete $Scratch->{debug_body};
	sub display_template {
		my ($ref) = @_;
		my $sku = $ref->{sku};
		my $code = $ref->{code};
		$ref->{"products.description"} = tag_data($ptab, '__DescriptionField__', $sku);
		$ref->{"url.description"} = $Tag->filter('urlencode', $ref->{"products.description"});
		$ref->{"products.price"} =  tag_data($ptab, $price_fld, $sku);

#### Just used to set up some levels in test data
#		my %dict = qw/
#						0 First
#						1 Second
#						2 Third
#						3 Fourth
#						4 Fifth
#						5 Sixth
#						/;
#		$dict{''} = 'First';
#		$dict = $dict{$ref->{level}};
#		if ($dict
#			and $ref->{"products.description"}
#			)
#		{
#			my $cat = tag_data('products', 'category', $sku);
#			$ref->{"products.description"} =~ s/^option\s+/$cat /i;
#			$pdb->set_field($sku, 'description', $ref->{"products.description"});
#		}
#
		if($ref->{level}) {
			my $level = $ref->{level} * 28;
			$ref->{ui_spacer} = qq{<IMG SRC="bg.gif" HEIGHT=1 WIDTH=$level>};
		}
		for(qw/ products.description description /) {
			if(length($ref->{$_}) >=30) {
				substr($ref->{$_}, 27) = '...';
			}
		}
		my $indent = $ref->{level} + 1;
		if($ref->{o_enable}) {
			if($expand->{$code} or $explode && ! defined $expand->{$code}) {
				$ref->{ui_action} = "contract=$code";
				$ref->{ui_action_img} = 
					 qq{<FONT size=1>$indent</FONT><IMG SRC="down.gif" height=11 width=14 BORDER=0>};
			}
			else {
				$ref->{ui_action} = "expand=$code";
				$ref->{ui_action_img} = 
					 qq{<FONT size=1>$indent</FONT><IMG SRC="right.gif" height=11 width=14 BORDER=0>};
			}
		}
		else {
			$ref->{ui_action_img} = 
				 qq{<IMG SRC="bg.gif" HEIGHT=1 WIDTH=14>};
		}
		my $body = $Scratch->{mod_template};
#Log("body first:\n$body\n\n") unless $Scratch->{debug_body};
		$body =~ s!(\{\w+)\%2e(\w+\})!$1.$2!g;
		$body =~ s!\{([-\w.]+)\}!$ref->{$1}!g;
		$body =~ s!\{([-\w.]+)\|((?s:.)*?)\}!$ref->{$1} || $2!eg;
		$body =~ s!\{([-\w.]+)\s+((?s:.)*?)\}! $ref->{$1} ? $2 : ''!eg;
		$body =~ s!\{([-\w.]+)\?\}((?s:.)*?){/\1\?\}! $ref->{$1} ? $2 : ''!eg;
		$body =~ s!\{([-\w.]+):\}((?s:.)*?){/\1:\}! $ref->{$1} ? '' : $2!eg;
#Log("body now:\n$body\n\n") unless $Scratch->{debug_body}++;
		return $body;
	}
	return;
[/perl]







[query
	table=options
	hashref=master
	sql="SELECT * FROM options
			WHERE o_master = '[cgi name=item_id filter=sql]'
			ORDER BY o_group, o_sort
		"]
[sql-sub o_display]
	return shift;
[/sql-sub]
[sql-exec o_display][sql-code][/sql-exec]
[/query]

[perl tables=options]
	my $item_id = $Tag->cgi({name => 'item_id', filter => 'sql'});
	my $sql = "SELECT * FROM options WHERE o_master='$item_id' ORDER BY o_group, o_sort";
#Log("sql=$sql");
	my $hash_results = $Tag->query({ sql => $sql, hashref => 'my_results'});

	my %above;
	my @rows;
	foreach my $row (@{$hash_results}) {
		my $master = $row->{code};
#Log("master=$master");
		$above{$CGI->{item_id}} = 1;
		$above{$master} = 1;
		my @stack = ( [ $row ] );
		ARY: for (;;) {
			my $ary;
			$ary = pop(@stack)
				or last ARY;
			ROW: for(;;) {
				$row = shift @$ary
					or last ROW;
				$row->{level} = scalar(@stack);
				push(@rows, $row);
				my $sku = $row->{sku};
				next ROW if ! $row->{o_enable};
				my $code = $row->{code};
				if($above{$sku}) {
					$row->{description} = errmsg("ENDLESS TREE") . " $row->{description}";
					display_template($_);
					next ROW;
				}
				if( $expand->{$code}
						or 
					($explode and ! defined $expand->{$code})
					)
				{
					push(@stack, $ary);
					my $key = $odb->quote($sku, 'sku');
					my $q = "SELECT * FROM options WHERE o_master = $key ORDER BY o_sort",
					$ary = $odb->query(
							{ 
								hashref => 1,
								sql => "SELECT * FROM options
										WHERE o_master = $key
										ORDER BY o_sort
									",
							}
					);
					$above{$sku} = 1 if $ary and scalar @{$ary};
					
				}
			}  # END ROW
		}  # END ARY
	}
	my $out;
	for(@rows) {
		$out .= display_template($_);
	}
	return $out;
[/perl]

</table>

[page href=__UI_BASE__/flex_editor
		form="
			mv_data_table=[cgi mv_data_table]
			ui_class=Admin
			ui_data_fields=code sku o_master o_group phantom o_enable description
			ui_te_default:o_master=[cgi item_id]
			ui_meta_view=modular_create
			ui_hide_key=1
			ui_new_item=1
			ui_page_banner=[cgi ui_page_banner]
			ui_page_title=[cgi ui_page_title]
			ui_return_to=@@MV_PAGE@@
			ui_return_to=item_id=[cgi item_id]
			ui_return_to=ignore_sku=1
		"]<IMG SRC="plus.gif" ALT="Add new option" BORDER=0>Add option</A>
&nbsp;&nbsp;


[page href=__UI_BASE__/item_option_phantom
		form="
			mv_data_table=[cgi mv_data_table]
			ui_class=Items
			item_id=[cgi item_id]
			ui_return_to=@@MV_PAGE@@
			ui_return_to=ui_return_table=[cgi mv_data_table]
			ui_return_to=item_id=[cgi item_id]
			ui_return_to=ignore_sku=1
		"]<IMG SRC="plus.gif" ALT="Add phantom" BORDER=0> Add phantom</A>
&nbsp;&nbsp;
<FORM ACTION="[area @@MV_PAGE@@]">
<input type=hidden name=mv_action value=back>
<input type=hidden name=mv_data_table value="[cgi mv_data_table]">
<INPUT TYPE=hidden NAME=sku VALUE="[cgi item_id]">
<INPUT TYPE=hidden NAME=item_id VALUE="[cgi item_id]">
<INPUT TYPE=hidden NAME=ignore_sku	VALUE="1">

[loop
	prefix=clone
	search="
		st=db
		fi=[cgi mv_data_table]
		co=yes

		se=1
		sf=o_modular

		un=yes
	"
	more=1]<SELECT NAME=ui_clone_id>
<OPTION VALUE=""> --
[list]
<OPTION VALUE="[clone-code]">[clone-filter 20][clone-description][/clone-filter]
[/list]
</SELECT>[more-list]<BR>[more]<BR>[/more-list][/loop]&nbsp;[button text="Clone options"]
[flag type=write table="[cgi mv_data_table]"]
ui_clone_options=1
mv_todo=back
mv_nextpage=@@MV_PAGE@@
[/button]
</FORM>
