Location: PHPKode > projects > GPDE > gpde_1.1.4/gpde/module.php
<?php
/*
	GPDE (Griss Proteomics Database Engine) 
	>> A biological view on PRIDE
    Copyright (C) 2008, 2009  Johannes Griss - hide@address.com

    This file is part of GPDE.
    For license see notice in /LICENSE.txt and <http://www.griss.co.at/?gpde:License>.
*/

abstract class Module {	
	protected $modules; // array holding the modules to load
	
	abstract function render();
	
	/**
	 * Loads the xml specifying the available sub modules.
	 *
	 * @param string $path
	 * @return bool
	 */
	function LoadModuleXml($path)
	{
		// check if the file exists
		if (!is_file($path)) {
			return false;
		}
		
		// load the xml file
		$xml = file_get_contents($path);
		
		// make sure the file was loaded
		if (!$xml) {
			return false;
		}
		
		// create the xml parser and parse the file
		$parser = xml_parser_create();
		$result = xml_parse_into_struct($parser, $xml, $values, $index);
		xml_parser_free($parser);
		
		// check if the parsing was successfull
		if (!$result) {
			return false;
		}
		
		// create the array to hold the result
		$this->modules = array();
		$counter = 0; // counter for the number of elements in the array
		
		// check if there are any modules to load
		if (!isset($index['MODULE'])) {
			return true;
		}
		
		// load all modules
		foreach ($values as $item) {
			// a new module starts with the "name" tag ...
			if ($item['tag'] == "NAME") {
				$this->modules[$counter]['name'] = $item['value']; 
			}
			
			// a module ends with the link tag
			if ($item['tag'] == "LINK") {
				$this->modules[$counter]['link'] = $item['value'];
				$counter++;
			}
		}
		
		return true;
	}
	
	/**
	 * Send a 404 Not found error
	 *
	 */
	protected function Send404()
	{
		header("HTTP/1.0 404 Not Found");
	}
	
	/**
	 * Send a 500 Internal server error
	 *
	 */
	protected function Send500()
	{
		header("HTTP/1.0 500 Internal Server Error");
	}
	
	/**
	 * Checks whether the database was set offline.
	 * @return bool
	 */
	protected function DbIsOffline()
	{
		return is_file(OFFLINE_STATUS_FILE);
	}
	
	protected function SendOfflineMessage()
	{
		$msg = '<div class="module-text">
					<h1>Database currently offline</h1>
					<p>
						The database is currently offline as the database is being updated. We hope that the caused 
						delay is compensated by the enhanced functionality we thus can provide.
					</p>
				</div>';
		
		echo $msg;
	}
	
	/**
	 * Transforms the given array in a json object and sends this json object to the client. If supported by the
	 * browser a gzip compression is used. WARNING: the array is being unset as soon as it's sent.
	 *
	 * @param array $array
	 */
	protected function SendResult($array)
	{
		if (!count($array)) {
			// if nothing was found, send the string "false"
			$json = "false";
		}
		else {				
			// create the json object
			$json = json_encode($array);
		}
		
		// free the array since the information is now contained in the json object
		unset($array);
		
		// set the "uncompressed-size" header
		header("Uncompressed-size: " . strlen($json));
		
		// check if compression is supported by the browser
		if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip") !== false) {
			// compress the json object and send the required header
			$json = gzencode($json, 9);
			header("Content-encoding: gzip");
		}
		
		// send the javascript header
		header("Content-type: application/json");
		header("Content-Length: " . strlen($json));
		
		// send the json object
		echo $json;
	}
	
	/**
	 * Truncates the given list of authors to ... et al. Returns the truncated author list
	 * @param $authors
	 * @return string
	 */
	protected function TruncateAuthors($authors)
	{
		// find the end of the first author (like Gerner C, ...)I
		$firstAuthorEnd = strpos($authors, ",");
		
		// if no "," is found, there's only one author
		if ($firstAuthorEnd === false) {
			return $authors;
		}
		
		// only add "et al." if there's a third author
		if (strpos($authors, ",", $firstAuthorEnd + 1) === false) {
			return $authors;
		}
		
		// truncate the authors list and return it
		$truncAuthors = substr($authors, 0, $firstAuthorEnd) . " et al.";
			
		return $truncAuthors;
	}
}
?>
Return current item: GPDE