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

/**
 * Country
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_Country extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_Country";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_Country() { }
	
	/**
	 * 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,	'Validate' => 'REGEX:[A-Za-z0-9_-]+:{%string%} is not a valid string with valid length ({%length%});string=>P,length=>12'), 
				'CountryID' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'CountryID',				'Default' => NULL,	'Validate' => 'REGEX:[0-9]*'), 
				'AddCountry' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddCountry',			'Default' => NULL,	'Validate' => 'INT:{2-6}'), 
				'Name' 							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL,	'Validate' => 'STRING'), 
				'FullName'					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Fullname',				'Default' => NULL,	'Validate' => 'STRING'), 
				'alpha2'	 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'alpha2',					'Default' => NULL,	'Validate' => 'STRING'), 
				'alpha3' 						=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Country',					'Default' => NULL,	'Validate' => 'STRING'), 
				'Code' 							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Code',						'Default' => NULL,	'Validate' => 'STRING'), 
				'CloseTab' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'CloseTab',				'Default' => NULL,	'Validate' => 'INT'), 
				'ModFilter' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'ModFilter/',			'Default' => NULL,	'Validate' => 'ARRAY'), 
				'Start'							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Start',						'Default' => 0,			'Validate' => 'INT') 
			);
		}
		
		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(
				'DeleteCountry' 	=> Array('Type' 				=> "HTML", 
																	 'Name' 				=> "Delete Country", 
																	 'DivID' 				=> "UserField", 
																	 'FunctionName' => "DeleteCountry"), 
				'EditCountry' 	 	=> Array('Type' 				=> "HTML", 
															 		 'Name' 				=> "Edit Country", 
															 		 'DivID' 				=> "EditField", 
															 		 'FunctionName' => "EditCountry"), 
				'SaveCountry' 		=> Array('Type' 				=> "HTML", 
															 		 'Name' 				=> "Save Country", 
															 		 'DivID'			 	=> "ErrorDiv", 
															 		 'FunctionName' => "SaveCountry"), 
				'FilterMod' 			=> Array('Type' 				=> "HTML", 
															 		 'Name' 				=> "Filter Country List", 
															 		 'DivID' 				=> "UserField", 
															 		 'FunctionName' => "ShowCountryList"), 
				'CountryList' 		=> Array('Type' 				=> "HTML", 
															 		 'Name' 				=> "Country List", 
															 		 'DivID' 				=> "UserField", 
															 		 'FunctionName' => "ShowCountryList")
			);
		}
		
		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('Country Management'), $this->ShowCountryList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * DeleteCountry function
	 */
	function DeleteCountry()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);

		$CountryID = (int)$this->GetSetting('CountryID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('Country', 
													Array('CountryID' => $CountryID), 
													Array('CountryID' => $CountryID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowCountryList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditCountry function
	 */
	function EditCountry()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$CountryID = (int)($this->GetSetting('CountryID') ? $this->GetSetting('CountryID') : 0);
		
		if ($this->GetSetting('AddCountry')) {
			$data = Array("Name" => "", "Fullname" => "", "alpha2" => "", "Country" => "", "Code" => "");
			$formadd = TRUE;
			$Name = "Add New Country";
		} else {
			$Query = "SELECT 
									* 
								FROM 
									`Country` 
								WHERE 
									CountryID = " . $CountryID;
			$data = $this->DB->getRow($Query);
			$formadd = FALSE;
			$Name = $data["Name"];
		}
		
		$Form = new Form("EditCountryForm_" . $this->GetSetting('CountryID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("CountryID", "HIDDEN/VALUE=" . $this->GetSetting('CountryID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddCountry", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $CountryID, "DIV/LABLE= ");
		$Form->AddOptionString("Name", "TEXT/VALUE=" . $data["Name"] . "/LABLE=" . Language('Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Fullname", "TEXT/VALUE=" . $data["Fullname"] . "/LABLE=" . Language('Full Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("alpha2", "TEXT/VALUE=" . $data["alpha2"] . "/LABLE=" . Language('Alpha2') . "/STYLE=width: 25px;/MAXLENGTH=2");
		$Form->AddOptionString("Country", "TEXT/VALUE=" . $data["Country"] . "/LABLE=" . Language('Alpha3') . "/STYLE=width: 25px;/MAXLENGTH=3");
		$Form->AddOptionString("Code", "TEXT/VALUE=" . $data["Code"] . "/LABLE=" . Language('Code') . "/STYLE=width: 250px;/MAXLENGTH=3");
		
		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveCountry', GetXMLURL('EditCountryForm_" . $this->GetSetting('CountryID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveCountry', GetXMLURL('EditCountryForm_" . $this->GetSetting('CountryID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $CountryID . "'); 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('" . $CountryID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveCountry function
	 */
	function SaveCountry()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$CountryID = (int)$this->GetSetting('CountryID');
		
		$Query = "SELECT 
								CountryID 
							FROM 
								`Country` 
							WHERE 
								Name = " . NitroPrepareDB($this->GetSetting('Name'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('Name'))) {					
			$RV = Array("ErrorDiv_" . $CountryID => $this->PrePareMSG(Array("Error", "Please fill in a Name.", TRUE), $CountryID));
		} else if (!strlen($this->GetSetting('FullName'))) {
			$RV = Array("ErrorDiv_" . $CountryID => $this->PrePareMSG(Array("Error", "Please fill in a FullName.", TRUE), $CountryID));
		} else if ($this->GetSetting('AddCountry') && strlen($this->GetSetting('Name')) && $Result) {
			$RV = Array("ErrorDiv_" . $CountryID => $this->PrePareMSG(Array("Error", "The Name you filled in is already taken.", TRUE), $CountryID));
		} else if ($this->GetSetting('AddCountry') && !strlen($this->GetSetting('alpha2'))) {
			$RV = Array("ErrorDiv_" . $CountryID => $this->PrePareMSG(Array("Error", "Please fill in a Alpha2.", TRUE), $CountryID));
		} else if ($this->GetSetting('AddCountry') && !strlen($this->GetSetting('alpha3'))) {
			$RV = Array("ErrorDiv_" . $CountryID => $this->PrePareMSG(Array("Error", "Please fill in a Alpha3.", TRUE), $CountryID));
		} else if ($this->GetSetting('AddCountry') && !strlen($this->GetSetting('Code'))) {
			$RV = Array("ErrorDiv_" . $CountryID => $this->PrePareMSG(Array("Error", "Please fill in a Code.", TRUE), $CountryID));
		} else {
			$RV = $this->_SaveCountryInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveCountryInfo function (private)
	**/
	function _SaveCountryInfo()
	{
		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('Country', 
																Array('Name' => $this->GetSetting('Name'), 
																		  'Fullname' => $this->GetSetting('FullName'), 
																		  'Country' => $this->GetSetting('alpha3'), 
																		  'alpha2' => $this->GetSetting('alpha2'), 
																		  'Code' => $this->GetSetting('Code'), 
																		  'CountryID' => (int)$this->GetSetting('CountryID')));
		
		if ($Transaction->Commit() !== FALSE) {
			$RV = Array("NoError", Language('Country Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('Country could not be Saved'));
		}
		
		if ($this->GetSetting('AddCountry')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 CountryID 
																						 FROM 
																							 Country 
																						 ORDER 
																						 BY 
																							 CountryID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('CountryID');
		}
		
		$ResultID = $newObjectID;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddCountry')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditCountry', '&CountryID=" . $ResultID . "'); ") . "DeletePageTab('0', true);", "UserField" => $this->ShowCountryList());
		} else {
			return Array( "UserField" => $this->ShowCountryList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('Name') . "');"));
		}
	}
	
	/**
	 * ShowCountryList function
	 */
	function ShowCountryList($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("", "Country_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "Countries_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditCountry', '&AddCountry=1'); return false;", "", Language('Add Country'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddColumn(Language('Full Name'), "FullName");
		$List->AddColumn(Language('Alpha2'), "Alpha2");
		$List->AddColumn(Language('Alpha3'), "Alpha3");
		$List->AddColumn(Language('Code'), "Code");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$List->SetColumnWidth('25px', 'Alpha2');
		$List->SetColumnWidth('25px', 'Alpha3');
		$List->SetColumnWidth('25px', 'Code');
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								CountryID, 
								Name, 
								Fullname, 
								Country AS alpha3, 
								alpha2, 
								Code 
							FROM 
								`Country` 
							WHERE 1
								" . ($List->Filter['Name']			? " AND Name LIKE " . NitroPrepareDB("%" . $List->Filter['Name'] . "%")					. " " : "") . "
								" . ($List->Filter['FullName']	? " AND Fullname LIKE " . NitroPrepareDB("%" . $List->Filter['FullName'] . "%")	. " " : "") . "
								" . ($List->Filter['Alpha2']		? " AND alpha2 LIKE " . NitroPrepareDB( "%" . $List->Filter['Alpha2'] . "%" )		. " " : "") . "
								" . ($List->Filter['Alpha3']		? " AND Country LIKE " . NitroPrepareDB("%" . $List->Filter['Alpha3'] . "%")		. " " : "") . "
								" . ($List->Filter['Code']			? " AND Code LIKE " . NitroPrepareDB("%" . $List->Filter['Code'] . "%")				. " " : "") . "
							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 = "CountryList";

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