Location: PHPKode > projects > OpenNitro > trunk/Nitro/Modules/BackOffice/Modules/Module.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Modules :: NitroBOSubModule :: Modules                           |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2006 June Systems B.V.                                      |
// +---------------------------------------------------------------------------+
// | This source file is copyrighted by June Systems BV, the Netherlands       |
// | If you would like to use this file in your projects, please contact       |
// | hide@address.com                                                     |
// +---------------------------------------------------------------------------+
// | Authors: Jesper Avôt <hide@address.com>                        |
// +---------------------------------------------------------------------------+
//
// $Id: Module.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * Include Form controls and Listing classes
 */
require_once "Nitro/Libraries/Form.inc.php";
require_once "Nitro/Libraries/Listing.inc.php";

/**
 * Module
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_Modules extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_Modules";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_Modules() { }
	
	/**
	 * GetSettingsDefinition function
	 *
	 * Which Settings are allowed in this Module?
	 */
	function GetSettingsDefinition()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		if (!isset($this->_ModuleSettings)) {
			$this->_ModuleSettings = Array(
				'P'			 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'P', 							'Default' => NULL), 
				'ModuleID' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ModuleID',				'Default' => NULL), 
				'AddModule'	 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddModule',				'Default' => NULL), 
				'IDString' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'IDString',				'Default' => NULL), 
				'File'							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'File',						'Default' => NULL), 
				'Class'		 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Class',						'Default' => NULL), 
				'Template' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Template',				'Default' => NULL), 
				'CloseTab' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'CloseTab',				'Default' => NULL), 
				'ModFilter' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ModFilter/',			'Default' => NULL), 
				'Start'							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Start',						'Default' => 0) 
			);
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $this->_ModuleSettings;
	}
	
	/**
	 * GetSettingsDefinition function
	 *
	 * Which Settings are allowed in this Module for XML Requests?
	 */
	function GetXMLDefinition()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		if (!isset($this->_XMLDefinition)) {
			$this->_XMLDefinition = Array(
				'DeleteModule' 	=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Delete Module", 
																 'DivID' 				=> "UserField", 
																 'FunctionName' => "DeleteModule"), 
				'EditModule' 	 	=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Edit Module", 
														 		 'DivID' 				=> "EditField", 
														 		 'FunctionName' => "EditModule"), 
				'SaveModule' 		=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Save Module", 
														 		 'DivID'			 	=> "ErrorDiv", 
														 		 'FunctionName' => "SaveModule"), 
				'FilterMod' 		=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Filter Module List", 
														 		 'DivID' 				=> "UserField", 
														 		 'FunctionName' => "ShowModuleList"), 
				'ModuleList' 		=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Module List", 
														 		 'DivID' 				=> "UserField", 
														 		 'FunctionName' => "ShowModuleList")
			);
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $this->_XMLDefinition;
	}
	
	/**
	 * GetObjectsDefinition function
	 *
	 * Which Objects are allowed ?
	 */
	function GetObjectsDefinition()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$this->_ModuleObjects = Array(
			'Draw' => Array('Type'					=> 'HTML',
											'Name'					=> 'Text',
											'FunctionName'	=> 'Draw',
											'Default'				=> TRUE)
		);
	
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $this->_ModuleObjects;
	}

	/**
	 * Draw function
	 */
	function Draw()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$RV = $this->CreateTabInterface(Language('Module Management'), $this->ShowModuleList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * DeleteModule function
	 */
	function DeleteModule()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$ModuleID = (int)$this->GetSetting('ModuleID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('Module', 
													Array('ModuleID' => $ModuleID), 
													Array('ModuleID' => $ModuleID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowModuleList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditModule function
	 */
	function EditModule()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$ModuleID = (int)($this->GetSetting('ModuleID') ? $this->GetSetting('ModuleID') : 0);
		
		if ($this->GetSetting('AddModule')) {
			$data = Array("IDString" => "", "File" => "", "Class" => "", "Template" => "");
			$formadd = TRUE;
			$Name = "Add New Module";
		} else {
			$Query = "SELECT 
									M.*, 
									MT.TemplateID AS Template 
								FROM 
									`Module` AS M 
								LEFT 
								JOIN 
									`Module_Template` AS MT 
								ON 
									MT.ModuleID = M.ModuleID 
								WHERE 
									M.ModuleID = " . $ModuleID;
			$data = $this->DB->getRow($Query);
			$formadd = FALSE;
			$Name = $data["IDString"];
		}
				
		$Form = new Form("EditModuleForm_" . $this->GetSetting('ModuleID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("ModuleID", "HIDDEN/VALUE=" . $this->GetSetting('ModuleID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddModule", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $ModuleID, "DIV/LABLE= ");
		$Form->AddOptionString("IDString", "TEXT/VALUE=" . urlencode($data["IDString"]) . "/LABLE=" . Language('IDString') . "/STYLE=width: 250px;");
		$Form->AddOptionString("File", "TEXT/VALUE=" . urlencode($data["File"]) . "/LABLE=" . Language('File') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Class", "TEXT/VALUE=" . urlencode($data["Class"]) . "/LABLE=" . Language('Class') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Template", "SELECT/VALUES=0: -- ".Language('Select a Template')." -- /DB=NitroBO:Template:TemplateID:Name" . ($data["Template"] ? "/SELECTED=" . $data["Template"] : "") . "/LABLE=" . Language('Template') . "/STYLE=width: 250px;");

		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveModule', GetXMLURL('EditModuleForm_" . $this->GetSetting('ModuleID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveModule', GetXMLURL('EditModuleForm_" . $this->GetSetting('ModuleID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $ModuleID . "'); return false;\" value='" . Language('Cancel') . "' />");

		$Form->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . $Custom);
		$Form -> SetTemplateIDs("file:" . NITRO_PATH . "Defaults/Templates/BackOffice/form.tpl", "file:" . NITRO_PATH . "Defaults/Templates/BackOffice/widget.tpl");
		$RV = $Form->Draw();

		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("JSCRIPT:()" => "AddPageTab('" . $ModuleID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveModule function
	 */
	function SaveModule()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$ModuleID = (int)$this->GetSetting('ModuleID');
		
		$Query = "SELECT 
								ModuleID 
							FROM 
								`Module` 
							WHERE 
								IDString = " . NitroPrepareDB($this->GetSetting('IDString'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('IDString'))) {					
			$RV = Array("ErrorDiv_" . $ModuleID => $this->PrePareMSG(Array("Error", "Please fill in an IDString.", TRUE), $ModuleID));
		} else if (!strlen($this->GetSetting('File'))) {
			$RV = Array("ErrorDiv_" . $ModuleID => $this->PrePareMSG(Array("Error", "Please fill in a Filename.", TRUE), $ModuleID));
		} else if (!strlen($this->GetSetting('Class'))) {
			$RV = Array("ErrorDiv_" . $ModuleID => $this->PrePareMSG(Array("Error", "Please fill in a Classname.", TRUE), $ModuleID));
		} else if (!strlen($this->GetSetting('Template'))) {
			$RV = Array("ErrorDiv_" . $ModuleID => $this->PrePareMSG(Array("Error", "Please choose a Template.", TRUE), $ModuleID));
		} else {
			$RV = $this->_SaveModuleInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveModuleInfo function (private)
	**/
	function _SaveModuleInfo()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$ID = $Transaction->addData('Module', 
																Array('IDString' => $this->GetSetting('IDString'), 
																		  'File' => $this->GetSetting('File'), 
																		  'Class' => $this->GetSetting('Class'), 
																		  'ModuleID' => (int)$this->GetSetting('ModuleID')));
																		  
		if (!$this->GetSetting('AddModule')) {
			$Transaction->addData('Module_Template', 
														Array('ModuleID' => (int)$this->GetSetting('ModuleID')), 
														Array('ModuleID' => (int)$this->GetSetting('ModuleID')), 
														'DELETE');
		}
		
		$Commit = $Transaction->Commit();
		
		if ($this->GetSetting('AddModule')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 ModuleID 
																						 FROM 
																							 Module 
																						 ORDER 
																						 BY 
																							 ModuleID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('ModuleID');
		}
		
		$ResultID = $newObjectID;
		
		$Transaction2 = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction2->addData('Module_Template', 
													 Array('ModuleID' => $ResultID, 
																 'TemplateID' => (int)$this->GetSetting('Template')), 
													 FALSE, 
													 'INSERT');
		$Transaction2->Commit();
		
		if ($Commit !== FALSE) {
			$RV = Array("NoError", Language('Module Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('Module could not be Saved'));
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddModule')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditModule', '&ModuleID=" . $ResultID . "'); ") . "DeletePageTab('0', true);", "UserField" => $this->ShowModuleList());
		} else {
			return Array( "UserField" => $this->ShowModuleList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('IDString') . "');"));
		}
	}
	
	/**
	 * ShowModuleList function
	 */
	function ShowModuleList($inDiv = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);

		$addimg = "<img src=\"./GetObject.php?NitroDefault=Images/Add.gif\" width=\"16\" height=\"16\" alt\"Add\" border=\"0\" />";
		$editimg = "<img src=\"./GetObject.php?NitroDefault=Images/Edit.gif\" width=\"16\" height=\"16\" alt\"Edit\" border=\"0\" />";
		$deleteimg = "<img src=\"./GetObject.php?NitroDefault=Images/Delete.gif\" width=\"16\" height=\"16\" alt\"Delete\" border=\"0\" />";
		
		$Start = Array(TRUE, (strlen($this->GetSetting('Start')) && $this->GetSetting('Start') !== 0 ? $this->GetSetting('Start') : 0));
		$Filter = Array(TRUE, (strlen($this->GetSetting('ModFilter')) && $this->GetSetting('ModFilter') !== 0 ? $this->GetSetting('ModFilter') : FALSE));

		$List = new Listing2("", "Module_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "Modules_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditModule', '&AddModule=1'); return false;", "", Language('Add Module'));
		$List->AddColumn(Language('IDString'), "IDString");
		$List->AddColumn(Language('File'), "File");
		$List->AddColumn(Language('Class'), "Class");
		$List->AddAction("Edit", Language('Edit'));
		$List->AddAction("Delete", Language('Delete'));
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								ModuleID, 
								IDString, 
								File, 
								Class 
							FROM 
								`Module` 
								" . ($List->Filter['IDString'] || $List->Filter['File'] || $List->Filter['Class'] ? "WHERE " : "") . "
								" . ($List->Filter['IDString'] ? "IDString LIKE " . NitroPrepareDB("%" . $List->Filter['IDString'] . "%") . " " : "") . "
								" . ($List->Filter['IDString'] && $List->Filter['File'] ? "AND " : "") . ($List->Filter['File'] ? "File LIKE " . NitroPrepareDB("%" . $List->Filter['File'] . "%") . " " : "") . "
								" . (($List->Filter['IDString'] || $List->Filter['File']) && $List->Filter['Class'] ? "AND " : "") . ($List->Filter['Class'] ? "Class LIKE " . NitroPrepareDB( "%" . $List->Filter['Class'] . "%" ) . " " : "") . "
							ORDER BY 
								ModuleID 
							" . ($List->usePages !== FALSE ? "LIMIT " . (int)$List->Start . ", " . (int)$List->maxPerPage : "");
		
		$Result = $this->DB->query($Query);
		
		$List->allPages = $this->DB->getOne("SELECT FOUND_ROWS()");
		$List->onChange = "ModuleList";

		if ($Result->numRows()) {
			while ($Data = $Result->fetchArray()) {
				$ListRow = new ListingRow2();
				$ListRow->AddData("IDString", $Data["IDString"]);
				$ListRow->AddData("File",$Data["File"]);
				$ListRow->AddData("Class", $Data["Class"]);
				$ListRow->SetAction("Edit", $editimg, "#", "ModuleXMLRequest('EditModule', '&ModuleID=" . $Data["ModuleID"] . "');", FALSE, TRUE, Language('Edit') . space() . $Data["IDString"]);
				$ListRow->SetAction("Delete", $deleteimg, "#", "if(confirm('Item is going to be removed, are you sure?')) ModuleXMLRequest('DeleteModule', '&ModuleID=" . $Data["ModuleID"] . "'); return false;", FALSE, TRUE, Language('Delete') . space() . $Data["IDString"]);
				
				$List->AddListRow($ListRow);
				
				unset($ListRow);
			}
		}
		$Result->free();
		$List-> SetTemplate("file:" . NITRO_PATH . "Defaults/Templates/BackOffice/Listing.tpl");
		$RV = $List->Draw();
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
}
?>
Return current item: OpenNitro