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

/**
 * PageSecurity
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_PageSecurity extends NitroBOSubModule {
	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_PageSecurtiy";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_PageSecurity() { }
	
	/**
	 * GetSettingsDefinition function
	 *
	 * Which Settings are allowed in this Module?
	 */
	function GetSettingsDefinition()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		if (!isset($this->_ModuleSettings)) {
			$this->_ModuleSettings = Array(
				'P'			 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'P', 							'Default' => NULL), 
				'PageID' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'PageID',					'Default' => NULL), 
				'SecurityID'				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'SecurityID',			'Default' => NULL), 
				'Action'						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Action',					'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(
				'ChangePageRight' 		=> Array('Type' => "HTML", 
															 			 	 'Name' => 'Change Page Rights', 
															 			 	 'DivID' => "UserField", 
															 			 	 'FunctionName' => "ChangePageRight"), 
				'FilterMod' 					=> Array('Type' => "HTML", 
															 			 	 'Name' => 'Filter PageSecurity List', 
															 			 	 'DivID' => "UserField", 
															 			 	 'FunctionName' => "ShowPageRightsList"), 
				'ShowPageRightsList' 	=> Array('Type' => "HTML", 
															 		 		 'Name' => 'Change Page Rights', 
															 		 		 'DivID' => "UserField", 
															 				 'FunctionName' => "ShowPageRightsList")
			);
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $this->_XMLDefinition;
	}
	
	/**
	 * GetObjectsDefinition function
	 *
	 * Which Objects are allowed ?
	 */
	function GetObjectsDefinition()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$this->_ModuleObjects = Array(
			'Draw' => Array('Type'					=> 'HTML',
											'Name'					=> 'Text',
											'FunctionName'	=> 'Draw',
											'Default'				=> TRUE)
		);
	
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $this->_ModuleObjects;
	}
	
	function Draw()
	{
		$RV = "<h2>". Language('Page Security') . "</h2>";
		$RV.= $this->ShowPageRightsList(TRUE);
		
		return $RV;
	}
	
	/**
	 * ChangePageRight function
	 */
	function ChangePageRight()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$updateID = "PageID" . (int)$this->GetSetting( 'PageID' ) . "SecurityID" . (int)$this->GetSetting( 'SecurityID' );
		$PageID = (int)$this->GetSetting('PageID');		
		$SecurityID = (int)$this->GetSetting('SecurityID');							
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		
		if ($this->GetSetting('Action') == "Activate") {
			$Transaction->addData('Page_SecurityGroup', 
														Array('PageID' => $PageID, 
																	'SecurityGroupID' => $SecurityID), 
														FALSE, 
														'INSERT');
			$IMG = "<div id=\"" . $updateID . "\"><img src=\"./GetObject.php?NitroDefault=Images/checked.gif\" width=\"16\" height=\"16\" alt\"Active\" border=\"0\" style=\"cursor: pointer;\" title=\"Deactivate\" onclick=\"ModuleXMLRequest('ChangePageRight', '&Action=Deactivate&PageID=" . $PageID . "&SecurityID=" . $SecurityID . "'); return false;\" /></div>";
		} else {
			$Transaction->addData('Page_SecurityGroup', 
														Array('PageID' => $PageID, 
																	'SecurityGroupID' => $SecurityID), 
														Array('PageID' => $PageID, 
																	'SecurityGroupID' => $SecurityID), 
														'DELETE');
			$IMG = "<div id=\"" . $updateID . "\"><img src=\"./GetObject.php?NitroDefault=Images/unchecked.gif\" width=\"16\" height=\"16\" alt\"Not Active\" border=\"0\" style=\"cursor: pointer;\" title=\"Activate\" onclick=\"ModuleXMLRequest('ChangePageRight', '&Action=Activate&PageID=" . $PageID . "&SecurityID=" . $SecurityID . "'); return false;\" /></div>";
		}

		$RV = ($Transaction->Commit() !== FALSE) ? Array($updateID => $IMG) : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * ShowPageRightsList function
	**/
	function ShowPageRightsList($inDiv = FALSE)
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$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( "", "PagesSecurity_" . $this->GetSetting( 'P' ), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "PagesSecurities_" );							 
		$List->EnableRowHighlighting = TRUE;
		$List->AddColumn(Language('Page'), "Page");
		$List->SetColumnWidth("90%", "Page");
		
		// SecuurityGroups
		$SecurityGroups = array();
		
		$Query = "SELECT 
								SecurityGroupID, 
								Name 
							FROM 
								`SecurityGroup` 
							ORDER BY 
								Name";
		$Result = $this->DB->query($Query);
		
		if( $Result->numRows() ) {
			while( $Data = $Result->fetchArray() ) {
				$SecurityGroups[$Data["SecurityGroupID"]] = $Data;
			}
		}
		
		$Result->free();
		
		// Page Stuff
		$Pages = array();
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								PageID, 
								Name 
							FROM 
								`Page` 
 								" . ($List->Filter['Page'] ? "WHERE " : "") . "
								" . ($List->Filter['Page'] ? "Name LIKE " . NitroPrepareDB("%" . $List->Filter['Page'] . "%") . " " : "") . "
							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 = "ShowPageRightsList";
		
		if ($Result->numRows()) {
			while ($Data = $Result->fetchRow()) {
				$Pages[$Data["PageID"]] = $Data;
			}
		}
		
		$Result->free();
		
		// Page Security Stuff
		$PagesSecurity = Array();
		
		$Query = "SELECT 
								PageID, 
								SecurityGroupID 
							FROM 
								`Page_SecurityGroup`";
		$Result = $this->DB->query($Query);
		
		if ($Result->numRows()) {
			while ($Data = $Result->fetchArray()) {
				$PagesSecurity[$Data["PageID"]][$Data["SecurityGroupID"]] = TRUE;
			}
		}
		
		$Result->free();
		
		foreach ($SecurityGroups AS $ID => $Row) {
			$List->AddColumn("<span style='writing-mode: tb-rl'>" . $Row["Name"] . "</span>", $ID, TRUE);
			$List->hideFilter[] = $ID;
		}
		
		$n = 1;
		
		foreach ($Pages AS $ID => $Row) {
			$ListRow = new ListingRow2();
			$ListRow->AddData("Page", $Row["Name"]);
			
			foreach ($SecurityGroups AS $SGID => $SGRow) {				
				if ($PagesSecurity[$ID][$SGID]) {
					$IMG = "<div id=\"PageID" . $ID . "SecurityID" . $SGID . "\"><img src=\"./GetObject.php?NitroDefault=Images/checked.gif\" width=\"16\" height=\"16\" alt\"Active\" border=\"0\" style=\"cursor: pointer;\" title=\"Deactivate\" onclick=\"ModuleXMLRequest('ChangePageRight', '&Action=Deactivate&PageID=" . $ID . "&SecurityID=" . $SGID . "'); return false;\" /></div>";
				} else {
					$IMG = "<div id=\"PageID" . $ID . "SecurityID" . $SGID . "\"><img src=\"./GetObject.php?NitroDefault=Images/unchecked.gif\" width=\"16\" height=\"16\" alt\"Not Active\" border=\"0\" style=\"cursor: pointer;\" title=\"Activate\" onclick=\"ModuleXMLRequest('ChangePageRight', '&Action=Activate&PageID=" . $ID . "&SecurityID=" . $SGID . "'); return false;\" /></div>";
				}
				
				$ListRow->AddData($SGID, $IMG);
			}
			
			$List->AddListRow($ListRow);
			
			unset($ListRow);
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		$List-> SetTemplate("file:" . NITRO_PATH . "Defaults/Templates/BackOffice/Listing.tpl");
		return $List->Draw();
	}
}
?>
Return current item: OpenNitro