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

/**
 * LoginStack
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_LoginStack extends NitroBOSubModule {
	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_LoginStack";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_LoginStack() { }
	
	/**
	 * 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), 
				'LoginStackID' 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'LoginStackID',		'Default' => NULL), 
				'AddLoginStack' 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddLoginStack',		'Default' => NULL), 
				'Name' 							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL), 
				'Sortorder' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Sortorder',				'Default' => NULL), 
				'Description' 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Description',			'Default' => NULL), 
				'ForceLastStack' 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ForceLastStack',	'Default' => NULL), 
				'Pages' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Pages',						'Default' => NULL), 
				'PagesSelect'				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'PagesSelect',			'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(
				'DeleteLoginStack' 	=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> 'Delete LoginStack', 
																		 'DivID' 				=> "UserField", 
																		 'FunctionName' => "DeleteLoginStack"), 
				'EditLoginStack' 	 	=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> 'Edit LoginStack', 
																		 'DivID' 				=> "EditField", 
																		 'FunctionName' => "EditLoginStack"), 
				'SaveLoginStack' 		=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> 'Save LoginStack', 
																		 'DivID' 				=> "ErrorDiv", 
																		 'FunctionName' => "SaveLoginStack"), 
				'FilterMod' 				=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> 'Filter LoginStack List', 
																		 'DivID' 				=> "UserField", 
																		 'FunctionName' => "ShowLoginStackList"), 
				'LoginStackList' 		=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> 'LoginStack List', 
																		 'DivID' 				=> "UserField", 
																		 'FunctionName' => "ShowLoginStackList")
			);
		}
			
		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('Login Stack Management'), $this->ShowLoginStackList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;	
	}
	
	/**
	 * DeleteLoginStack function
	 */
	function DeleteLoginStack()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LoginStackID = (int)$this->GetSetting('LoginStackID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('LoginStack', 
													Array('LoginStackID' => $LoginStackID), 
													Array('LoginStackID' => $LoginStackID), 
													'DELETE');
		$Transaction->addData('LoginStackPage', 
													Array('LoginStackID' => $LoginStackID), 
													Array('LoginStackID' => $LoginStackID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowLoginStackList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditLoginStack function
	 */
	function EditLoginStack()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LoginStackID = (int)($this->GetSetting('LoginStackID') ? $this->GetSetting('LoginStackID') : 0);
		
		if ($this->GetSetting('AddLoginStack')) {
			$data = Array("Name" => "", "Sortorder" => "", "Description" => "", "ForceLastStack" => "");
			$pages = Array();
			$conditions = Array();
			$formadd = TRUE;
			$Name = "Add LoginStack";
		} else {
			$Query = "SELECT 
									Name, 
									Sortorder, 
									Description, 
									ForceLastStack 
								FROM 
									`LoginStack` 
								WHERE 
									LoginStackID = " . $LoginStackID;
			$data = $this->DB->getRow($Query);
			$PageQuery = "SELECT 
											LSP.PageID, 
											IF(P.Name IS NULL, '-', P.Name) AS Name, 
											LSP.Condition 
										FROM 
											`LoginStackPage` AS LSP 
										LEFT JOIN 
											`Page` AS P 
										ON 
											LSP.PageID = P.PageID 
										WHERE 
											LoginStackID = " . (int)$LoginStackID . " 
										ORDER BY 
											P.Name";
			$Result = $this->DB->query($PageQuery);
			$pages = Array();
			$conditions = Array();
			
			if ($Result->numRows()) {
				while ($tmp = $Result->fetchArray()) {
					$pages[] = $tmp["PageID"] . ":" . $tmp["Name"];
					$conditions[$tmp["PageID"]] = $tmp["Condition"];
				}
			}
				
			$formadd = FALSE;
			$Name = $data["Name"];
		}
		
		$Form = new Form("EditLoginStackForm_" . $this->GetSetting('LoginStackID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("LoginStackID", "HIDDEN/VALUE=" . $this->GetSetting('LoginStackID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddLoginStack", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $LoginStackID, "DIV/LABLE= ");
		$Form->AddOptionString("Name", "TEXT/VALUE=" . $data["Name"] . "/LABLE=" . Language('Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Sortorder", "TEXT/VALUE=" . $data["Sortorder"] . "/LABLE=" . Language('Sort order') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Description", "TEXTAREA/ROWS=4/VALUE=" . $data["Description"] . "/LABLE=" . Language('Description') . "/STYLE=width: 250px;");
		$Form->AddOptionString("ForceLastStack", "SELECT/VALUES=0:" . Language('No') . ",1:" . Language('Yes') . "/SELECTED=" . $data["ForceLastStack"] . "/LABLE=" . Language('Force Last Stack') . "");
		$Form->AddOptionString("SelectPage[" . $LoginStackID . "]", "SELECT/VALUES=doNotAdd: -- Select a Page -- /DB=NitroBO:Page:PageID:Name/LABLE=" . Language('Select a Page') . "/STYLE=width: 250px;");
		
		$Custom = urlencode("<input type='button' onclick=\"AddPage('SelectPage[" . $LoginStackID . "]', 'PagesSelect[" . $LoginStackID . "]', '" . $LoginStackID . "'); return false;\" value='" . Language('Add Selected') . "' />");
		$Form->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . $Custom);
		$Form->AddOptionString("PagesSelect[" . $LoginStackID . "]", "SELECTMULTIPLE" . (count($pages) > 0 ? "/VALUES=" . implode(",", $pages) : "") . "/LABLE=" . Language('Added Pages') . "/STYLE=width: 250px;");
		
		$Custom = urlencode("<input type='button' onclick=\"MoveSelected('PagesSelect[" . $LoginStackID . "]', 'UP');\" value='" . Language( "Move Up" ) . "' />" );
		$Custom.= urlencode("<input type='button' onclick=\"MoveSelected('PagesSelect[" . $LoginStackID . "]', 'DOWN');\" value='" . Language( "Move Down" ) . "' />" );
		$Custom.= urlencode("<input type='button' onclick=\"DeletePage('PagesSelect[" . $LoginStackID . "]', '" . $LoginStackID . "'); return false;\" value='" . Language('Delete') . "' />");
		
		$Form->AddOptionString("Temp2", "HTML/LABLE= /VALUE=" . $Custom);
		$Form->AddOptionString("HiddenDiv_" . $LoginStackID, "DIV/LABLE= ");
		
		$Custom = urlencode("<input type='button' onclick=\"PrepareForSave('" . $LoginStackID . "'); ModuleXMLRequest('SaveLoginStack', GetXMLURL('EditLoginStackForm_" . $this->GetSetting('LoginStackID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"PrepareForSave('" . $LoginStackID . "'); ModuleXMLRequest('SaveLoginStack', GetXMLURL('EditLoginStackForm_" . $this->GetSetting('LoginStackID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $LoginStackID . "'); return false;\" value='" . Language('Cancel') . "' />");

		$Form->AddOptionString("Temp3", "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();
		
		$tmp = "";
		
		if (is_array($conditions) && count($conditions)) {
			foreach ($conditions AS $PageID => $Condition) {
				$tmp.= "Conditions[" . $LoginStackID . "][" . $PageID . "] = '" . rawurlencode($Condition) . "'; ";
			}
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("JSCRIPT:()" => "CallOnLoadString += \"Conditions[" . $LoginStackID . "] = new Array(); " . $tmp . "\"; AddPageTab('" . $LoginStackID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveLoginStack function
	 */
	function SaveLoginStack()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LoginStackID = (int)$this->GetSetting('LoginStackID');
		
		if (!strlen($this->GetSetting('Name'))) {
			$RV = Array("ErrorDiv_" . $LoginStackID => $this->PrePareMSG(Array("Error", "Please fill in a Name.", TRUE), $LoginStackID));
		} else {
			$RV = $this->_SaveLoginStackInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveLoginStackInfo function (private)
	**/
	function _SaveLoginStackInfo()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LoginStackID = (int)$this->GetSetting('LoginStackID');
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$ID = $Transaction->addData('LoginStack', 
																Array('Name' => $this->GetSetting('Name'), 
																		  'Sortorder' => $this->GetSetting('Sortorder'), 
																		  'Description' => $this->GetSetting('Description'), 
																		  'ForceLastStack' => $this->GetSetting('ForceLastStack'), 
																		  'LoginStackID' => $LoginStackID));
																		  
		if (!$this->GetSetting('AddLoginStack')) {
			$Transaction->addData('LoginStackPage', 
														Array('LoginStackID' => $LoginStackID), 
														Array('LoginStackID' => $LoginStackID), 
														'DELETE');
		}
		
		$Commit = $Transaction->Commit();
		
		if ($this->GetSetting('AddLoginStack')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 LoginStackID 
																						 FROM 
																							 LoginStack 
																						 ORDER 
																						 BY 
																							 LoginStackID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('LoginStackID');
		}
		
		$ResultID = $newObjectID;
		
		$Pages = $this->GetSetting('Pages');
		
		if ($Pages && $Pages[$LoginStackID]) {
			$Transaction2 = new Transaction($this->DB, $this->Sess->UserID, TRUE);
			
			foreach ($Pages[$LoginStackID] AS $PageID => $Page) {
					$Transaction2->addData('LoginStackPage', 
																 Array('LoginStackID' => $ResultID, 
																 			 'Sortorder' => '', 
																 			 'Condition' => implode("", $Page), 
																 			 'PageID' => $PageID), 
																 FALSE, 
																 'INSERT');	
			}
			
			$Transaction2->Commit();
		}
		
		if ($Commit !== FALSE) {
			$RV = Array("NoError", Language('LoginStack Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('LoginStack could not be Saved'));
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddLoginStack')) {
			return Array("JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditLoginStack', '&LoginStackID=" . $ResultID . "'); " ) . "DeletePageTab('0', true);", "UserField" => $this->ShowLoginStackList());
		} else {
			return Array("UserField" => $this->ShowLoginStackList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('Name') . "');"));
		}
	}
	
	/**
	 * ShowLoginStackList function
	 */	
	function ShowLoginStackList($inDiv = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'ShowLoginStackList function', __FILE__, __LINE__, DEBUG_MOD_OK);
		
		global $DB; // TODO: Remove Globals
		
		$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(FALSE, (strlen($this->GetSetting('ModFilter')) && $this->GetSetting('ModFilter') !== 0 ? $this->GetSetting('ModFilter') : FALSE));

		$List = new Listing2("", "LoginStack_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "LoginStacks_");
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditLoginStack', '&AddLoginStack=1&UserID=AddLoginStack'); return false;", "", Language('Add Login Stack'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddColumn(Language('Sort order'), "Sortorder");
		$List->AddColumn(Language('Description'), "Description");
		$List->AddColumn(Language('Force Last Stack'), "ForceLastStack");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								LoginStackID, 
								Name, 
								Sortorder, 
								Description, 
								IF(ForceLastStack = 1, '" . Language('Yes') . "', '" . Language('No') . "') AS ForceLastStack 
							FROM 
								`LoginStack` 
							ORDER BY 
								Sortorder 
							" . ($List->usePages !== FALSE ? "LIMIT " . (int)$List->Start . ", " . (int)$List->maxPerPage : "");
							
		$Result = $this->DB->query($Query);
		
		// Set navigation options
		$List->allPages = $DB['Nitro']->getOne("SELECT FOUND_ROWS()");
		$List->onChange = "LoginStackList";

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