#!/usr/local/bin/perl
require "weblib.lib";
use DBI;
use DB_File;
&getparams;
if($param{photo}){$in{file}=$param{photo};}
$dir="/home/todd/public_html/pictures/gallery/temp";
@time=localtime(time);
$YEAR=$param{year}?$param{year}:($time[5]+1900);
$TOURPREFIX=$param{tourprefix}?$param{tourprefix}:"t";
$TOURID=$param{tourid}?$param{tourid}:"$TOURPREFIX$YEAR";
dbmopen(%TOUR,"/home/todd/private/tours/$TOURID.db",undef);
$dbh=DBI->connect("DBI:mysql:database=trcgallery","todd",'o$fcpsh');
#dbmopen(%TOURtemp,"$dir/$TOURID.db",0666);
$style=<<EOF;
body, table {font: 8pt/10pt Verdana, sans-serif;}
.minicheckbox   {width:10px; height: 10px;}
EOF
&htmlopen("Photo classifications for tour $YEAR|","style=$style");
&ReadParse;
@file=split(/\0/,$in{file});
%file=map {$_,1} @file;
if($in{name} eq "New" && $in{NewKey} && $in{NewName}){
	print "Adding $in{NewKey} -> $in{NewName}<br>\n";
	$dbh->do("INSERT photoset set setname='$in{NewKey}',label='$in{NewName}',categoryid=2");
	$in{name}=$in{NewKey};
}
open(TOUR,"/home/todd/private/tours/$TOURID.info");
$_=<TOUR>;
$_=<TOUR>;
$select=qq{<select name="venue"><option value="">Select Venue</option>\n};
$optgroup=0;
while(<TOUR>){
	chop;
	if(/^#(.*)%%(.*)/){
		$tag=$1;
		$label=$2;
		if($optgroup){$select.="</optgroup>\n";}
		$select.=qq{<optgroup label="$label">\n};
		$optgroup=1;
		next;
	}
	($date,$city,$venue,$note)=split(/\t/);
	$selected=$in{venue} eq "$date, $venue, $city"?" selected":"";
	$select.="<option$selected>$date, $venue, $city</option>\n";
}
if($optgroup){$select.="</optgroup>\n";}
$select.="</select>\n";
$selectname=qq{<select name="name"><option value="">Select Photographer</option>\n};
$selectname.=qq{<option value="New">New Photographer</option>\n};
$sth=$dbh->prepare("SELECT *,substring_index(label,' ',-1) as lastname from photoset where categoryid=2 order by concat(lastname,label)");
$sth->execute();
$optgroup=0;
$previousinitial="";
while($info=$sth->fetchrow_hashref()){
	$photographerkey=$info->{'setname'};
	$photographername=$info->{'label'};
	$initial=uc(substr($info->{'lastname'},0,1));
	if($initial ne $previousinitial){
		if($optgroup){$selectname.="</optgoup>\n";}
		$selectname.=qq{<optgroup label="$initial">\n};
		$previousinitial=$initial;
		$optgroup=1;
	}
	$photographer{$photographerkey}=$photographername;
	$selected=$in{name} eq $photographerkey?" selected":"";
	$selectname.=qq{<option value="$photographerkey"$selected>$photographername</option>\n};
}
if($optgroup){$selectname.="</optgroup>\n";}
$selectname.="</select>\n";
opendir(DIR,$dir);
chdir $dir;
@files=grep /\.jpe?g/i,readdir(DIR);
$selectfile=qq{<select name="file" multiple size="40"><option value="">Select File</option>\n};
foreach $file(sort @files){
	$selected=$file{$file}?" selected":"";
	$selectfile.=qq{<option$selected>$file</option>\n};
}
$selectfile.="</select>\n";
print "<table border><tr>";
if(@file){
	if($in{name} && $in{venue}){
		$caption="";
		$caption.="Caption: $in{caption}#" if $in{caption};
		$nameid=$in{name};
		($setdate,$venue)=split(/, /,$in{venue},2);
		$key="$setdate.photo";
		$tourdateset=$TOURtemp{$key}?$TOURtemp{$key}:$TOUR{$key};
		@tourdateset=split(/;/,$tourdateset);
		foreach $set(@tourdateset){
			($photographer,$URL)=split(/:/,$set,2);
			push(@set,$URL);
			$set{$URL}=$photographer;
		}
		($day,$date)=split(/ /,$setdate);
		($month,$day,$year)=split(m#/#,$date);
		if(!$year){$year=$YEAR;}
		$year+=2000 if length($year)==2;
		$ymd=sprintf("%04d%02d%02d",$year,$month,$day);
		$date=sprintf("%d/%d/%d",$month,$day,$year);
		if(!$set{"$nameid$ymd"}){
			push(@set,"$nameid$ymd");
			$set{"$nameid$ymd"}=$photographer{$nameid};
		}
		$caption.="Photographer: $photographer{$nameid}#";
		$caption.="Date: $date#Venue: $venue#";
		$caption.=qq{Tour URL: <a href=\"http://TRConnection.com/tour/$TOURID\">http://TRConnection.com/tour/$TOURID</a>};
	}
	print "<td>";
	if($nameid && $year){
		if($year<2000){$decade=sprintf("decade%2d",($year-1900)-$year%10);}
		else{$decade="decade".($year-$year%10);}
		$photographer=~s/'/\\'/g;
		$venue=~s/'/\\'/g;
		$caption=~s/'/\\'/g;
		foreach $file(@file){
			chop($picinfo=`identify '$file'`);
			($name,$type,$size)=split(/\s+/,$picinfo);
			($width,$height)=split(/x/,$size);
			$insert="INSERT photoinfo set photo='$name',dateadded=now(),".
				"caption='$caption',setnames='$TOURID,$TOURPREFIX$ymd,$nameid,$nameid$ymd,$decade'";
			$dbh->do($insert);
			$dbh->do("UPDATE photoinfo set width='$width',height='$height' where photo='$name'");
			$sth=$dbh->prepare("SELECT count(*) as c from photoset where categoryid=1 and setname='$TOURPREFIX$ymd'");
			$sth->execute();
			$info=$sth->fetchrow_hashref();
			if(!$info->{'c'}){
				$insert="INSERT photoset set setname='$TOURPREFIX$ymd',label='from $venue on $date',categoryid=1;";
				$dbh->do($insert);
			}
			$sth=$dbh->prepare("SELECT count(*) as c from photoset where categoryid=1 and setname='$nameid$ymd'");
			$sth->execute();
			$info=$sth->fetchrow_hashref();
			if(!$info->{'c'}){
				$insert="INSERT photoset set setname='$nameid$ymd',".
					"label='from $venue on $date by $photographer{$nameid}',categoryid=1";
				$dbh->do($insert);
			}
			printthumb($file);
			print "</td></tr><tr><td>";
		}
		foreach $URL(@set){
			$URL="$set{$URL}:$URL";
		}
		if($setdate){$updateDB=$TOURtemp{"$setdate.photo"}=join(";",@set);}
	} else {print "A photographer name and a date must be specified\n";}
	print "</td>";
}
print <<EOF;
<td>
<form action="$weblib::URL/year=$YEAR/tourid=$TOURID" method="POST">
$selectfile<br>
$selectname<br>New Key:<input name="NewKey"><br>New Name: <input name="NewName"><br>
$select<br>
<input type="submit">
</form>
</td></tr></table>
EOF
if("$nameid$ymd"){
	open(TMP,">/tmp/$nameid$ymd-$$.sh");
	$sshdata= <<EOF;
/home/todd/public_html/pictures/gallery/temp/xu $nameid$ymd
cd /home/todd/private/tours
/usr/local/bin/editdb -f $TOURID -r '$setdate.photo' "$updateDB" 2>&1
EOF
	print "<pre>$sshdata</pre>\n";
	print TMP $sshdata;
	print qq{<a href="http://TRConnection.com/todd/exectmp/$nameid$ymd-$$.sh" target="_blank">Update Database</a>\n};
	close TMP;
}
&htmlclose;
sub thumbnail{
	local($filename)=shift;
	local(@filename)=split(/\./,$filename);
	local($thumbdir)=shift;
	local($type)=pop(@filename);
	local($thumbnailname)=join(".",@filename);
	$thumbnailname="tn_${thumbnailname}_$type.jpg";
	if(-f $filename && !-f "$thumbdir/$thumbnailname"){
		system("convert -geometry '95x95>' '$filename' 'jpeg:$thumbdir/$thumbnailname'");
	}
	return $thumbnailname;
}
sub printthumb{
	local($file)=shift;
	local($caption,$info,%caption,%info,$tn,$value,$key);
	$sth=$dbh->prepare("SELECT caption,width,height,setnames from photoinfo where photo='$file'");
	$sth->execute();
	$info=$sth->fetchrow_hashref();
	$tn=thumbnail($file,"$dir/thumbnails");
	print qq{<a target="photoclass" href="/todd/photoclass/photo=$file">},
		qq{<img src="/trc/pictures/gallery/temp/thumbnails/$tn" align="left">$file</a></td><td>\n};
	foreach $caption(split(/#/,$info->{'caption'})){
		print "$caption<br>\n";
		($key,$value)=split(/: /,$caption);
		$caption{$key}=$value;
	}
	print "Width: ",$info{'width'}=$info->{'width'},"</br />\n";;
	print "Height: ",$info{'height'}=$info->{'height'},"</br />\n";;
	print "Setnames: ",$info{'setnames'}=$info->{'setnames'},"</br />\n";;
}
