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

/**
 * Users
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_Users extends NitroBOSubModule {
	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_Users";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_Users() { }
	
	/**
	 * 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), 
				'UserID' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'UserID',					'Default' => NULL), 
				'AddUser' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddUser',					'Default' => NULL), 
				'Name' 							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL), 
				'UserName' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'UserName',				'Default' => NULL), 
				'pName' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'pName',						'Default' => NULL), 
				'pNameTwo'					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'pNameTwo',				'Default' => NULL), 
				'SecurityGroups' 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'SecurityGroups/',	'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(
				'DeleteUser' 	=> Array('Type' => "HTML", 
															 'Name' => "Delete User", 
															 'DivID' => "UserField", 
															 'FunctionName' => "DeleteUser"), 
				'EditUser' 	 	=> Array('Type' => "HTML", 
															 'Name' => "Edit User", 
															 'DivID' => "EditField", 
															 'FunctionName' => "EditUser"), 
				'SaveUser' 		=> Array('Type' => "HTML", 
															 'Name' => "Save User", 
															 'DivID' => "ErrorDiv", 
															 'FunctionName' => "SaveUser"), 
				'FilterMod' 	=> Array('Type' => "HTML", 
															 'Name' => "Filter User List", 
															 'DivID' => "UserField", 
															 'FunctionName' => "ShowUserList"), 
				'UserList' 		=> Array('Type' => "HTML", 
															 'Name' => "User List", 
															 'DivID' => "UserField", 
															 'FunctionName' => "ShowUserList")
			);
		}
		
		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;
	}
	
	/**
	 * DeleteUser function
	 */
	function DeleteUser()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$UserID = (int)$this->GetSetting('UserID');
		
		if ($UserID != $this->Sess->UserID) {
			$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
			$Transaction->addData('User', 
														Array('UserID' => $UserID), 
														Array('UserID' => $UserID), 
														'DELETE');
			$Transaction->addData('User_SecurityGroup', 
														Array('UserID' => $UserID), 
														Array('UserID' => $UserID), 
														'DELETE');
	
			$RV = Array("UserField" => ($Transaction->Commit() !== FALSE) ? $this->ShowUserList() : "");
		} else {
			$RV = Array("JSCRIPT:()" => "alert('" . Language('You cannot delete yourself!') . "');");
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * EditUser function
	 */
	function EditUser()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);

		$UserID = (int)($this->GetSetting('UserID') ? $this->GetSetting('UserID') : "0");
		
		if ($this->GetSetting('AddUser')) {
			$SGQuery = "SELECT 
										SecurityGroupID AS ID, 
										Name 
									FROM 
										`SecurityGroup`";	
			$data = Array("Name" => "", "Username" => "");
			$formadd = TRUE;
			$Name = "Add New User";
			$count = 10;
		} else {
			$Query = "SELECT 
									* 
								FROM 
									`User` 
								WHERE 
									UserID = " . $UserID;
			$data = $this->DB->getRow($Query);
			$SGQuery = "SELECT 
										SG.SecurityGroupID AS ID, 
										Name, 
										UserID AS SELECTED
									FROM SecurityGroup AS SG
									LEFT JOIN 
										User_SecurityGroup AS U_SG 
									ON U_SG.SecurityGroupID = SG.SecurityGroupID 
									AND U_SG.UserID = " . $UserID;
			$formadd = FALSE;
			$Name = $data["Username"];
			$countresult = $this->DB->query($SGQuery);
			$count = ($countresult->numRows() > 20) ? 20 : $countresult->numRows();
		}
		
		$Form = new Form("EditUserForm_" . $this->GetSetting('UserID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("UserID", "HIDDEN/VALUE=" . $this->GetSetting('UserID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddUser", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $UserID, "DIV/LABLE= ");
		$Form->AddOptionString("Name", "TEXT/VALUE=" . $data["Name"] . "/LABLE=" . Language('Name') . "/STYLE=width: 200px;");
		$Form->AddOptionString("UserName", "TEXT/VALUE=" . $data["Username"] . "/LABLE=" . Language('UserName') . "/STYLE=width: 200px;");
		$Form->AddOptionString("pName", "PASSWORD/LABLE=" . Language('PassWord') . "/STYLE=width: 200px;");
		$Form->AddOptionString("pNameTwo", "PASSWORD/LABLE=" . Language('PassWord') . " (Repeat)/STYLE=width: 200px;");
		$Form->AddOptionString("SecurityGroups/", "SELECTMULTIPLE/QUERY=".$this->NitroBODBAlias.":" . $SGQuery . "/LABLE=" . Language('Security Groups') . "/STYLE=width: 200px;/SIZE=" . $count . "");
		
		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveUser', GetXMLURL('EditUserForm_" . $this->GetSetting('UserID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveUser', GetXMLURL('EditUserForm_" . $this->GetSetting('UserID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $UserID . "'); 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('" . $UserID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveUser function
	 */
	function SaveUser()
	{		
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$UserID = (int)$this->GetSetting('UserID');
		
		$securitygroups = $this->GetSetting('SecurityGroups');
		$Query = "SELECT Username 
							FROM `User` 
							WHERE Username = " . NitroPrepareDB($this->GetSetting('UserName'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('UserName'))) {
			$RV = Array("ErrorDiv_" . $UserID => $this->PrePareMSG(Array("Error", "Please fill in a UserName.", TRUE), $UserID));
		} else if ($this->GetSetting('AddUser') && strlen($this->GetSetting('UserName')) && $Result) {
			$RV = Array("ErrorDiv_" . $UserID => $this->PrePareMSG(Array("Error", "The UserName you filled in is already taken.", TRUE), $UserID));
		} else if ($this->GetSetting('AddUser') && !strlen($this->GetSetting('pName'))) {
			$RV = Array( "ErrorDiv_" . $UserID => $this->PrePareMSG(Array("Error", "Please fill in a Password.", TRUE), $UserID));
		} else if (strlen($this->GetSetting('pName' )) && ($this->GetSetting('pName') !== $this->GetSetting('pNameTwo'))) {
			$RV = Array("ErrorDiv_" . $UserID => $this->PrePareMSG(Array("Error", "Please fill in both passwords the same.", TRUE), $UserID));
		} else if (!is_array( $securitygroups ) || ($securitygroups && $securitygroups[0] == "")) {
			$RV = Array("ErrorDiv_" . $UserID => $this->PrePareMSG(Array("Error", "Please select one or more SecurityGroups.", TRUE), $UserID));
		} else {
			$RV = $this->_SaveUserInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveUserInfo function (private)
	**/
	function _SaveUserInfo()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		//var_dump($this->Settings); exit;
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$ID = $Transaction->addData('User', 
																Array('Name' => $this->GetSetting('Name'), 
																		  'Username' => $this->GetSetting('UserName'), 
																		  'Password' => (strlen($this->GetSetting('pName')) ? Array('SHA1' => $this->GetSetting('pName')) : 'EMPTY:()'),
																		  'UserID' => (int)$this->GetSetting('UserID')));
																		  
		if (!$this->GetSetting('AddUser')) {
			$Transaction->addData('User_SecurityGroup', 
														Array('UserID' => (int)$this->GetSetting('UserID')), 
														Array('UserID' => (int)$this->GetSetting('UserID')), 
														'DELETE');
		}
		
		$Commit = $Transaction->Commit();
		
		if ($this->GetSetting('AddUser')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 UserID 
																						 FROM 
																							 User 
																						 ORDER 
																						 BY 
																							 UserID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('UserID');
		}
		
		$ResultID = $newObjectID;
		
		$Transaction2 = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		
		foreach ($this->GetSetting('SecurityGroups') AS $SecurityGroupID) {
				$Transaction2->addData('User_SecurityGroup', 
															 Array('UserID' => $ResultID, 
																		 'SecurityGroupID' => $SecurityGroupID), 
															 FALSE,  
															 'INSERT');
		}
		
		$Transaction2->Commit();
		
		if ($Commit !== FALSE) {
			$RV = Array("NoError", Language('User Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('User could not be Saved'));
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);

		if ($this->GetSetting('AddUser')) {
			return Array("JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditUser', '&UserID=" . $ResultID . "'); " ) . "DeletePageTab('0', true);", "UserField" => $this->ShowUserList());
		} else {
			return Array("UserField" => $this->ShowUserList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('UserName') . "');"));
		}
	}

	/**
	 * Draw function
	 */
	function Draw()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$RV = $this->CreateTabInterface(Language('User Management'), $this->ShowUserList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * ShowUserList function
	 */	
	function ShowUserList($inDiv = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, 'ShowUserList 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("", "Users_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "Users_");
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditUser', '&AddUser=1&UserID=AddUser'); return false;", "", Language('Add User'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddColumn(Language('Username'), "Username");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->SetColumnWidth("20px", "Edit");
		$List->SetColumnWidth("20px", "Delete");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								UserID, 
								Name, 
								Username 
							FROM 
								`User` 
								" . ($List->Filter['Name'] || $List->Filter['Username'] ? "WHERE " : "") . " 
								" . ($List->Filter['Name'] ? "Name LIKE " . NitroPrepareDB("%" . $List->Filter['Name'] . "%") . " " : "") . "
								" . ($List->Filter['Name'] && $List->Filter['Username'] ? "AND " : "") . ($List->Filter['Username'] ? "Username LIKE " . NitroPrepareDB("%" . $List->Filter['Username'] . "%") . " " : "") . "
							ORDER BY 
								Username 
							" . ($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 = "UserList";

		if ($Result->numRows()) {
			while ($Data = $Result->fetchArray()) {
				$ListRow = new ListingRow2();
				$ListRow->AddData("Name", $Data["Name"]);
				$ListRow->AddData("Username", $Data["Username"]);				
				$ListRow->SetAction("Edit", $editimg, "#", "ModuleXMLRequest('EditUser', '&UserID=" . $Data["UserID"] . "'); 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('DeleteUser', '&UserID=" . $Data["UserID"] . "'); 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