<?php
	require_once("dompdf/dompdf_config.inc.php");
	
	//BL: This generates a pdf from the public listings page. NOT the admin table. 
	$db_host = 'uwfinaid.ovid.u.washington.edu';
	$db_port = 3798;
	$db_user = 'root';
	$db_pwd = '03798';
	$database = 'workstudy';
	$table = 'americaReads';
	$id = $_GET['id'];
		
	if (!($GLOBALS["___mysqli_ston"] = mysqli_connect($db_host,  $db_user,  $db_pwd, $database, $db_port)))
	    die("Can't connect to database");

	if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . $database)))
	    die("Can't select database");
	
	if(!isset($_GET['id'])){
		$id = sanitizeMySQL($_GET['id']);
	}
	
	

	$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM $table WHERE ID = $id");
	$row = mysqli_fetch_array($result);
	if (!$result) {
		die("Query to show fields from table failed");
	} 
	
	function unsanitizeText($text) { 
	    $text =  stripslashes($text); 
	    $text = str_replace("&#039;", "'", $text); 
	    $text = str_replace("&gt;", ">", $text); 
	    $text = str_replace("&quot;", "\"", $text);    
	    $text = str_replace("&lt;", "<", $text); 
		//clean_up($text);
	    return $text; 
    } 

	
	function sanitizeMySQL($var) {
		$var = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $var) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
		$var = sanitizeString($var);
		return $var;
	}
		
	function sanitizeString($var) {
		$var = htmlentities($var);
		$var = strip_tags($var);
		return $var;
	}

		$html =
	  '<html><head><link rel="stylesheet" type="text/css" href="report.css"><title></title></head><body>'.
	   '<div id="container" style="page-break-after:always;">'.
	   '<img src="images/osfa2.png"/><hr />'.
	   '<h2>American Reads Job Description</h2>' .
	   '<table id="main">'.
		'<tr>'.
			'<td class="title"><strong>Job Title</strong></td>'.
			'<td id="right">'. unsanitizeText($row["jobTitle"]). '</td>'.
		'</tr>'.
		'<tr>'.
			'<td class="title"><strong>Job Type</strong></td>
			<td>'; 
			
		//BL:  this needs to be changed later to account for non-profit / public listings. 	
			if ($row["formType"] == 'Public') {
				$html .= 'Public';
			}  else if ($row["formType"] == 'Non-profit') {
				$html .= 'Non-profit';
			}
			
		$html .= '</td>'.
		'</tr>';
			
			if ($row["formType"] == 'Public') {
				$html .= 
					'<tr><td class="title">School Name</td><td>' . unsanitizeText($row["schoolName"]) . '</td></tr>
					<tr><td class="title">Grade Level</td><td>' . unsanitizeText($row["schoolType"]) . '</td></tr>
					<tr><td class="title">District</td><td>' . unsanitizeText($row["schoolDistrict"]) . '</td></tr>';
			}  else if ($row["formType"] == 'Non-profit') {
				$html .= 
					'<tr><td class="title">Organization</td><td>' . unsanitizeText($row["organization"]) . '</td></tr>
					<tr><td class="title">Pay Rate</td><td>' . unsanitizeText($row["payRate"]) . '</td></tr>';
			}

			
		$html .= 
			'<tr>'.
				'<td class="title"><strong>Availability</strong></td>'.
				'<td id ="right">' . unsanitizeText($row["availability"]) . '</td>'.
			'</tr>'.

			'<tr>'.
				'<td class="title"><strong>Contact Supervisor</strong></td>'.
				'<td id ="right">' . unsanitizeText($row["contactFName"]) . " " .  unsanitizeText($row["contactLName"]) . '</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Phone Number</strong></td>'.
				'<td id ="right">' . unsanitizeText($row["phoneNumber"]) . '</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Email Address</strong></td>'.
				'<td id ="right">'. unsanitizeText($row["emailAddress"]) . '</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Website</strong></td>'.
				'<td id ="right">'. unsanitizeText($row["website"]) . '</td>'.
			'</tr>'.
			'<tr>';

		
		$html .= 
			'</table><table id="long">'.
			'<tr>'.
				'<td class="title"><strong>Duties and Responsibilities</strong></td>'.
			'</tr>'.
			'<tr>'.
				'<td>'.unsanitizeText(nl2br($row["duties"])).'</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Minimum Qualifications</strong></td>'.
			'</tr>'.
			'<tr>'.
				'<td>'.unsanitizeText(nl2br($row["qualifications"])).'</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Educational Benefits</strong></td>'.
			'</tr>'.
			'<tr>'.
				'<td>'.unsanitizeText(nl2br($row["benefits"])).'</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Additional Information</strong></td>'.
			'</tr>'.
			'<tr>'.
				'<td>'.unsanitizeText(nl2br($row["additional"])).'</td>'.
			'</tr>'.
			'</table><hr />';
		

	  $html .= '</div></body></html>'; 
	
	$dompdf = new DOMPDF();
	$dompdf->load_html($html);
	
	$dompdf->render();
	$dompdf->stream($row['id'] . ".pdf");
	
	/*
	if (is_null($row["jobNumber"]) == true || $row["jobNumber"] == "") {
		echo "Error: Please input a job number before generating a report.";
		echo "'</tr>'.
						'<tr>'.
				'<td>'.unsanitizeText(nl2br($row["duties"])).'</td>'.
			'</tr>'. dfd
			'<tr>'.
				'<td class="title"><strong>Email Address</strong></td>'.
				'<td id ="right">'. unsanitizeText($row["emailAddress"]) . '</td>'.
			'</tr>'.
			'<tr>'.
				'<td class="title"><strong>Website</strong></td>'.
				'<td id ="right">'. unsanitizeText($row["website"]) . '</td>'.
			'</tr>'.
			'<tr>';"
	} else {
		file_put_contents("../pdf/". $table . "/" . $row['jobNumber'] . ".pdf", $dompdf->output());
		header("Location: ../pdf/". $table . "/" . $row['jobNumber'] . ".pdf"); 
	}
*/

/* BL 10/30

	function clean_up($str){
		$str = stripslashes($str);
		$str = strtr($str, get_html_translation_table(HTML_ENTITIES));
		$str = str_replace( array("\x82", "\x84", "\x85", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96",  "\x97"), array("&#8218;", "&#8222;", "&#8230;", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8226;", "&#8211;", "&#8212;"),$str);
		return $str;
	}
*/
	
	?>
