#!/usr/local/bin/perl
require "weblib.lib";
&ReadParse;
$class=join(",",split(/\0/,$in{class}));
&getparams;
dbmopen(%DBset,"/home/todd/db/photoindex",undef);
if($param{photo}){$in{file}=$param{photo};}
$dir="/home/todd/public_html/pictures/gallery";
$url="/trc/pictures/gallery";
&htmlopen("Photo classifications");
$fileexists=1;
if(-f "$dir/$in{file}"){dbmopen(%DB,"/home/todd/db/photoindex",undef);}
else{
	$dir.="/temp";
	$url="/trc/pictures/gallery/temp";
	if(-f "$dir/$in{file}"){dbmopen(%DB,"$dir/photoindex",undef);}
	else{
		$fileexists=0;
		print "Photo $in{$file} does not exist";
		dbmopen(%DB,"/home/todd/db/photoindex",undef);
	}
}
@sets=grep /^SetID%%/,keys %DBset;
foreach $set(@sets){
	next if $DBset{$set}=~/^(Photographers|Artists|Special Events|By Decade)%%/;
	$DBset{$set}=~/^(.*)%%/;
	push(@{$1},$set);
	$set{$1}=1 unless $1 eq "Shows";
}
if(!$in{file}){
	$select=qq{<select name="Shows">\n<option value="">Select Show</option>\n};
	foreach $element(@Shows){
		($tag,$elementtag)=split(/%%/,$element);
		($settag,$title)=split(/%%/,$DBset{$element});
		next unless $title =~ / by /;
		$select.=qq{<option value="$elementtag">$title</option>\n};
	}
	$select.="</select>\n";
}
$check="";
foreach $set(keys %set){
	$check.=qq{<h3>$set</h3>\n};
	foreach $element(@{$set}){
		($tag,$elementtag)=split(/%%/,$element);
		($settag,$title)=split(/%%/,$DBset{$element});
		$check.=qq{<input type="checkbox" name="class" value="$elementtag">$title<br>\n};
	}
}
print "<table border><tr>";
$file=$in{file};
printphoto($file) if $fileexists;
print <<EOF;
<td>
<form action="$weblib::URL/photo=$file" method="POST">
$select<br>
$check
<input type="submit">
</form>
</td></tr></table>
EOF
&htmlclose;
sub bylastname{
	return join(/ /,reverse(split(/\s+/,$DB{$a}))) cmp join(/ /,reverse(split(/\s+/,$DB{$b})));
}
sub printphoto{
	local($file)=shift;
	local($caption,$info,$value,$key);
	($caption,$info)=split(/%%/,$DB{$file});
	print qq{<td><img src="$url/$file"><br>$file</td>\n<td>};
	foreach $caption(split(/#/,$caption)){
		print "$caption<br>\n";
		($key,$value)=split(/: /,$caption);
		$caption{$key}=$value;
		push(@caption,$key);
	}
	foreach $info(split(/;/,$info)){
		($key,$value)=split(/=/,$info);
		if($key eq "SetID"){
			$value.=",$class" if $class;
			print "$key=",join(", ",split(/,/,$value)),"<br>\n";
		}
		else{print "$key=$value<br>\n";}
		$info{$key}=$value;
		push(@info,$key);
	}
	print "</td>\n";
	return unless $class;
	foreach $caption(@caption){$caption="$caption: $caption{$caption}";}
	foreach $info(@info){$info="$info=$info{$info}";}
	$DB{$file}=join("#",@caption)."%%".join(";",@info);
}
