#!/usr/local/bin/perl
require "weblib.lib";
use DBI;
&ReadParse;
@class=split(/\0/,$in{class});
&getparams;
$dbh=DBI->connect("DBI:mysql:database=trcgallery","todd",'o$fcpsh');
if($param{photo}){$in{file}=$param{photo};}
$dir="/home/todd/public_html/pictures/gallery";
$url="/trc/pictures/gallery";
$script="script=http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js%%trcscripts.js";
$style=<<EOF;
style=body, table {font: 8pt/10pt Verdana, sans-serif;}
.minicheckbox	{width:10px; height: 10px;} 
EOF
&htmlopen("Photo classifications|",$style,$script);
$fileexists=1;
if(!-f "$dir/$in{file}"){
	$dir.="/temp";$url="/trc/pictures/gallery/temp";
	if(!-f "$dir/$in{file}"){$fileexists=0;print "Photo $in{$file} does not exist";}
}
$sth=$dbh->prepare("SELECT * from photoset where categoryid=1 order by label");
$sth->execute();
if(!$in{file}){
	$select=qq{<select name="Shows">\n<option value="">Select Show</option>\n};
	while($shows=$sth->fetchrow_hashref()){
		$setname=$shows->{'setname'};
		$title=$shows->{'label'};
		next unless $title =~ / by /;
		$select.=qq{<option value="$setname">$title</option>\n};
	}
	$select.="</select>\n";
}
else{
	$sth=$dbh->prepare("SELECT * from photoinfo where photo='$in{file}'");
	$sth->execute();
	$info=$sth->fetchrow_hashref();
	%setnames=map {$_,1} split(/,/,$info->{'setnames'});
}
$check="";
print "<table border><tr>";
$file=$in{file};
printphoto($file) if $fileexists;
makeset("Special Collection",5,"label");
makeset("Fans",8,"label");
makeset("Personalities",4,"concat(substring_index(label,' ',-1),label)");
print <<EOF;
<td><form action="$weblib::URL/photo=$file" method="POST">
<input type="submit">
$select<br>
$check
<input type="submit">
</form>
</td></tr></table>
EOF
&htmlclose;
sub makeset{
	local($setlabel,$id,$sort)=@_;
	local($sth,$title,$setname,$info);
	$sth=$dbh->prepare("SELECT * from photoset where categoryid=$id order by $sort");
	$sth->execute();
	$check.=qq{<h3>$setlabel</h3>\n};
	while($info=$sth->fetchrow_hashref()){
		$setname=$info->{'setname'};
		$title=$info->{'label'};
		$checked=$setnames{$setname}?" checked":"";
		$check.=qq{<label><input class="minicheckbox" type="checkbox" name="class" value="$setname"$checked>$title</label><br>\n};
	}
}
sub printphoto{
	local($file)=shift;
	$sth=$dbh->prepare("SELECT * from photoinfo where photo='$file'");
	$sth->execute();
	$info=$sth->fetchrow_hashref();
	$width=$info->{'width'};
	$newheight=$height=$info->{'height'};
	$newwidth=$width>320?320:$width;
	if($width != $newwidth){$newheight=int($height*($newwidth/$width));}
	print qq{<td width="360" align="center">};
	print qq{<a href="http://TRConnection.com/gallery/edit/picture=$file">};
	print qq{<img src="$url/$file" width="$newwidth" height="$newheight">};
	print qq{</a>};
	print qq{<br>$file<br />\n};
	foreach $caption(split(/#/,$info->{'caption'})){print "$caption<br>\n";}
	@setnames=split(/,/,$info->{'setnames'});
	%setnames=map {$_,1} @setnames;
	foreach $class(@class){push(@setnames,$class),$setnames{$class}=1 unless $setnames{$class};}
	$setnames=join(',',@setnames);
	$printsetnames=join(', ',@setnames);
	print "Setnames: $printsetnames<br />\n";
	print "Width: $width<br />\n";
	print "Height: $height<br />\n";
	print "</td>\n";
	return unless @class;
	$update="UPDATE photoinfo set setnames='$setnames' where photo='$file'";
	$dbh->do($update);
}
