<!-- begin page_edit_old -->

<form action="[base-url]/@@MV_PAGE@@" method=POST ENCTYPE="multipart/form-data">
<input type=hidden name=mv_session_id value="[data session id]">
<input type=hidden name=mv_action value=back>

<table __UI_T_PROPERTIES__>
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
[perl tables="__UI_META_TABLE__"] 
	my $ref;
	my $ref_in_page;
	my $t_name = '';
	my @filters = grep /^ui_filter:/, keys %$CGI;
	foreach my $key (@filters) {
		my $val = delete $CGI->{$key};
		$key =~ s/ui_filter://;
		next unless $val;
		next unless defined $CGI->{$key};
		$CGI->{$key} = $Tag->filter($val, $CGI->{$key}, $key);
	}
	if($CGI->{ui_template}) {
#Log("reading template=$CGI->{ui_template}");
		$CGI->{ui_template} = $Tag->filter('filesafe', $CGI->{ui_template});
		my ($ary) = $Tag->read_ui_template($CGI->{ui_template});
		$ref = shift @$ary || {};
		$t_name = $CGI->{ui_template};
		$t_desc = $ref->{ui_template_description} || 'none';
	}
#Log("t_name=$t_name");
	my @choices = split /\s*\0\s*/, $CGI->{ui_page};
	my $page;
	for(@choices) {
		next if ! $_;
		$page = $_;
		$page .= $Config->{HTMLsuffix} if $page !~ /$Config->{HTMLsuffix}$/;
		if ($page !~ m(^pages/)) {
			if ($Session->{ui_cwd} =~ m(^pages/)) {
				$page = "$Session->{ui_cwd}/$page";
			} else {
				$page = "pages/$page";
			}
		}
		last;
	}

	my $ary;
	# force substitution of [L..]-stuff off	
	# Can't do former method of delete $Vend::Cfg->{Locale} in foreground
	my $current = $Tag->file($page, 'raw');
	delete $CGI->{save_in_page};

	# This call returns an array of hashes if exists
	if(! $ref and $current) {
		# Force array context for return value (wantarray test)
		($ary) = $Tag->read_ui_template($page);
		if(ref $ary) {
			my $refp =  shift (@$ary);
			if($refp->{ui_template_name} and ! $refp->{ui_template_layout}) {
				# Force array context for return value (wantarray test)
				($ary) = $Tag->read_ui_template("templates/$refp->{ui_template_name}");
				if(ref $ary) {
					$ref = shift @$ary || {};
				}
				else {
					$ref = $refp;
				}
			}
			else {
				$ref = $refp;
				$CGI->{save_in_page} = 'CHECKED';
			}
		}
	}

	$ref = {} if ! $ref;
	length($current) and
		$current !~ m{<!--\s+begin\s+content\s+-->\n(.*)\n<!--\s+end\s+content\s+-->}is
		and $current = "<!-- BEGIN CONTENT -->$current<!-- END CONTENT -->";

	$t_name = $ref->{ui_template_name} || 'None'
		if ! $t_name;
	$t_desc = $ref->{ui_template_description} || 'n/a'
		if ! $t_desc;

	my @def;

	my $def_string = $ref->{ui_definition};
	$def_string =~ s/"/&quot;/g;

	my $short_def = "ui_template: Yes\nui_template_name: $ref->{ui_template_name}";

	my $r;
#Log("element=$ref->{element}");
	if ($r = $ref->{element} and ref($r)) {
			my $widget;
			my @order;
			if($ref->{ui_display_order}) {
				@order = @{$ref->{ui_display_order}};
			}
			else {
				@order = sort keys %$r;
			}
			for(@order) {
				my ($set) = $current =~ m{\[set\s+$_\](.*?)\[/set\]};
				my $t_set = $r->{$_};
				my $wtype = $ref->{widget}{$_} || '';
				if($wtype eq 'break') {
					push @controls, ['break'];
					next;
				}
				my $options = $ref->{options}{$_} || $t_set || '=No,1=Yes';
#Log("r key $_ set='$set' options=$options value=$Values->{qq!$_!}");
				my $help = $ref->{help}{$_} || '';

				if($wtype) {
					$widget = $Tag->widget( {
											name => "ui_control_$_",
											type => $wtype,
											passed => $options,
											rows =>  $ref->{height}{$_},
											cols =>  $ref->{width}{$_},
											set => $set,
											filter => $ref->{filter}{$_},
											pre_filter => $ref->{prefilter}{$_},
										});
				}
				else {
					my $val = $set || $t_set;
					$val =~ s/"/&quot;/g;
					$widget = qq{<INPUT NAME="ui_control_$_" VALUE="$val" SIZE=60>};
				}
				my $desc = $ref->{description}{$_} || $_;
				$desc .= "<BR><small><i>$help</i></small>" if $help;
				push @controls, [ $desc, $widget ];
			}
	}
	my $out = '';
#Log("t_name=$t_name");
	$out .= <<EOF;
<tr bgcolor="__UI_C_TITLEBARBG__">
	<td><font color="__UI_C_TITLEBARTXT__"><b>Page file</b></font></td>
	<td><font color="__UI_C_TITLEBARTXT__"><b>
		$page
		<INPUT TYPE=hidden NAME=ui_page VALUE="$page">
		<INPUT NAME=ui_elements TYPE=hidden VALUE="$ref->{ui_template_layout}">
		<input type=hidden name=ui_definition value="$def_string">
		<input type=hidden name=ui_short_definition value="$short_def">
		</b></font>
	</td>
</tr>
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr>
	<td bgcolor=__UI_C_INTBLOCK__><b>Template</b></td>
	<td bgcolor=__UI_C_INTBLOCK__><small>
<select name="ui_template" size=1>
<option value=""> Select a page template
EOF

	my @files = $Tag->list_glob('templates/*');
	my @opt_out;
	for(@files) {
		next unless -f $_;
		my $name = $_;
		my $sel = '';
		my $data = $Tag->file($_);
		$name =~ s:.*/::;
		my $r= {};
		$data =~ m{\[comment\]\s*(ui_.*?)\[/comment\]}s;
		my $structure = $1 || "ui_template_description: Unknown $name";
		my @lines = grep /\S/, split /\n/, $structure;
		for(@lines) {
			my ($el, $el_item, $el_data) = split /\s*:\s*/, $_;
			#Log ("found el=$el el_item=$el_item el_data=$el_data");
			if($el_data) {
				$r->{$el} = { $el_item, $el_data };
			}
			else {
				$r->{$el} = $el_item;
			}
		}
		$sel = ' SELECTED' if $ref->{ui_template_name} eq $r->{ui_template_name};
		#Log("ref is " . $Tag->uneval( { ref => $r } ));
		push @opt_out, qq{<OPTION VALUE="$_"$sel>$r->{ui_template_description}};
	}
	$out .= join "\n", @opt_out;
	$out .= <<EOF;
	</select>
	[button text="Change template"]
		mv_todo=back
		[js]onclick="this.form.target='_self'"[/js]
		mv_nextpage=@@MV_PAGE@@
	[/button]
	 </small>
	</td>
</tr>
EOF
	if($CGI->{ui_show_description}) {
		$out .= <<EOF;

<tr>
	<td bgcolor=__UI_C_INTBLOCK__><b>Template description</b></td>
	<td bgcolor=__UI_C_INTBLOCK__>$t_desc</td>
</tr>
<tr>
	<td bgcolor=__UI_C_INTBLOCK__>
		<b>Template sequence</b><br>
	</td>
	<td bgcolor=__UI_C_INTBLOCK__ VALIGN=top>$ref->{ui_template_layout}</td>
</tr>
<tr>
	<td bgcolor=__UI_C_INTBLOCK__ colspan=2>
		<small><i>UI_CONTENT is the content portion(s), all others refer
		to Knar elements.</i></small>
	</td>
</tr>
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
EOF
	}
	else {
		$out .= <<EOF;
EOF
	}
	my $table_done;
	for(@controls) {
		if($_->[0] eq 'break') {
			$out .= <<EOF;
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
EOF
			next;
		}
		$out .= qq{<tr><td bgcolor=__UI_C_INTBLOCK__><b>$_->[0]</td><td bgcolor=__UI_C_INTBLOCK__>$_->[1]</td></tr>};
	}

	my $content_input;
	my $isfile = $Tag->value_extended(
				{
					name => 'ui_upload_content',
					test => 'isfile',
				}
			);
#Log("upload_content=$Values->{ui_upload_content} isfile=$isfile");
	if( $Values->{ui_upload_content}
		and $Tag->value_extended(
				{
					name => 'ui_upload_content',
					test => 'isfile',
				}
			)
		)							
	{
#Log("content from upload.");
		$content_input = $Tag->value_extended( {
												name => 'ui_upload_content',
												file_contents => 1,
												});
#Log("content from upload is:\n$content_input");
		$content_input =~ s{(<!--\s+begin\s+content\s+-->)([^\n])}{$1\n$2}is;
		$content_input =~ s{([^\n])(<!--\s+end\s+content\s+-->)}{$1\n$2}is;
	}

	$content = $content_input || $current || '';

	$content =~ m{<!--\s+begin\s+content\s+-->\n(.*)\n<!--\s+end\s+content\s+-->}is
		and $content = $1;

	$content =~ s/\&/\&amp;/g;
	$content =~ s/\[/&#91;/g;
	$content =~ s/</&lt;/g;

	# If nothing else, display the content
	$ref->{ui_template_layout} = 'UI_CONTENT'
		if ! $ref->{ui_template_layout};

	$out .= <<EOF;
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr>
	<td bgcolor=__UI_C_INTBLOCK__ valign=top>
	<b>Content</b>
	<small><br><br>
	<A HREF="$Config->{VendURL}/ui_download/$page?mv_no_cache=$Session->{pageCount}"><img src="down.gif" border=0 height=11 width=10 alt="download $page">download content</a>
	<br><i>(will not reflect changes below unless saved)</I></small>
	</td>
	<td bgcolor=__UI_C_INTBLOCK__>
	<B>
	<small>
	<INPUT TYPE=file NAME=ui_upload_content><BR>
	[button text="Upload content portion from file"]
		mv_todo=return
		[js]onclick="this.form.target='_self'"[/js]
		mv_nextpage=@@MV_PAGE@@
	[/button]</small></b>
	</td>
</tr>

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

<tr>
	<td colspan=2 bgcolor=__UI_C_INTBLOCK__>
		<b>or input below:</b><br>
		<TEXTAREA NAME=ui_content COLS=85 ROWS=20>$content</TEXTAREA>
	</td>
</tr>
EOF
	$out .= <<EOF;
</td>
</tr>
EOF
	return $out;
[/perl]

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

<tr>
<td colspan=2 bgcolor=__UI_C_INTBLOCK__>
[set Preview]
mv_nextpage=__UI_BASE__/page_preview
mv_action=back
[/set]

[set Save]
mv_nextpage=__UI_BASE__/page_save
[/set]

[set Cancel]
mv_nextpage=__UI_BASE__/page
mv_todo=back
[/set]

<INPUT TYPE=submit NAME=mv_click VALUE=Preview onClick="this.form.target='page_preview'">
<INPUT TYPE=submit NAME=mv_click VALUE=Save onClick="this.form.target='_self'">
<INPUT TYPE=submit NAME=mv_click VALUE=Cancel onClick="this.form.target='_self'"><br>
<INPUT TYPE=checkbox NAME=ui_save_t_in_page VALUE=1 [cgi save_in_page]> Save template in page

</td>
</tr>

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

</table>
</form>
