 <?php
/**
 * render.php - Controller for handling input pages. Please make sure output buffering
 * 				 in php.ini is turned on else the PDF generation will fail.
 *
 * @author $Author: dtong $
 * @version $Id: render.php,v 1.36 2011/04/14 15:12:36 dtong Exp $
 * @copyright Copyright (c) 2010, Tiller Software Co., Ltd.
**/
 require_once(APPPATH . 'libraries/mapping/display_list.php');
 class Render extends CI_Controller{
	function __construct() {
		parent::__construct();
	}
	/* (issue: 152) No longer used
 	function header($img,$folder=FALSE) {
 		$url_img = osa_image_toppath() . $img;
 		if ($folder!=FALSE){
 			$url_img = osa_image_toppath() . $folder . '/' .$img;
 		}
		$data->body = '<div class="header_pdf"><img src='.$url_img.'></div>';
 		$this->load->view('pdf/url_view', $data);

	}
 	function footer($img,$folder=FALSE,$time=FALSE) {
 		$url_img = osa_image_toppath() . $img;
 		if ($folder!=FALSE){
 			$url_img = osa_image_toppath() . $folder . '/' . $img;
 		}
 		$date = date('j F Y, H:i:s',$time );
 		$today = lang('pdf_generated').' '.$date;
 		$data->body = '<br /><div class="footer_generated">'.$today.'</div>';
 		$data->body .= '<div class="footer_pdf"><img src='.$url_img.'></div>';
 		$this->load->view('pdf/url_view', $data);
	}
	*/
	//Special for mapping
	function mapping($divid, $session_id) {
		if ( empty($divid) || empty($session_id) )	{
			$this->_print_error();
		}
		global $g_pdf_mode;
		$g_pdf_mode = TRUE;
		osa_init_phpci_sessions($session_id);
		osa_load_lang('mapping');
		$this->load->library('search/searchutil');
		osa_set_pdf_timeout();

		$display = & $this->searchutil->get_session($divid, TRUE, FALSE);
		//get_session() will print error msg and exit but still have the following
		if ( empty($display) || !is_object($display) ) {
			$this->_print_error();
		}
		//Do not save session at the end
		$display->no_session();
		$display->allpage = TRUE;
		$display->closebutton = FALSE;
		$display->pdfbutton = FALSE;
		$display->use_div_table = TRUE;
		$display->sort_columns = FALSE;
		//$tmpobj = new stdClass();
		//$tmpobj->display = & $display;
/*
		//Not sure about doing this since it can generate a huge string...
		$output = & $this->load->view('mapping/result_list_view', $tmpobj, TRUE);
		//$output =  preg_replace(array('/\<a(.*)\>/Ui','/\<\/a\>/Ui'), array('',''), $output);
		//TODO: Make all searches/pdf-pages to use string template
		//so we can skip all the link stuff and take out the line below and also $output
 		//$output = & str_replace('href=', 'xxx=', $output);

		$data->output = &$output;
		$data->use_jscript = osa_jscript_check_tags($output);
*/
		//$evalstr = '$this->load->view("mapping/result_list_view", $tmpobj);';
		$tmpobj = new stdClass();
		$tmpobj->display = $display;
		$output = & $this->load->view('mapping/result_list_view', $tmpobj, TRUE);
		unset($tmpobj);
		unset($display);
		$data = new stdClass();
		//Should be faster to do it here instead of relying on Javascript running Wkhtmltopdf to find the pattern
		$data->use_jscript = osa_jscript_check_tags($output);
		$data->output = $output;
		$this->load->view('pdf/url_jscript_view', $data);
	}

	//For pages without jscript includes
	//If later all pages requires jscript then use this as a wrapper for urljs
	function url($link) {
		$this->urljs($link);
	   //$link = base_url().str_replace('_','/',$link);
	   //$this->load->view('pdf/url_view', array('link'=>$link));
	}

	//Pages like unit page that requires jscript
	function urljs($link) {
		if ( empty($link) || strlen($link) < 20 || strpos($link, '_') === FALSE )	{
			$this->_print_error();
		}
	   $link = base_url().str_replace('_','/',$link);

	   //$output = & file_get_contents($link);
	   /*
	   $output = & str_replace('type="button"', 'type="hidden"', file_get_contents($link));
	   $output = & str_replace('href=', 'xxx=', $output);
	   $output = & preg_replace(array('/\<img(.*)\>/Ui','/\<\/img\>/Ui'), array('',''), $output);
	   	//$output =  preg_replace(array('/\<a(.*)\>/Ui','/\<\/a\>/Ui'), array('',''), $output);
	   */
	   //Check to see do we have math and svg tags in html output
	   $data = new stdClass();
	   //$data->use_jscript = osa_jscript_check_tags($output);
	   //$data->output = & $output;
	   $data->url_link = $link;
	   //This is always for smaller pages so just always include the Javascripts
	   $data->use_jscript = TRUE;
	   $this->load->view('pdf/url_jscript_view', $data);
	}

	private function _print_error() {
		//This message will be displayed in a PDF document.
		echo '<center><font color="red"><h2>' . lang('gen_pdf_failed_msg_inpdf') . '</h2></font></center>';
		exit;
	}

 }
?>