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

/**
 * Templates
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_Templates extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_Templates";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_Templates() { }
	
	/**
	 * 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),
				'TemplateID' 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TemplateID',			'Default' => NULL),
				'ObjectID'	 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ObjectID',				'Default' => NULL),
				'AddTemplate' 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddTemplate',			'Default' => NULL),
				'Name' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL),
				'IDString' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'IDString',				'Default' => NULL),
				'TemplateData' 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TemplateData',		'Default' => NULL),
				'TemplateVariable'=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TemplateVariable/','Default' => NULL),
				'TemplateSettings'=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TemplateSettings','Default' => NULL),
				'TplVariable'			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'TplVariable',			'Default' => NULL), // This on is used when dblClicking objects in template variable selection
				'AddVar'					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddVar',					'Default' => NULL),
				'VariableName'		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'VariableName',		'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),
				'RemoveVariable'	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'RemoveVariable',	'Default' => NULL),
				'DeleteVariables'	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'DeleteVariables/',	'Default' => NULL),
				'RemoveVersion'		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'RemoveVersion',		'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(
				'DeleteTemplate' 		=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> "Delete Template", 
																		 'DivID' 				=> "UserField", 
																		 'FunctionName' => "DeleteTemplate"), 
				'EditTemplate' 	 		=> Array('Type' 				=> "HTML", 
																 		 'Name' 				=> "Edit Template", 
																 		 'DivID' 				=> "EditField", 
																 		 'FunctionName' => "EditTemplate"), 
				'SaveTemplate' 			=> Array('Type' 				=> "HTML", 
																 		 'Name' 				=> "Save Template", 
																 		 'DivID'			 	=> "ErrorDiv", 
																 		 'FunctionName' => "SaveTemplate"), 
				'Version' 			=> Array('Type' => "HTML", 
																 'Name' => "Add Template Version", 
																 'DivID' => "UserField", 
																 'FunctionName' => "addVersion"), 
				'Variable' 			=> Array('Type' => "HTML", 
																 'Name' => "Add Template Variable", 
																 'DivID' => "UserField", 
																 'FunctionName' => "addVariable"), 
				'FilterMod' 		=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Filter Templates List", 
														 		 'DivID' 				=> "UserField", 
														 		 'FunctionName' => "ShowTemplatesList"), 
				'TemplateList' 	=> Array('Type' 				=> "HTML", 
														 		 'Name' 				=> "Templates List", 
														 		 'DivID' 				=> "UserField", 
														 		 'FunctionName' => "ShowTemplatesList"), 
				'GetObjectOptions' => Array('Type' 				=> "HTML", 
															 		  'Name' 				=> "Get the RunTimeSettings for an Object.", 
															 		  'DivID' 				=> "UserField", 
															 		  'FunctionName' => "GetObjectSettings"),
														 		 
			);
		}
		
		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('Template Management'), $this->ShowTemplatesList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}

	/**
	 * DeleteTemplate function
	 */
	function DeleteTemplate()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$TemplateID = (int)$this->GetSetting('TemplateID');
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('Template', 
													Array('TemplateID' => $TemplateID), 
													Array('TemplateID' => $TemplateID), 
													'DELETE');
		$Transaction->addData('TemplateData', 
													Array('TemplateID' => $TemplateID), 
													Array('TemplateID' => $TemplateID), 
													'DELETE');
		$Transaction->addData('TemplateObjects', 
													Array('TemplateID' => $TemplateID), 
													Array('TemplateID' => $TemplateID), 
													'DELETE');
		$Transaction->addData('TemplateVariable', 
													Array('TemplateID' => $TemplateID), 
													Array('TemplateID' => $TemplateID), 
													'DELETE');

		$RV = Array("UserField" => ($Transaction->Commit() !== FALSE) ? $this->ShowTemplatesList() : "");
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * EditTemplate function
	 */
	function EditTemplate()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$TemplateID = (int)($this->GetSetting('TemplateID') ? $this->GetSetting('TemplateID') : 0);
		
		if ($this->GetSetting('AddTemplate')) {
			$data = Array("Name" => "", "IDString" => "");
			$formadd = TRUE;
			$Name = "Add New Template";
			$TemplateData = Array();
			$TemplateVariable = Array("Body" => Array("TemplateID" => 0, 
																								"Variable" => "Body", 
																								"TemplateObjectOnly" => 0));
		} else {
			$Query = "SELECT * 
								FROM `Template` 
								WHERE TemplateID = " . $TemplateID;
								
			$data = $this->DB->getRow($Query);
			$formadd = FALSE;
			$Name = $data["Name"];
			$Query = "SELECT *
								FROM `TemplateData`
								WHERE TemplateID = " . (int)$TemplateID . "
								ORDER BY Language";
			$Result = $this->DB->query($Query);
			
			while ($Row = $Result->fetchArray()) {
				if ($Row["Language"] == "") $Row["Language"] = "Default";
				$TemplateData[$Row["Language"]][$Row["Version"]] = $Row;
			}
			
			$Query = "SELECT 
									TV.Variable, 
									TV.TemplateObjectOnly, 
									TmO.ObjectID,
									TmO.TemplateVariable, 
									TmO.Settings,  
									TmO.SortOrder
								FROM `TemplateVariable` AS TV 
								LEFT JOIN 
									`TemplateObjects` AS TmO 
								ON TmO.TemplateVariable = TV.Variable
								AND TmO.TemplateID = TV.TemplateID 
								WHERE TV.TemplateID = " . (int)$TemplateID . "
								ORDER BY 
									TV.Variable, 
									TmO.SortOrder";
			$Result = $this->DB->query($Query);
			
			$TemplateVariable = Array();
			while ($Row = $Result->fetchArray()) {  
				if (!$TemplateVariable[$Row["Variable"]]) $TemplateVariable[$Row["Variable"]] = Array();
				if (!$TemplateVariable[$Row["Variable"]]["Variable"]) $TemplateVariable[$Row["Variable"]]["Variable"] = $Row["Variable"];
				if (!$TemplateVariable[$Row["Variable"]]["TemplateObjectOnly"]) $TemplateVariable[$Row["Variable"]]["TemplateObjectOnly"] = $Row["TemplateObjectOnly"];
				if (!$TemplateVariable[$Row["Variable"]]["TemplateObjects"]) $TemplateVariable[$Row["Variable"]]["TemplateObjects"] = Array();
				if (is_numeric($Row["ObjectID"]) || $Row["ObjectID"] > 0) {
					$ObID = $Row["ObjectID"] . ":" . urlencode($this->DB->GetOne("SELECT Name FROM Object WHERE ObjectID = " . $Row["ObjectID"]).($Row['Settings'] && $Row['Settings'] !== 'NULL' ? ' **' : '') );
					$TemplateVariable[$Row["Variable"]]["TemplateObjects"][$ObID]= $Row["Settings"];
				}
				if ($Row['TemplateVariable']) {
					$this->HiddenSettings[$Row["TemplateVariable"]][$Row["ObjectID"]] .= "<div style=\"border:1px solid black\" id=\"TemplateVariable/[" . $Row["TemplateVariable"] . "][RunTimeSettings][" . $Row["ObjectID"] . "]\"> <input type=\"hidden\" id=\"HiddenInput_TemplateVariable/[" . $Row["TemplateVariable"] . "][RunTimeSettings][" . $Row["ObjectID"] . "]\" name=\"TemplateVariable/[" . $Row["TemplateVariable"] . "][RunTimeSettings][" . $Row["ObjectID"] . "]\" value=\"". urlencode($Row["Settings"]) ."\" />". urlencode($Row["Settings"]) ." RunTimeSettings][" . $Row["TemplateVariable"] . "][" . $Row["ObjectID"] . "</div> \n";
				}
			}
		}	

		if (is_array($TemplateVariable) && !count(array_values($TemplateVariable))) {
			$TemplateVariable = Array(
				"Body" => Array(
					"TemplateID" => 0, 
					"Variable" => "Body", 
					"TemplateObjectOnly" => 0, 
					"TemplateObjects" => Array()
				)
			);
		}

		if ($this->GetSetting('AddVer')) {
			$Lang = $this->GetSetting('Lang');
			$Vers = $this->GetSetting('Ver');
			$TemplateData[$Lang][$Vers] = Array("Language" => $Lang, "Version" => $Vers, "Body" => "", "Published" => 0, "VisibleFrom" => "", "VisibleTill" => "", "TemplateID" => $TemplateID, "TemplateObjects" => Array());
		}
		
		if ($this->GetSetting('AddVar')) {
			$VarName = $this->GetSetting('VariableName');
			$TemplateVariable[$VarName] = Array("TemplateID" => 0, "Variable" => $VarName, "TemplateObjectOnly" => 0);
		}
		
		$FormName = "EditTemplateForm_" . ($this->GetSetting('TemplateID') ? $this->GetSetting('TemplateID') : '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("TemplateID", "HIDDEN/VALUE=" . $this->GetSetting('TemplateID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddTemplate", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $TemplateID, "DIV/LABLE= ");
		$Form->AddOptionString("Name", "TEXT/VALUE=" . urlencode($data["Name"]) . "/LABLE=" . Language('Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("IDString", "TEXT/VALUE=" . urlencode($data["IDString"]) . "/LABLE=" . Language('IDString') . "/STYLE=width: 250px;");

		$tabJscript = "";
		$VariableTab = new FormTab("Variable_" . $TemplateID);
		$tabJscript.= "theTabs['Variable_" . $TemplateID . "'] = new Array();";
		
		foreach ($TemplateVariable AS $Key => $Value) {
			$this->EditVariableTab($VariableTab, $tabJscript, $TemplateID, $Key, $Value);
		}

		$VariableTab->AddAction(Language('Add Variable'), "#", "", "addTemplateVariable('" . $TemplateID . "'); return false;");
		
		$Form->AddOption($VariableTab);
	
		$LanguageTab = new FormTab("Language_" . $TemplateID);
		
		if (is_array($this->AllowedLanguages) && count($this->AllowedLanguages)) {
			$tabJscript.= "if(!theTabs['Language_" . $TemplateID . "']) theTabs['Language_" . $TemplateID . "'] = new Array();";
			
			foreach ($this->AllowedLanguages AS $LangCode => $LangName) {
				if (!is_array($TemplateData[$LangCode])) $TemplateData[$LangCode] = Array();
				$this->EditLanguageTab($LanguageTab, $tabJscript, $TemplateID, $LangCode, $LangName, $TemplateData);
			}
		}
		
		$Form->AddOption($LanguageTab);

		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveTemplate', GetXMLURL('" . $FormName . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveTemplate', GetXMLURL('" . $FormName . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $TemplateID . "'); 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:()" => $tabJscript . ($this->GetSetting('AddVer') || $this->GetSetting('RemoveVersion') || $this->GetSetting('AddVar') || $this->GetSetting('RemoveVariable') ? "UpdatePageTab('" . $TemplateID . "', 'Content', '" . rawurlencode($RV) . "');" : "AddPageTab('" . $TemplateID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');"));
	}
	
	function EditVariableTab(&$VariableTab, &$tabJscript, $TemplateID, $Key, $Value)
	{
		$tabJscript.= "theTabs['Variable_" . $TemplateID . "']['" . 'TemplateVariable_'.$Key . "'] = '" . $Key . "';";
		$VariableTab->AddTab($Key, 'TemplateVariable_'.$Key);
		$VariableTab->AddOptionString("TemplateVariable/[" . $Key . "][Variable]", "HIDDEN/VALUE=" . $Key);
		$VariableTab->AddOptionString("TemplateVariable/[" . $Key . "][TemplateObjectOnly]", "CHECKBOX/LABEL=" . Language('Template Object Only') . "/VALUE=1:" . Language('Yes') . "/SELECTED=" . $Value["TemplateObjectOnly"]);
		
		// Custom query to check if object has runtime settings
		$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();
		$VariableTab->AddOptionString("TemplateVariable/[" . $Key . "][Objects]", "SELECT/LABEL=" . Language('Objects') . "/VALUE=0: -- " . Language('Choose an Object') . " -- /VALUES=".implode(',', $Objects)."/STYLE=width: 250px;");
		
		$Custom = urlencode("<input type='button' onclick=\"AddTemplateObject('TemplateVariable/[" . $Key . "][Objects]', 'TemplateVariable/[" . $Key . "][TemplateObjects]', '" . $Key . "'); return false;\" value='" . Language('Add Selected') . "' />");
	
		$VariableTab->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . $Custom);
		
		$TemplateObjects = (is_array($Value["TemplateObjects"])	? array_keys($Value["TemplateObjects"]) : "");
		$TemplateObjects = (is_array($TemplateObjects) ? implode(",", $TemplateObjects) : ""); 

		$tmp = 'GetTemplateObjectOptions(this.options[this.selectedIndex].value, this.selectedIndex, "' . $TemplateID . '", "' . $Key . '")';
		$tmp = rawurlencode($tmp);
		$VariableTab->AddOptionString("TemplateVariable/[" . $Key . "][TemplateObjects]", "SELECTMULTIPLE/LABEL=" . Language('Template Objects') . "/STYLE=width: 250px;/SIZE=5/SCRIPT=ondblclick:".$tmp."/VALUES=" . $TemplateObjects);
		$VariableTab->AddOptionString("temp", "HTML/LABLE= /VALUE=** = ".Language('Object has RunTime settings').' ('.Language('double-click to edit').')');
		$VariableTab->AddOptionString("TemplateVariable/[" . $Key . "][TemplateObjectOptionsBody]", "DIV/LABLE= /VALUE=");
		
		$tabJscript.= "SelectAllItemsObjects['TemplateVariable/[" . $Key . "][TemplateObjects]'] = 'TemplateVariable/[" . $Key . "][TemplateObjects]'; ";
		
		$Custom = urlencode("<input type='button' onclick=\"MoveSelected('TemplateVariable/[" . $Key . "][TemplateObjects]', 'UP');\" value='" . Language( "Move Up" ) . "' />" );
		$Custom.= urlencode("<input type='button' onclick=\"MoveSelected('TemplateVariable/[" . $Key . "][TemplateObjects]', 'DOWN');\" value='" . Language( "Move Down" ) . "' />" );
		$Custom.= urlencode("<input type='button' onclick=\"DeleteTemplateObject('TemplateVariable/[" . $Key . "][TemplateObjects]'); return false;\" value='" . Language('Delete') . "' />");
		$Custom.= urlencode("<div id='go'></div>");
		$VariableTab->AddOptionString("Temp2", "HTML/LABLE= /VALUE=" . $Custom);

		$divjes = '';
		if (is_array($this->HiddenSettings[$Key])) {
			foreach($this->HiddenSettings[$Key] AS $objectID => $hiddensettings) {
				$divjes.= rawurlencode("<div style=\"border:1px solid green\" id = \"TemplateObjectOptionsHidden[" .$Key . "][" . $objectID . "]\">TemplateObjectOptionsHidden[" .$Key . "][" . $objectID . "]" . $hiddensettings . "</div>\n");
			}
		}
		$hiddenDiv = rawurlencode("<div id =\"hiddenDivContainer[" . $Key . "]\" style=\"display: none\">") .$divjes. urlencode("</div>");
		$VariableTab->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . $hiddenDiv);
		
		$DeleteBTN = urlencode("<input type='button' onclick=\"ModuleXMLRequest('Variable', GetXMLURL('" . $FormName . "') + '&VariableName=" . $Key . "&TemplateID=" . $TemplateID . "&RemoveVariable=1'); return false;\" value='" . Language('Delete Variable') . "' />");
		$VariableTab->AddOptionString("DeleteBTN", "HTML/LABLE= /VALUE=" . $DeleteBTN);

		// Add Div to set template object options
		$VariableTab->AddOptionString("TemplateObjectOptionsBody", "DIV/LABLE= /VALUE=");
	}

	function EditLanguageTab(&$LanguageTab, &$tabJscript, $TemplateID, $LangCode, $LangName, $TemplateData)
	{
		$tabJscript.= "theTabs['Language_" . $TemplateID . "']['" . $LangCode . "'] = '" . $LangName . "';";
		$LanguageTab->AddTab($LangName, $LangCode);

		$LanguageVersionTab = new FormTab($LangCode . "Version_" . $TemplateID);
		$tabJscript.= "theTabs['" . $LangCode . "Version_" . $TemplateID . "'] = new Array();";
		
		foreach ($TemplateData[$LangCode] AS $Ver => $Content) {
			$this->EditLanguageVersionTab($LanguageVersionTab, $tabJscript, $TemplateID, $LangCode, $Ver, $Content);
			$LanguageVersionTab->SetSelectedTab($LangCode.$Ver); // Set last tab selected
		}
		
		$LanguageVersionTab->AddAction(Language('Add Version'), "#", "", "ModuleXMLRequest('Version', GetXMLURL('Language_" . $TemplateID . "_TAB_" . $LangCode . "') + '&AddVer=1&Ver=" . (count($TemplateData[$LangCode]) + 1) . "&Lang=" . $LangCode . "&TemplateID=" . $TemplateID . "'); return false;");
		$LanguageTab->AddOption($LanguageVersionTab);
	}
	
	function EditLanguageVersionTab(&$LanguageVersionTab, &$tabJscript, $TemplateID, $LangCode, $Ver, $Content)
	{
		$tabJscript.= "theTabs['" . $LangCode . "Version_" . $TemplateID . "']['" . $LangCode . $Ver . "'] = '" . $LangCode . $Ver . "';";
		$LanguageVersionTab->AddTab( "v" . $Ver, $LangCode . $Ver);
		
		$Widgets = Array("TemplateData[" . $LangCode . "][" . $Ver . "][Body]" => "TEXTAREA/LABEL=" . Language('Body') . "/VALUE=" . urlencode($Content['Body']) . "/STYLE=width: 500px;/ROWS=20",
										 "TemplateData[" . $LangCode . "][" . $Ver . "][Published]" => "CHECKBOX/LABEL=" . Language('Published') . "/VALUE=1:" . Language('Yes') . "/SELECTED=" . $Content['Published'],
										 "TemplateData[" . $LangCode . "][" . $Ver . "][VisibleFrom]" => "TEXT/LABEL=" . Language('Visible From') . "/VALUE=" . $Content['VisibleFrom'] . "/STYLE=width: 250px;",
										 "TemplateData[" . $LangCode . "][" . $Ver . "][VisibleTill]" => "TEXT/LABEL=" . Language('Visible Till') . "/VALUE=" . $Content['VisibleTill'] . "/STYLE=width: 250px;");
		$LanguageVersionTab->AddOptionsFromArray($Widgets);
		
		/* No removing of versions in this version of Nitro
		$DeleteBTN = urlencode("<input type='button' onclick=\"ModuleXMLRequest('Version', '" . $FormName . "') + '&Ver=" . $Ver . "&Lang=" . $LangCode . "&TemplateID=" . $TemplateID . "&RemoveVersion=1'); return false;\" value='" . Language('Delete Version') . "' />");
		if ($LangCode == "Default" && $Ver != 1 && $Ver == count($TemplateData[$LangCode])) {
			$LanguageVersionTab->AddOptionString("DeleteBTN", "HTML/LABLE= /VALUE=" . $DeleteBTN);
		} else if ($LangCode != "Default" && $Ver == count($TemplateData[$LangCode])) {
			$LanguageVersionTab->AddOptionString("DeleteBTN", "HTML/LABLE= /VALUE=" . $DeleteBTN);
		}
		*/
	}

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

		$TemplateID = (int)$this->GetSetting('TemplateID');
		
		if (!strlen($this->GetSetting('Name'))) {					
			$RV = Array("ErrorDiv_" . $TemplateID => $this->PrePareMSG(Array("Error", "Please fill in a Name.", TRUE), $TemplateID));
		} else {
			$RV = $this->_SaveTemplateInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * addVersion function
	 *
	 * @return mixed Output of the EditTemplate 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");
		$TemplateID		= $this->GetSetting('TemplateID');
		$LangCode	= $this->GetSetting('Lang');

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

		$this->EditLanguageTab($LanguageTab, $tabJscript, $TemplateID, $LangCode, $this->AllowedLanguages[$LangCode], $TemplateData);
		// 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_' . $TemplateID . '_TAB_' . $LangCode => $LanguageTabRV['Controls']);
	}
	
	/**
	 * RemoveVersion function
	 */
	function RemoveVersion($TemplateID, $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('TemplateData', 
							  Array("TemplateID" => (int)$TemplateID, 
							  		"Version" => (int)$Version, 
							  		"Language" => ($Lang == "Default" ? "" : $Lang)), 
							  Array("TemplateID" => (int)$TemplateID, 
							  		"Version" => (int)$Version, 
							  		"Language" => ($Lang == "Default" ? "" : $Lang)), 
							  "DELETE");
				  
		$RV = ($Transaction->Commit() !== FALSE) ? TRUE : FALSE;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * addVariable function
	 *
	 * @return mixed Output of the EditTemplate function.
	 */
	function addVariable()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$WidgetTemplate = new NitroTemplate('file:' . NITRO_PATH . "Defaults/Templates/BackOffice/widget.tpl");
		$TemplateID		= $this->GetSetting('TemplateID');
		$LangCode	= $this->GetSetting('Lang');

		$VariableTab = new FormTab("Variable_" . $TemplateID);
		$tabJscript = '';
		
		// Get TemplateData from POST and add new empty version
		$NewVariable = $this->GetSetting('VariableName');
		$TemplateVariable = $this->GetSetting('TemplateVariable');

		if ($this->GetSetting('RemoveVariable')) {
			unset($TemplateVariable[$NewVariable]);
		} else {
			$TemplateVariable[$NewVariable] = Array();
			$VariableTab->SetSelectedTab('TemplateVariable_'.$NewVariable);
		}

		foreach ($TemplateVariable AS $Key => $Value) {
			if (strlen($Value['TemplateObjects'])) {
				$tmp = explode(',', $Value['TemplateObjects']);
				$Value['TemplateObjects'] = Array();
				foreach($tmp AS $id) {
					$ObID = $id . ":" . urlencode($this->DB->GetOne("SELECT Name FROM Object WHERE ObjectID = " . $id).($Value['RunTimeSettings'][$id] && $Value['RunTimeSettings'][$id] !== 'NULL' ? ' **' : '') );
					$Value['TemplateObjects'][$ObID] = $Value['RunTimeSettings'][$id];
					$this->HiddenSettings[$Key][$id].= "<div style=\"border:1px solid black\" id=\"TemplateVariable/[" . $Key . "][RunTimeSettings][" . $id . "]\"> <input type=\"hidden\" id=\"HiddenInput_TemplateVariable/[" . $Key . "][RunTimeSettings][" . $id . "]\" name=\"TemplateVariable/[" . $Key . "][RunTimeSettings][" . $id . "]\" value=\"". $Value['RunTimeSettings'][$id] ."\" />". $Value['RunTimeSettings'][$id] ." RunTimeSettings][" . $Key . "][" . $id . "</div> \n";
				}
			}
			$this->EditVariableTab($VariableTab, $tabJscript, $TemplateID, $Key, $Value);
		}
		$VariableTab->AddAction(Language('Add Variable'), "#", "", "addTemplateVariable('" . $TemplateID . "'); return false;");

		// There is only 1 widget on this tab, the version tab widget !
		$VariableTabRV = $VariableTab->Draw($WidgetTemplate);
		
		$RV = '';
		if (is_array($deleteVariables = $this->GetSetting('DeleteVariables'))) {
			foreach($deleteVariables AS $var) {
				$RV.= '<input type="hidden" name="DeleteVariables/['.$var.']" value="'.$var.'">';
			}
		}
		if ($this->GetSetting('RemoveVariable')) $RV.= '<input type="hidden" name="DeleteVariables/['.$NewVariable.']" value="'.$NewVariable.'">';
		$RV.= $VariableTabRV['Controls'];

		DebugCloseGroup(DEBUG_MOD_OK);
		return array('JSCRIPT:()' => $tabJscript, 'Variable_' . $TemplateID . '_TAB' => $RV);
	}
	
	/**
	 * RemoveVariable function
	 */
	function RemoveVariable($TemplateID, $VariableName)
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('TemplateVariable', 
							  Array("TemplateID" => (int)$TemplateID, 
							  			"Variable" => $VariableName), 
							  Array("TemplateID" => (int)$TemplateID, 
							  			"Variable" => $VariableName), 
							  "DELETE");
				  
		$RV = ($Transaction->Commit() !== FALSE) ? TRUE : FALSE;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * GetObjectOptions function
	 */
	function GetObjectSettings()
	{
		$Settings = ($this->GetSetting('TemplateSettings') ? unserialize($this->GetSetting('TemplateSettings')) : "");
		$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('TemplateID') && $this->GetSetting('TemplateID') > 0) {
			// get the settings from db 	
			
			if (is_array($Settings)) {
				// settings where posted
				$objectRuntimeSettings = $Settings;
			} else {
				$objectRuntimeSettings = $this->DB->GetOne("
					SELECT Settings 
					FROM `TemplateObjects` 
					WHERE TemplateID = ". $this->GetSetting('TemplateID') ."
						AND TemplateVariable = '" . $this->GetSetting('TplVariable') ."'
						AND ObjectID = " . $this->GetSetting('ObjectID')."
					");
				$objectRuntimeSettings = unserialize($objectRuntimeSettings);
			}
		}
		
		$FormGroup = new FormGroup("TemplateObjectSettings_" . $this->GetSetting('TplVariable') . "_" . $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=\"SaveTemplateObjectSettings('TemplateVariable/[" . $this->GetSetting('TplVariable') . "][TemplateObjectOptionsBody]', 'TemplateVariable/[" . $this->GetSetting('TplVariable') . "][RunTimeSettings][" . $this->GetSetting('ObjectID') . "]', '" . $this->GetSetting('TplVariable') . "', '" . $this->GetSetting('TemplateID') . "', '" . $this->GetSetting('ObjectID') . "');\" /> <input type=\"button\" value=\"" . Language('Cancel') . "\" onclick=\"SaveTemplateObjectSettings(false, false, '" . $this->GetSetting('TplVariable') . "', '" . $this->GetSetting('TemplateID') . "', '" . $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=\"SaveTemplateObjectSettings(false, false, '" . $this->GetSetting('TplVariable') . "', '" . $this->GetSetting('TemplateID') . "', '" . $this->GetSetting('ObjectID') . "');\" />"));
				}	
			}
		}
		$DrawResult = $FormGroup->Draw($Widget);

		$RV = $DrawResult['Controls'];

		$RV = Array(
			"TemplateVariable/[" . $this->GetSetting("TplVariable") . "][TemplateObjectOptionsBody]" => $RV
		);

		return $RV;
	}
	
	/**
	 * SaveVersions function
	 *
	 * @param int $TemplateID The current TemplateID
	 * @param array $Langs Array with the data to save.
	 * @return boolean (TRUE/FALSE)
	 */
	function SaveVersions($TemplateID, $Langs)
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Ret = FALSE;
		
		if (is_array($Langs)) {
			foreach ($Langs AS $Lang => $Versions) {
				if (is_array($Versions)) {
					foreach ($Versions AS $Version => $Data) {
						if (is_array($Data)) {							
							$Data['TemplateID'] = (int)$TemplateID;
							$Data['Language'] = ($Lang == "Default" ? "" : $Lang);
							$Data['Version'] = (int)$Version;
							if ($Data['VisibleFrom'] == "") $Data['VisibleFrom'] = NULL;
							if ($Data['VisibleTill'] == "") $Data['VisibleTill'] = NULL;
							
							if ($Data['Body'] != "") {
								$Ret = TRUE;
								$Transaction->addData('TemplateData', $Data);
							} else {
								$Ret = FALSE;
							}
						}
					}
				}
			}
		}
		
		$Commit = $Transaction->Commit();
		
		$RV = ($Commit !== FALSE && $Ret !== FALSE ? TRUE : FALSE);
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * SaveVariable function
	 *
	 * @param int $TemplateID The current TemplateID
	 * @param array $Langs Array with the data to save.
	 * @return boolean (TRUE/FALSE)
	 */
	function SaveVariable($TemplateID, $Variables, $DeleteVariables = Array())
	{
		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($Variables)) {
			$S = 1;
			foreach ($Variables AS $Key => $Value) {				
				$Objects = (eregi(",", $Value["TemplateObjects"]) ? explode(",", $Value["TemplateObjects"]) : (strlen($Value["TemplateObjects"]) ? Array($Value["TemplateObjects"]) : FALSE) );
				
				if (is_array($Objects) && count($Objects)) {
					foreach ($Objects AS $ObjectID) {
						if ($ObjectID > 0) {
							$settings = urldecode($Value['RunTimeSettings'][$ObjectID]);
							$Transaction->addData('TemplateObjects', 
																		Array('TemplateID' => $TemplateID, 
																					'SortOrder' => $S, 
																					'TemplateVariable' => $Value["Variable"], 
																					'ObjectID' => $ObjectID,
																					'Settings' => ($settings ? $settings : "NULL")
																				)
																			);
							$S++;
						}
					}
					// delete all old template objects in db that are not in Object array
					$Query = "
						DELETE FROM TemplateObjects
						WHERE TemplateID = ".(int)$TemplateID."
							AND TemplateVariable = ".NitroPrepareDB($Value["Variable"])."
							AND ObjectID NOT IN (".implode(', ', $Objects).")
						";
					$this->DB->query($Query);
					// TODO: use Transaction for delete
					/*
					$Transaction->addData('TemplateObjects', 
																Array('TemplateID' => $TemplateID, 'TemplateVariable' => $Value["Variable"], 'ObjectID' => 'NOT IN ...'), 
																Array('TemplateID' => $TemplateID, 'TemplateVariable' => $Value["Variable"], 'ObjectID' => 'NOT IN ...'), 
																'DELETE');
					*/
				} else {
					// delete all old template objects in db
					$Transaction->addData('TemplateObjects', 
																Array('TemplateID' => $TemplateID, 'TemplateVariable' => $Value["Variable"]),
																Array('TemplateID' => $TemplateID, 'TemplateVariable' => $Value["Variable"]),
																'DELETE');
				}
				
				$Transaction->addData('TemplateVariable', 
															Array('TemplateID'	=> $TemplateID, 
																		'Variable'		=> $Key, 
																		'TemplateObjectOnly' => $Value["TemplateObjectOnly"]));
			}
		}
		
		if (is_array($DeleteVariables)) {
			foreach($DeleteVariables AS $deleteVar) {
				$Transaction->addData('TemplateObjects', 
															Array('TemplateID' => $TemplateID, 'TemplateVariable' => $deleteVar),
															Array('TemplateID' => $TemplateID, 'TemplateVariable' => $deleteVar),
															'DELETE');
			}
		}
		
		$Commit = $Transaction->Commit();
		
		$RV = ($Commit !== FALSE ? TRUE : FALSE);
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveTemplateInfo function (private)
	**/
	function _SaveTemplateInfo()
	{
		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(" TemplateID FROM Template 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('Template', Array('Name' 					 => $this->GetSetting('Name'), 
																									'IDString' 			 => $IDString, 
																									'TemplateID' 		 => (int)$this->GetSetting('TemplateID')));

		$Commit = $Transaction->Commit();
		
		if ($this->GetSetting('AddTemplate')) {
			// TODO: use InsertID from transaction class!!
			$ResultID = (int)$this->DB->getOne("
				SELECT 
					 TemplateID 
				 FROM 
					 Template 
				 ORDER 
				 BY 
					 TemplateID 
				 DESC 
				 LIMIT 1
			");
		} else {
			$ResultID = (int)$this->GetSetting('TemplateID');
		}
		
		// Save the VersionSettings
		$this->SaveVersions($ResultID, $this->GetSetting('TemplateData'));
		
		// Save the VariableSettings
		$this->SaveVariable($ResultID, $this->GetSetting('TemplateVariable'), $this->GetSetting('DeleteVariables'));
		
		if ($Commit !== FALSE) {
			$RV = Array("NoError", Language('Template Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('Template could not be Saved'));
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddTemplate')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditTemplate', '&TemplateID=" . $ResultID . "'); ") . "DeletePageTab('0');", "UserField" => $this->ShowTemplatesList());
		} else {
			return Array( "UserField" => $this->ShowTemplatesList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "');" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('Name') . "');"));
		}
	}
	
	/**
	 * ShowTemplatesList function
	 */
	function ShowTemplatesList($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("", "Template_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "Templates_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditTemplate', '&AddTemplate=1'); return false;", "", Language('Add Template'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddColumn(Language('IDString'), "IDString");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								TemplateID, 
								Name, 
								IDString 
							FROM 
								`Template` 
								" . ($List->Filter['Name'] || $List->Filter['IDString'] ? "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'] . "%") . " " : "") . "
							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->SetAction("Edit", $editimg, "#", "ModuleXMLRequest('EditTemplate', '&TemplateID=" . $Data["TemplateID"] . "');", FALSE, TRUE, Language('Edit') . space() . $Data["Name"]);
				$ListRow->SetAction("Delete", $deleteimg, "#", "if(confirm('Item is going to be removed, are you sure?')) ModuleXMLRequest('DeleteTemplate', '&TemplateID=" . $Data["TemplateID"] . "'); 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