<?php
#
# mailing_labels.php (c) 2007 Dan "Ducky" Little
# Written for the GeoMOOSE project, sponsored by the OpenMNND Organization
#

# Include GeoMOOSE PHP Utility Function Library
include('geomoose_php_util.php');
include('Configuration.php');

# Make the PHP Script Shut Up, but still give something useful if there
# is a legit error or parsing bug.
error_reporting(E_ERROR | E_PARSE);

# Get the basics from CGI
$queryId = $_REQUEST['query'];
$outputFormat = $_REQUEST['output'];





# Load up the shapes from the previous selection

$shapefile = ms_newShapefileObj($TEMP_DIRECTORY.$queryId.'_buffered',-1);
$selectionShape = ms_newShapeObj(MS_SHAPE_POLYGON);
for($i = 0; $i < $shapefile->numshapes; $i++) {
	$shape = $shapefile->getShape($i);
	if($selectionShape->numlines <= 0) {
		for($l = 0; $l < $shape->numlines; $l++) {
			$selectionShape->add($shape->line($l));
		}
	} else {
		$selectionShape = $selectionShape->union_geos($shape);
	}
}
$shapefile->free();


$parcelsMap = ms_newMapObj($PARCELS_MAP);
$shapes = getShapesFromLayer($parcelsMap, $PARCELS_LAYER, $selectionShape);

$parcels = $parcelsMap->getLayerByName($PARCELS_LAYER);
$parcels->open();

$columns  = getLayerColumnHash($parcels);
$numShapes = count($shapes);
$numLines = count($LABEL_LINES);

$addressLabels = array();
$currentLabel = 0;

for($i = 0; $i < $numShapes; $i++) {
	$addressLines = array();
	$currentLine = 0;

	for($l = 0; $l < $numLines; $l++) {
		$line = $LABEL_LINES[$l];
		foreach($columns as $key => $value) {	
			$line = preg_replace('/\['.$key.'\]/', $shapes[$i]->getvalue($parcels,$key), $line);
			$line = preg_replace('/ +/',' ',$line);
			$line = preg_replace('/^ */','',$line);
		}
		if(($COLLAPSE_LINES == 1 and $line != ' ' and $line != '') or $COLLAPSE_LINES != 1) {
			$addressLines[$currentLine] = $line;
			$currentLine++;
		}

	}
	$addressLabels[$currentLabel] = $addressLines;
	$currentLabel++;
}
$parcels->close();

if($outputFormat == 'html') {
	print "<html>\n";
	print "<head><title>GeoMOOSE Address Labels Output</title></head>\n";
	print "<body>\n";
	foreach($addressLabels as $label) {
		foreach($label as $line) {
			print $line."<br/>\n";
		}
		print "<br/>\n";
	}

	print "</body>";
	print "</html>";
} elseif($outputFormat == 'csv') {
	header('Content-type: text/csv');
	header('Content-Disposition: attachment; filename="mailing_labels.csv"');
	$columnTitles = array();
	for($i = 0; $i < $numLines; $i++) {
		$columnTitles[$i] = "LINE".($i+1);
	}
	$str = '"'.implode('","',$columnTitles)."\"\n";
	print $str;

	foreach($addressLabels as $label) {
		$str = '"'.implode('","',$label)."\"\n";
		print $str;
	}

} elseif($outputFormat == 'pdf') {
	header('Content-type: application/pdf');
	#header('Content-Disposition: attachment; filename="mailing_labels.pdf"');
	# This prevents fpdf from being loaded if it's not needed.
	require($FPDF_LOCATION.'/fpdf.php');

	# New PDF Object
	$PageSize = array(11,8.5);

	$pdf = new FPDF('L','in', $PageSize);
	$pdf->SetAutoPageBreak(false);

	$pdf->AddPage();
	$pdf->SetMargins(.5,.5,.5);
	$pdf->SetFont($LABEL_FONT,'',$LABEL_FONT_SIZE);
	$pdf->SetTextColor(0,0,0);


	$CurrentX = $LABEL_ORIGIN_X;
	$CurrentY = $LABEL_ORIGIN_Y;
	$CurrentRow = 0;
	$CurrentColumn = 0;
	foreach($addressLabels as $label) {
		$lineY = $CurrentY;
		foreach($label as $line) {
			$lineFontSize = $LABEL_FONT_SIZE;
			$pdf->SetXY($CurrentX,$lineY);
			if($LABELS_AUTOFIT_WIDTH) {	
				while($pdf->GetStringWidth($line) > $LABEL_WIDTH) {
					$lineFontSize--;
					$pdf->SetFontSize($lineFontSize);
				}
			}

			$pdf->Cell(0,.25,$line,0,0,'L');
			# Make the lines even despite variable sizes
			$lineY = $lineY + $LABEL_FONT_SIZE/72;
		}
		$CurrentY += $LABEL_HEIGHT;
		$CurrentRow++;
		if($CurrentRow >= $LABEL_ROWS) {
			$CurrentRow = 0;
			$CurrentColumn ++;
			$CurrentX+=$LABEL_WIDTH;
			$CurrentY = $LABEL_ORIGIN_Y;
			if($CurrentColumn >= $LABEL_COLUMNS) {
				$CurrentColumn = 0;
				$CurrentRow = 0;
				$CurrentY = $LABEL_ORIGIN_Y;
				$CurrentX = $LABEL_ORIGIN_X;
				$pdf->AddPage();
				$pdf->SetMargins(.5,.5,.5);

			}
		}
	}
	#$pdf->Output('out.pdf');
	$pdf->Output();
}
?>
