Location: PHPKode > projects > OpenNitro > trunk/Nitro/Modules/BackOffice/NitroBOModule.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: NitroBOModule                                                    |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2006 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library 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 Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// |          Jesper Avôt     <hide@address.com>                         |
// +---------------------------------------------------------------------------+
//
// $Id: NitroBOModule.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * This file contains the Main NitroBO (Back Office) Nitro module class
 *
 * This module will be called by Nitro when using NitroBO modules in Nitro.
 * This module will create an instance of the NitroBO submodule (if it exists)
 * and us that module to do the real Processing and drawing.
 *
 * @package			Modules
 * @version   	$Revision:$
 * @author			Siggi Oskarsson <hide@address.com>
 * @author			Jesper Avôt     <hide@address.com>
 * @copyright		2006 June Systems BV
 */
 
/**
 * Include the Nitro Module and NitroBO SubModule classes
 */
include_once "Nitro/Module.inc.php";
include_once "Nitro/Modules/BackOffice/NitroBOSubModule.inc.php";

/**
 * NitroBO (Back Office) Nitro module class
 *
 * This is the NitroBO (Back Office) module class to be used in the Nitro environment
 *
 * @access	public
 */
class NitroBOModule extends NitroModule {
	/**
	 * @var	int	Unique ID of object of instance of the class
	 */
	/*private*/
	var $ObjectID;
	/**
	 * @var	string NitroBO SubModule object
	 */
	/*private*/
	var $SubModule;
	/**
	 * @var	array	Variables used in module from session and forms
	 */
	/*private*/
	var $SubModuleSettings = Array();
	/**
	 * Module settings
	 *
	 * <CODE>
	 * $this->Settings['SettingName'] = "SettingValue";
	 * </CODE>
	 *
	 * @var	array	Modules settings array
	 */
	/*private*/
	var $Settings = Array();
	/**
	 * @var boolean Allow module to save settings
	 */
	var $AllowSaveSettings;
	/**
	 * @var boolean Ignore session in working with submodule
	 */
	var $ignoreSession = FALSE;
	/**
	 * @var object NitroBO DB Object
	 */
	var $NitroBODB;
	/**
	 * @var string NitroBO SubModules Directory
	 */
	var $SubModulesDir;
 
	/**
	 * Module constructor
	 *
	 * @param int $ObjectID
	 * @param boolean $Settings
	 * @access public
	 */
	function NitroBOModule($ObjectID, $Settings = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'NitroBO Module Constructor', __FILE__, __LINE__, DEBUG_MOD_OK);
		
		global $DB; // You dirty little slut!
		
		$this->_ModuleSettings = $Settings; /*Set the current Module settings*/
		parent::NitroModule(); /*Retrieve parent stuff*/
		$this->SetModuleSettings($Settings);
		$this->NitroBODBAlias = "NitroBO";
		$this->DBAlias = $this->NitroBODBAlias; // Set DBAlias to NitroBODBAlias because it was empty and therefore didn't work properly online.
		$this->SubModulesDir = NITRO_PATH . "Modules/BackOffice"; /*Path to the NitroBO Submodules*/
		$this->ObjectID = $ObjectID;
		$this->NitroBODB = $this->DB;
		
		if (!$this->Sess->User["NitroBOConfigID"]) {
			$this->Sess->User["NitroBOConfigID"] = current(array_keys($this->Conf["NitroBOConfigs"]));
		}
		
		$ConfigFile = $this->Conf["NitroBOConfigs"][$this->Sess->User["NitroBOConfigID"]];
		if (!file_exists($ConfigFile)) {
			/**
			 * Probably an relative path, so check again for existing, but this time with
			 * the file_exists_include_path function, which is located in Common.inc.php
			 */
			$ConfigFile = file_exists_include_path($ConfigFile);
		}
		
		if (file_exists($ConfigFile)) {
			$this->NitroBOConf = parse_ini_file($ConfigFile, TRUE);
			
			if (is_Array($this->NitroBOConf["DB"])) {
				foreach($this->NitroBOConf["DB"] AS $dbID => $dbString) {
					if ($dbID === 'Nitro' OR $dbID === '*Nitro') {						
						$DB[$this->DBAlias] = new DB($dbString, TRUE, $autoconnect);
						if ($DB[$this->DBAlias]->Error) {
							echo "ERROR: DB connection could not be initialized!";
							exit;
						}
						$this->NitroBODB = $DB[$this->DBAlias];
						break;
					}
				}
				if (!$this->NitroBODB) {
					echo "ERROR: DB connection string not found => ".$this->NitroBOConf["DB"]["Nitro"]." in ".$ConfigFile." (ConfigID = ".$this->Sess->User["NitroBOConfigID"].")";
					exit;
				}
			} else {
				echo "ERROR: no DB connection section in config found => ".$ConfigFile;
				exit;
			}

		} else {
			echo "ERROR: file not found => " . $ConfigFile;
			exit;
		}
		
		$DB[$this->NitroBODBAlias] =& $this->NitroBODB;

		DebugCloseGroup(DEBUG_MOD_OK);
	}

	/**
	 * Get the modules configuration options for Nitro
	 *
	 * This function returns an array of option strings for use in the
	 * NitroBO Configuration.
	 *
	 * @return	array		Array of options strings
	 * @access public
	 */
	function GetModuleConfiguration($moduleDir = NULL)
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'Retreive module configuration', __FILE__, __LINE__, DEBUG_MOD_OK);

		$moduleDir = isset($moduleDir) ? $moduleDir : $this->SubModulesDir;

		if (is_dir($moduleDir)) {
			$Dirs = Array();
			
			$dh = @opendir($moduleDir);
			
			while (($file = readdir($dh)) !== false) {
				if ($file != '..' && $file != '.' && !ereg("^\..*", $file) && is_dir($moduleDir . '/' . $file)) {
					$Dirs[] = $file . ':' . $file;
				}
			}
			
			closedir($dh);
			
			$SubModules = Array();
			
			if ($Dirs) {
				foreach ($Dirs AS $Dir) {
					$SubModules[] = $Dir;
					
					$file = explode(":", $Dir);
					
					$subdirs = Array();
					
					$dh = @opendir($moduleDir . '/' . $file[0]);
			
					while (($file = readdir($dh)) !== false) {
						if ($file != '..' && $file != '.' && !ereg("^\..*", $file) && is_dir($moduleDir . '/' . $file[0] . '/' . $file)) {
							$subdirs[] = $file[0] . "-" . $file . ':' . "-- " . $file;
						}
					}
					
					closedir($dh);
							
					foreach ($subdirs AS $subdir) {
							$SubModules[] = $subdir;
					}
				}
			} else {
				/*Could not open the Submodules Directory, ERROR TIME !!!*/
				Debug(__CLASS__, __FUNCTION__, 'Couldn\'t open directory for reading: ' . $moduleDir, __FILE__, __LINE__, DEBUG_MOD_ERR);
			}
		} else {
			$SubModules = Array('0: -- ' . Language('No Directories') . ' -- ');
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("SubModule" => "SELECT/LABLE=" . Language('SubModule') . "/VALUES=" . implode(",", $SubModules));
	}

	/**
	 * SaveModuleDataConfiguration function
	 */
	function SaveModuleConfiguration($Settings)
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'SaveModuleConfiguration called', __FILE__, __LINE__, DEBUG_MOD_OK);
		
		if(is_array($Settings)) {
			if(eregi("-", $Settings["SubModule"])) {
				$Settings["SubModule"] = str_replace( "-", "/", $Settings["SubModule"]);
			}
		}
		
		$this->Settings = $Settings;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $Settings;
	}

	/**
	 * Pre-Process the data for the module
	 *
	 * NitroBOModule asks the sub module which "input" it expects via "GetSettingsDefinition".
	 * The result is examined and carefully crafted into a Settings object for the NitroBO sub module.
	 * The Settings object is then passed to the sub module to set its desired state. 
	 * Lastly the sub modules PreProcess function is called.
	 * 
	 * param:		void
	 * @return 	boolean	TRUE/FALSE
	 */
	/*public*/ 
	function PreProcess()
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'PreProcess sub module: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);

		$moduleFile = $this->SubModulesDir . '/' . $this->Settings['SubModule'] . '/' . ($this->Settings['SubSubModule'] ? $this->Settings['SubSubModule'] . '/' : '') . 'Module.inc.php';

		if (file_exists($moduleFile)) {
			if (include_once($moduleFile)) {
				$objClass = 'NitroBO_' . $this->Settings['SubModule'] . (array_key_exists('SubSubModule', $this->Settings) ? '_' . $this->Settings['SubSubModule'] : '');
				
				if (class_exists($objClass)) {
					@eval('$this->SubModule = new ' . $objClass . '();');
					
					if (is_subclass_of($this->SubModule, 'NitroBOSubModule')) {
						$this->SubModule->NitroBOSubModule($this->ObjectID, $this->NitroBODB, $this->Sess, $this->SubModulesDir);
						$this->SubModule->NitroBODBAlias = $this->NitroBODBAlias; // set NitroBODBAlias in module
						
						/*Get Settings definition*/
						$this->SubModuleSettings = $this->SubModule->GetSettingsDefinition($this->ignoreSession);
						
						/*Set Session variables from definition*/
						if (!$this->ignoreSession && function_exists('SetSessionVariables')) {
							SetSessionVariables($this->SubModule->GetSettingsDefinitionSession());
						}
	
						/*Get settings from session and if no session variable then from ($_REQUEST)*/
						$settings = $this->GetSettingsFromDefinition($this->SubModuleSettings);
						
						if (!$settings) {
							Debug(__CLASS__, __FUNCTION__, 'Settings empty for submodule: ' . $moduleFile . '=>' . $objClass, __FILE__, __LINE__, DEBUG_MOD_OK);
						} else {							
							$this->SubModule->SetSettings($settings);
						}
						
						$RV = $this->SubModule->PreProcess();
					} else {
						/*Error: Object is not a subclass of NitroBOSubModule*/
						Debug(__CLASS__, __FUNCTION__, 'Object is not a subclass of NitroBOSubModule: ' . $moduleFile . '=>' . $objClass, __FILE__, __LINE__, DEBUG_MOD_ERR);
						
						$RV = FALSE;
					}
				} else {
					/*Error: Class does not exist*/
					Debug(__CLASS__, __FUNCTION__, 'Class does not exist (Class Name?): ' . $moduleFile . '=>' . $objClass, __FILE__, __LINE__, DEBUG_MOD_ERR);
					
					$RV = FALSE;
				}
			} else {
				/*Error: Including of file failed!*/
				Debug(__CLASS__, __FUNCTION__, 'Including of file failed (Parse error?): ' . $moduleFile, __FILE__, __LINE__, DEBUG_MOD_ERR);
			
				$RV = FALSE;
			}
		} else {
			/*Error: file does not exist!*/
			Debug(__CLASS__, __FUNCTION__, 'Cannot find submodule: ' . $moduleFile, __FILE__, __LINE__, DEBUG_MOD_ERR);
			
			$RV = FALSE;
		}

		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;	
	}

	/**
	 * Process the data for the module
	 *
	 * Process implements the actions to be taken for the NitroBO sub module(s). 
	 * NitroBOSubModules' Process function will be called here. 
	 * 
	 * param:	void
	 * return boolean			TRUE/FALSE
	 * @access public
	 */
	function Process()
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'Process submodule: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);

		if (is_subclass_of($this->SubModule, 'NitroBOSubModule')) {
			$RV = $this->SubModule->Process();
		} else {
			Debug(__CLASS__, __FUNCTION__, 'Object is not a subclass of NitroBOSubModule: ' . get_class($this->SubModule), __FILE__, __LINE__, DEBUG_MOD_ERR);
		}

		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}

	/**
	 * Draw the module
	 *
	 * This function calls DrawResult and draws that with the toolbar on
	 * a template
	 * 
	 * param:		void
	 * @return	mixed	
	 * @access public
	 */
	function Draw($RuntimeSettings = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'Draw sub module: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);

		if (is_subclass_of($this->SubModule, 'NitroBOSubModule')) {
			$divs = $this->DrawResult('Default');
			$templateFile = $this->SubModulesDir . '/' . $this->Settings['SubModule'] . '/Templates/Default.tpl';
			$template = FALSE;
			
			if (file_exists($templateFile)) {
				/*module has own template	*/
				Debug(__CLASS__, __FUNCTION__, 'Using module template', __FILE__, __LINE__, DEBUG_MOD_OK);
				
				$template = 'file:' . $templateFile;
			} else {
				/*use the default template*/
				Debug(__CLASS__, __FUNCTION__, 'No default submodule template, use default template --TODO--', __FILE__, __LINE__, DEBUG_MOD_OK);
				
				$template = 'file:'.NITRO_PATH.'Defaults/Templates/BackOffice.tpl';
			}
			
			if ($template) {
				$Template = new NitroTemplate($template);
				
				if (is_object($Template)) {
					/*Assign the Basic stuff*/
					$Template->assign("Divs", $divs);
					$Template->assign("ObjectID", $this->ObjectID);
					
					/*Module Comment*/
					$ModuleComment = $this->DoModuleComment($this->SubModule->ModuleName, $this->SubModule->ModuleVersion, $this->SubModule->ModuleAuthor);
					$Template->assign("ModuleComment", $ModuleComment);
					
					$objDefs = $this->SubModule->GetObjectsDefinition();
					
					$Template->assign("ignoreSession", (int)$this->ignoreSession);
					
					$RV = $Template->fetch();
				} else {
					/*Whoops: Template is not an object, ERROR TIME !!!*/
					Debug(__CLASS__, __FUNCTION__, 'Template is not an object: ' . $template, __FILE__, __LINE__, DEBUG_MOD_ERR);
					
					$RV = FALSE;
				}
			} else {
				/*No template, just draw all objects seperated by <HR>*/
				Debug(__CLASS__, __FUNCTION__, 'No template was found', __FILE__, __LINE__, DEBUG_MOD_ERR);
				
				$RV = '<HR>';
				
				foreach ($divs AS $objID => $div) {
					$RV.= $div . '<HR>';
				}
			}
		} else {
			/*Error: Object is not a subclass of NitroBOSubModule*/
			Debug(__CLASS__, __FUNCTION__, 'Object is not a subclass of NitroBOSubModule: ' . get_class( $this->SubModule ), __FILE__, __LINE__, DEBUG_MOD_ERR);
			
			$RV = FALSE;
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}

	/**
	 * Draw the module result(s)
	 *
	 * This function draws the results of sub modules on a template
	 * 
	 * param:		void
	 * return: 	void
	 * @access public
	 */ 
	function DrawResult($IDs = Array())
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'DrawResult of sub module: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);

		$objects = $this->GetObjects($IDs);
		$objDefs = $this->SubModule->GetObjectsDefinition();
		$RV = Array();

		if (is_array($objects)) {
			foreach ($objects AS $objID => $object) {
				if (is_object($object) && method_exists($object, 'Draw')) {
					$RV[$objID] = $object->Draw();
				} elseif (is_object($object)) {
					/*Don't know how to draw this object!*/
				} elseif (is_string($object)) {
					$RV[$objID] = $object;
				} else {
					$RV[$objID] = $object;
				}
			}
		}

		Debug(__CLASS__, __FUNCTION__, count($RV) . ' objects drawn onto array: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);
		
		DebugCloseGroup(DEBUG_MOD_OK);

		return $RV;
	}
	
	/**
	 * Xml Call function outside page generation, passthru to submodule
	 *
	 * This function is used to call functions of modules directly in stead
	 * of drawing the whole page.
	 * This function should always be used in stead of calling the function
	 * $Function directly, because the real module and its function could be
	 * housed in a submodule and will only be available through this call!
	 *
	 * @param	string	$Function	ID of function(s) to call
	 * @param	array		$Params		Parameters to pass to the function
	 */
	function XMLCall ($Functions, $Params = FALSE)
	{
		if (strlen($Functions) && is_object($this->SubModule) && method_exists($this->SubModule, 'GetXMLDefinition')) {
			$xmlobjects = $this->SubModule->GetXMLDefinition();
			$Functions = explode(',', $Functions);
			$rv = Array();
			
			foreach ($Functions AS $func) {
				if (is_array($xmlobjects[$func]) && count($xmlobjects[$func]) > 0) {
					$funcname = $xmlobjects[$func]['FunctionName'];
					
					if (method_exists($this->SubModule, $funcname)) {
						eval('$func_rv = $this->SubModule->' . $funcname . '();');

						if (!is_array($func_rv)) {
							if (strlen($xmlobjects[$func]['DivID'])) {
								$func_rv = array($xmlobjects[$func]['DivID'] => $func_rv);
							} else if (false) {
								/*TODO, resultset kan ook array zijn, in dat geval keys checken*/
							} else {
								/*$func_rv = array('Error' => $func.' is not properly defined');*/
							}
						}
					} else {
						$func_rv = array('Error' => $func.' is not properly defined');
					}
				} else {
					$func_rv = array('Error' => $func.' is not properly defined');
				}
				
				$rv = array_merge($rv, $func_rv);
			}	
		} else {
			$rv = FALSE;
		}
		
		return $rv;
	}

	/**
	 * Returns created default objects
	 *
	 * This function returns all objects of the module that are defined
	 * as default.
	 * 
	 * param:		void
	 * return: 	void
	 * @access public
	 */
	function GetObjects($IDs = Array())
	{	
		DebugGroup(__CLASS__, __FUNCTION__, 'Get default objects of sub module: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);

		if (is_subclass_of($this->SubModule, 'NitroBOSubModule')) {
			// Process $objDefs
			$objDefs = $this->SubModule->GetObjectsDefinition();
			$objects = Array();
			
			if (is_array($objDefs)) {
				foreach ($objDefs AS $objID => $Row) {
 					$doObject = FALSE;
 					
 					if (array_key_exists('DEBUG', $_GET)) {
 						$doObject = TRUE;
 					} else if (is_array($IDs) && count($IDs)) {
 						foreach ($IDs AS $ID) {
 							if (isset($Row[$ID]) && $Row[$ID]) {
 								$doObject = TRUE;
 							}
 						}
 					} else if (is_string($IDs) && strlen($IDs)) {
 						if (isset($Row[$IDs]) && $Row[$IDs]) {
 							$doObject = TRUE;
 						}
 					}
 					
 					if ($doObject) {
						if (method_exists($this->SubModule, $Row['FunctionName'])) {
							eval('$objects[$objID] = $this->SubModule->' . $Row['FunctionName'].'();');
						} else {
							Debug(__CLASS__, __FUNCTION__, 'Function does not exist: ' . $Row['FunctionName'], __FILE__, __LINE__, DEBUG_MOD_ERR);
						}
					} else {
						$objects[$objID] = FALSE;
					}
				}
			}
			
			$RV = $objects;
		} else {
			/*Error: Object is not a subclass of NitroBOSubModule*/
			Debug(__CLASS__, __FUNCTION__, 'Object is not a subclass of NitroBOSubModule: ' . $moduleFile . '=>' . $objClass, __FILE__, __LINE__, DEBUG_MOD_ERR);
			
			$RV = FALSE;
		}
		
		Debug(__CLASS__, __FUNCTION__, count($RV) . ' default objects found: ' . $this->Settings['SubModule'], __FILE__, __LINE__, DEBUG_MOD_OK);

		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;	
	}
	
	/**
	 * DoModuleComment
	 *
	 * Functions creates HTML comment for the Module with the Name, Version and Author(s)
	 */
	function DoModuleComment($ModName, $ModVersion, $ModAuthor)
	{
		$RV = "<!--\n\t";
		$RV.= "ModuleName:\t\t\t\t" . (!empty($ModName) ? $ModName: "Unknown") . "\n\t";
		$RV.= "ModuleVersion:\t\t" . (!empty($ModVersion) ? $ModVersion : "Unknown") . "\n\t";
		$RV.= "ModuleAuthor(s):\t" . (!empty($ModAuthor) ? implode(", ", $ModAuthor) : "Unknown");
		$RV.= "\n-->";
		
		return $RV;
	}
}
?>
Return current item: OpenNitro