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

/**
 * Module
 *
 * @author			Jesper Avôt <hide@address.com>
 * @copyright		2006 June Systems B.V.
 * @package			Modules
 * @subpackage  NitroBO
 */
class NitroBO_LanguageStrings extends NitroBOSubModule {	
	/**
	 * Define some Module things
	 */
	var $ModuleName = "NitroBO_LanguageStrings";
	var $ModuleVersion = "1.0";
	var $ModuleAuthor = Array("Jesper Avôt");
	
	/**
	 * Does Nothing
	 */
	function NitroBO_LanguageStrings() { }
	
	/**
	 * 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), 
				'LanguageStringID' 	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'LanguageStringID',	'Default' => NULL), 
				'AddLanguageString'	=> Array('SessionVariable' => FALSE, 'FormVariable' => 'AddLanguageString',	'Default' => NULL), 
				'IDString' 					=> Array('SessionVariable' => FALSE, 'FormVariable' => 'IDString',					'Default' => NULL), 
				'LanguageData'			=> Array('SessionVariable' => FALSE, 'FormVariable' => 'LanguageData',			'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(
				'DeleteLanguageString' 	=> Array('Type' 				=> "HTML", 
																				 'Name' 				=> "Delete LanguageString", 
																				 'DivID' 				=> "UserField", 
																				 'FunctionName' => "DeleteLanguageString"), 
				'EditLanguageString' 	 	=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "Edit LanguageString", 
																		 		 'DivID' 				=> "EditField", 
																		 		 'FunctionName' => "EditLanguageString"), 
				'SaveLanguageString' 		=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "Save LanguageString", 
																		 		 'DivID'			 	=> "ErrorDiv", 
																		 		 'FunctionName' => "SaveLanguageString"), 
				'FilterMod' 						=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "Filter LanguageString List", 
																		 		 'DivID' 				=> "UserField", 
																		 		 'FunctionName' => "ShowLanguageStringList"), 
				'LanguageStringList' 		=> Array('Type' 				=> "HTML", 
																		 		 'Name' 				=> "LanguageString List", 
																		 		 'DivID' 				=> "UserField", 
																		 		 'FunctionName' => "ShowLanguageStringList")
			);
		}
		
		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('Language String Management'), $this->ShowLanguageStringList(TRUE));
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * DeleteModule function
	 */
	function DeleteLanguageString()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LanguageStringID = (int)$this->GetSetting('LanguageStringID');									
		$Transaction = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		$Transaction->addData('LanguageString', 
													Array('LanguageStringID' => $LanguageStringID), 
													Array('LanguageStringID' => $LanguageStringID), 
													'DELETE');
		$Transaction->addData('LanguageStringData', 
													Array('LanguageStringID' => $LanguageStringID), 
													Array('LanguageStringID' => $LanguageStringID), 
													'DELETE');

		$RV = ($Transaction->Commit() !== FALSE) ? $this->ShowLanguageStringList() : "";
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return Array("UserField" => $RV);
	}
	
	/**
	 * EditLanguageString function
	 */
	function EditLanguageString()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LanguageStringID = (int)($this->GetSetting('LanguageStringID') ? $this->GetSetting('LanguageStringID') : 0);
		$IDString = "";
		
		if ($this->GetSetting('AddLanguageString')) {
			$data = Array("IDString" => "", "LanguageData" => "");
			$formadd = TRUE;
			$Name = "Add New LanguageString";
		} else {
			$Query = "SELECT 
									LS.LanguageStringID, 
									LS.IDString, 
									LSD.LanguageCode, 
									LSD.Text 
								FROM 
									LanguageString AS LS 
								LEFT 
								JOIN 
									LanguageStringData AS LSD 
								ON 
									LS.LanguageStringID = LSD.LanguageStringID 
								WHERE 
									LS.LanguageStringID = " . $LanguageStringID;
			$Result = $this->DB->query($Query);
			$LanguageStringInfo = Array();
			
			if ($Result->numRows()) {
				while ($data = $Result->fetchArray()) {
					if (isset($data["LanguageCode"])) $LanguageStringInfo["LanguageCode"][$data["LanguageCode"]] = $data["Text"];
					$LanguageStringInfo["IDString"] = $data["IDString"];
					$IDString = $data["IDString"];
				}
			}
			
			$Result->free();
			
			$formadd = FALSE;
			$Name = $IDString;
		}
		
		$Form = new Form("EditLanguageStringForm_" . $this->GetSetting('LanguageStringID'), "/?P=" . $this->GetSetting('P'), "POST", "application/x-www-form-urlencoded");
		$Form->HideButtons();
		$Form->AddOptionString("P", "HIDDEN/VALUE=" . $this->GetSetting('P'));
		$Form->AddOptionString("LanguageStringID", "HIDDEN/VALUE=" . $this->GetSetting('LanguageStringID'));
		
		if ($formadd == TRUE) {
			$Form->AddOptionString("AddLanguageString", "HIDDEN/VALUE=1");
		}
		
		$Form->AddOptionString("ErrorDiv_" . $LanguageStringID, "DIV/LABLE= ");
		$Form->AddOptionString("IDString", "TEXT/VALUE=" . urlencode($IDString) . "/LABLE=" . Language('IDString') . "/STYLE=width: 250px;");
		
		foreach ($this->AllowedLanguages AS $LangCode => $LangName) {
			if ($LangCode != "Default") {
				$Form->AddOptionString("LanguageData[" . $LangCode . "]", "TEXT/VALUE=" . urlencode($LanguageStringInfo["LanguageCode"][$LangCode]) . "/LABLE=" . Language($LangName) . "/STYLE=width: 250px;");
			}
		}

		$Custom = urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveLanguageString', GetXMLURL('EditLanguageStringForm_" . $this->GetSetting('LanguageStringID') . "')); return false;\" value='" . Language('Save') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"ModuleXMLRequest('SaveLanguageString', GetXMLURL('EditLanguageStringForm_" . $this->GetSetting('LanguageStringID') . "') + '&CloseTab=1'); return false;\" value='" . Language('Save and Close') . "' />");
		$Custom.= urlencode("<input type='button' onclick=\"DeletePageTab('" . $LanguageStringID . "'); 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('" . $LanguageStringID . "', '" . rawurlencode($RV) . "', '" . rawurlencode($Name) . "');");
	}
	
	/**
	 * SaveLanguageString function
	 */
	function SaveLanguageString()
	{
		DebugGroup(__CLASS__, __FUNCTION__, "NitroBO->SubModule->" . $this->ModuleName . "->" . __FUNCTION__, __FILE__, __LINE__, DEBUG_MOD_OK);
		
		$LanguageStringID = (int)$this->GetSetting('LanguageStringID');
		
		$Query = "SELECT 
								LanguageStringID 
							FROM 
								`LanguageString` 
							WHERE 
								IDString = " . NitroPrepareDB($this->GetSetting('IDString'));
		$Result = $this->DB->getRow($Query);
		
		if (!strlen($this->GetSetting('IDString'))) {					
			$RV = Array("ErrorDiv_" . $LanguageStringID => $this->PrePareMSG(Array("Error", "Please fill in an IDString.", TRUE), $LanguageStringID));
		} else if ($this->GetSetting('AddLanguageString') && strlen($this->GetSetting('IDString')) && $Result) {
			$RV = Array("ErrorDiv_" . $LanguageStringID => $this->PrePareMSG(Array("Error", "The IDString you filled in is already taken.", TRUE), $LanguageStringID));
		} else {
			$RV = $this->_SaveLanguageStringInfo();
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		return $RV;
	}
	
	/**
	 * _SaveLanguageStringInfo function (private)
	**/
	function _SaveLanguageStringInfo()
	{
		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('LanguageString', 
																Array('IDString' => $this->GetSetting('IDString'), 
																		  'LanguageStringID' => (int)$this->GetSetting('LanguageStringID')));
																		  
		$Transaction->Commit();
		
		if ($this->GetSetting('AddLanguageString')) {
			$newObjectID = (int)$this->DB->getOne("SELECT 
																							 LanguageStringID 
																						 FROM 
																							 LanguageString 
																						 ORDER 
																						 BY 
																							 LanguageStringID 
																						 DESC 
																						 LIMIT 
																							 1");
		} else {
			$newObjectID = (int)$this->GetSetting('LanguageStringID');
		}
		
		$ResultID = $newObjectID;
		
		if (!$this->GetSetting('AddLanguageString')) {
			$LQuery = "SELECT 
									 LanguageCode 
								 FROM 
									 `LanguageStringData` 
								 WHERE 
									 LanguageStringID = " . (int)$this->GetSetting('LanguageStringID');
			$Result2 = $this->DB->query($LQuery);
			$Items = Array();
			
			if ($Result2->numRows()) {
				while ($data = $Result2->fetchArray()) {
					$Items[] = $data["LanguageCode"];
				}
			}
		}
		
		if (!is_array($Items)) $Item = Array();
		
		$Transaction2 = new Transaction($this->DB, $this->Sess->UserID, TRUE);
		
		foreach ($this->GetSetting('LanguageData') AS $LangCode => $LangValue) {
			if ($LangValue != "") {
				if (in_array($LangCode, $Items)) {
					$Transaction2->addData('LanguageStringData', 
															   Array('Text' => $LangValue), 
															   Array('LanguageCode' => $LangCode, 
															 			   'LanguageStringID' => (int)$this->GetSetting('LanguageStringID')), 
															   'UDPATE');
				} else {
					$Transaction2->addData('LanguageStringData', 
															   Array('LanguageStringID' => $ResultID, 
															   			 'LanguageCode' => $LangCode, 
															   			 'Text' => $LangValue), 
															   FALSE, 
															   'INSERT');
				}
			}
		}
		
		if ($Transaction2->Commit() !== FALSE) {
			$RV = Array("NoError", Language('Language String Saved'), TRUE);
		} else {
			$RV = Array("Error", Language('Language String could not be Saved'));
		}
		
		DebugCloseGroup(DEBUG_MOD_OK);
		
		if ($this->GetSetting('AddLanguageString')) {
			return Array( "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "" : "ModuleXMLRequest('EditLanguageString', '&LanguageStringID=" . $ResultID . "'); ") . "DeletePageTab('0', true);", "UserField" => $this->ShowLanguageStringList());
		} else {
			return Array( "UserField" => $this->ShowLanguageStringList(), (!$this->GetSetting('CloseTab') ? "ErrorDiv_" . $ResultID : "") => $this->PrePareMSG($RV, $ResultID), "JSCRIPT:()" => ($this->GetSetting('CloseTab') ? "DeletePageTab('" . $ResultID . "', true);" : "UpdatePageTab('" . $ResultID . "', 'DisplayName', '" . $this->GetSetting('IDString') . "');"));
		}
	}
	
	/**
	 * ShowLanguageStringList function
	 */
	function ShowLanguageStringList($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("", "LanguageString_" . $this->GetSetting('P'), ($inDiv !== FALSE ? "UserField" : FALSE), $Start, $Filter, "LanguageStrings_");				 
		$List->EnableRowHighlighting = TRUE;
		$List->AddListAction($addimg, "#", "ModuleXMLRequest('EditLanguageString', '&AddLanguageString=1'); return false;", "", Language('Add Language String'));
		$List->AddColumn(Language('IDString'), "IDString");
		$List->AddAction("Edit", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$List->AddAction("Delete", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		$Query = "SELECT 
								SQL_CALC_FOUND_ROWS 
								LanguageStringID, 
								IDString 
							FROM 
								`LanguageString` 
								" . ($List->Filter['IDString'] ? "WHERE " : "") . "
								" . ($List->Filter['IDString'] ? "IDString LIKE " . NitroPrepareDB("%" . $List->Filter['IDString'] . "%") . " " : "") . "
							ORDER 
							BY 
								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 = "LanguageStringList";

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