<?
ob_clean();
include("../../startup.php"); 
###########################################  	END  FILTER CONITIONS 	#####################################

$header = "id,organisation_name,sector,first_name,last_name,phone_no,landline_phone_no,email,address1,address2,address3,city,county,postcode,created_date,consultant\n";

$data  = $header;
//$sql = "SELECT * FROM organisation_management $where ORDER BY id DESC";
$sql = "SELECT id,organisation_name,sector,first_name,last_name,phone_no,landline_phone_no,email,address1,address2,address3,city,county,postcode,common_date FROM organisation_management $where ORDER BY id DESC";

$rs = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($rs)){
	$row=utility::validate_crypt_data($row);
	$sector = "";
	if(isset($row["sector"]) && $row["sector"] != 0){
		$sector = Keycode::getkeycodeById($row["sector"]);
	}
	$county = "";
	if(isset($row["county"]) && $row["county"] != 0){
		$county =  $gsi->getCountyById($row['county']);
	}
	
	$common_date = ($row['common_date'] != "0000-00-00 00:00:00") ? date("d-m-Y",strtotime($row['common_date'])) : "";
	$consultant_string = "";
	//$sql_for_enquiry_admin = "SELECT * FROM job_admin_mapping WHERE job_id = ".$row['id']." ";
	$sql_for_enquiry_admin = "SELECT admin_id FROM job_admin_mapping WHERE job_id = ".$row['id']." ";
	$result_for_enquiry_admin = mysqli_query($conn,$sql_for_enquiry_admin);
	while($row_for_enquiry_admin = mysqli_fetch_array($result_for_enquiry_admin)){
		$consultant_string .= ucfirst(utility::getTableFieldById("admin","name","id",$row_for_enquiry_admin["admin_id"]))."#";
	}
	$consultant_string = substr($consultant_string,0,-1);
	
	$data .= '"'.$row['id'].'"'.",".'"'.$row['organisation_name'].'"'.",".'"'.$sector.'"'.",".'"'.$row['first_name'].'"'.",".'"'.$row['last_name'].'"'.",".'"'.$row['phone_no'].'"'.",".'"'.$row['landline_phone_no'].'"'.",".'"'.$row['email'].'"'.",".'"'.$row['address1'].'"'.",".'"'.$row['address2'].'"'.",".'"'.$row['address3'].'"'.",".'"'.$row['city'].'"'.",".'"'.$county.'"'.",".'"'.$row['postcode'].'"'.",".'"'.$common_date.'"'.",".'"'.$consultant_string.'"'."\n";
}

$file_name = "organisation_details_export.csv";
ob_clean();
echo $data;
header("Content-type: application/csv"); 
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
die;
?>