Location: PHPKode > projects > OpenNitro > trunk/Nitro/Modules/BackOffice/Pages/Module.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Modules :: NitroBOSubModule :: Pages                             |
// +---------------------------------------------------------------------------+
// | 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";

/**
 * Pages
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_Pages extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_Pages";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_Pages() { }
	
	/**
	 * 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), 
				'PageID' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'PageID',					'Default' => NULL), 
				'ObjectID'				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ObjectID',				'Default' => NULL), 
				'AddPage' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddPage',					'Default' => NULL), 
				'Name' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL), 
				'IDString' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'IDString',				'Default' => NULL), 
				'SystemIDString' 	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'SystemIDString',	'Default' => NULL), 
				'SecurityGroups' 	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'SecurityGroups/',	'Default' => NULL), 
				'TemplateID' 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TemplateID',			'Default' => NULL), 
				'PageData' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'PageData',				'Default' => NULL), 
				'AddVer'					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddVer',					'Default' => NULL), 
				'Lang'						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Lang',						'Default' => NULL), 
				'Ver'							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Ver',							'Default' => NULL), 
				'RemoveVersion'		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'RemoveVersion',		'Default' => NULL), 
				'TemplateVariable'=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TemplateVariable','Default' => NULL), 
				'PageDataID'			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'PageDataID',			'Default' => NULL), 
				'ObjectSettings'	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ObjectSettings',	'Default' => NULL), 
				'CloseTab' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'CloseTab',				'Default' => NULL), 
				'ModFilter' 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ModFilter/',			'Default' => NULL), 
				'Position' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Position',				'Default' => NULL), 
				'RuntimeSettings'	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'RuntimeSettings',	'Default' => NULL), 				
				'HiddenSettings'	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'HiddenSettings',	'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(
				'DeletePage' 		=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Delete Page", 
																 'DivID' 				=> "UserField", 
																 'FunctionName' => "DeletePage"), 
				'EditPage' 	 		=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Edit Page", 
														 		 'DivID' 				=> "EditField", 
														 		 'FunctionName' => "EditPage"), 
				'SavePage' 			=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Save Page", 
														 		 'DivID'			 	=> "ErrorDiv", 
														 		 'FunctionName' => "SavePage"), 
				'Version' 			=> Array('Type' => "HTML", 
																 'Name' => "Add Page Version", 
																 'DivID' => "UserField", 
																 'FunctionName' => "addVersion"), 
				'FilterMod' 		=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Filter Pages List", 
														 		 'DivID' 				=> "UserField", 
														 		 'FunctionName' => "ShowPagesList"), 
				'PageList' 			=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Pages List", 
														 		 'DivID' 				=> "UserField", 
														 		 'FunctionName' => "ShowPagesList"), 
				'GetTemplateVariable' => Array('Type' 				=> "HTML", 
																	 		 'Name' 				=> "Get a Template his Variablen from the DB", 
																	 		 'DivID' 				=> "UserField", 
																	 		 'FunctionName' => "GetTemplateVariable"),
				'GetObjectOptions' => Array('Type' 				=> "HTML", 
															 		  'Name' 				=> "Get the RunTimeSettings for an Object.", 
															 		  'DivID' 				=> "UserField", 
															 		  'FunctionName' => "GetObjectSettings"),
				'SavePageObject' => Array('Type' 				=> "HTML", 
														 		  'Name' 				=> "Save an PageObject", 
														 		  'DivID' 				=> "UserField", 
														 		  'FunctionName' => "SavePageObject")
			);
		}
		
		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;
	}
	
	function PreProcess()
	{
		if (strstr($this->GetSetting('ObjectID'),'_S')) {
			$this->Settings['ObjectID'] = ereg_replace('_S-([0-9])*', '', $this->GetSetting('ObjectID'));
		}
	}

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

		$RV = Array("UserField" => ($Transaction->Commit() !== FALSE) ? $this->ShowPagesList() : "");
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * GetTemplateVariable function
	 */
	function GetTemplateVariable($TID = FALSE, $PID = FALSE, $Settings = FALSE, $RunTimeSettings = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$TemplateID = (int)($this->GetSetting('TemplateID') ? $this->GetSetting('TemplateID') : $TID);
		$PageID = (int)($this->GetSetting('PageID') ? $this->GetSetting('PageID') : $PID);
		$LangCode = ($this->GetSetting('Lang') ? $this->GetSetting('Lang') : $Settings['LangCode']);
		$Version = (int)($this->GetSetting('Ver') ? $this->GetSetting('Ver') : $Settings['Version']);
		$PageDataID = ($this->GetSetting('PageDataID') ? $this->GetSetting('PageDataID') : $Settings['PageDataID']);
		
		if ((int)$PageDataID > 0 && !$RunTimeSettings) {
			$Settings = Array();
			$Query = "
				SELECT
					PO.TemplateVariable,
					PO.ObjectID,
					PO.Settings,
					PO.SortOrder,
					PO.PageDataID,		
					PD.Language,		
					PD.Version
				FROM `PageObjects` AS PO
				INNER JOIN `PageData` AS PD
					ON PO.PageDataID = PD.PageDataID
				WHERE PD.PageID = ".(int)$PID."
					AND PD.Language = ".NitroPrepareDB((($LangCode == "Default") ? "" : $LangCode))."
					AND PD.Version = " . (int)$Version."
					ORDER BY PO.SortOrder
			";
			$Result = $this->DB->query($Query);
			if ($Result->numRows()) {
				while ($Row = $Result->fetchArray()) { 
					$Settings[$Row["TemplateVariable"]][$Row["ObjectID"]][$Row["SortOrder"]] = $Row["Settings"];
				}
			}
		} else {
			$Settings = $RunTimeSettings;
		}

		if ($TemplateID) {
			$TemplateVariableQuery = "
				SELECT Variable
				FROM `TemplateVariable` 
				WHERE TemplateID = " . $TemplateID . " 
					AND TemplateObjectOnly = 0 
				ORDER BY Variable
			";											
			$Result = $this->DB->query($TemplateVariableQuery);
			$templateVariables = Array();
			if ($Result->numRows()) {
				while ($Data = $Result->fetchArray()) {
					$templateVariables[] = $Data;
				}
			}
			$Result->free();

			$tabJscript = "";
			$selectsJscript = " ";
 			if (count($templateVariables)) {
				$tabJscript.= "if(!theTabs['TemplateVariables_" . $LangCode . "_" . $Version . "_" . $PageID . "']) theTabs['TemplateVariables_" . $LangCode . "_" . $Version . "_" . $PageID . "'] = new Array();";
				$TemplateTab = new FormTab("TemplateVariables_" . $LangCode . "_" . $Version . "_" . $PageID);
				$Widget = new NitroTemplate("file:" . NITRO_PATH . "Defaults/Templates/BackOffice/widget.tpl");
				$Widget->ClearAll();
				
				foreach($templateVariables AS $Data) {
					$theSet = Array();
					$n=0;
					
					if (is_array($Settings[$Data["Variable"]])) { 
						foreach ($Settings[$Data["Variable"]] AS $Key => $Value) {
							if ($Name = $this->DB->GetOne("SELECT Name FROM Object WHERE ObjectID = " . (int)$Key)) {
								foreach($Value AS $sort => $settings) {
									$theSet[$sort] = $Key."_S-".$n . ":" . urlencode($Name.($settings && $settings !== 'NULL' ? ' **' : ''));
									$n++;
								}		
							} 
						}	
					}
					ksort($theSet);

					$tabJscript.= "theTabs['TemplateVariables_" . $LangCode . "_" . $Version . "_" . $PageID . "']['" . $LangCode . "_" . $Version . "_" . $Data["Variable"] . "'] = '" . $LangCode . "_" . $Version . "_" . $Data["Variable"] . "';";
					$TemplateTab->AddTab($Data["Variable"], $LangCode . "_" . $Version . "_" . $Data["Variable"]);
					$ObjectQuery = "
						SELECT O.Name, O.ObjectID, M.File AS ModuleFile, M.Class AS ModuleClass
						FROM Object AS O
						LEFT JOIN Module AS M
							 ON M.ModuleID = O.ModuleID
					";
					$Result = $this->DB->query($ObjectQuery);
					$Objects = Array();
					while($Row = $Result->fetchArray()) {
						$concat = '';
						if ($Row['ModuleFile'] && $Row['ModuleClass']) {
							if (include_once $Row["ModuleFile"]) {
								if (is_callable(array($Row["ModuleClass"],"GetModuleRuntimeConfiguration")) && is_array($Configuration = call_user_func(array($Row["ModuleClass"],"GetModuleRuntimeConfiguration")))) {
									$concat = ' **';
								}
							}
						}
						$Objects[] = $Row['ObjectID'].':'.urlencode($Row['Name'].$concat);
					}
					$Result->free();
					$TemplateTab->AddOptionString("PageData[" . $LangCode . "][" . $Version . "][PageSelect_" . $Data["Variable"] . "_" . $PageID . "]", "SELECT/VALUES=doNotAdd: -- " . Language('Select an Object') . " -- /STYLE=width: 250px;/VALUES=".implode(',', $Objects)."/LABLE=" . Language('Available Objects'));

					$Custom = urlencode("<input type='button' onclick=\"AddPageObject('PageData[" . $LangCode . "][" . $Version . "][PageSelect_" . $Data["Variable"] . "_" . $PageID . "]', 'PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]', 'PageData[" . $LangCode . "][" . $Version . "][PageObjectOptions_" . $Data["Variable"] . "_" . $PageID . "]', '" . $PageID . "', '" . $LangCode . "', '" . $Version . "', '" . $Data["Variable"] . "',  '" . $PageDataID . "'); return false;\" value='" . Language('add selected') . "' />");

					$TemplateTab->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . $Custom);
					
					$tmp = 'GetObjectOptions(this.options[this.selectedIndex].value, this.selectedIndex, "' . $PageID . '", "' . $LangCode . '", "' . $Version . '", "' . $Data["Variable"] . '", "' . $PageDataID . '")';
					$tmp = rawurlencode($tmp);  
					
					$TemplateTab->AddOptionString("PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]", "SELECTMULTIPLE/LABLE=" . Language('Added Objects') . "/STYLE=width: 250px;/SCRIPT=ondblclick:" . $tmp . "/VALUES=" . (count($theSet) ? implode(",", $theSet) : ""));
					$TemplateTab->AddOptionString("temp", "HTML/LABLE= /VALUE=** = ".Language('Object has RunTime settings').' ('.Language('double-click to edit').')');
					
					$Custom = urlencode("<table cellpadding='0' cellspacing='0'><tr><td><input type='button' onclick=\"MoveSelectedPageObject('PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]', 'UP', '" . $LangCode . "', '" . $Version . "', '" . $Data["Variable"] . "', '" . $PageDataID . "');\" value='" . Language( "Move Up" ) . "' />&nbsp;</td>" );
					$Custom.= urlencode("<td><input type='button' onclick=\"MoveSelectedPageObject('PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]', 'DOWN', '" . $LangCode . "', '" . $Version . "', '" . $Data["Variable"] . "', '" . $PageDataID . "');\" value='" . Language( "Move Down" ) . "' />&nbsp;</td>" );
					$Custom.= urlencode("<td><input type='button' onclick=\"DeletePageObject('PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]', '" . $PageID . "', '" . $LangCode . "', '" . $Version . "', '" . $Data["Variable"] . "', '" . $PageDataID . "'); return false;\" value='" . Language('Delete') . "' /></td></tr></table>");

					$TemplateTab->AddOptionString("Temp2", "HTML/LABLE= /VALUE=" . $Custom);
					$TemplateTab->AddOptionString("PageData[" . $LangCode . "][" . $Version . "][PageObjectOptions_" . $Data["Variable"] . "_" . $PageID . "]", "DIV/LABLE= ");

					$selectsJscript.= "SelectAllItemsObjects['PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]'] = 'PageData[" . $LangCode . "][" . $Version . "][PageObjects][" . $Data["Variable"] . "][" . $PageDataID . "]'; ";
				}
						
				$DrawResult = $TemplateTab->Draw($Widget);
				$RV = Array("PageData[" . $LangCode . "][" . $Version . "][TemplateVariablesDiv_" . $PageDataID . "]" => $DrawResult['Controls']);
			} else {
				$RV = Array("PageData[" . $LangCode . "][" . $Version . "][TemplateVariablesDiv_" . $PageDataID . "]" => "");
			}
			
			$RV['JSCRIPT:()'] = $tabJscript . $selectsJscript;
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * GetObjectOptions function
	 */
	function GetObjectSettings()
	{
		$Settings = ($this->GetSetting('ObjectSettings') ? unserialize($this->GetSetting('ObjectSettings')) : "");
		$ModuleID = $this->DB->GetOne("SELECT ModuleID 
																	 FROM `Object` 
																	 WHERE ObjectID = " . $this->GetSetting('ObjectID'));												 	
		
		$ModuleInfo = $this->DB->getRow("SELECT Class, 
																				File 
																		 FROM `Module` 
																		 WHERE ModuleID = " . $ModuleID);
		
		if ($this->GetSetting('PageDataID') && $this->GetSetting('PageDataID') > 0) {
			// get the settings from db 	
			if (is_array($Settings)) {
				$objectRuntimeSettings = $Settings;
			} else {
				$objectRuntimeSettings = $this->DB->GetOne("
					SELECT Settings 
					FROM `PageObjects` 
					WHERE PageDataID = ". $this->GetSetting('PageDataID') ."
						AND TemplateVariable = '" . $this->GetSetting('TemplateVariable') ."'												 	
						AND SortOrder = " . $this->GetSetting('Position')."
					");
				$objectRuntimeSettings = unserialize($objectRuntimeSettings);
			}
		}
		
		$FormGroup = new FormGroup("PageObjectSettings_" . $this->GetSetting('TemplateVariable') . "_" . $this->GetSetting('ObjectID'), "", $this->DB->GetOne("SELECT Name FROM Object WHERE ObjectID = " . $this->GetSetting('ObjectID')) . " Settings");
		$Widget = new NitroTemplate($this->WidgetTemplateID);
		$Widget->ClearAll();

		if ($ModuleInfo) {
			if (include_once $ModuleInfo["File"]) {	
				if (is_callable(array($ModuleInfo["Class"],"GetModuleRuntimeConfiguration")) && is_array($Configuration = call_user_func(array($ModuleInfo["Class"],"GetModuleRuntimeConfiguration"), $this->SubModulesDir))) {
					foreach ($Configuration AS $Key => $Value) {
						if (ereg("^(\[)|(\])$", $Key)) {
							$Key = substr($Key, 0, strpos($Key, "["));
						}
						
						if (!strpos($Value, "LABLE") && !strpos($Value, "LABEL")) $Value.= "/LABLE=" . Language($Key);
						
						if (strtoupper(substr($Value, 0, 4)) == 'TEXT') {

							$Value = $Value . "/VALUE=" . (strlen($objectRuntimeSettings[$Key]) ? urlencode($objectRuntimeSettings[$Key]) : "");
							
						} else {

							$Value = $Value . "/SELECTED=" . (strlen($objectRuntimeSettings[$Key]) ? urlencode($objectRuntimeSettings[$Key]) : "");
						}
						$FormGroup->AddOptionString($Key , $Value);
					}

					$FormGroup->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . urlencode("<input type=\"button\" value=\"" . Language('Save Settings') . "\" onclick=\"SavePageObjectSettings('PageData[" . $this->GetSetting('Lang') ."][" .  $this->GetSetting('Ver') . "][PageObjectOptionsBody]','[" . $this->GetSetting('Lang') ."][" .  $this->GetSetting('Ver') . "][RunTimeSettings][" . $this->GetSetting('TemplateVariable') . "][" . $this->GetSetting('PageDataID') . "][" . $this->GetSetting('Position') . "]','" . $this->GetSetting('Lang') . "','" . $this->GetSetting('Ver') . "', '" . $this->GetSetting('TemplateVariable') . "', '" . $this->GetSetting('PageID') . "', '" . $this->GetSetting('ObjectID') . "');\" /> <input type=\"button\" value=\"" . Language('Close') . "\" onclick=\"SavePageObjectSettings('','','" . $this->GetSetting('Lang') . "','" . $this->GetSetting('Ver') . "', '" . $this->GetSetting('TemplateVariable') . "', '" . $this->GetSetting('PageID') . "', '" . $this->GetSetting('ObjectID') . "');\" />"));
				} else {
					$FormGroup->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . urlencode("<div><i>There are no configurable runtime settings for this object.</i></div>"));
					$FormGroup->AddOptionString("Temp2", "HTML/LABLE= /VALUE=" . urlencode("<input type=\"button\" value=\"" . Language('Close') . "\" onclick=\"SavePageObjectSettings('','','" . $this->GetSetting('Lang') . "','" . $this->GetSetting('Ver') . "', '" . $this->GetSetting('TemplateVariable') . "', '" . $this->GetSetting('PageID') . "', '" . $this->GetSetting('ObjectID') . "');\" />"));
				}	
			}
		}
		$DrawResult = $FormGroup->Draw($Widget);

		$RV = $DrawResult['Controls'];
		$RV = Array("PageData[" . $this->GetSetting('Lang') . "][" . $this->GetSetting('Ver') . "][PageObjectOptionsBody]" => $RV);

	 return $RV;
	}
	
	/**
	 * EditPage function
	 */
	function EditPage()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		$PageID = (int)($this->GetSetting('PageID') ? $this->GetSetting('PageID') : 0);
		
		if ($this->GetSetting('AddPage')) {
			$SGQuery = "
				SELECT 
					SecurityGroupID AS ID, 
					Name 
				FROM `SecurityGroup`
			";
			$data = Array("Name" => "", "IDString" => "", "SystemIDString" => "");
			$formadd = TRUE;
			$Name = "Add New Page";
			$PageData = Array();
			$countresult = $this->DB->query($SGQuery);
			
			$count = ($countresult->numRows() > 20) ? 20 : $countresult->numRows();
		} else { 
			$Query = "SELECT 	* 
								FROM `Page` 
								WHERE PageID = " . $PageID;
			$data = $this->DB->getRow($Query);
			$SGQuery = "SELECT 
										SG.SecurityGroupID AS ID, 
										Name, 
										PageID AS SELECTED 
									FROM SecurityGroup AS SG
									LEFT JOIN 
										Page_SecurityGroup AS P_SG
									ON 
										P_SG.SecurityGroupID = SG.SecurityGroupID 
									AND 
										P_SG.PageID = " . $PageID;
			$formadd = FALSE;
			$Name = $data["Name"];
			$countresult = $this->DB->query($SGQuery);
			$count = ($countresult->numRows() > 20) ? 20 : $countresult->numRows();
			$Query = "SELECT *
								FROM `PageData`
								WHERE PageID = " . (int)$PageID . "
								ORDER BY Language";
			$Result = $this->DB->query($Query);
			
			while ($Row = $Result->fetchArray()) {
				if($Row["Language"] == "") $Row["Language"] = "Default";
				$PageData[$Row["Language"]][$Row["Version"]] = $Row;
			}
		}
		
		if ($this->GetSetting('AddVer')) {
			$Lang = $this->GetSetting('Lang');
			$Vers = $this->GetSetting('Ver');
			
			$PageData[$Lang][$Vers] = Array("Language" => $Lang, "Version" => $Vers, "PageDataID" => 0, "Title" => "", "Description" => "", "Published" => 0, "VisibleFrom" => "", "VisibleTill" => "", "TemplateID" => 0);
		}
		
		$FormName = "EditPageForm_" . ($this->GetSetting('PageID') ? $this->GetSetting('PageID') : 'NEW'.NitroCreateRandomString(4));
		$Form = new Form($FormName, "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("PageID", "HIDDEN/VALUE=" . $this->GetSetting('PageID'));
	
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddPage", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $PageID, "DIV/LABLE= ");
		$Form->AddOptionString("Name", "TEXT/VALUE=" . $data["Name"] . "/LABLE=" . Language('Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("IDString", "TEXT/VALUE=" . $data["IDString"] . "/LABLE=" . Language('IDString') . "/STYLE=width: 250px;");
		$Form->AddOptionString("SystemIDString", "TEXT/VALUE=" . $data["SystemIDString"] . "/LABLE=" . Language('System ID String') . "/STYLE=width: 250px;");
		$Form->AddOptionString("SecurityGroups/", "SELECTMULTIPLE/QUERY=".$this->NitroBODBAlias.":" . $SGQuery . "/LABLE=" . Language('Security Groups') . "/STYLE=width: 250px;/SIZE=" . $count . "");
		
		$tabJscript = "";
		$LanguageTab = new FormTab("Language_" . $PageID);
		
		if (is_array($this->AllowedLanguages) && count($this->AllowedLanguages)) {
			$tabJscript.= "if(!theTabs['Language_" . $PageID . "']) theTabs['Language_" . $PageID . "'] = new Array();";
			
			foreach ($this->AllowedLanguages AS $LangCode => $LangName) {
				if (!is_array($PageData[$LangCode])) $PageData[$LangCode] = Array();
				$this->EditLanguageTab($LanguageTab, $tabJscript, $PageID, $LangCode, $LangName, $PageData);
			}
		}
		
		$Form->AddOption($LanguageTab);

		$Custom = urlencode("<table cellpadding='0' cellspacing='0'><tr><td><input type='button' onclick=\"ModuleXMLRequest('SavePage', GetXMLURL('" . $FormName ."')); return false;\" value='" . Language('Save') . "' />&nbsp;</td>");
		$Custom.= urlencode("<td><input type='button' onclick=\"ModuleXMLRequest('SavePage', GetXMLURL('" . $FormName ."') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />&nbsp;</td>");
		$Custom.= urlencode("<td><input type='button' onclick=\"DeletePageTab('" . $PageID . "'); return false;\" value='" . Language('Cancel') . "' /></td></tr></table>");
		$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");
		
		$Drawn_Form = $Form->Draw();
		
		$return_javascript = $tabJscript;

		if ($this->GetSetting('AddVer') || $this->GetSetting('RemoveVersion')) {
			$return_javascript.= "UpdatePageTab('" . $PageID . "', 'Content', '" . rawurlencode($Drawn_Form) . "');";
		} else {
			$return_javascript.= "AddPageTab('" . $PageID . "', '" . rawurlencode($Drawn_Form) . "', '" . rawurlencode($Name) . "');";
		}
	
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return array('JSCRIPT:()' => $return_javascript);
		
	}
	
	/**
	 * Draw a Language tab and contents with the given options
	 *
	 * object	$LanguageTab	LanguageVersion tab to draw into
	 * string	$tabJscript	String to append and Javascript added by this function
	 * int	$PageID	ID of page being editted
	 * string	$LangCode	Language code
	 * string	$LangName	Language Name (should not be needed!)
	 * array	$PageData	 ???
	 */
	function EditLanguageTab(&$LanguageTab, &$tabJscript, $PageID, $LangCode, $LangName, $PageData)
	{
		$tabJscript.= "theTabs['Language_" . $PageID . "']['" . $LangCode . "'] = '" . $LangName . "';";
		$LanguageTab->AddTab($LangName, $LangCode);
		
		$LanguageVersionTab = new FormTab($LangCode . "Version_" . $PageID);
		$LanguageVersionTab->AddAction(Language('Add Version'), "#", "", "ModuleXMLRequest('Version', GetXMLURL('Language_" . $PageID . "_TAB_" . $LangCode . "') + '&AddVer=1&Lang=" . $LangCode . "&PageID=" . $PageID . "'); return false;");  

		$tabJscript.= "if(!theTabs['" . $LangCode . "Version_" . $PageID . "']) theTabs['" . $LangCode . "Version_" . $PageID . "'] = new Array();";
		
		foreach ($PageData[$LangCode] AS $Ver => $Content) {
			$this->EditLanguageVersionTab($LanguageVersionTab, $tabJscript, $PageID, $LangCode, $Ver, $Content);
			if ($Content['Published'] OR ereg('NEW', $Content['PageDataID'])) $LanguageVersionTab->SetSelectedTab($LangCode.$Ver); // Set last published tab selected
		}

		$LanguageTab->AddOption($LanguageVersionTab); 
	}

	/**
	 * Draw a Language Version tab and contents with the given options
	 *
	 * object	$LanguageVersionTab	LanguageVersion tab to draw into
	 * string	$tabJscript	String to append and Javascript added by this function
	 * int	$PageID	ID of page being editted
	 * string	$LangCode	Language code being editted/added
	 * int	$Ver	Version being editted/added
	 * array	$Content	 ???
	 */
	function EditLanguageVersionTab(&$LanguageVersionTab, &$tabJscript, $PageID, $LangCode, $Ver, $Content)
	{
		$tabJscript.= "theTabs['" . $LangCode . "Version_" . $PageID . "']['" . $LangCode . $Ver . "'] = '" . $LangCode . $Ver . "';";
		$LanguageVersionTab->AddTab( "v" . $Ver, $LangCode . $Ver);
		$Sets = Array();
		if (is_array($Content['RunTimeSettings'])) {
			// From POST!
			foreach($Content['RunTimeSettings'] AS $TemplateVariable => $values) {
				foreach($values AS $objID => $rest) {
					foreach($rest AS $sortOrder => $settings) {
						$this->HiddenSettings[$LangCode][$Ver][$TemplateVariable].= "<div style=\"border:1px solid black\" id=\"[" . $LangCode . "][" .  $Ver . "][RunTimeSettings][" . $TemplateVariable . "][" . $Content['PageDataID'] . "][" . $sortOrder . "]\"> <input type=\"hidden\" id=\"HiddenInput_PageData[" . $LangCode . "][" .  $Ver . "][RunTimeSettings][" . $TemplateVariable . "][" . $Content["PageDataID"] . "][" . $sortOrder . "]\" name=\"PageData[" . $LangCode . "][" .  $Ver . "][RunTimeSettings][" . $TemplateVariable . "][" . $Content["PageDataID"] . "][" . $sortOrder . "]\" value=\"". $settings ."\" />". $settings ." " . $LangCode . "][" .  $Ver . "][RunTimeSettings][" . $TemplateVariable . "][" . $Content["PageDataID"] . "][" . $sortOrder . "</div> \n";
					}
				}
			}
		} else {
			$Query = "
				SELECT
					PO.TemplateVariable,
					PO.ObjectID,
					PO.Settings,
					PO.SortOrder,
					PO.PageDataID,		
					PD.Language,			
					PD.Version
				FROM `PageObjects` AS PO 
				INNER JOIN `PageData` AS PD 
					 ON PO.PageDataID = PD.PageDataID 
				WHERE PD.PageID = " . (int)$PageID . " 
					AND PD.Language = " . NitroPrepareDB((($LangCode == "Default") ? "" : $LangCode)) . " 
					AND PD.Version = " . (int)$Ver ."
				ORDER BY PO.SortOrder
			";
			$Result = $this->DB->query($Query);															
			if ($Result->numRows()) {
				while ($Row = $Result->fetchArray()) {
					$this->HiddenSettings[($Row["Language"] ? $Row["Language"] : "Default")][$Row["Version"]][$Row["TemplateVariable"]].= "<div style=\"border:1px solid black\" id=\"[" . ($Row["Language"] ? $Row["Language"] : "Default") . "][" .  $Row["Version"] . "][RunTimeSettings][" . $Row["TemplateVariable"] . "][" . $Row["PageDataID"] . "][" . $Row["SortOrder"] . "]\"> <input type=\"hidden\" id=\"HiddenInput_PageData[" . ($Row["Language"] ? $Row["Language"] : "Default") . "][" .  $Row["Version"] . "][RunTimeSettings][" . $Row["TemplateVariable"] . "][" . $Row["PageDataID"] . "][" . $Row["SortOrder"] . "]\" name=\"PageData[" . ($Row["Language"] ? $Row["Language"] : "Default") . "][" .  $Row["Version"] . "][RunTimeSettings][" . $Row["TemplateVariable"] . "][" . $Row["PageDataID"] . "][" . $Row["SortOrder"] . "]\" value=\"". urlencode($Row["Settings"]) ."\" />". urlencode($Row["Settings"]) ." " . ($Row["Language"] ? $Row["Language"] : "Default") . "][" .  $Row["Version"] . "][RunTimeSettings][" . $Row["TemplateVariable"] . "][" . $Row["PageDataID"] . "][" . $Row["SortOrder"] . "</div> \n";
				}
			}
		}

		$Sets['LangCode'] = $LangCode;
		$Sets['Version'] = $Ver;
		$Sets['PageDataID'] = $Content['PageDataID'];							
		$TemplateVariable = $this->GetTemplateVariable($Content['TemplateID'], $PageID, $Sets, $Content['RunTimeSettings']);
		
		if (is_array($TemplateVariable) && count(array_values($TemplateVariable))) {
			$TemplateVariable_Content = $TemplateVariable["PageData[" . $LangCode . "][" . $Ver . "][TemplateVariablesDiv_" . $Content['PageDataID'] . "]"];
			
			if (strlen($TemplateVariable['JSCRIPT:()'])) {
				$tabJscript.= $TemplateVariable['JSCRIPT:()'];
			}
		} else {
			$TemplateVariable_Content = '';
		}

		$Widgets = Array(
			"PageData[" . $LangCode . "][" . $Ver . "][PageDataID]" => "HIDDEN/VALUE=" . $Content['PageDataID'],
			"PageData[" . $LangCode . "][" . $Ver . "][Title]" => "TEXT/LABEL=" . Language('Title') . "/VALUE=" . $Content['Title'] . "/STYLE=width: 250px;",
			"PageData[" . $LangCode . "][" . $Ver . "][Description]" => "TEXT/LABEL=" . Language('Description') . "/VALUE=" . $Content['Description'] . "/STYLE=width: 250px;",
			"PageData[" . $LangCode . "][" . $Ver . "][Published]" => "CHECKBOX/LABEL=" . Language('Published') . "/VALUE=1:" . Language('Yes') . "/SELECTED=" . $Content['Published'],
			"PageData[" . $LangCode . "][" . $Ver . "][VisibleFrom]" => "DATE/LABEL=" . Language('Visible From') . "/VALUE=" . rawurlencode($Content['VisibleFrom']) . "/STYLE=width: 250px;",
			"PageData[" . $LangCode . "][" . $Ver . "][VisibleTill]" => "DATE/LABEL=" . Language('Visible Till') . "/VALUE=" . rawurlencode($Content['VisibleTill']) . "/STYLE=width: 250px;",
			
			"PageData[" . $LangCode . "][" . $Ver . "][TemplateID]" => "SELECT/LABEL=" . Language('Template') . "/VALUES=0:-- " . Language('Select a Template') . " --/DB=NitroBO:Template:TemplateID:Name/STYLE=width: 250px;/SELECTED=" . $Content['TemplateID'] . "/ONCHANGE=" . urlencode("ModuleXMLRequest('GetTemplateVariable', '&PageID=" . $PageID . "&AddPage=" . ($formadd == TRUE ? "1" : "0") . "&Lang=" . $LangCode . "&Ver=" . $Ver . "&TemplateID=' + this.options[this.selectedIndex].value + '&PageDataID=" . $Content['PageDataID'] . "'); return false;"),
			"PageData[" . $LangCode . "][" . $Ver . "][TemplateVariablesDiv_" . $Content['PageDataID'] . "]" => "DIV/LABLE= /VALUE=" . urlencode($TemplateVariable_Content),
			"PageData[" . $LangCode . "][" . $Ver . "][PageObjectOptionsBody]" => "DIV/LABLE= /VALUE="
		);

		$LanguageVersionTab->AddOptionsFromArray($Widgets);

		if (is_array($this->HiddenSettings[$LangCode][$Ver])){
			foreach ($this->HiddenSettings[$LangCode][$Ver] as $templatevar => $hiddensettings) {$n++;
				$hiddenDivs .= rawurlencode("<div style=\"border:1px solid green\" id = \"PageObjectOptionsHidden[" .  $LangCode ."][" . $Ver . "][" .$templatevar . "]\">PageObjectOptionsHidden[" .  $LangCode ."][" . $Ver . "][" .$templatevar . "]" . $hiddensettings . "</div>\n");
			}	
		}	
		$hiddenDivs	= rawurlencode("<div id =\"hiddenDivContainer[".$LangCode."][".$Ver."]\" style=\"display: none\">") .$hiddenDivs. urlencode("</div>");
		$LanguageVersionTab->AddOptionString("HiddenDivContainer[".$LangCode."][".$Ver."]", "HTML/LABLE= /VALUE=" . $hiddenDivs);

		/* No removing of versions in this version of Nitro
		$DeleteBTN = urlencode("<input type='button' onclick=\"ModuleXMLRequest('Version', GetXMLURL('" . $FormName ."') + '&Ver=" . $Ver . "&Lang=" . $LangCode . "&PageID=" . $PageID . "&RemoveVersion=1'); return false;\" value='" . Language('Delete Version') . "' />");
		if ($LangCode == "Default" && $Ver != 1 && $Ver == count($PageData[$LangCode])) {
			$LanguageVersionTab->AddOptionString("DeleteBTN", "HTML/LABLE= /VALUE=" . $DeleteBTN);
		} else if ($LangCode != "Default" && $Ver == count($PageData[$LangCode])) {
			$LanguageVersionTab->AddOptionString("DeleteBTN", "HTML/LABLE= /VALUE=" . $DeleteBTN);
		}
		*/
	}

	/**
	 * SavePage function
	 */
	function SavePage()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);

		$PageID = (int)$this->GetSetting('PageID');
		$securitygroups = $this->GetSetting('SecurityGroups');
		
		if (!strlen($this->GetSetting('Name'))) {					
			$RV = Array("ErrorDiv_" . $PageID => $this->PrePareMSG(Array("Error", "Please fill in a Name.", TRUE), $PageID));
		} else if (!is_array( $securitygroups ) || ($securitygroups && $securitygroups[0] == "")) {
			$RV = Array("ErrorDiv_" . $PageID => $this->PrePareMSG(Array("Error", "Please select one or more SecurityGroups.", TRUE), $PageID));
		} else {
			$RV = $this->_SavePageInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * addVersion function
	 *
	 * @return mixed Output of the EditMenu function.
	 */
	function addVersion()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$WidgetTemplate = new NitroTemplate('file:' . NITRO_PATH . "Defaults/Templates/BackOffice/widget.tpl");
		$PageID		= $this->GetSetting('PageID');
		$LangCode	= $this->GetSetting('Lang');

		$LanguageTab = new FormTab("Language_" . $PageID);
		$LanguageTab->SetSelectedTab('');
		$tabJscript = '';
		
		// Get PageData from POST and add new empty version
		$PageData = $this->GetSetting('PageData');
		$NewVersion = count($PageData[$LangCode]) + 1;

		// Prepare PageData for RunTimeSettings
		// TODO: clean this up. It should not have to be necessary to do this !
		if (is_array($PageData)) {
			foreach($PageData AS $languageCode => $rest) {
				foreach($rest AS $version => $rest2) {
					$OldRunTimeSettings = $rest2['RunTimeSettings'];
					$pageDataID = $rest2['PageDataID'];
					$PageData[$languageCode][$version]['RunTimeSettings'] = Array();
					if (is_Array($rest2['PageObjects'])) {
						foreach($rest2['PageObjects'] AS $templateVariable => $rest3) {
							foreach($rest3 AS $pageDataID => $vars) {
								$vars = ereg_replace('_S-([0-9])*', '', $vars);
								$vars = explode(',', $vars);
								foreach($vars AS $sortOrder => $var) {
									$PageData[$languageCode][$version]['RunTimeSettings'][$templateVariable][$var][$sortOrder] = $OldRunTimeSettings[$templateVariable][$pageDataID][$sortOrder];
								}
							}
						}
					}
				}
			}
		}
		$PageData[$LangCode][$NewVersion] = Array('PageDataID' => 'NEW-'.$PageID.'-'.$LangCode.'-'.$NewVersion);

		$this->EditLanguageTab($LanguageTab, $tabJscript, $PageID, $LangCode, $this->AllowedLanguages[$LangCode], $PageData);
		$LanguageTab->SetSelectedTab(''); // set selected tab to new tab
		// There is only 1 widget on this tab, the version tab widget !
		$LanguageTabRV = $LanguageTab->Options[$LangCode][0]->Draw($WidgetTemplate);
		
		DebugCloseGroup(DEBUG_MOD_OK);
		return array('JSCRIPT:()' => $tabJscript, 'Language_' . $PageID . '_TAB_' . $LangCode => $LanguageTabRV['Controls']);
	}
	
	/**
	 * RemoveVersion function
	 */
	function RemoveVersion($PageID, $Version, $Lang)
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('PageData', 
							  Array("PageID" => (int)$PageID, 
							  		"Version" => (int)$Version, 
							  		"Language" => ($Lang == "Default" ? "" : $Lang)), 
							  Array("PageID" => (int)$PageID, 
							  		"Version" => (int)$Version, 
							  		"Language" => ($Lang == "Default" ? "" : $Lang)), 
							  "DELETE");
				  
		$RV = ($Transaction->Commit() !== FALSE) ? TRUE : FALSE;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * SaveVersions function
	 *
	 * @param int $PageID The current PageID
	 * @param array $Langs Array with the data to save.
	 * @return boolean (TRUE/FALSE)
	 */
	function SaveVersions($PageID, $Langs, $Add = FALSE)
	{
		
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		
		if (is_array($Langs)) {
			foreach ($Langs AS $Lang => $Versions) {
				if (is_array($Versions)) {
					foreach ($Versions AS $Version => $Data) {
						if (is_array($Data)) {
							foreach ($Data AS $Key => $Value) {
								if (strstr($Key, "RunTimeSettings") || strstr($Key, "PageSelect") || $Key == "PageObjects") {
									unset($Data[$Key]);
								}
							}
							
							$Data['PageDataID'] = (int)$Data['PageDataID']; // Remove NEW-?-?-? ids
							$Data['PageID'] = (int)$PageID;
							$Data['Language'] = ($Lang == "Default" ? "" : $Lang);
							$Data['Version'] = (int)$Version;
							if ($Data['VisibleFrom'] == "") $Data['VisibleFrom'] = NULL;
							if ($Data['VisibleTill'] == "") $Data['VisibleTill'] = NULL;
							
							if ($Add !== FALSE) {
								if ($Data['Title'] != "" && ($Data['TemplateID'] != "" || $Data['TemplateID'] != 0)) {
									$Transaction->addData('PageData', $Data);
								}
							} else {
								$Transaction->addData('PageData', $Data);
							}
							
							// TODO: dit moet weg! Niet alles deleten voor het inserten. Update bestaande, delete verwijderde ....
							$Transaction->addData('PageObjects', 
			 					Array('PageDataID' => $Data['PageDataID']), 
			 					Array('PageDataID' => $Data['PageDataID']), 
			 					"DELETE"
				 			);
						}
					}
				}
			}
		}
		$Commit = $Transaction->Commit();

		$Transaction2 = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		if (is_array($Langs)) { 
			foreach ($Langs AS $Lang => $Versions) {
				if (is_array($Versions)) {
					foreach ($Versions AS $Version => $Data) {
						if (is_array($Data)) {
							if (is_array($Data['PageObjects'])) {
								// TODO: dit moet weg! InsertID van transaction gebruiken ...
								if ($Data['PageDataID'] == 0) {
									$Data['PageDataID'] = $this->DB->GetOne("SELECT PageDataID FROM PageData ORDER BY PageDataID DESC LIMIT 1");
								}
								 
								foreach ($Data['PageObjects'] AS $TemplateVariable => $TemplateData) {									
									foreach ($TemplateData AS $PageDataID => $Objects) {
										$Objects = ereg_replace('_S-([0-9])*', '',$Objects);
										$theObjects = (eregi(",", $Objects) ? explode(",", $Objects) : Array($Objects));

										foreach ($theObjects AS $sortorder => $Object) {
											if ($Object != "") { 
												$newData = Array();
												$newData['PageDataID'] = $Data['PageDataID'];
												$newData['TemplateVariable'] = $TemplateVariable;
												$newData['SortOrder'] = $sortorder;
												$newData['ObjectID'] = $Object;
												$newData['Settings'] =  urldecode($Data['RunTimeSettings'][$TemplateVariable][$PageDataID][$sortorder]); 
											  $newData['CreateSections'] = 0;

												$Transaction2->addData('PageObjects', $newData);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		$Transaction2->Commit();
		
		$RV = ($Commit !== FALSE ? TRUE : FALSE);
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SavePageInfo function (private)
	**/
	function _SavePageInfo()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		if (!$this->GetSetting('IDString') || ($this->GetSetting('IDString') && $this->GetSetting('IDString') == "")) {
			$IDString = NitroCreateRandomString(20);
			while ($this->DB->getOne("SELECT PageID FROM Page WHERE IDString LIKE " . NitroPrepareDB($IDString))) $IDString = NitroCreateRandomString(20);
		} else {
			$IDString = $this->GetSetting('IDString');
		}
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$ID = $Transaction->addData('Page', Array('Name' 					 => $this->GetSetting('Name'), 
																							'IDString' 			 => $IDString, 
																							'SystemIDString' => ($this->GetSetting('SystemIDString') && $this->GetSetting('SystemIDString') != "" ? $this->GetSetting('SystemIDString') : NULL), 
																							'PageID' 				 => (int)$this->GetSetting('PageID')));
		
		if (!$this->GetSetting('AddPage')) {
			$Transaction->addData('Page_SecurityGroup', 
														Array('PageID' => (int)$this->GetSetting('PageID')), 
														Array('PageID' => (int)$this->GetSetting('PageID')), 
														'DELETE');
		}
		
		$Commit = $Transaction->Commit();
		
		if ($this->GetSetting('AddPage')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 PageID 
																						 FROM 
																							 Page 
																						 ORDER 
																						 BY 
																							 PageID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('PageID');
		}
		
		$ResultID = $newObjectID;
		
		$Transaction2 = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		
		foreach ($this->GetSetting('SecurityGroups') AS $SecurityGroupID) {
				$Transaction2->addData('Page_SecurityGroup', 
															 Array('PageID' => $ResultID, 
																		 'SecurityGroupID' => $SecurityGroupID), 
															 FALSE,  
															 'INSERT');
		}
		
		$Transaction2->Commit();
		
		// Save the VersionSettings
		$this->SaveVersions($ResultID, $this->GetSetting('PageData'), ($this->GetSetting('AddPage') ? TRUE : FALSE));
		
		if ($Commit !== FALSE) {
			$RV = Array("NoError", Language('Page Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('Page could not be Saved'));
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddPage')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditPage', '&PageID=" . $ResultID . "'); ") . "DeletePageTab('0');", "UserField" => $this->ShowPagesList());
		} else {
			return Array( "UserField" => $this->ShowPagesList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "');" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('Name') . "');"));
		}
	}
	
	/**
	 * ShowPagesList function
	 */
	function ShowPagesList($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("", "Page_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "Pages_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditPage', '&AddPage=1'); return false;", "", Language('Add Page'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddColumn(Language('IDString'), "IDString");
		$List->AddColumn(Language('System ID String'), "SystemIDString");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		

		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								PageID, 
								Name, 
								IDString, 
								SystemIDString 
							FROM 
								`Page` 
								" . ($List->Filter['Name'] || $List->Filter['IDString'] || $List->Filter['SystemIDString'] ? "WHERE " : "") . " 
								" . ($List->Filter['Name'] ? "Name LIKE " . NitroPrepareDB("%" . $List->Filter['Name'] . "%") . " " : "") . "
								" . ($List->Filter['Name'] && $List->Filter['IDString'] ? "AND " : "") . ($List->Filter['IDString'] ? "IDString LIKE " . NitroPrepareDB("%" . $List->Filter['IDString'] . "%") . " " : "") . "
								" . (($List->Filter['Name'] || $List->Filter['IDString']) && $List->Filter['SystemIDString'] ? "AND " : "") . ($List->Filter['SystemIDString'] ? "SystemIDString LIKE " . NitroPrepareDB( "%" . $List->Filter['SystemIDString'] . "%") . " " : "") . "
							ORDER BY 
								Name 
							" . ($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 = "PageList";

		if ($Result->numRows()) {
			while ($Data = $Result->fetchArray()) {
				$ListRow = new ListingRow2();
				$ListRow->AddData("Name", $Data["Name"]);
				$ListRow->AddData("IDString", $Data["IDString"]);
				$ListRow->AddData("SystemIDString", $Data["SystemIDString"]);
				$ListRow->SetAction("Edit", $editimg, "#", "ModuleXMLRequest('EditPage', '&PageID=" . $Data["PageID"] . "');", FALSE, TRUE, Language('Edit') . $Data["Name"]);
				$ListRow->SetAction("Delete", $deleteimg, "#", "if(confirm('Item is going to be removed, are you sure?')) ModuleXMLRequest('DeletePage', '&PageID=" . $Data["PageID"] . "'); return false;", FALSE, TRUE, Language('Delete') . space() . $Data["Name"]);
				
				$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