<?php
include('../../includes/con2pma3.php');
include('../../includes/checkSession.php');	
$WID = $_REQUEST['WID'];
$intoo = $_REQUEST['intoo'];
	 $result = mysql_query("SELECT sortCode, WBSIDA, WBSIDB, WBSIDC, WBSIDD, WBSIDE, description, WBSID FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");

		$csv_terminated = "\n";
	$csv_separator = ",";
	$csv_enclosed = '"';
	$csv_escaped = "\\";
	
	$fields_cnt = mysql_num_fields($result);


 
	$schema_insert = '';
 
 	$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 0))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;

	$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes("WBS / WP Code")) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
	/*
	$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 1))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
	

	
		$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 2))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
	
		$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 3))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
		$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 4))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
	
			$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 5))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
	*/
			$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
		stripslashes(mysql_field_name($result, 6))) . $csv_enclosed;
	$schema_insert .= $l;
	$schema_insert .= $csv_separator;
	/*
	for ($i = 0; $i < $fields_cnt; $i++)
	{
		$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
			stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
		$schema_insert .= $l;
		$schema_insert .= $csv_separator;
	} // end for
 */
	$out = trim(substr($schema_insert, 0, -1));
	$out .= $csv_terminated;
 
	// Format the data
	while ($row = mysql_fetch_array($result))
	{
		$WBSID = $row["WBSID"];
		$schema_insert = '';
		for ($j = 0; $j < $fields_cnt-1; $j++)
		{
			if ($row[$j] == '0' || $row[$j] != '')
			{
 
				if ($csv_enclosed == '')
				{
					$schema_insert .= $row[$j];
				} else
				{
					if($j>=1 && $j<=5)
					{
						if($j==1)
							$schema_insert .= $csv_enclosed;
						
						$schema_insert .= 	str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]); 
						
						if($j==5)
							$schema_insert .=  $csv_enclosed;
						else
							$schema_insert .='.';
					}
					else
					{
						$schema_insert .= $csv_enclosed .
						str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;

					}
				}
			} else
			{
				$schema_insert .= '';
			}
 
			if ($j < $fields_cnt - 2 && !($j>=1 && $j<5))
			{
				$schema_insert .= $csv_separator;
			}
		} // end for
 
		$out .= $schema_insert;
		$out .= $csv_terminated;
		
		
		if ($intoo == 1){
		
		//start item
		$result1 = mysql_query("SELECT sortCode, itemID, description FROM globalitems where WBSID = '$WBSID' && itemParent = '0' ORDER BY itemID ASC");
		while ($row1 = mysql_fetch_array($result1))
	{
		$schema_insert = '';
		$schema_insert = "$row1[0],$row[1].$row[2].$row[3].$row[4].$row[5].$row1[1],$row1[2]";
		
			
 
		$out .= $schema_insert;
		$out .= $csv_terminated;
		
		
		//sub item 
		
		
		//start item
		$result2 = mysql_query("SELECT sortCode, itemID, description FROM globalitems where WBSID = '$WBSID' && itemParent = '$row1[1]' ORDER BY itemID ASC");
		while ($row2 = mysql_fetch_array($result2))
	{
		$schema_insert = '';
		$schema_insert = "$row2[0],$row[1].$row[2].$row[3].$row[4].$row[5].$row2[1],$row2[2]";
		
			
 
		$out .= $schema_insert;
		$out .= $csv_terminated;
	} // end while item sub 
		
		
	} // end while item
		}
		
	} // end while
 
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Content-Length: " . strlen($out));
	// Output to browser with appropriate mime type, you choose ;)
	//header("Content-type: text/x-csv");
	header("Content-type: text/csv");
	//header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=export.csv");
	echo $out;
	exit;
?>