<?
 require("common.inc.php");
 include "register_participants.inc.php";

 //authenticate based on email address and registration number from the SESSION
 if(!$_SESSION['email'])
 {
 	header("Location: register_participants.php");
	exit;
 }
 if(!$_SESSION['registration_number'])
 {
 	header("Location: register_participants.php");
	exit;
 }

 $q=mysql_query("SELECT registrations.id AS regid, students.id AS studentid, students.firstname FROM registrations,students ".
 	"WHERE students.email='".$_SESSION['email']."' ".
	"AND registrations.num='".$_SESSION['registration_number']."' ". 
	"AND registrations.id='".$_SESSION['registration_id']."' ".
	"AND students.registrations_id=registrations.id ".
	"AND registrations.year=".$config['FAIRYEAR']." ".
	"AND students.year=".$config['FAIRYEAR']);
echo mysql_error();

 if(mysql_num_rows($q)==0)
 {
 	header("Location: register_participants.php");
	exit;
 
 }
 $authinfo=mysql_fetch_object($q);

 //END OF AUTH, now lets try to generate a PDF using only PHP :) this should be fun!

 function loc($inch)
 {
 	return $inch*72;
 }

 $pdf=pdf_new();
 pdf_open_file($pdf,null);
 //Letter size (8.5 x 11) is 612,792
 pdf_begin_page($pdf,612,792);
// pdf_set_parameter($pdf, "FontOutline", "Arial=/home/sfiab/www.sfiab.ca/sfiab/arial.ttf");
 //$arial=pdf_findfont($pdf,"Arial","host",1);
 $normalfont=pdf_findfont($pdf,"Times-Roman","host",0);
 $headerfont=pdf_findfont($pdf,"Times-Bold","host",0);
 pdf_set_info($pdf,"Author","SFIAB");
 pdf_set_info($pdf,"Creator","SFIAB");
 pdf_set_info($pdf,"Title","SFIAB - Participant Signature Page");
 pdf_set_info($pdf,"Subject","Signature Form");

 pdf_setlinewidth($pdf,0.3);

//The title of the fair
 $yloc=10.25;
 $height['title']=0.25;
 $height['subtitle']=0.22;
 $height['topbox']=0.8;
 $height['exhibitortitle']=0.2;
 $height['exhibitorbox']=1.3;
 $height['exhibitorsigspace']=0.40;
 $height['exhibitorsigtext']=0.13;
 $height['parenttitle']=0.2;
 $height['parentbox']=2.60;
 $height['parentsigspace']=0.40;
 $height['parentsigtext']=0.13;

 pdf_setfont($pdf,$headerfont,18);
 pdf_show_boxed($pdf,$config['fairname'] ,loc(0.75),loc($yloc),loc(7),loc($height['title']),"center",null);
 $yloc-=$height['title'];

 pdf_setfont($pdf,$headerfont,14);
 pdf_show_boxed($pdf,i18n("Signature Form"),loc(0.75),loc($yloc),loc(7),loc($height['subtitle']),"center",null);
 $yloc-=$height['subtitle'];

 pdf_moveto($pdf,loc(0.5),loc($yloc));
 pdf_lineto($pdf,loc(8),loc($yloc));
 pdf_stroke($pdf);

 //grab the project info
 $q=mysql_query("SELECT * FROM projects WHERE registrations_id='".$_SESSION['registration_id']."' AND year='".$config['FAIRYEAR']."'");
 $projectinfo=mysql_fetch_object($q);

 $q=mysql_query("SELECT * FROM students WHERE registrations_id='".$_SESSION['registration_id']."' AND year='".$config['FAIRYEAR']."'");
 while($si=mysql_fetch_object($q))
	$studentinfoarray[]=$si;

 $topboxtext="Registration Number: ".$_SESSION['registration_number']."\n".
                         "Project Title: $projectinfo->title\n";
 if(count($studentinfoarray)>1)
 	$plural="s";
 else
 	$plural="";

 $topboxtext.="Exhibitor$plural: ";
 foreach($studentinfoarray AS $studentinfo)
 {
	$topboxtext.="$studentinfo->firstname $studentinfo->lastname, "; 
 }
 //strip off the last comma
 $topboxtext=substr($topboxtext,0,-2);
 //add the newline
 $topboxtext.="\n";
			 
 pdf_setfont($pdf,$normalfont,11);
 pdf_set_value($pdf,"leading",15);

 $yloc-=$height['topbox'];
 pdf_show_boxed($pdf,$topboxtext, loc(0.75),loc($yloc),loc(7),loc($height['topbox']),"left",null);

 pdf_moveto($pdf,loc(0.5),loc($yloc));
 pdf_lineto($pdf,loc(8),loc($yloc));
 pdf_stroke($pdf);


 pdf_setfont($pdf,$headerfont,12);
 $yloc-=$height['exhibitortitle'];
 pdf_show_xy($pdf,"Exhibitor Declaration",loc(0.5),loc($yloc));

 pdf_setfont($pdf,$normalfont,11);
 pdf_set_value($pdf,"leading",15);

 $studentbox="The following section is to be read and signed by the exhibitor$plural\n\n".
	($plural?"We":"I")." certify that:\n". 
	" - The preparation of this project is mainly ".($plural?"our":"my")." own work\n". 
	" - ".($plural?"We":"I")." have read the rules and regulations and agree to abide by them\n".
	" - ".($plural?"We":"I")." agree that the decision of the judges will be final\n";

 $yloc-=$height['exhibitorbox'];
 pdf_show_boxed($pdf,$studentbox, loc(0.75),loc($yloc),loc(7),loc($height['exhibitorbox']),"left",null);

 foreach($studentinfoarray AS $studentinfo)
 {
	$yloc-=$height['exhibitorsigspace'];
	//signature line
	pdf_moveto($pdf,loc(1),loc($yloc));
	pdf_lineto($pdf,loc(4.5),loc($yloc));
	pdf_stroke($pdf);

	//date line
	pdf_moveto($pdf,loc(5),loc($yloc));
	pdf_lineto($pdf,loc(7),loc($yloc));
	pdf_stroke($pdf);

	$yloc-=$height['exhibitorsigtext'];
 	//show their name
	pdf_show_xy($pdf,"$studentinfo->firstname $studentinfo->lastname (signature)",loc(1.25),loc($yloc));

	//show the Date text
	pdf_show_xy($pdf,"Date",loc(5.25),loc($yloc));

 }

 $yloc-=3*$height['exhibitorsigtext'];
 pdf_moveto($pdf,loc(0.5),loc($yloc));
 pdf_lineto($pdf,loc(8),loc($yloc));
 pdf_stroke($pdf);


 //now for the parent/guardian signatures
 pdf_setfont($pdf,$headerfont,12);
 $yloc-=$height['parenttitle'];
 pdf_show_xy($pdf,"Parent/Guardian Declaration",loc(0.5),loc($yloc));

 pdf_setfont($pdf,$normalfont,11);
 pdf_set_value($pdf,"leading",15);

 $parentbox="The following is to be read and signed by the exhibitor$plural parent$plural/guardian$plural.\n\n".
		" - As a parent/guardian I certify to the best of my knowledge and believe the information contained in this application is correct, and the project is the work of the student. I also understand that the material used in the project is the responsibility of the student and that neither the school, the teacher, nor the ".$_config['fairname']." can be held responsible for loss, damage, or theft, however caused. I further understand that all exhibits entered must be left on display until the end of the Fair. If my son/daughter does not remove the exhibit at the end of the Fair, the ".$config['fairname']." or the owner of the exhibition hall cannot be responsible for the disposal of the exhibit.\n\n". 

		" - If my son/daughter is awarded the honour of having his/her exhibit chosen for presentation at the Canada-Wide Science Fair, I consent to having him/her journey to the Fair, and will not hold the Fair responsible for any accident or mishap to the student or the exhibit.\n";

 $yloc-=$height['parentbox'];
 pdf_show_boxed($pdf,$parentbox, loc(0.75),loc($yloc),loc(7.0),loc($height['parentbox']),"left",null);

  
 foreach($studentinfoarray AS $studentinfo)
 {
	$yloc-=$height['parentsigspace'];
	//signature line
	pdf_moveto($pdf,loc(1),loc($yloc));
	pdf_lineto($pdf,loc(4.5),loc($yloc));
	pdf_stroke($pdf);

	//date line
	pdf_moveto($pdf,loc(5),loc($yloc));
	pdf_lineto($pdf,loc(7),loc($yloc));
	pdf_stroke($pdf);

	$yloc-=$height['parentsigtext'];
 	//show their name
	pdf_show_xy($pdf,"Parent/Guardian of $studentinfo->firstname $studentinfo->lastname (signature)",loc(1.25),loc($yloc));

	//show the Date text
	pdf_show_xy($pdf,"Date",loc(5.25),loc($yloc));

 }


 //now put a nice little footer at the bottom
 $footertext="Reg Num: ".$_SESSION['registration_number']." - Generated: ".date("Y-m-d h:ia")." - ".$config['fairname']." - ".$config['FAIRYEAR'];
 $footerwidth=pdf_stringwidth($pdf,$footertext,$normalfont,9);
 pdf_setfont($pdf,$normalfont,9);
 pdf_show_xy($pdf,$footertext,loc(4.25)-$footerwidth/2,loc(0.5));

 //footer line
 pdf_moveto($pdf,loc(0.5),loc(0.7));
 pdf_lineto($pdf,loc(8.0),loc(0.7));
 pdf_stroke($pdf);

 pdf_end_page($pdf);
 pdf_close($pdf);
 $pdfdata=pdf_get_buffer($pdf);
 header("Content-type: application/pdf");
 header("Content-disposition: inline; filename=sfiab_sig_".$_SESSION['registration_id'].".pdf");
 header("Content-length: ".strlen($pdfdata));
 echo $pdfdata;
?>