<?
 require("common.inc.php");
 include "register_participants.inc.php";
 require("lpdf.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!

$pdf=new lpdf(	i18n($config['fairname']),
	i18n("Participant Signature Page"),
	$_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"
	);

$pdf->newPage();
 $height['sigspace']=0.50;
 $pdf->setFontSize(11);

/*
//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['exhibitorsigtext']=0.13;
 $height['parenttitle']=0.2;
 $height['parentbox']=2.80;
 $height['parentsigtext']=0.13;

*/

 //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
 $pdf->heading(i18n("Registration Summary"));
 $topboxtext=substr($topboxtext,0,-2);
 //add the newline
// $topboxtext.="\n";
			 
 $pdf->addText($topboxtext);
 $pdf->hr();
 $pdf->heading(i18n("Exhibitor Declaration"));

 $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";

 $pdf->addText($studentbox);

 foreach($studentinfoarray AS $studentinfo)
 {
 	$pdf->vspace($height['sigspace']);

	//signature line
	$pdf->hline(1,4.5);
	//date line
	$pdf->hline(5,7);
	//go to next line
	$pdf->nextLine();

 	//show their name
	$pdf->addTextX(i18n("%1 %2 (signature)",array($studentinfo->firstname,$studentinfo->lastname)),1.25);
	//show the Date text
	$pdf->addTextX(i18n("Date"),5.25);

	//go to next line
	$pdf->nextLine();
 }

 $pdf->hr();

 //now for the parent/guardian signatures
 $pdf->heading(i18n("Parent/Guardian Declaration"));

 $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";

 $pdf->addText($parentbox);

  
 foreach($studentinfoarray AS $studentinfo)
 {
 	$pdf->vspace($height['sigspace']);

	//signature line
	$pdf->hline(1,4.5);

	//date line
	$pdf->hline(5,7);
	$pdf->nextLine();

 	//show their name
	$pdf->addTextX(i18n("Parent/Guardian of %1 %2 (signature)",array($studentinfo->firstname,$studentinfo->lastname)),1.25);

	//show the Date text
	$pdf->addTextX(i18n("Date"),5.25);
	$pdf->nextLine();

 }


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