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

/**
 * Locale
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_Locale extends NitroBOSubModule {
	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_Locale";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_Locale() { }
	
	/**
	 * 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), 
				'LocaleID' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'LocaleID',				'Default' => NULL), 
				'AddLocale' 				=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddLocale',				'Default' => NULL), 
				'Name' 							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Name',						'Default' => NULL), 
				'Langs'							=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Langs',						'Default' => NULL), 
				'Territories'	 			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Territories',			'Default' => NULL), 
				'Charsets' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'Charsets',				'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(
				'DeleteLocale' 	=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Delete Locale", 
																 'DivID' 				=> "UserField", 
																 'FunctionName' => "DeleteLocale"), 
				'EditLocale' 	 	=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Edit Locale", 
																 'DivID' 				=> "EditField", 
																 'FunctionName' => "EditLocale"), 
				'SaveLocale' 		=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Save Locale", 
																 'DivID' 				=> "ErrorDiv", 
																 'FunctionName' => "SaveLocale"), 
				'FilterMod' 		=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Filter Locale List", 
																 'DivID' 				=> "UserField", 
																 'FunctionName' => "ShowLocaleList"), 
				'LocaleList' 		=> Array('Type' 				=> "HTML", 
																 'Name' 				=> "Locale List", 
																 'DivID' 				=> "UserField", 
																 'FunctionName' => "ShowLocaleList")
			);
		}
		
		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('Locale Management'), $this->ShowLocaleList(TRUE));

		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;	
	}
	
	/**
	 * DeleteLocale function
	 */
	function DeleteLocale()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LocaleID = (int)$this->GetSetting('LocaleID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('Locale', 
													Array('LocaleID' => $LocaleID), 
													Array('LocaleID' => $LocaleID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowLocaleList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditLocale function
	 */
	function EditLocale()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LocaleID = (int)($this->GetSetting('LocaleID') ? $this->GetSetting('LocaleID') : 0);
		
		if ($this->GetSetting( 'AddLocale' )) {
			$data = Array("Name" => "", "Langs" => "", "Territories" => "", "Charsets" => "");
			$formadd = TRUE;
			$Name = "Add New Locale";
		} else {
			$Query = "SELECT 
									* 
								FROM 
									`Locale` 
								WHERE 
									LocaleID = " . $LocaleID;
			$data = $this->DB->getRow($Query);
			$formadd = FALSE;
			$Name = $data["Name"];
		}
		
		$Form = new Form("EditLocaleForm_" . $this->GetSetting('LocaleID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("LocaleID", "HIDDEN/VALUE=" . $this->GetSetting('LocaleID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddLocale", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $LocaleID, "DIV/LABLE= " );
		$Form->AddOptionString("Name", "TEXT/VALUE=" . $data["Name"] . "/LABLE=" . Language('Name') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Langs", "TEXT/VALUE=" . $data["Langs"] . "/LABLE=" . Language('Languages') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Territories", "TEXT/VALUE=" . $data["Territories"] . "/LABLE=" . Language('Territories') . "/STYLE=width: 250px;");
		$Form->AddOptionString("Charsets", "TEXT/VALUE=" . $data["Charsets"] . "/LABLE=" . Language('Charsets') . "/STYLE=width: 250px;");
		
		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveLocale', GetXMLURL('EditLocaleForm_" . $this->GetSetting('LocaleID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveLocale', GetXMLURL('EditLocaleForm_" . $this->GetSetting('LocaleID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $LocaleID . "'); 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('" . $LocaleID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveLocale function
	 */
	function SaveLocale()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LocaleID = (int)$this->GetSetting('LocaleID');
		
		$Query = "SELECT 
								LocaleID 
							FROM 
								`Locale` 
							WHERE 
								Name = " . NitroPrepareDB($this->GetSetting('Name'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('Name'))) {
			$RV = Array("ErrorDiv_" . $LocaleID => $this->PrePareMSG(Array("Error", "Please fill in a Name.", TRUE), $LocaleID));
		} else if (!strlen($this->GetSetting('Langs'))) {
			$RV = Array("ErrorDiv_" . $LocaleID => $this->PrePareMSG(Array("Error", "Please fill in a Language.", TRUE), $LocaleID));
		} else if ($this->GetSetting('AddLocale') && strlen($this->GetSetting('Name')) && $Result) {
			$RV = Array("ErrorDiv_" . $LocaleID => $this->PrePareMSG(Array("Error", "The Name you filled in is already taken.", TRUE), $LocaleID));
		} else if (!strlen($this->GetSetting('Territories'))) {
			$RV = Array("ErrorDiv_" . $LocaleID => $this->PrePareMSG(Array("Error", "Please fill in a Territory.", TRUE), $LocaleID));
		} else if (!strlen($this->GetSetting('Charsets'))) {
			$RV = Array("ErrorDiv_" . $LocaleID => $this->PrePareMSG(Array("Error", "Please fill in a Charset.", TRUE), $LocaleID));
		} else {
			$RV = $this->_SaveLocaleInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveLocaleInfo function (private)
	**/
	function _SaveLocaleInfo()
	{
		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('Locale', 
																Array('Name' => $this->GetSetting('Name'), 
																		  'Langs' => $this->GetSetting('Langs'), 
																		  'Territories' => $this->GetSetting('Territories'), 
																		  'Charsets' => $this->GetSetting('Charsets'), 
																		  'LocaleID' => (int)$this->GetSetting('LocaleID')));
		
		if ($Transaction->Commit() !== FALSE) {
			$RV = Array("NoError", Language('Locale Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('Locale could not be Saved'));
		}
		
		if ($this->GetSetting('AddLocale')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 LocaleID 
																						 FROM 
																							 Locale 
																						 ORDER 
																						 BY 
																							 LocaleID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('LocaleID');
		}
		
		$ResultID = $newObjectID;
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddLocale')) {
			return Array("JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditLocale', '&LocaleID=" . $ResultID . "'); ") . "DeletePageTab('0', true);", "UserField" => $this->ShowLocaleList());
		} else {
			return Array("UserField" => $this->ShowLocaleList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('Name') . "');"));
		}
	}
	
	/**
	 * ShowLocaleList function
	 */
	function ShowLocaleList($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("", "Locale_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "Locales_");					 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditLocale', '&AddLocale=1'); return false;", "", Language('Add Locale'));
		$List->AddColumn(Language('Name'), "Name");
		$List->AddColumn(Language('Languages'), "Langs");
		$List->AddColumn(Language('Territories'), "Territories");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								LocaleID, 
								Name, 
								Langs, 
								Territories, 
								Charsets 
							FROM 
								`Locale` 
								" . ($List->Filter['Name'] || $List->Filter['Langs'] || $List->Filter['Territories'] ? "WHERE " : "") . " 
								" . ($List->Filter['Name'] ? "Name LIKE " . NitroPrepareDB("%" . $List->Filter['Name'] . "%") . " " : "") . "
								" . ($List->Filter['Name'] && $List->Filter['Langs'] ? "AND " : "") . ($List->Filter['Langs'] ? "langs LIKE " . NitroPrepareDB("%" . $List->Filter['Langs'] . "%") . " " : "") . "
								" . (($List->Filter['Name'] || $List->Filter['Langs']) && $List->Filter['Territories'] ? "AND " : "") . ($List->Filter['Territories'] ? "Territories LIKE " . NitroPrepareDB("%" . $List->Filter['Territories'] . "%") . " " : "") . "
							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 = "LocaleList";

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