Location: PHPKode > projects > Lithron > lithron-1.0.2/prado/RenderPage.php
<?php


/**
 * Class file.
 *
 * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
 * @copyright 2005, diemeisterei GmbH. All rights reserved.
 * @author $Author: schmunk $
 * @version $Revision: 1 $  $Date: 2006-06-01 15:11:24 +0000 (Do, 01 Jun 2006) $
 * @package Phundament
 * @subpackage
 */

/**
 * class
 *
 * @package Phundament
 * @subpackage
 * @ todo doubled in MasterLayout for search ...
 */
class RenderPage extends TPage
{
	const IM_PATHS= "/sw/bin;/home/cad/bin;/usr/bin";
	const OUTPUT_PATH= "public/lithron";
	const LICENSE_FILE= "config/pdflib.license";
	const MODE_DEBUG= 1;
	const MODE_NORMAL= 2;
	private static $_mode= self :: MODE_NORMAL;

	private $_do_render= false;
	private $_do_tar= false;

	public function onInit($param)
	{
		parent :: onInit($param);

		$this->_do_tar= ($this->Request->itemAt("get_tar") !== null);
		$this->_do_render= ($this->Request->itemAt("render") !== null) && !$this->_do_tar;
		if (self :: $_mode == self :: MODE_DEBUG && !$this->_do_tar)
			$this->_do_render= true;

		if ($this->_do_tar)
		{
			$this->InfoPanel->Visible= false;
			$this->RunPanel->Visible= false;

			$wanted= preg_replace("/[^A-Za-z0-9]+/", "", $this->Request->itemAt("get_tar"));

			#echo ob$wanted."<br>";
			#die();

			if (!is_dir(self :: OUTPUT_PATH . "/$wanted"))
				die("nope.");
			$cmd= "tar cC " . self :: OUTPUT_PATH . " " . $wanted;
			$tarhandle= popen($cmd, "rb");
			//echo $cmd."<br>";
			//die();
			$this->Response->setContentType("application/x-tar");
			$this->Response->appendHeader('Content-Disposition: attachment; filename="' . $wanted . '.tar"');
			do
			{
				$read= fread($tarhandle, 4096);
				$this->Response->write($read);
			}
			while (!feof($tarhandle));
			$this->Response->flush();
			pclose($tarhandle);
			die();
		}
		elseif ($this->_do_render)
		{
			$this->InfoPanel->Visible= false;
			$this->RunPanel->Visible= true;
		}
		else
		{
			$this->InfoPanel->Visible= true;
			$this->RunPanel->Visible= false;
			$script=<<<EOS
			Event.observe(window, "load", function(e) 
			{
				window.setTimeout("doReload()", 100);
			});
			
			function doReload()
			{
				window.location.href = window.location.href + "&render=1";
			}
EOS;
			$this->Page->ClientScript->registerEndScript("renderpage_reload", $script);
		}
	}

	public function onPreRender($param)
	{
		parent :: onPreRender($param);

		if (!$this->_do_render)
			return;

		$ret= self :: executeJob();

		#if (is_string($ret[1])) $this->XMLhighlight->Controls[] = $ret[1];

		if ($ret[2] instanceof Lithron)
			$this->LOGpanel->Controls[]= $ret[2]->dumpLog();
		if (isset ($ret[3]))
			$this->DEBUGpanel->Controls[]= $ret[3];
		if ($ret[0] == false)
		{
			$this->PDFpanel->Controls[]= '<h2>Rendering failed!</h2>';
			$this->PDFpanel->Controls[]= 'See debug panel for error message.';
			if (self :: $_mode != self :: MODE_DEBUG)
			{
				ob_start();
			}
			$this->Application->ErrorHandler->handleError($this, $ret[2]);
			if (self :: $_mode != self :: MODE_DEBUG)
			{
				$this->DEBUGpanel->Controls[]= ob_get_contents();
				ob_end_clean();
			}
		}
		else
		{
			$L= $ret[2];
			foreach ($L->Files as $file)
			{
				
				$this->PDFpanel->Controls[]= '<div class="pdf-download"><b id="DownloadPDF" >Download <a href="' . $file->FilePath . '">' . $file->propName() . '</a></b><br/><br/>';
				$preview = new LImage;
				$preview->ImageUrl = $file->FilePath;
				$preview->DestHeight = 250;
				$preview->DestWidth = 400;
				$preview->DestType = "png";
				$preview->Style->setStyleField('border','1px solid gray');
				$this->PDFpanel->Controls[]= '<a href="' . $file->FilePath . '">';
				$this->PDFpanel->Controls[]= $preview;
				$this->PDFpanel->Controls[]= '</a></div>';
			}
			$this->PDFpanel->Controls[]= '<div class="pdf-download-rightclick">Please right-click to download files to your harddisk</div>';
			$hl= new THyperLink();
			$hl->setText("Download a package with all files");
			$hl->setNavigateUrl("?get_tar=" . $L->JobId);
			$hl->setTarget("_blank");
			$this->PDFpanel->Controls[]= "<p id='DownloadPackage'>";
			$this->PDFpanel->Controls[]= $hl;
			$this->PDFpanel->Controls[]= "</p>";
			#$this->PDFpanel->Controls[]= "<hr><div style=\"float:right\">finished on " . date("H:i:s") . "</div>";
		}
	}

	private static function compileTemplate($textWriter, $job= null)
	{
		if ($job === null)
		{
			$uid= Prado :: getApplication()->Request->itemAt("uid");
			$job= Prado :: getApplication()->Session->itemAt($uid);
		}
		
		foreach ($job as $instruction)
			switch ($instruction[0])
			{

				case "memory_limit" :
					ini_set("memory_limit", $instruction[1]);
					break;
				case "time_limit" :
					set_time_limit($instruction[1]);
					break;
				case "using" :
					Prado :: using($instruction[1]);
					break;
				case "generator" :
					$page= call_user_func_array($instruction[1], $instruction[2]);
					break;
			}
		$renderer= new THtmlWriter($textWriter);
		$page->run($renderer);
	}

	public static function addJob($job, $renderpage_name, $auto_popup= true)
	{
		$page= Prado :: getApplication()->getService()->getRequestedPage();
		$sess= Prado :: getApplication()->getSession();

		$uid= "pdf_" . uniqid();
		$sess->add($uid, $job);

		if ($auto_popup)
		{
			$script=<<<EOS
		Event.observe(window, "load", function(e)
		{
			var renderWin = window.open('$renderpage_name?uid=%%UID%%', '%%UID%%', 'width=850,height=740,scrollbars=yes,resizable=yes');
		});
EOS;
			$page->ClientScript->registerPradoScript("prado");
			$page->ClientScript->registerEndScript($uid, str_replace("%%UID%%", $uid, $script));
		}
		return $uid;
	}

	public static function createTemplate($tpath)
	{
		$service= Prado :: getApplication()->Service;
		$realpath= $service->getBasePath() . DIRECTORY_SEPARATOR . strtr($tpath, '.', DIRECTORY_SEPARATOR);
		//var_dump($realpath);
		//die();
		$page= Prado :: createComponent("Pages." . $tpath);
		$page->setTemplate($service->getTemplateManager()->getTemplateByFileName($realpath . TPageService :: PAGE_FILE_EXT));
		return $page;
	}

	public static function executeJob($job= null)
	{
		if (self :: $_mode != self :: MODE_DEBUG)
		{
			ob_start();
		}	
		if (!Prado :: getPathOfAlias("LithronBase"))
			Prado :: setPathOfAlias("LithronBase", dirname(__FILE__) . "/..");
		Prado :: using("LithronBase.Lithron");
		$L= null;
		$xml= null;
		$ret= null;
		try
		{
			$textWriter= new TTextWriter();
			self :: compileTemplate($textWriter, $job);
			$xml= $textWriter->flush();
			$L= new Lithron($xml);
			$paths= explode(";", self :: IM_PATHS);
			foreach ($paths as $path)
			{
				if (is_executable($path . "/identify"))
				{
					$L->setIMagickPath($path);
					break;
				}
			}

			$xml= html_entity_decode($L->CleanedXML);
			$L->LicenseFile= self :: LICENSE_FILE;

			// HACK - todo
			$app = Prado :: getApplication();
			if($app->Parameters['PDFlibLicenseFile']) 
				$L->LicenseFile= $app->Parameters['PDFlibLicenseFile'];
			
			$L->setOutputPath(self :: OUTPUT_PATH);
			$L->work();
		}
		catch (Exception $e)
		{
			$ret= array (
				false,
				$xml,
				$e
			);
		}
		if ($ret === null)
			$ret= array (
				true,
				$xml,
				$L
			);
		if (self :: $_mode != self :: MODE_DEBUG)
		{
			$ret[]= ob_get_contents();
			ob_end_clean();
		}
		return $ret;
	}

}
?>
Return current item: Lithron