Location: PHPKode > projects > Community Learning Network > cln/lib/CLN/Cln_Module_Loader.php
<?php
/*
 * Module Loader Class
 *
 * Copyright (c) 2003-4 St. Christopher House
 *
 * Developed by The Working Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @version $Id: Cln_Module_Loader.php,v 1.15 2004/11/19 04:41:21 cbooth7575 Exp $
 *
 */

class Cln_Module_Loader {
	var $loadList = Array();
	var $moduleTypes = Array();

	/*
	 *
	 * Constructor:  Cln_Module_Loader()
	 *
	 * 		creates an instance of a Module Loader
	 * 		doesn't do much, but allows for calling directly
	 * 		while adding modules to the list
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function Cln_Module_Loader() {
		// Do we need to do anything here?
	}


	/*
	 *
	 * Function:  __wakeup()
	 *
	 * 		called when Module Loader instance is reinitialized
	 * 		from session.  Loads classes.
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function __wakeup() {
		foreach($this->loadList as $modId => $true) {
			$this->requireOnce($modId);
		}
	   	return TRUE;
	}


	/*
	 *
	 * Function:  requireOnce()
	 *
	 * 		requires the file for the passed module
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function requireOnce($modId) {
		if (file_exists(CLN_FILE_BASE . $this->moduleTypes[$modId]['modPath'])) {
			require_once(CLN_FILE_BASE . $this->moduleTypes[$modId]['modPath']);
			return TRUE;
		}
		else {
			return FALSE;
		}
	}


	/*
	 *
	 * Function:  addToLoadList()
	 *
	 * 		adds a module to be loaded during session initialization
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function addToLoadList($modId) { //:TODO: this is screwy..what's it do?
		$this->loadList[$modId] = 1;

	   	return TRUE;
	}

	/*
	 *
	 * Function:  clearLoadList()
	 *
	 * 		removes all modules stored for loading
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function clearLoadList() {
		// can't unset this here, as this is called at the end of each page.
		// this needs to be called once the KO is not being saved anymore
	   	unset($_SESSION['Module_Loader']->loadList);
	   	$this->loadList = Array();

	   	return TRUE;
	}

	/*
	 *
	 * Function:  loadModuleType()
	 *
	 * 		adds a module type to the storage array
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function loadModuleType($modId) {
		// Only add it if it isn't already there
		if(!isset($this->moduleTypes[$modId])) {
			$sql = sprintf("SELECT modName, class, variables, path, visible FROM `%s` WHERE modId = '%s' ", MODULE_TABLE, $modId);

			$db = &Cln_Db::singleton(MAIN_CLN_DSN);
			$result = $db->getRow($sql);
			if (PEAR::isError($result)) {
				PEAR::raiseError("Couldn't get module info for modId $modId", E_WARNING);
				return FALSE;
			}
			else {
				$this->moduleTypes[$modId]['modName'] = stripslashes($result[0]);
				$this->moduleTypes[$modId]['modClass']  = stripslashes($result[1]);
				$this->moduleTypes[$modId]['variables'] = stripslashes($result[2]);
				$this->moduleTypes[$modId]['modPath']  = stripslashes($result[3]);
				$this->moduleTypes[$modId]['visible']  = stripslashes($result[4]);
			}
		}
	}


	/*
	 *
	 * Function:  getModulePath()
	 *
	 * 		returns a path to a module
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function getModulePath($modId) {
		$this->loadModuleType($modId);
		return $this->moduleTypes[$modId]['modPath'];
	}


	/*
	 *
	 * Function:  getModuleClassname()
	 *
	 * 		returns a classname of a module
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function getModuleClassname($modId) {
		$this->loadModuleType($modId);
		return $this->moduleTypes[$modId]['modClass'];
	}


	/*
	 *
	 * Function:  getModuleName()
	 *
	 * 		returns a name of a module
	 *
	 * @access public
	 * @return boolean 	TRUE or FALSE
	 *
	 */
	function getModuleName($modId) {
		$this->loadModuleType($modId);
		return $this->moduleTypes[$modId]['modName'];
	}

}

?>
Return current item: Community Learning Network