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

/**
 * ObjectLinks
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_ObjectLink extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_ObjectLink";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_ObjectLink() { }
	
	/**
	 * 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), 
				'ObjectLinkID' 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ObjectLinkID',		'Default' => NULL), 
				'AddObjectLink' 		=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddObjectLink',		'Default' => NULL), 
				'IDString' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'IDString',				'Default' => NULL), 
				'ObjectID'					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ObjectID',				'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(
				'DeleteObjectLink' 	=> Array('Type' 				=> "HTML", 
																		 'Name' 				=> "Delete ObjectLink", 
																		 'DivID' 				=> "UserField", 
																		 'FunctionName' => "DeleteObjectLink"), 
				'EditObjectLink' 	 	=> Array('Type' 				=> "HTML", 
																 		 'Name' 				=> "Edit ObjectLink", 
																 		 'DivID' 				=> "EditField", 
																 		 'FunctionName' => "EditObjectLink"), 
				'SaveObjectLink' 		=> Array('Type' 				=> "HTML", 
																 		 'Name' 				=> "Save ObjectLink", 
																 		 'DivID'			 	=> "ErrorDiv", 
																 		 'FunctionName' => "SaveObjectLink"), 
				'FilterMod' 				=> Array('Type' 				=> "HTML", 
																 		 'Name' 				=> "Filter ObjectLinks List", 
																 		 'DivID' 				=> "UserField", 
																 		 'FunctionName' => "ShowObjectLinksList"), 
				'ObjectLinkList' 		=> Array('Type' 				=> "HTML", 
																 		 'Name' 				=> "ObjectLinks List", 
																 		 'DivID' 				=> "UserField", 
																 		 'FunctionName' => "ShowObjectLinksList")
			);
		}
		
		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('ObjectLink Management'), $this->ShowObjectLinksList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * DeleteObjectLink function
	 */
	function DeleteObjectLink()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$ObjectLinkID = (int)$this->GetSetting('ObjectLinkID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('ObjectLink', 
													Array('ObjectLinkID' => $ObjectLinkID), 
													Array('ObjectLinkID' => $ObjectLinkID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowObjectLinksList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditObjectLink function
	 */
	function EditObjectLink()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$ObjectLinkID = (int)($this->GetSetting('ObjectLinkID') ? $this->GetSetting('ObjectLinkID') : 0);
		
		if ($this->GetSetting('AddObjectLink')) {
			$data = Array("IDString" => "", "ObjectID" => "");
			$formadd = TRUE;
			$Name = "Add New ObjectLink";
		} else {
			$Query = "SELECT 
									* 
								FROM 
									`ObjectLink` 
								WHERE 
									ObjectLinkID = " . $ObjectLinkID;
			$data = $this->DB->getRow($Query);
			$formadd = FALSE;
			$Name = $data["IDString"];
		}
		
		$Form = new Form("EditObjectLinkForm_" . $this->GetSetting('ObjectLinkID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("ObjectLinkID", "HIDDEN/VALUE=" . $this->GetSetting('ObjectLinkID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddObjectLink", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $ObjectLinkID, "DIV/LABLE= ");
		$Form->AddOptionString("IDString", "TEXT/VALUE=" . $data["IDString"] . "/LABLE=" . Language('IDString') . "/STYLE=width: 250px;");
		$Form->AddOptionString("ObjectID", "SELECT/VALUES=0: -- Select an Object -- /DB=NitroBO:Object:ObjectID:Name" . ($data["ObjectID"] ? "/SELECTED=" . $data["ObjectID"] : "") . "/LABLE=" . Language('Object') . "/STYLE=width: 250px;");
		
		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveObjectLink', GetXMLURL('EditObjectLinkForm_" . $this->GetSetting('ObjectLinkID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveObjectLink', GetXMLURL('EditObjectLinkForm_" . $this->GetSetting('ObjectLinkID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $ObjectLinkID . "'); 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('" . $ObjectLinkID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveObjectLink function
	 */
	function SaveObjectLink()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$ObjectLinkID = (int)$this->GetSetting('ObjectLinkID');
		
		$Query = "SELECT 
								ObjectLinkID 
							FROM 
								`ObjectLink` 
							WHERE 
								IDString = " . NitroPrepareDB($this->GetSetting('IDString'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('IDString'))) {					
			$RV = Array("ErrorDiv_" . $ObjectLinkID => $this->PrePareMSG(Array("Error", "Please fill in a IDString.", TRUE), $ObjectLinkID));
		} else if ($this->GetSetting('AddObjectLink') && strlen($this->GetSetting('IDString')) && $Result) {
			$RV = Array("ErrorDiv_" . $ObjectLinkID => $this->PrePareMSG(Array("Error", "The IDString you filled in is already taken.", TRUE), $ObjectLinkID));
		} else {
			$RV = $this->_SaveObjectLinkInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveObjectLinkInfo function (private)
	**/
	function _SaveObjectLinkInfo()
	{
		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('ObjectLink', 
																Array('IDString' => $this->GetSetting('IDString'), 
																		  'ObjectID' => $this->GetSetting('ObjectID'), 
																		  'ObjectLinkID' => (int)$this->GetSetting('ObjectLinkID')));
		
		if ($Transaction->Commit() !== FALSE) {
			$RV = Array("NoError", Language('ObjectLink Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('ObjectLink could not be Saved'));
		}
		
		if ($this->GetSetting('AddObjectLink')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 ObjectLinkID 
																						 FROM 
																							 ObjectLink 
																						 ORDER 
																						 BY 
																							 ObjectLinkID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('ObjectLinkID');
		}
		
		$ResultID = $newObjectID;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddObjectLink')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditObjectLink', '&ObjectLinkID=" . $ResultID . "'); ") . "DeletePageTab('0', true);", "UserField" => $this->ShowObjectLinksList());
		} else {
			return Array( "UserField" => $this->ShowObjectLinksList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('IDString') . "');"));
		}
	}
	
	/**
	 * ShowObjectLinksList function
	 */
	function ShowObjectLinksList($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("", "ObjectLink_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "ObjectLinks_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditObjectLink', '&AddObjectLink=1'); return false;", "", Language('Add Country'));
		$List->AddColumn(Language('IDString'), "IDString");
		$List->AddColumn(Language('Object'), "Object");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								OL.ObjectLinkID AS ObjectLinkID, 
								OL.IDString AS IDString, 
								O.Name AS Object 
							FROM 
								`ObjectLink` AS OL 
							LEFT 
							JOIN 
								`Object` AS O 
							ON 
								OL.ObjectID = O.ObjectID 
								" . ($List->Filter['IDString'] || $List->Filter['Object'] ? "WHERE " : "") . " 
								" . ($List->Filter['IDString'] ? "OL.IDString LIKE " . NitroPrepareDB("%" . $List->Filter['IDString'] . "%") . " " : "") . "
								" . ($List->Filter['IDString'] && $List->Filter['Object'] ? "AND " : "") . ($List->Filter['Object'] ? "O.Name LIKE " . NitroPrepareDB("%" . $List->Filter['Object'] . "%") . " " : "") . "
							ORDER BY 
								OL.IDString 
							" . ($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 = "ObjectLinkList";

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