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

/**
 * SecurityGroups
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_SecurityGroups extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_SecurityGroups";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_SecurityGroups() { }
	
	/**
	 * 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), 
				'SecurityGroupID' 	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'SecurityGroupID',	'Default' => NULL), 
				'AddSecurityGroup' 	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddSecurityGroup','Default' => NULL), 
				'Name' 							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL), 
				'Description'				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Description',			'Default' => NULL), 
				'LoginStackID'	 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'LoginStackID',		'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(
				'DeleteSecurityGroup' 	=> Array('Type' 				=> "HTML", 
																				 'Name' 				=> "Delete SecurityGroup", 
																				 'DivID' 				=> "UserField", 
																				 'FunctionName' => "DeleteSecurityGroup"), 
				'EditSecurityGroup' 	 	=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "Edit SecurityGroup", 
																		 		 'DivID' 				=> "EditField", 
																		 		 'FunctionName' => "EditSecurityGroup"), 
				'SaveSecurityGroup' 		=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "Save SecurityGroup", 
																		 		 'DivID'			 	=> "ErrorDiv", 
																		 		 'FunctionName' => "SaveSecurityGroup"), 
				'FilterMod' 						=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "Filter SecurityGroups List", 
																		 		 'DivID' 				=> "UserField", 
																		 		 'FunctionName' => "ShowSecurityGroupsList"), 
				'SecurityGroupsList' 		=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "SecurityGroups List", 
																		 		 'DivID' 				=> "UserField", 
																		 		 'FunctionName' => "ShowSecurityGroupsList")
			);
		}
		
		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('Security Group Management'), $this->ShowSecurityGroupsList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * DeleteSecurityGroup function
	 */
	function DeleteSecurityGroup()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$SecurityGroupID = (int)$this->GetSetting('SecurityGroupID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('SecurityGroup', 
													Array('SecurityGroupID' => $SecurityGroupID), 
													Array('SecurityGroupID' => $SecurityGroupID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowSecurityGroupsList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditSecurityGroup function
	 */
	function EditSecurityGroup()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$SecurityGroupID = (int)($this->GetSetting('SecurityGroupID') ? $this->GetSetting('SecurityGroupID') : 0);
		
		if ($this->GetSetting('AddSecurityGroup')) {
			$data = Array("Name" => "", "Description" => "", "LoginStackID" => "");
			$formadd = TRUE;
			$Name = "Add New SecurityGroup";
		} else {
			$Query = "SELECT 
									* 
								FROM 
									`SecurityGroup` 
								WHERE 
									SecurityGroupID = " . $SecurityGroupID;
			$data = $this->DB->getRow($Query);
			$formadd = FALSE;
			$Name = $data["Name"];
		}
		
		$Form = new Form("EditSecurityGroupForm_" . $this->GetSetting('SecurityGroupID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("SecurityGroupID", "HIDDEN/VALUE=" . $this->GetSetting('SecurityGroupID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddSecurityGroup", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $SecurityGroupID, "DIV/LABLE= ");
		$Form->AddOptionString("Name", "TEXT/VALUE=" . $data["Name"] . "/LABLE=" . Language('Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Description", "TEXTAREA/VALUE=" . $data["Description"] . "/LABLE=" . Language('Description') . "/COLS=30/ROWS=8/STYLE=width: 250px;");
		$Form->AddOptionString("LoginStackID", "SELECT/VALUES=:-- " . Language('None') . " --/DB=NitroBO:LoginStack:LoginStackID:Name/SELECTED=". $data["LoginStackID"] . "/LABLE=" . Language('Login stack') . "/STYLE=width: 250px;");
		
		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveSecurityGroup', GetXMLURL('EditSecurityGroupForm_" . $this->GetSetting('SecurityGroupID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveSecurityGroup', GetXMLURL('EditSecurityGroupForm_" . $this->GetSetting('SecurityGroupID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $SecurityGroupID . "'); return false;\" value='" . Language('Cancel') . "' />");

		$Form->AddOptionString("Temp", "HTML/LABLE= /VALUE=" . $Custom);
		$Form -> SetTemplateIDs("file:" . NITRO_PATH . "Defaults/Templates/BackOffice/form.tpl", "file:" . NITRO_PATH . "Defaults/Templates/BackOffice/widget.tpl");
		$RV = $Form->Draw();
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("JSCRIPT:()" => "AddPageTab('" . $SecurityGroupID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveSecurityGroup function
	 */
	function SaveSecurityGroup()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$SecurityGroupID = (int)$this->GetSetting('SecurityGroupID');
		
		$Query = "SELECT 
								SecurityGroupID 
							FROM 
								`SecurityGroup` 
							WHERE 
								Name = " . NitroPrepareDB($this->GetSetting('Name'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('Name'))) {					
			$RV = Array("ErrorDiv_" . $SecurityGroupID => $this->PrePareMSG(Array("Error", "Please fill in a Name.", TRUE), $SecurityGroupID));
		} else if ($this->GetSetting('AddSecurityGroup') && strlen($this->GetSetting('Name')) && $Result) {
			$RV = Array("ErrorDiv_" . $SecurityGroupID => $this->PrePareMSG(Array("Error", "The Name you filled in is already taken.", TRUE), $SecurityGroupID));
		} else {
			$RV = $this->_SaveSecurityGroupInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveSecurityGroupInfo function (private)
	**/
	function _SaveSecurityGroupInfo()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$ID = $Transaction->addData('SecurityGroup', 
																Array('Name' => $this->GetSetting('Name'), 
																		  'Description' => $this->GetSetting('Description'), 
																		  'LoginStackID' => (int)$this->GetSetting('LoginStackID'), 
																		  'SecurityGroupID' => (int)$this->GetSetting('SecurityGroupID')));
		
		if ($Transaction->Commit() !== FALSE) {
			$RV = Array("NoError", Language('SecurityGroup Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('SecurityGroup could not be Saved'));
		}
		
		if ($this->GetSetting('AddSecurityGroup')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 SecurityGroupID 
																						 FROM 
																							 SecurityGroup 
																						 ORDER 
																						 BY 
																							 SecurityGroupID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('SecurityGroupID');
		}
		
		$ResultID = $newObjectID;
				
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddSecurityGroup')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditSecurityGroup', '&SecurityGroupID=" . $ResultID . "'); ") . "DeletePageTab('0', true);", "UserField" => $this->ShowSecurityGroupsList());
		} else {
			return Array( "UserField" => $this->ShowSecurityGroupsList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('Name') . "');"));
		}
	}
	
	/**
	 * ShowSecurityGroupsList function
	 */
	function ShowSecurityGroupsList($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("", "SecurityGroup_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "SecurityGroups_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditSecurityGroup', '&AddSecurityGroup=1'); return false;", "", Language('Add Security Group'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								SecurityGroupID, 
								Name 
							FROM 
								`SecurityGroup` 
								" . ($List->Filter['Name'] ? "WHERE " : "") . " 
								" . ($List->Filter['Name'] ? "Name LIKE " . NitroPrepareDB("%" . $List->Filter['Name'] . "%") . " " : "") . "
							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 = "SecurityGroupsList";

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