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

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

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

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