<?php

require('fpdf/fpdf.php');

function printPDF($xml,$outputPdf, $mapOverride = false, $mapLayer = false, $mapItem = false) {
	global $PARENT_DIRECTORY;
	$PDF_SEARCH_MAP = $PARENT_DIRECTORY.'select.map';
	$PDF_SEARCH_LAYER = 'Parcels';
	$PDF_SEARCH_ITEM = 'PIN';
	if($mapOverride != false) { $PDF_SEARCH_MAP = $mapOverride; }
	if($mapLayer != false) { $PDF_SEARCH_LAYER = $mapLayer; }
	if($mapItem != false) { $PDF_SEARCH_ITEM = $mapItem; }

	$showParcel = false;

	$inputObj = $xml->getElementsByTagName('input')->item(0);

	$extent = split(' ',$inputObj->getAttribute('mapext'));

	$canvasObj = $xml->getElementsByTagName('canvas')->item(0);

	$title = 'GeoMOOSE Map';
	for($i = 0; $i < $canvasObj->childNodes->length && !$mapTitle; $i++) {
		$child = $canvasObj->childNodes->item($i);
		if($child->tagName == 'param') {
			if($child->getAttribute('name') == 'title') {
				$title = $child->getAttribute('value');
			}
		}
	}
	$quality = ($inputObj->hasAttribute('quality') ? $inputObj->getAttribute('quality') : 2);

	# Check for parcels
	$parcels = $inputObj->getElementsByTagName('parcels');
	if($parcels->length > 0 && $parcels->item(0)->firstChild) {
		$parcels = split(',', $parcels->item(0)->firstChild->nodeValue);
		if(count($parcels) == 1) {
			$showParcel = true;
			$searchMap = ms_newMapObj($PDF_SEARCH_MAP);
			$searchMap->setExtent($extent[0],$extent[1],$extent[2],$extent[3]);
			$numResults = 0;

			#error_reporting(E_PARSE);
			$searchLayer = $searchMap->getLayerByName($PDF_SEARCH_LAYER);
			$results = $searchLayer->queryByAttributes($PDF_SEARCH_ITEM, $parcels[0], MS_SINGLE);
			$numResults = $searchLayer->getNumResults();

			if($numResults == 0) {
				$showParcel = false;
			}

			if ($numResults == 1) {
				$searchLayer->open();
				for ($iRes=0; $iRes < $numResults; $iRes++)
				{
					$oRes = $searchLayer->getResult($iRes);
					$oShape = $searchLayer->getShape($oRes->tileindex,$oRes->shapeindex);
			$pin = $oShape->values["PIN"];
			$taxpayer = $oShape->values["OWNAME"];
			$ownadd1 = $oShape->values["OWADR1"];
			$ownadd2 = $oShape->values["OWADR2"];
			$ownadd3 = $oShape->values["OWCITY"] .", ".$oShape->values["OWSTAT"] ." ".$oShape->values["OWZIPF"];
			$propadd = $oShape->values["ADPH2"] ." ".$oShape->values["ADPSNM"] ." ".$oShape->values["ADPSDR"] ." ".$oShape->values["ADPZIP2"];
			$gis_acres = $oShape->values["GIS_ACRES"];
			$avcer = $oShape->values["AVCER"];
			$aptown2 = $oShape->values["APTOWN2"];
			$aprang2 = $oShape->values["APRANG2"];
			$apsect2 = $oShape->values["APSECT2"];
			$web_notes = $oShape->values["PLATDESC"];
					$oShape->free();
				}
				$searchLayer->close();
			}

		}
	}

#$querytype=(isset($_REQUEST['querytype']))?$_REQUEST['querytype']:"spatial";
#$queryfield=(isset($_REQUEST['queryfield']))?$_REQUEST['queryfield']:"none";
#$querystring=(isset($_REQUEST['querystring']))?$_REQUEST['querystring']:"none";


#$map = ms_newMapObj('douglas_print.map');
#$map->selectOutputFormat('jpeg');
//I need to add in code so this works with an item query.
//draw selected parcels
/*
if ($_REQUEST['querystatus'] == "true") {
	$map2 = ms_newMapObj('douglas_queries.map');
	$map2->setExtent($extent[0], $extent[1],$extent[2], $extent[3]);
	$query_extent=trim($_REQUEST['query_extent']);
	$queryextent=explode(' ',$query_extent);
	$queryextent2 = array();
	if ($queryextent[0] > $queryextent[2]) {
		$queryextent2[0] = $queryextent[2];
		$queryextent2[2] = $queryextent[0];
	}  else {
		$queryextent2[0] = $queryextent[0];
		$queryextent2[2] = $queryextent[2];
	}
	if ($queryextent[1] > $queryextent[3]) {
		$queryextent2[1] = $queryextent[3];
		$queryextent2[3] = $queryextent[1];
	}  else {
		$queryextent2[1] = $queryextent[1];
		$queryextent2[3] = $queryextent[3];
	}


	$layerObj = $map2->getLayerByName("parcel_query");

	if ($querytype == "spatial") {
		  $rect = ms_newRectObj();
		  $rect->setExtent($queryextent2[0], $queryextent2[1], $queryextent2[2], $queryextent2[3]);
		  $results = $layerObj->queryByRect($rect);
		} else {
		$queryString2 = "('[" . $queryfield . "]' = '" . $querystring . "')";
		$results = $layerObj->queryByAttributes($queryfield, $queryString2, MS_SINGLE);
	}

	//get the attributes of the selected parcels, only if one parcel is selected
	$numResults = $layerObj->getNumResults();
	if ($numResults = 1) {
		$layerObj->open();
		for ($iRes=0; $iRes < $numResults; $iRes++)
		{
			$oRes = $layerObj->getResult($iRes);
			$oShape = $layerObj->getShape($oRes->tileindex,$oRes->shapeindex);
			$pin = $oShape->values["PIN"];
			$taxpayer = $oShape->values["OWNAME"];
			$ownadd1 = $oShape->values["OWADR1"];
			$ownadd2 = $oShape->values["OWADR2"];
			$ownadd3 = $oShape->values["OWCITY"] .", ".$oShape->values["OWSTAT"] ." ".$oShape->values["OWZIPF"];
			$propadd = $oShape->values["ADPH2"] ." ".$oShape->values["ADPSNM"] ." ".$oShape->values["ADPSDR"] ." ".$oShape->values["ADPZIP2"];
			$gis_acres = $oShape->values["GIS_ACRES"];
			$avcer = $oShape->values["AVCER"];
			$aptown2 = $oShape->values["APTOWN2"];
			$aprang2 = $oShape->values["APRANG2"];
			$apsect2 = $oShape->values["APSECT2"];
			$web_notes = $oShape->values["PLATDESC"];
			$oShape->free();
		}
		$layerObj->close();
	}
}
*/


// Go through each layer and adjust label sizes
/*
for ($i=0; $i < $numLayers; $i++) {
  $layer = $map->getlayer($i);
  if ($layer->status == 1 ){
    for ($j=0; $j < $layer->numclasses; $j++) {
      $class = $layer->getClass($j);
      $label = $class->label;
      if ($label->mindistance>1){
        $md=$label->mindistance;
        $newmd=$md*$scale;
        $label->set('mindistance',$newmd);
      }
      if ($label->type == 'TRUETYPE') {
        $labelsize=$label->size;
        $newlabelsize=$labelsize*$scale*$labelfudge;
        $label->set('size',$newlabelsize);
      }
      for ($k=0; $k < $class->numstyles; $k++) {
        $style = $class->getStyle($k);
        $style->set("size", $style->size * $scale * $stylefudge);
        $style->set("offsetx", $style->offsetx * $scale);
        $style->set("offsety", $style->offsety * $scale);
      }
    }
  }
}
*/

	//Determine Map Size
	$mapheight=9;
	$mapwidth = 7.6;
	$legheight=0;
	$legstart=$mapheight+.85;

	if($showParcel) {
		$mapheight=7.5;
		$legheight=1.3;
		$legstart=$mapheight+.85;
	}
	//Start fpdf to place all of the components
	class PDF extends FPDF
	{
	  //Page header
	  function Header()
	  {
		   #global $title;
		   #$this->SetFont('Arial','B',12);
		   #$this->Cell(0,.50,$title,0,1,'C');
	  }

	  //Page footer
	  function Footer()
	  {
		   $this->SetY(-1);
		   $this->SetFont('Arial','I',7);
	  }
	}

	//Instanciation of inherited class
	$pdf=new PDF('P','in','letter');

	$pdf->SetDisplayMode('fullpage');
	$pdf->SetMargins(.25,.25);

	$pdf->AddPage();
	$pdf->SetFont('Arial', 'B', 12);
	$pdf->Text(.5,.50,$title,0,1,'C');

	#$pdf->SetX(.5);
	#$pdf->SetY(.5);
	#$pdf->Cell(3.5,.50,$title,0,1,'C');

	$pdf->SetLineWidth(.1);
	$pdf->SetDrawColor(0,0,150);
	$pdf->Rect(.25,.25,8,10.5);

	//add outline around map image
	$pdf->SetLineWidth(.01);
	$pdf->SetDrawColor(0,0,200);
	$pdf->Rect(.40,.65,7.7,$mapheight+.1);
	if ($legheight > 0) {
		$pdf->Rect(.40,$legstart,7.7,$legheight+.1);
		$text2="Parcel Report";
		$text3="PIN: " . $pin;
		$text4="Taxpayer: " . $taxpayer;
		$text7="Taxpayer Address: ".$ownadd1;
		$text8="Taxpayer Address: ".$ownadd2;
		$text9="Taxpayer Address: ".$ownadd3;
		$text10="Prop. Address: ".$propadd;
		$text5="GIS Acres: " . $gis_acres;
		$text13="Twp/Rng/Sec.: " . $aptown2."/".$aprang2."/".$apsect2;
		$text6="Plat Desc.: " . $web_notes;
		$text14="CER: " . abs($avcer);
		$pdf->SetFont('Arial','B',12);
		$pdf->Text(.42,8.55,$text2);
		$pdf->SetFont('Arial','',10);
		$pdf->Text(.5,8.75,$text3);
		$pdf->Text(.5,8.95,$text4);
		$pdf->Text(.5,9.15,$text7);
		$pdf->Text(.5,9.35,$text8);
		$pdf->Text(.5,9.55,$text9);
		$pdf->Text(4.5,8.75,$text10);
		$pdf->Text(4.5,8.95,$text5);
		$pdf->Text(4.5,9.15,$text14);
		$pdf->Text(4.5,9.35,$text13);
		$pdf->Text(4.5,9.55,$text6);
	}

	//adds footer text
	$date=date('M j, Y g:i');
	$datemessage="Map created on $date.
	This information is to be used for reference purposes only.";
	$copyright="Copyright © 2008 GIS, All Rights Reserved";
	$disclaimer1 = "Disclaimer: Line1 goes here.";
	$disclaimer2 = "Line 2 goes here.";
	$disclaimer3 = "Line 3 goes here.";
	$pdf->SetFont('Arial','',8);
	$pdf->Text(.42,9.9,$disclaimer1);
	$pdf->Text(.42,10.03,$disclaimer2);
	$pdf->Text(.42,10.16,$disclaimer3);
	$pdf->SetFont('Arial','I',8);
	$pdf->Text(.42,10.55,$datemessage);
	$pdf->Text(.42,10.68,$copyright);
	$pdf->Image("resources/north.jpg",7.5,10,.6,.6);

	//output file
	$pdf->SetTitle('GeoMOOSE');
	renderImage($xml, $outputPdf.'.jpeg', $mapwidth*$quality*72, $mapheight*$quality*72,
		$extent[0], $extent[1], $extent[2], $extent[3], $quality);

	$pdf->Image($outputPdf.'.jpeg',.45,.70,$mapwidth,$mapheight);


	$pdf->Output($outputPdf.'.pdf');
}

?>
