<?php require_once("dompdf/dompdf_config.inc.php");

	$db_host = 'uwfinaid.ovid.u.washington.edu:3798';
	$db_user = 'root';
	$db_pwd = '03798';
	$database = 'workstudy';
		
	if (!($GLOBALS["___mysqli_ston"] = mysqli_connect($db_host,  $db_user,  $db_pwd))) {
	    die("Can't connect to database");
	}

	if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . $database))) {
	    die("Can't select database");
	}
	
	if(!isset($_GET['id'])){
		die("Error: ID was not passed.");
	}

	if(!isset($_GET['table'])){
		die("Error: table was not passed.");
	}

	$id = $_GET['id'];
	$table = $_GET['table'];
	$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM $table WHERE id = $id");
	
	if (!$result) {
		die("Query to show fields from table failed");
	} 
	$row = mysqli_fetch_array($result);
	
	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 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;
	}*/
	
	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>Work Study Job Description</h2>' .
	   '<table id="main">'.
		'<tr>'.
			'<td class="title"><strong>Job Title</strong></td>'.
			'<td id="right">'. unsanitizeText($row["jobTitle"]). '</td>'.
		'</tr>';
			
		$html .= '<tr>';
			if ($_GET['table'] == 'offCampus') {
				$html .= '<td class="title"><strong>Organization Name</strong></td>';
			} else {
				$html .= '<td class="title"><strong>Department Name</strong></td>';
			}
			$html .= '<td id ="right">' . unsanitizeText($row["orgName"]) . '</td>'.
		'</tr>';

		$html .= '<tr><td class="title"><strong>Job Location</strong></td>';
			if ($_GET['table'] == 'offCampus') {
				$html .= '<td id ="right">' . unsanitizeText($row["street"]) . " " . unsanitizeText($row["city"]) . " " . unsanitizeText($row["state"]) . " " . unsanitizeText($row["zip"]) . '</td>';
			} else {
				$html .= '<td id ="right">' . unsanitizeText($row["location"]) . '</td>';
			}
		$html .= '</tr>';

		$html .= '<tr>'.
			'<td class="title"><strong>Pay Rate</strong></td>'.
			'<td id ="right">';

			if (isset($row["min"]) && !isset($row["max"])) {
				$html .= '$' . unsanitizeText($row["min"]);
			} else if (!isset($row["min"]) && isset($row["max"])) {
				$html .= '$' . unsanitizeText($row["min"]);
			} else if (isset($row["min"]) && isset($row["max"])) {
				$html .= '$' . unsanitizeText($row["min"]) . ' - $' . unsanitizeText($row["max"]);
			} else {
				$html .= '---';
			}			

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

		'<tr>'.
			'<td class="title"><strong>Hours Per Week</strong></td>'.
			'<td id ="right">' . unsanitizeText($row["hoursPerWeek"]) . '</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>';

		if($table == 'onCampus') {
			$html .= '<tr>'.
					'<td class="title"><strong>Box Number</strong></td>'.
					'<td id ="right">'. unsanitizeText($row["boxNumber"]) . '</td>'.
					'</tr>';
		} else {
			$html .='<tr>'.
					'<td class="title"><strong>EIN</strong></td>'.
					'<td id ="right">'. unsanitizeText($row["ein"]) . '</td>'.
					'</tr>';
		}

		
		$html .= 
		
		'</table><table id="long">'.
		'<tr>'.
			'<td class="title"><strong>Nature of Organization</strong></td>'.
		'</tr>'.
		'<tr>'.
			'<td>'.unsanitizeText(nl2br($row["nature"])).'</td>'.
		'</tr>'.
		'<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>How to Apply</strong></td>'.
		'</tr>'.
		'<tr>'.
			'<td>'.unsanitizeText(nl2br($row["apply"])).'</td>'.
		'</tr>'.
		'</table><hr/>';

		if ($_GET['table'] == 'onCampus') {
			$html .= '<div style="text-align:center">Job Number: '. unsanitizeText($row["jobNumber"])
			. ' | Job Class: '.unsanitizeText($row["jobClass"])
			. ' | Category: '.unsanitizeText($row["category"])
			. ' | 51% Comp. To Classified: '.unsanitizeText($row["classified"])
			. ' | Program: '.unsanitizeText($row["program"]) .'</div></div>';
		} else {
			$html .= '<div style="text-align:center">Job Number: '. unsanitizeText($row["jobNumber"])
			. ' | Category: '.unsanitizeText($row["category"])
			. ' | Program: '.unsanitizeText($row["program"])
			. ' | Reimbursement Rate: '.unsanitizeText($row["reimburseRate"]) .'%</div></div>';
		}

	  $html .= '</body></html>' ;
	
	$dompdf = new DOMPDF();
	$dompdf->load_html($html);
	
//	if (is_null($row["jobNumber"]) == true || $row["jobNumber"] == "") {
//		echo "Error: Please input a job number before generating a report.";
//	} else {
		$dompdf->render();
		$dompdf->stream($row['jobNumber'] . ".pdf");
	//	file_put_contents("../pdf/". $table . "/" . $row['jobNumber'] . ".pdf", $dompdf->output());
	//	header("Location: ../pdf/". $table . "/" . $row['jobNumber'] . ".pdf");
//	}
	?>
