<?php

$head_string = '<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Spine Label Generation</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>';
$foot_string = '</body></html>';
$label_template = 'labels.doc';

switch($_POST['template'])
{
	case 'fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel':
		include('40_28_template.inc');
		$tr_start = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_tr_start;
		$td_start = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_td_start;
		$td_end = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_td_end;
		$tr_end = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_tr_end;
		$doc_header = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_header;
		$doc_footer = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_footer;
		$label_mid_start = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_mid_start;
		$label_mid_end = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_mid_end;
		$label_count = $fourtyL_twenty_eightW_thirteen_arial_bold_spinelabel_counter;
		break;
	case 'thirty_eightW_twenty_fiveL_thirteen_arial_bold_spinelabel':
		include('38_25_template.inc');
		$tr_start = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_tr_start;
		$td_start = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_td_start;
		$td_end = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_td_end;
		$tr_end = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_tr_end;
		$doc_header = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_header;
		$doc_footer = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_footer;
		$label_mid_start = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_mid_start;
		$label_mid_end = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_mid_end;
		$label_count = $thirty_eightW_twenty_fiveL_thirteen_arial_bold_counter;
		break;
	case 'thirty_twoL_twenty_fourW_fourteen_arial_bold_spinelabel':
		include('32_24_template.inc');
		$tr_start = $thirty_twoL_twenty_fourW_fourteen_arial_bold_tr_start;
		$td_start = $thirty_twoL_twenty_fourW_fourteen_arial_bold_td_start;
		$td_end = $thirty_twoL_twenty_fourW_fourteen_arial_bold_td_end;
		$tr_end = $thirty_twoL_twenty_fourW_fourteen_arial_bold_tr_end;
		$doc_header = $thirty_twoL_twenty_fourW_fourteen_arial_bold_header;
		$doc_footer = $thirty_twoL_twenty_fourW_fourteen_arial_bold_footer;
		$label_mid_start = $thirty_twoL_twenty_fourW_fourteen_arial_bold_mid_start;
		$label_mid_end = $thirty_twoL_twenty_fourW_fourteen_arial_bold_mid_end;
		$label_count = $thirty_twoL_twenty_fourW_fourteen_arial_bold_counter;
		break;
	case 'thirty_twoL_twenty_fourW_eighteen_arialnarrow_bold_lote':
		include('32_24_template.inc');
		$tr_start = $thirty_twoL_twenty_fourW_fourteen_arial_bold_tr_start;
		$td_start = $thirty_twoL_twenty_fourW_eighteen_arialnarrow_bold_td_start;
		$td_end = $thirty_twoL_twenty_fourW_fourteen_arial_bold_td_end;
		$tr_end = $thirty_twoL_twenty_fourW_fourteen_arial_bold_tr_end;
		$doc_header = $thirty_twoL_twenty_fourW_eighteen_arialnarrow_bold_header;
		$doc_footer = $thirty_twoL_twenty_fourW_fourteen_arial_bold_footer;
		$label_mid_start = $thirty_twoL_twenty_fourW_eighteen_arialnarrow_bold_mid_start;
		$label_mid_end = $thirty_twoL_twenty_fourW_fourteen_arial_bold_mid_end;
		$label_count = $thirty_twoL_twenty_fourW_fourteen_arial_bold_counter;
		break;
	case 'whitehorse_thirty_twoL_twenty_fourw':
		include('whitehorse_template.inc');
		$tr_start = $whitehorse_tr_start;
		$td_start = $whitehorse_td_start;
		$td_end = $whitehorse_td_end;
		$tr_end = $whitehorse_tr_end;
		$doc_header = $whitehorse_doc_header;
		$doc_footer = $whitehorse_doc_footer;
		$label_mid_start = $whitehorse_label_mid_start;
		$label_mid_end = $whitehorse_label_mid_end;
		$label_count = $whitehorse_label_count;
		break;
	case 'moreton_32L_24W':
		include('moreton_template.inc');
		$tr_start = $moreton_tr_start;
		$td_start = $moreton_td_start;
		$td_end = $moreton_td_end;
		$tr_end = $moreton_tr_end;
		$doc_header = $moreton_doc_header;
		$doc_footer = $moreton_doc_footer;
		$label_mid_start = $moreton_label_mid_start;
		$label_mid_end = $moreton_label_mid_end;
		$label_count = $moreton_label_count;
		break;
	case 'rockdale_dvd':
		include('rockdale_dvd_template.inc');
		$tr_start = $rockdale_dvd_tr_start;
		$td_start = $rockdale_dvd_td_start;
		$td_end = $rockdale_dvd_td_end;
		$tr_end = $rockdale_dvd_tr_end;
		$doc_header = $rockdale_dvd_doc_header;
		$doc_footer = $rockdale_dvd_doc_footer;
		$label_mid_start = $rockdale_dvd_label_mid_start;
		$label_mid_end = $rockdale_dvd_label_mid_end;
		$label_count = $rockdale_dvd_label_count;
		break;
	case 'prenax_bh':
		include('prenax_bh.inc');
		$tr_start = $prenax_tr_start;
		$td_start = $prenax_td_start;
		$td_end = $prenax_td_end;
		$tr_end = $prenax_tr_end;
		$doc_header = $prenax_doc_header;
		$doc_footer = $prenax_doc_footer;
		$label_mid_start = $prenax_label_mid_start;
		$label_mid_end = $prenax_label_mid_end;
		$label_count = $prenax_label_count;
		break;
	case 'slwa_twenty_twoL_thirty_fiveW':
		include('slwa_template.inc');
		$tr_start = $slwa_tr_start;
		$td_start = $slwa_td_start;
		$td_end = $slwa_td_end;
		$tr_end = $slwa_tr_end;
		$doc_header = $slwa_doc_header;
		$doc_footer = $slwa_doc_footer;
		$label_mid_start = $slwa_label_mid_start;
		$label_mid_end = $slwa_label_mid_end;
		$label_count = $slwa_label_count;
		break;
	case 'slwa_22L_35W_english':
		include('slwa_template.inc');
		$tr_start = $slwa_tr_start;
		$td_start = $slwa_td_start;
		$td_end = $slwa_td_end;
		$tr_end = $slwa_tr_end;
		$doc_header = $slwa_doc_header;
		$doc_footer = $slwa_doc_footer;
		$label_mid_start = $slwa_label_mid_start;
		$label_mid_end = $slwa_label_mid_end;
		$label_count = $slwa_label_count;
		break;
	case 'sutherland_22L_35W':
		include('sutherland_template.inc');
		$tr_start = $sutherland_tr_start;
		$td_start = $sutherland_td_start;
		$td_end = $sutherland_td_end;
		$tr_end = $sutherland_tr_end;
		$doc_header = $sutherland_doc_header;
		$doc_footer = $sutherland_doc_footer;
		$label_mid_start = $sutherland_label_mid_start;
		$label_mid_end = $sutherland_label_mid_end;
		$label_count = $sutherland_label_count;
		break;
	case 'VCML14':
		include('vcml_template_a4.inc');
		$tr_start = $vcml_tr_start;
		$td_start = $vcml_td_start;
		$td_end = $vcml_td_end;
		$tr_end = $vcml_tr_end;
		$doc_header = $vcml_doc_header;
		$doc_footer = $vcml_doc_footer;
		$label_mid_start = $vcml_mid12_start;
		$label_mid_end = $vcml_mid_end;
		$label_count = $vcml_label_count;
		break;
	case 'VCML14_landscape':
		include('vcml_landscape.inc');
		$tr_start = $vcml_landscape_tr_start;
		$td_start = $vcml_landscape_td_start;
		$td_end = $vcml_landscape_td_end;
		$tr_end = $vcml_landscape_tr_end;
		$doc_header = $vcml_landscape_header;
		$doc_footer = $vcml_landscape_footer;
		$label_mid_start = $vcml_landscape_mid12_start;
		$label_mid_end = $vcml_landscape_mid_end;
		$label_count = $vcml_landscape_label_count;
		break;
	case 'monash_verdana':
		include('monash_template.inc');
		$tr_start = $monash_tr_start;
		$td_start = $monash_td_start;
		$td_end = $monash_td_end;
		$tr_end = $monash_tr_end;
		$doc_header = $monash_doc_header;
		$doc_footer = $monash_doc_footer;
		$label_mid_start = $monash_label_mid_start;
		$label_mid_end = $monash_label_mid_end;
		$label_count = $monash_label_count;
		break;
	case 'SLWA_Book':
	case 'MPLS_Book':
		include('MPLS_book_template.inc');
		$tr_start = $MPLS_book_tr_start;
		$td_start = $MPLS_book_td_start;
		$td_end = $MPLS_book_td_end;
		$tr_end = $MPLS_book_tr_end;
		$doc_header = $MPLS_book_doc_header;
		$doc_footer = $MPLS_book_doc_footer;
		$label_mid_start = $MPLS_book_label_mid_start;
		$label_mid_end = $MPLS_book_label_mid_end;
		$label_count = $MPLS_book_label_count;
		break;
	case 'VU_Book_Label':
		include('VU_book_template.inc');
		$tr_start = $VU_book_tr_start;
		$td_start = $VU_book_td_start;
		$td_end = $VU_book_td_end;
		$tr_end = $VU_book_tr_end;
		$doc_header = $VU_book_doc_header;
		$doc_footer = $VU_book_doc_footer;
		$label_mid_start = $VU_book_label_mid_start;
		$label_mid_end = $VU_book_label_mid_end;
		$label_count = $VU_book_label_count;
		break;
	case 'VU_32L_24W':
		include('VU_32_24_template.inc');
		$tr_start = $VU_32_24_tr_start;
		$td_start = $VU_32_24_td_start;
		$td_end = $VU_32_24_td_end;
		$tr_end = $VU_32_24_tr_end;
		$doc_header = $VU_32_24_doc_header;
		$doc_footer = $VU_32_24_doc_footer;
		$label_mid_start = $VU_32_24_label_mid_start;
		$label_mid_end = $VU_32_24_label_mid_end;
		$label_count = $VU_32_24_label_count;
		break;
		// CQ Addition End 1 7/10/2014 - Auburn SpineLabel
	case 'AUBURN_Test':
		include('auburn_temp_template.inc');
		$tr_start = $AUBURN_Temp_tr_start;
		$td_start = $AUBURN_Temp_td_start;
		$td_end = $AUBURN_Temp_td_end;
		$tr_end = $AUBURN_Temp_tr_end;
		$doc_header = $AUBURN_Temp_doc_header;
		$doc_footer = $AUBURN_Temp_doc_footer;
		$label_mid_start = $AUBURN_Temp_label_mid_start;
		$label_mid_end = $AUBURN_Temp_label_mid_end;
		$label_count = $AUBURN_Temp_label_count;
		$label_template = 'auburn_label.doc';
		break;
		// CQ Addition End 1 7/10/2014 - Auburn SpineLabel
	case 'blacktown':
		include('blacktown_template.inc');
		$tr_start = $blacktown_tr_start;
		$td_start = $blacktown_td_start;
		$td_end = $blacktown_td_end;
		$tr_end = $blacktown_tr_end;
		$doc_header = $blacktown_doc_header;
		$doc_footer = $blacktown_doc_footer;
		$label_mid_start = $blacktown_label_mid_start;
		$label_mid_end = $blacktown_label_mid_end;
		$label_count = $blacktown_label_count;
		break;
	case 'unimelb':
		include('unimelb_template.inc');
		$tr_start = $unimelb_tr_start;
		$td_start = $unimelb_td_start;
		$td_end = $unimelb_td_end;
		$tr_end = $unimelb_tr_end;
		$doc_header = $unimelb_doc_header;
		$doc_footer = $unimelb_doc_footer;
		$label_mid_start = $unimelb_label_mid_start;
		$label_mid_end = $unimelb_label_mid_end;
		$label_count = $unimelb_label_count;
		$label_template = 'um_labels.doc';
		break;
	case 'ato':
		include('ato_template.inc');
		$tr_start = $ato_tr_start;
		$td_start = $ato_td_start;
		$td_end = $ato_td_end;
		$tr_end = $ato_tr_end;
		$doc_header = $ato_doc_header;
		$doc_footer = $ato_doc_footer;
		$label_mid_start = $ato_label_mid_start;
		$label_mid_end = $ato_label_mid_end;
		$label_count = $ato_label_count;
		$label_template = 'um_labels.doc';
		break;
}

function findall($needle, $haystack)
{
	//Setting up
	$buffer=''; //We will use a 'frameshift' buffer for this search
	$pos=0; //Pointer
	$end = strlen($haystack); //The end of the string
	$getchar=''; //The next character in the string
	$needlelen=strlen($needle); //The length of the needle to find (speeds up searching)
	$found = array(); //The array we will store results in

	while($pos<$end)//Scan file
	{
		$getchar = substr($haystack,$pos,1); //Grab next character from pointer
		if($getchar!="\n" || buffer<$needlelen) //If we fetched a line break, or the buffer is still smaller than the needle, ignore and grab next character
		{
			$buffer = $buffer . $getchar; //Build frameshift buffer
			if(strlen($buffer)>$needlelen) //If the buffer is longer than the needle
			{
				$buffer = substr($buffer,-$needlelen);//Truncunate backwards to needle length (backwards so that the frame 'moves')
			}
			if($buffer==$needle) //If the buffer matches the needle
			{
				$found[]=$pos-$needlelen+1; //Add the location of the needle to the array. Adding one fixes the offset.
			}
		}
		$pos++; //Increment the pointer
	}
	if(array_key_exists(0,$found)) //Check for an empty array
	{
		return $found; //Return the array of located positions
	}
	else
	{
		return false; //Or if no instances were found return false
	}
}

$target = "/var/www/htdocs/spine_labels/uploads/spine_label.xml";
//$target = $target . basename( $_FILES['upload']['name']) ;
//echo "Target: ".$target."<br />";
//echo "Tempname: ".$_FILES['upload']['tmp_name']."<br />";
$ok=1;
$up_success=1;

//This is our size condition
if ($uploaded_size > 350000)
{
	echo $head_string."Your file is too large.<br>".$foot_string;
	$ok=0;
}

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
	echo $head_string."No PHP files<br>".$foot_string;
	$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
	Echo $head_string."Sorry your file was not uploaded".$foot_string;
}

//If everything is ok we try to upload it
else
{
	if(move_uploaded_file($_FILES['upload']['tmp_name'], $target))
	{
		//echo "The spine label file has been sucessfully uploaded<br /><br />";
		$up_success = 0;
	}
	else
	{
		echo $head_string."Sorry, there was a problem uploading your file.".$foot_string;
	}
}

$line_no = 0;
if($up_success==0)
{
	$handle = @fopen($target, "r");
	$fp = fopen("/var/www/htdocs/spine_labels/uploads/$label_template", 'w+');
	fwrite($fp, $str);

	$col_count = 0;
	$table_row = 0;
	$xml = simplexml_load_file("spine_label.xml");

	if($_POST['template']=='unimelb')
	{

		foreach($xml->{'section-01'} as $child)
		{
			$version = '';
			$label_text = $child->{'z30-call-no'};
			$label_parts = explode("\$\$h",$label_text);
			$location = $label_parts[0];
			$call_no = $label_parts[1];
			if(strpos($call_no,"\$\$j") == true)
			{
				$version_part = explode("\$\$j",$call_no);
				$call_no = $version_part[0];
				$version = $version_part[1];
			}
			if($col_count==0)
			{
				if ($table_row==3)
				{
					$doc_text .= '<tr style="page-break-inside:avoid;height:118pt">';

				}
				else
				{
					$doc_text .= $tr_start;
				}
			}
			$doc_text .= $td_start;

			if(strpos($location,"Vet P") == true)
			{
				$location = str_replace("Vet P", "Vet~P", $location);
			}
			if(strpos($location,"Vet W") == true)
			{
				$location = str_replace("Vet W", "Vet~W", $location);
			}
			if(strpos($location,"ERC Archit") == true)
			{
				$location = str_replace("ERC Archit", "ERC~Archit", $location);
			}

			if(strpos($location,"Law") == true)
			{
				$doc_text .= $label_mid_start.$location.$label_mid_end;
			}
			elseif(strpos($location," f") == true)
			{
				$location_parts = explode(" ",$location,3);

				//$location_parts[1] = str_replace("~", " ", $location_parts[1]);

				$doc_text .= $label_mid_start.$location_parts[0].$label_mid_end;
				$doc_text .= $label_mid_start.str_replace("~", " ", $location_parts[1]).$label_mid_end;
				$doc_text .= $label_mid_start.$location_parts[2].$label_mid_end;
			}
			else
			{
				$location_parts = explode(" ",$location,2);

				$doc_text .= $label_mid_start.$location_parts[0].$label_mid_end;
				$doc_text .= $label_mid_start.str_replace("~", " ", $location_parts[1]).$label_mid_end;
			}

			$doc_text .= $label_mid_start.'<br />'.$label_mid_end;

			$call_no = str_replace("/", "/ ", $call_no);
			$call_no = str_replace("^", "/ ", $call_no);
			$split_text = split(" ", $call_no);
			$split_length = sizeof($split_text);
			$split_count = 0;

			while($split_count<$split_length)
			{
				if(substr_count($split_text[$split_count],'.')>0)
				{
					$doc_text .= $label_mid_start.substr($split_text[$split_count],0,8).$label_mid_end;
					$doc_text .= $label_mid_start.substr($split_text[$split_count],8).$label_mid_end;
				}
				else
				{
					$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
				}
				$split_count++;
			}
			if(strlen($version) > 1)
			{
				$doc_text .= $label_mid_start.$version.$label_mid_end;
			}

			$doc_text .= $td_end;

			//echo $call_no.'<br />';
			//echo 'xml_count= '.$xml_count.', label_count= '.$label_count.'<br />';
			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				if ($table_row==4)
				{ $table_row = 0;}
				$col_count = 0;
			}
		}
	}
	elseif($_POST['template']=='ato')
	{

		foreach($xml->{'section-01'} as $child)
		{
			$label_text = $child->{'z30-call-no'};
			$label_parts = explode(" ",$label_text);

			if($col_count==0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;

			$doc_text .= $label_mid_start.$label_parts[0].$label_mid_end;
			$doc_text .= $label_mid_start.$label_parts[1].$label_mid_end;
			$doc_text .= $label_mid_start.$label_parts[2].$label_mid_end;

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
		}
	}
	elseif($_POST['template']=='prenax_bh')
	{

		foreach($xml->{'section-01'} as $child)
		{
			$label_text = $child->{'z30-call-no'};
			$temp_found_text = str_replace(".", ". ", $label_text);
			$label_text = $temp_found_text;
			$label_parts = explode(" ",$label_text);

			if($col_count==0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;

			$doc_text .= $label_mid_start.$label_parts[0].$label_mid_end;
			$doc_text .= $label_mid_start.$label_parts[1].$label_mid_end;
			$doc_text .= $label_mid_start.$label_parts[2].$label_mid_end;
			$doc_text .= $label_mid_start.$label_parts[3].$label_mid_end;

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
		}
	}
	elseif($_POST['template']=='rockdale_dvd')
	{

		foreach($xml->{'section-01'} as $child)
		{
			$label_text = $child->{'z30-call-no'};
			$label_parts = explode(" ", $label_text);
			$firstLetter = substr($label_parts[count($label_parts)-1], 0, 1);

			if($col_count == 0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;

			$span_size = '<span style="font-size:75.0pt; font-family:\'Swis721 Blk BT\'">';

			if($firstLetter == 'W'){
				$span_size = '<span style="font-size:66.0pt; font-family:\'Swis721 Blk BT\'">';
			}
			elseif($firstLetter == 'M'){
				$span_size = '<span style="font-size:70.0pt; font-family:\'Swis721 Blk BT\'">';
			}

			$doc_text .= $label_mid_start.$span_size.$firstLetter.'</span>'.$label_mid_end;

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
		}
	}
	elseif($_POST['template']=='sutherland_22L_35W')
	{
		foreach($xml->{'section-01'} as $child)
		{
			$label_text = $child->{'z30-call-no'};
			/*if (preg_match("\$\$h[0-9]", $label_text)){
				$label_text = str_replace("\$\$h", "", $label_text);
				}
				else {
				$label_text = str_replace("\$\$h", " ", $label_text);
				}*/

			$label_text = preg_replace('/([A-Z]*)(\$\$h)([0-9]*\.*[0-9]*)(.*)/','$1$3 $4', $label_text);
			//$temp_found_text = str_replace("\$\$h", " ", $label_text);
			$label_text = str_replace("\$\$j", " ", $label_text);
			//$label_text = $temp_found_text;
			//$label_parts = explode(" ",$label_text);

			if($col_count==0)
			{
				//if ($table_row==9)
				//{
				//	$doc_text .= '<tr style="page-break-inside:avoid;height:118pt">';
				//}
				//else
				//{
				$doc_text .= $tr_start;
				//}
			}
			$doc_text .= $td_start;

			$doc_text .= $label_mid_start.$label_text.$label_mid_end;


			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				if($table_row<$sutherland_row_count)
				{
					//$doc_text .= $slwa_tr_middle.'table_row= '.$table_row.', slwa_row_count= '.$slwa_row_count;
					$doc_text .= $sutherland_tr_middle;
					//$table_row++;
				}
				elseif($table_row==$sutherland_row_count)
				{
					//$doc_text .= 'table row = row count';
					$doc_text .= $sutherland_page_break;
					$table_row = 0;
				}
				$col_count = 0;
			}
			else
			{
				$doc_text .= $sutherland_td_middle;
			}
		}
	}
	elseif($_POST['template']=='MPLS_Book' || $_POST['template']=='VU_Book_Label')
	{
		foreach($xml->{'section-01'} as $child)
		{
			$version = '';

			$library_text = $child->{'z30-sub-library'};
			if($_POST['template']=='MPLS_Book'){
				$collection_text = $child->{'z30-collection'};
			}

			$call_text = $child->{'z30-call-no'};
			$call_replace = array("\$\$h","\$\$i","\$\$j");
			$call_text = str_replace($call_replace, " ", $call_text);

			$title_text = $child->{'z13u-user-defined-6'};
			$title_text = preg_replace("% [/:;=].*%", "", $title_text);
			$barcode = $child->{'z30-barcode'};
			if($col_count==0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;
			if($_POST['template']=='MPLS_Book'){
				$doc_text .= $MPLS_lib_start.$library_text.$MPLS_lib_end;
				$doc_text .= $label_mid_start.$collection_text.' '.$call_text.$label_mid_end;
			}
			if($_POST['template']=='VU_Book_Label'){
				$doc_text .= $VU_lib_start.$library_text.$VU_lib_end;
				$doc_text .= $label_mid_start.$call_text.$label_mid_end;
			}

			$doc_text .= $label_mid_start.$title_text.$label_mid_end;
			if($_POST['template']=='MPLS_Book'){
				$doc_text .= $label_mid_start.'<br /><br />'.$label_mid_end;
			}
			$doc_text .= $label_mid_start.$barcode.$label_mid_end;

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
			else
			{
				$doc_text .= $MPLS_book_td_middle;
			}
		}
	}
	elseif($_POST['template']=='blacktown')
	{
		foreach($xml->{'section-01'} as $child)
		{
			$call_text = $child->{'z30-call-no'};
			$call_replace = array("\$\$e","\$\$f");
			$call_text = str_replace($call_replace, " ", $call_text);

			if($col_count==0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;

			$split_text = split(" ", $call_text);
			$split_length = sizeof($split_text);
			$split_count = 0;

			while($split_count<$split_length)
			{
				if(substr_count($split_text[$split_count],'.')>0)
				{
					$doc_text .= $label_mid_start.substr($split_text[$split_count],0,8).$label_mid_end;
				}
				else
				{
					$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
				}
				$split_count++;
			}

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
		}
	}
	
	elseif($_POST['template']=='VU_32L_24W')
	{
		foreach($xml->{'section-01'} as $child)
		{
			$call_text = $child->{'z30-call-no'};
			$call_replace = array("\$\$h","\$\$j");
			$call_text = str_replace("\$\$jv. ", "\$\$jv.~", $call_text);
			$call_text = str_replace($call_replace, " ", $call_text);
				

			if($col_count==0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;

			$split_text = split(" ", $call_text);
			$split_length = sizeof($split_text);
			$split_count = 0;

			while($split_count<$split_length)
			{
				if(substr_count($split_text[$split_count],'v.~')>0)
				{
					$doc_text .= $label_mid_start.str_replace("v.~", "v. ", $split_text[$split_count]).$label_mid_end;
				}
				elseif(substr_count($split_text[$split_count],'.')>0)
				{
					$doc_text .= $label_mid_start.substr($split_text[$split_count],0,7).$label_mid_end;
					$doc_text .= $label_mid_start.substr($split_text[$split_count],7).$label_mid_end;
				}
				else
				{
					$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
				}
				$split_count++;
			}

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
		}
	}
	elseif($_POST['template']=='SLWA_Book')
	{
		foreach($xml->{'section-01'} as $child)
		{
			$version = '';

			$call_text = $child->{'z13u-user-defined-2'};
			$call_replace = array("\$\$h","\$\$i","\$\$j");
			$call_text = str_replace($call_replace, " ", $call_text);

			$author_text = $child->{'z13u-user-defined-5'};

			$title_text = $child->{'z13u-user-defined-6'};
			$title_text = preg_replace("% [/:;=].*%", "", $title_text);

			$edition_text = $child->{'z13u-user-defined-3'};
			$physical_text = $child->{'z13u-user-defined-4'};


			if($col_count==0)
			{
				$doc_text .= $tr_start;
			}
			$doc_text .= $td_start;

			$doc_text .= $label_mid_start.$call_text.$label_mid_end;
			$doc_text .= $label_mid_start.$author_text.$label_mid_end;
			$doc_text .= $label_mid_start.$title_text.$label_mid_end;
			$doc_text .= $label_mid_start.$edition_text.$label_mid_end;
			$doc_text .= $label_mid_start.$physical_text.$label_mid_end;

			$doc_text .= $td_end;

			$col_count++;

			if($col_count==$label_count)
			{
				$doc_text .= $tr_end;
				$table_row++;
				$col_count = 0;
			}
			else
			{
				$doc_text .= $MPLS_book_td_middle;
			}
		}
	}
	elseif ($handle)
	{

		$temp_out = fopen("/var/www/htdocs/spine_labels/uploads/spine_oneline.xml","w+");
		$text = fread($handle, filesize($target));
		$text = str_replace("\r\n","",$text);
		$text = str_replace("<printout>","\r\n<printout>",$text);
		fwrite($temp_out,$text);
		fclose($temp_out);
		fclose($handle);
		$handle = @fopen("/var/www/htdocs/spine_labels/uploads/spine_oneline.xml", "r");

		while (!feof($handle))
		{
			$haystack = fgets($handle);
			$needle = '<z30-call-no>';
			$needle_length = strlen($needle);
			//echo 'needle length= '.$needle_length.' ';

			//whitehorse template specific
			$whitehorse_needle = '<z30-collection>';
			$whitehorse_needle_length = strlen($whitehorse_needle);

			//slwa template specific secondary field
			$slwa_needle = '<z30-description>';
			$slwa_needle_length = strlen($slwa_needle);

			//vcml template specific secondary field
			$vcml_needle = '<z30-collection>';
			$vcml_needle_length = strlen($vcml_needle);

			if($line_no==1)
			{
				if($_POST['template']=='whitehorse_thirty_twoL_twenty_fourw' || $_POST['template']=='VCML14')
				{
					$whitehorse_found_array = findall($whitehorse_needle, $haystack);
					$whitehorse_array_length = sizeof($whitehorse_found_array);
					$whitehorse_results = array();

					foreach($whitehorse_found_array as $wh_entry)
					{
						$wh_next = strpos($haystack, '<', $wh_entry+$whitehorse_needle_length) - ($wh_entry+$whitehorse_needle_length);
						$wh_found_text = substr($haystack, ($wh_entry+$whitehorse_needle_length), $wh_next);
						$wh_pieces = explode(" ",$wh_found_text);
						$whitehorse_text = '';
						foreach($wh_pieces as $piece)
						{
							//if z30-collection is only 1 char 4-8-2011
							//if($_POST['template']=='VCML14')
							if($_POST['template']=='VCML14' && strlen($piece)==1)
							{
								$whitehorse_text .= $vcml_mid20_start.$piece.$label_mid_end;
							}
							else
							{
								$whitehorse_text .= $label_mid_start.$piece.$label_mid_end;
							}
						}
						$whitehorse_results[] = $whitehorse_text;
					}
				}

				if($_POST['template']=='slwa_twenty_twoL_thirty_fiveW')
				{
					$slwa_found_array = findall($slwa_needle, $haystack);
					$slwa_array_length = sizeof($slwa_found_array);
					$slwa_results = array();

					foreach($slwa_found_array as $slwa_entry)
					{
						$slwa_next = strpos($haystack, '<', $slwa_entry+$slwa_needle_length) - ($slwa_entry+$slwa_needle_length);
						$slwa_found_text = substr($haystack, ($slwa_entry+$slwa_needle_length), $slwa_next);
						$slwa_pieces = explode(" ",$slwa_found_text);
						$slwa_text = '';
						foreach($slwa_pieces as $slwa_piece)
						{
							if($slwa_piece!='')
							{
								$temp_found_text = str_replace("^", "/", $slwa_piece);
								$slwa_piece = $temp_found_text;
								$slwa_text .= $label_mid_start.$slwa_piece.$label_mid_end;
								//$slwa_text .= $slwa_piece;
							}
						}
						$slwa_results[] = $slwa_text;
					}

				}

				$found_array = findall($needle, $haystack);
				$array_length = count($found_array);
				//echo 'array length= '.$array_length.' ';
				$i = 0;
				$col_count = 0;
				$table_row = 0;
				while($i<$array_length)
				{
					$next = strpos($haystack, '<', $found_array[$i]+$needle_length) - ($found_array[$i]+$needle_length);
					$found_text = substr($haystack, ($found_array[$i]+$needle_length), $next);

					if($_POST['template']=='VCML14' || $_POST['template']=='VCML14_landscape')
					{
						if (strstr($found_text,"LEARN"))
						{
					  if(substr($found_text,3,6)=='MP3$$h')
					  {
					  	$temp_found_text = str_replace("TB MP3", "TB ", $found_text);
					  	$found_text = $temp_found_text;
					  	$temp_found_text = str_replace(" LEARN ", " LEARN MP3 ", $found_text);
					  	$found_text = $temp_found_text;
					  }
					  if(substr($found_text,0,3)=='TB ')
					  {
					  	$temp_found_text = substr_replace($found_text,"",0,3);
					  	$found_text = $temp_found_text;
					  }
					  if(substr($found_text,0,5)=='TB$$h')
					  {
					  	$temp_found_text = substr_replace($found_text,"",0,2);
					  	$found_text = $temp_found_text;
					  }
						}
					}

					if($_POST['template']=='slwa_twenty_twoL_thirty_fiveW')
					{
						//foreach added 11-7-2011
						$replace_letters = array('Q','QJ','EF','F','J');
						foreach($replace_letters as $letter)
						{
							//temporarily replacing characters for slwa on same line fix 21-6-2011
							$temp_found_text = str_replace($letter."\$\$i", $letter."_", $found_text);
							$found_text = $temp_found_text;
						}
					}
					if($_POST['template']=='slwa_22L_35W_english')
					{
						$prefix_found=='no';

						$replace_letters = array('1','2','3','4','5','6','7','8','9','0');
						foreach($replace_letters as $letter)
						{
							//temporarily replacing characters for slwa on same line fix 21-6-2011
							$temp_found_text = str_replace($letter."/", $letter."/ ", $found_text);
							$found_text = $temp_found_text;
							$temp_found_text = str_replace($letter."A/", $letter."A/ ", $found_text);
							$found_text = $temp_found_text;
						}
						$temp_found_text = str_replace("A[W]/", "A [W]/ ", $found_text);
						$found_text = $temp_found_text;

						$prefix_letters = array('Q','E','F','J');
						foreach($prefix_letters as $letter)
						{
							if (substr($found_text,0,1) == $letter)
							{
								$prefix_found = 'yes';
							}
						}
					}
					//replacing characters for monash changes 27-10-2011
					$temp_found_text = str_replace("\$\$k", " ", $found_text);
					$found_text = $temp_found_text;

					if($_POST['template']=='monash_verdana')
					{
						//ensuring that prefix H (orthopedic collection) is on own line 04-06-2014
						if (substr($found_text, 0, 4) == 'H$$h' || substr($found_text, 0, 4) == 'M$$h'){
							$temp_found_text = str_replace("\$\$h", " ", $found_text);
							$found_text = $temp_found_text;
						}
						//replacing characters for monash changes 27-10-2011
						$temp_found_text = str_replace("\$\$h", "|", $found_text);
						$found_text = $temp_found_text;
					}
					else
					{
						//replacing characters for VCML 6-6-2011
						$temp_found_text = str_replace("\$\$h", " ", $found_text);
						$found_text = $temp_found_text;
					}

					//replacing characters for monash changes 6-9-2011
					$temp_found_text = str_replace("\$\$v", " ", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M ADA", "M_ADA", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M CHN", "M_CHN", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M IND", "M_IND", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M JAP", "M_JAP", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M KOR", "M_KOR", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M REF", "M_REF", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M SER", "M_SER", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("M V ", "M_V ", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("v. ", "v._", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("^", "/", $found_text);
					$found_text = $temp_found_text;

					//replacing characters for slwa changes 14-6-2011
					$temp_found_text = str_replace("B/\$\$j", "B/", $found_text);
					$found_text = $temp_found_text;
					//replacing characters for slwa changes 2-6-2011
					$temp_found_text = str_replace("\$\$i", " ", $found_text);
					$found_text = $temp_found_text;
					$temp_found_text = str_replace("\$\$j", " ", $found_text);
					$found_text = $temp_found_text;

					//replacing carrat character with space for slnsw 1-6-2011
					$temp_found_text = str_replace("^", " ", $found_text);
					$found_text = $temp_found_text;

					if($_POST['template']=='VCML14' || $_POST['template']=='VCML14_landscape')
					{
						if(substr($found_text,0,4)=='$$h ')
						{
							$split_text = split(" ", substr($found_text,4));
						}
							
						elseif(substr($found_text,0,3)=='$$h')
						{
							$split_text = split(" ", substr($found_text,3));
						}

						else
						{
							$split_text = split(" ", $found_text);
						}
					}
					else
					{
						$split_text = split(" ", $found_text);
					}

					$split_length = sizeof($split_text);

					$split_count = 0;
					if($col_count==0)
					{
						$doc_text .= $tr_start;
					}
					$doc_text .= $td_start;
					$doc_text .= $whitehorse_results[$i];

					$number_found = 'no';
					if($_POST['template']=='VCML14' || $_POST['template']=='vcml_thermal' || $_POST['template']=='VCML14_landscape')
					{
						foreach($split_text as $split_t)
						{
							if(preg_match("/^[0-9]{1,}/",$split_t))
							{
								$number_found = 'yes';
							}
						}
					}

					while($split_count<$split_length)
					{

						if($split_text[$split_count]=="LOTE")
						{
							$doc_text .= $LOTE_start.$split_text[$split_count].$label_mid_end;
						}
						else
						{
							$whitehorse_decimal_found = 'no';
							if($_POST['template']=='whitehorse_thirty_twoL_twenty_fourw')
							{
								if(substr_count($split_text[$split_count],'.')>0)
								{
									$decimal_pos = strpos($split_text[$split_count],'.')+1;
									$doc_text .= $label_mid_start.substr($split_text[$split_count],0,$decimal_pos).$label_mid_end;
									$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos).$label_mid_end;
									$whitehorse_decimal_found = 'yes';
								}
							}
							if($_POST['template']=='moreton_32L_24W')
							{
								if(substr_count($split_text[$split_count],'.')>0)
								{
									$decimal_pos = strpos($split_text[$split_count],'.')+1;
									$doc_text .= $label_mid_start.substr($split_text[$split_count],0,$decimal_pos).$label_mid_end;
									if(strlen(substr($split_text[$split_count],$decimal_pos))>4){
										$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos,3 ).$label_mid_end;
										$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos + 3,3).$label_mid_end;
										$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos+6).$label_mid_end;
									}
									else{
										$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos).$label_mid_end;
									}

									$whitehorse_decimal_found = 'yes';
								}
							}

							if($_POST['template']=='monash_verdana')
							{
								if(substr_count($split_text[$split_count],'.')>0)
								{
									$split_required = 'yes';
									if(substr_count($split_text[$split_count],'_')>0)
									{
										$amp_split =  str_replace("_", " ", $split_text[$split_count]);
										$replace_split =  str_replace("|", " ", $amp_split);
										$split_required = 'no';
									}
									else
									{
										$amp_split = $split_text[$split_count];
										$replace_split =  str_replace("|", " ", $amp_split);
									}

									$decimal_split = explode('.',$replace_split);
									if(strlen($decimal_split[1])>4)
									{
										if($split_required=='yes')
										{
											$split_decimal_counter=1;
											foreach($decimal_split as $d_split)
											{
												$doc_text .= $label_mid_start.$d_split;
												if($split_decimal_counter<count($decimal_split))
												{
													$doc_text .= '.';
												}
												$doc_text .= $label_mid_end;
												$split_decimal_counter++;
											}
										}
										else
										{
											$doc_text .= $label_mid_start;
											$split_decimal_counter=1;
											foreach($decimal_split as $d_split)
											{
												if(strlen($d_split)>4 && is_numeric(substr($d_split,0,1)))
												{
													$doc_text .= $label_mid_end.$label_mid_start;
												}

												$doc_text .= $d_split;
												if($split_decimal_counter<count($decimal_split))
												{
													$doc_text .= '.';
												}

												$split_decimal_counter++;
											}
											$doc_text .= $label_mid_end;
											//$doc_text .= $label_mid_start.$decimal_split[0].'.'.$decimal_split[1].$label_mid_end;
										}
									}
									else
									{
										$doc_text .= $label_mid_start;
										$split_decimal_counter=1;
										foreach($decimal_split as $d_split)
										{
											$doc_text .= $d_split;
											if($split_decimal_counter<count($decimal_split))
											{
												$doc_text .= '.';
											}
											$split_decimal_counter++;
										}
										$doc_text .= $label_mid_end;
										//$doc_text .= $label_mid_start.$decimal_split[0].'.'.$decimal_split[1].$label_mid_end;
									}
									$whitehorse_decimal_found = 'yes';
								}
							}

							if($whitehorse_decimal_found!='yes')
							{
								if($_POST['template']=='VCML14' || $_POST['template']=='vcml_thermal' || $_POST['template']=='VCML14_landscape')
								{
									if(strlen($split_text[$split_count])==1 && $number_found=='no')
									{
										$doc_text .= $vcml_mid26_start.$split_text[$split_count].$label_mid_end;
									}
									elseif($number_found=='yes')
									{
										if(strstr($split_text[$split_count],'.'))
										{
											$decimal_pos = strpos($split_text[$split_count],'.')+1;
											if($_POST['template']=='VCML14' || $_POST['template']=='vcml_thermal')
											{
												$doc_text .= $vcml_mid12_start.substr($split_text[$split_count],0,$decimal_pos).$label_mid_end;
												$doc_text .= $vcml_mid12_start.substr($split_text[$split_count],$decimal_pos).$label_mid_end;
											}
											else
											{
												$doc_text .= $vcml_landscape_mid12_start.substr($split_text[$split_count],0,$decimal_pos).$label_mid_end;
												$doc_text .= $vcml_landscape_mid12_start.substr($split_text[$split_count],$decimal_pos).$label_mid_end;
											}
										}
										else
										{
											$doc_text .= $vcml_mid12_start.$split_text[$split_count].$label_mid_end;
										}
									}
									elseif(strlen($split_text[$split_count])==4)
									{
										$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
									}
									else
									{
										$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
									}
								}
								elseif($_POST['template']=='slwa_twenty_twoL_thirty_fiveW' || $_POST['template']=='monash_verdana')
								{
									//replace underscore inserted earlier with space so text appears on the same line 21-6-2011 & 6-9-2011
									$replace_split =  str_replace("_", " ", $split_text[$split_count]);
									if($_POST['template']=='monash_verdana'){
										$replace_split =  str_replace("|", "<br />", $replace_split);
									}
									$doc_text .= $label_mid_start.$replace_split.$label_mid_end;
								}
								elseif($_POST['template']=='slwa_22L_35W_english')
								{
									if(strstr($split_text[$split_count],'.'))
									{
										$decimal_pos = strpos($split_text[$split_count],'.');
										$doc_text .= $label_mid_start.substr($split_text[$split_count],0,$decimal_pos).$label_mid_end;
										if (strlen($split_text[$split_count]) > 8 && $prefix_found=='no')
										{
											$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos,5).$label_mid_end;
											$doc_text .= $label_mid_start.substr($split_text[$split_count],8).$label_mid_end;
										}
										else
										{
											$doc_text .= $label_mid_start.substr($split_text[$split_count],$decimal_pos).$label_mid_end;
										}
									}
									else
									{
										$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
									}
								}
								else
								{
									$doc_text .= $label_mid_start.$split_text[$split_count].$label_mid_end;
								}
							}
						}
						$split_count++;

						if($split_count==$split_length)
						{
							if($slwa_results[$i]!='')
							{
								$doc_text .= $slwa_results[$i];
							}
						}
					}
					$doc_text .= $td_end;
					$i++;
					$col_count++;
					echo 'col_count= '.$col_count.', label_count = '.$label_count.'<br />';
					if($col_count==$label_count)
					{
						$doc_text .= $tr_end;
						$table_row++;
						if(($_POST['template']=='slwa_twenty_twoL_thirty_fiveW' || $_POST['template']=='slwa_22L_35W_english') && $table_row<$slwa_row_count)
						{
							//$doc_text .= $slwa_tr_middle.'table_row= '.$table_row.', slwa_row_count= '.$slwa_row_count;
							$doc_text .= $slwa_tr_middle;
							//$table_row++;
						}
						elseif(($_POST['template']=='slwa_twenty_twoL_thirty_fiveW' || $_POST['template']=='slwa_22L_35W_english') && $table_row==$slwa_row_count)
						{
							//$doc_text .= 'table row = row count';
							$doc_text .= $slwa_page_break;
							$table_row = 0;
						}
						$col_count = 0;
					}
					elseif($_POST['template']=='slwa_twenty_twoL_thirty_fiveW' || $_POST['template']=='slwa_22L_35W_english')
					{
						$doc_text .= $slwa_td_middle;
					}
				}
			}
			else
			{
				$line_no++;
			}
		}



		fclose($handle);
	}
	unlink($target);
}
if($col_count!=$label_count)
{
	while($col_count<$label_count)
	{
	
		$doc_text .= $td_start.$label_mid_start.$label_mid_end.$td_end;
		$col_count++;
		if(($_POST['template']=='slwa_twenty_twoL_thirty_fiveW' || $_POST['template']=='slwa_22L_35W_english') && $col_count<$label_count)
		{
			$doc_text .= $slwa_td_middle;
		}
		if(($_POST['template']=='sutherland_22L_35W') && $col_count<$label_count)
		{
			$doc_text .= $sutherland_td_middle;
		}
		if(($_POST['template']=='MPLS_Book' || $_POST['template']=='SLWA_Book') && $col_count<$label_count)
		{
			$doc_text .= $MPLS_book_td_middle;
		}
		
	}
	$doc_text .= $tr_end;
}
$full_text = $doc_header.$doc_text.$doc_footer;

fwrite($fp, $full_text);
fclose($fp);
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');
header("Location: http://cavalweb2.caval.edu.au/spine_labels/uploads/$label_template");
//echo $full_text;

?>