Index: trunk/jabberit_messenger/inc/class.db_im.inc.php
===================================================================
--- trunk/jabberit_messenger/inc/class.db_im.inc.php	(revision 896)
+++ trunk/jabberit_messenger/inc/class.db_im.inc.php	(revision 946)
@@ -14,4 +14,5 @@
 define('PHPGW_API_INC','../phpgwapi/inc');
 require_once(PHPGW_API_INC . '/class.db.inc.php');
+require_once "class.fileDefine.inc.php";
 	
 class db_im
@@ -25,7 +26,9 @@
 	private $db_type;
 	private $user_id;
+	private $fileD;
 	
 	public final function __construct()
 	{
+		$this->fileD = new fileDefine();
 		$this->db_name = $_SESSION['phpgw_info']['jabberit_messenger']['server']['db_name'];
 		$this->db_host = $_SESSION['phpgw_info']['jabberit_messenger']['server']['db_host'];
@@ -51,4 +54,24 @@
 	}	
 
+	public final function editHostJabber($pItem)
+	{
+		$hostsJabber = unserialize($this->getHostsJabber());
+		$findHosts	= explode(":", $pItem['item']);
+		$return = "";	
+		
+		for( $i = 0 ; $i < count($hostsJabber); $i++ )
+			if( $hostsJabber[$i]['org'] == $findHosts[0] && $hostsJabber[$i]['jabberName'] == $findHosts[1] )
+			{
+				$return = "org:" . $hostsJabber[$i]['org'] . ";" .
+						  "jabberName:" . $hostsJabber[$i]['jabberName'] . ";" .						  	 
+						  "serverLdap:" . $hostsJabber[$i]['serverLdap'] . ";" .
+						  "contextLdap:" . $hostsJabber[$i]['contextLdap'] . ";" .
+						  "user:" . $hostsJabber[$i]['user'] . ";" .
+						  "password:" . $hostsJabber[$i]['password'] ;						  						  						  						  
+			}
+
+		return trim($return);
+	}
+	
 	public final function getApplicationsEnabled()
 	{
@@ -338,57 +361,63 @@
 	public final function setHostJabber($pParam)
 	{
-		$organization	= $pParam['organization'];
-		$hostJabber		= $pParam['hostJabber'];
-	
+		$confHostsJabber =  array();
+
+		foreach($pParam as $key => $itens)
+			$confHostsJabber[$key] = ( $key === 'org' ) ? strtoupper($itens) : $itens;
+
 		if( $this->db )
 		{
 			$query = "SELECT * FROM phpgw_config WHERE config_app = 'phpgwapi' AND config_name = 'map_org_realm_jabberit';";
 			
-			if($this->db->query($query))
-			{
-			
-				if ( $this->db->query($query) )
-				{	
-					while($this->db->next_record())
-						$result[] = $this->db->row();
-				}
-				
-				if( count($result) == 0 )
-				{
-					$confHostJabber = strtoupper($organization).":".$hostJabber;
-					$return = "<return><confServer ou='".strtoupper($organization)."' serverName='".$hostJabber."'>".$confHostJabber."</confServer></return>";
-					$query = "INSERT INTO phpgw_config(config_app,config_name, config_value) VALUES('phpgwapi','map_org_realm_jabberit','".serialize($confHostJabber)."')";
-					$_SESSION['debug']['gravar'] = $confHostJabber;
-					$this->db->query($query);					
-				}
+			if( $this->db->query($query) )
+			{
+				while($this->db->next_record())
+					$result[] = $this->db->row();				
+			}
+			
+			if( count($result) == 0 )
+			{
+				$return = "<return><confServer ou='".strtoupper($confHostsJabber['org'])."' serverName='".$confHostsJabber['jabberName']."'>".strtoupper($confHostsJabber['org']).":".$confHostsJabber['jabberName']."</confServer></return>";				
+				$hostsJabber[0] = $confHostsJabber;
+				
+				$this->fileD->ldapExternal($hostsJabber);
+				 
+				$query = "INSERT INTO phpgw_config(config_app, config_name, config_value) VALUES('phpgwapi','map_org_realm_jabberit','".serialize($hostsJabber)."')";
+				$this->db->query($query);				
+			}
+			else
+			{
+				$resultQuery = unserialize($result[0]['config_value']);	
+				$foundOrg = false;
+				
+				foreach($resultQuery as $key => $itens)
+				{
+					$foundString = array_search($confHostsJabber['org'], $itens);
+					if( $foundString )
+					{
+						$foundOrg = $foundString;
+						$ky = $key;
+					}
+				}	
+
+				if( ! $foundOrg )
+					$resultQuery[] = $confHostsJabber;	
 				else
-				{
-					$confHostJabber = unserialize($result[0]['config_value']);
-					$OrgHosts = explode(";", $confHostJabber);
-					$stringSearch = strtoupper($organization).":".$hostJabber;
-					$FoundString = array_search($stringSearch, $OrgHosts);
-
-					if( $FoundString === false)
-						$confHostJabber .= ";" . $stringSearch;
-				
-					$items = explode(";", $confHostJabber);
-					natcasesort($items);
-											
-					$return = "<return>";
-					foreach($items as $tmp)
-					{
-						$aux = explode(":",$tmp);
-						$return .= "<confServer ou='".$aux[0]."' serverName='".$aux[1]."'>".$tmp."</confServer>";
-					}					
-					$return .= "</return>";
-
-					$query = "UPDATE phpgw_config SET config_value = '".serialize($confHostJabber)."' WHERE config_name = 'map_org_realm_jabberit';";
-					$this->db->query($query);
-				}
-				
-				return $return;
-			}
-		}
-
+					$resultQuery[$ky] = $confHostsJabber;
+
+				$return = "<return>";
+				
+				foreach( $resultQuery as $itens )
+					$return .= "<confServer ou='".$itens['org']."' serverName='".$itens['jabberName']."'>".$itens['org'].":".$itens['jabberName']."</confServer>";
+				
+				$return .= "</return>";
+				
+				$this->fileD->ldapExternal($resultQuery);
+				
+				$query = "UPDATE phpgw_config SET config_value = '".serialize($resultQuery)."' WHERE config_name = 'map_org_realm_jabberit';";
+				$this->db->query($query);
+			}
+			return $return;
+		}	
 		return false;
 	}
@@ -466,90 +495,4 @@
 	}
 	
-	public final function setUseParticipantsExternal($pFlag)
-	{
-		$flag = $pFlag['value'];
-		$return = "";
-		
-		if( $this->db )
-		{
-			$query = "SELECT * FROM phpgw_config WHERE config_app = 'phpgwapi' AND config_name = 'use_external_participants_jabberit';";
-
-			if ( $this->db->query($query) )
-			{	
-				while($this->db->next_record())
-					$result[] = $this->db->row();
-			}
-
-			if(count($result) == 0)
-			{
-				$query = "INSERT INTO phpgw_config(config_app,config_name,config_value) VALUES('phpgwapi','use_external_participants_jabberit','".$flag."')";
-				$this->db->query($query);
-				return true;
-			}
-			else
-			{
-				$query = "UPDATE phpgw_config SET config_value = '".$flag."' WHERE config_app = 'phpgwapi' AND config_name = 'use_external_participants_jabberit'";
-				$this->db->query($query);
-				return true;
-			}
-		}
-		return false;
-	}
-	
-	public final function setOrganization($pOrganization)
-	{
-		$organization = $pOrganization['organization'];
-		$orgConfiguration = "";
-		
-		if( $this->db )
-		{
-			$query = "SELECT * from phpgw_config WHERE config_app = 'phpgwapi' and config_name = 'conf_organization_jabberit'";
-
-			if ( $this->db->query($query) )
-			{	
-				while($this->db->next_record())
-					$result[] = $this->db->row();
-		
-				if(count($result) > 0)
-					$orgConfiguration = $result[0]['config_value'];
-			}
-
-			if( trim($orgConfiguration) == "" )
-			{
-				$query = "INSERT INTO phpgw_config(config_app,config_name,config_value) VALUES('phpgwapi','conf_organization_jabberit','".$organization."')";
-				$this->db->query($query);
-				
-				return "<return><ou attr='".$organization."'>".$organization."</ou></return>";
-			}
-			else
-			{
-				$orgAux = explode(",",$orgConfiguration);
-				
-				foreach( $orgAux as $tmp )
-				{
-					if(strtolower(trim($tmp)) === strtolower(trim($organization)))
-						return false;
-				}
-
-				$value = $orgConfiguration . "," . $organization;
-				$query = "UPDATE phpgw_config SET config_value = '".$value."' WHERE config_app = 'phpgwapi' AND config_name = 'conf_organization_jabberit'";
-				$this->db->query($query);
-
-				$return = explode(",",$value);
-				natcasesort($return);
-
-				$values = "<return>";
-				
-				foreach($return as $tmp)
-					$values .= "<ou attr='" . $tmp . "'>" . $tmp . "</ou>";
-					
-				$values .= "</return>";
-				
-				return $values;			
-			}
-		}
-		return false; 
-	}
-
 	public final function removeAttributesLdap($pOrg)
 	{
@@ -610,21 +553,32 @@
 				{
 					$confHostsOrgs = unserialize($result[0]['config_value']);
-					$confHostsOrgs = explode(";", $confHostsOrgs);					
-					$foundValue = array_search($pItem['item'], $confHostsOrgs);					
-	
-					if( $foundValue !== false )
-						array_splice($confHostsOrgs, $foundValue, 1);
-					
-					if(count($confHostsOrgs) > 0 )
+					$hosts = explode(":", $pItem['item']);
+					$key = "";
+
+					if( count($confHostsOrgs) > 0 )
 					{
-						foreach( $confHostsOrgs as $values )
-							$stringHostsOrgs .= $values . ";";
-	
-						$stringHostsOrgs = substr($stringHostsOrgs, 0, strlen($stringHostsOrgs) -1 );
-						$query = "UPDATE phpgw_config SET config_value = '".serialize($stringHostsOrgs)."' WHERE config_name = 'map_org_realm_jabberit';";
+						for( $i = 0; $i < count($confHostsOrgs); $i++)
+							if( $confHostsOrgs[$i]['org'] == $hosts[0] && $confHostsOrgs[$i]['jabberName'] == $hosts[1])
+								$key = $i;	
+
+						array_splice($confHostsOrgs, $key, 1);
+				
+						if(count($confHostsOrgs) > 0)
+						{					
+							$this->fileD->ldapExternal($confHostsOrgs);
+							$query = "UPDATE phpgw_config SET config_value = '".serialize($confHostsOrgs)."' WHERE config_name = 'map_org_realm_jabberit';";
+						}
+						else
+						{
+							$this->fileD->ldapExternal("");
+							$query = "DELETE FROM phpgw_config WHERE config_name = 'map_org_realm_jabberit';";
+						}
 					}
 					else
+					{	
+						$this->fileD->ldapExternal("");						
 						$query = "DELETE FROM phpgw_config WHERE config_name = 'map_org_realm_jabberit';";
-					
+					}
+
 					if( $this->db->query($query) )
 						return "true";
@@ -692,46 +646,5 @@
 		return $return;
 	}
-	
-	public final function removeParticipantsExternal($pOrganization)
-	{
-		
-		$organization = $pOrganization['participants'];	
-		
-		if( $this->db )
-		{
-			$query = "SELECT * from phpgw_config WHERE config_app = 'phpgwapi' and config_name = 'conf_organization_jabberit'";
-				
-			if ( $this->db->query($query) )
-			{	
-				while( $this->db->next_record() )
-					$result[] = $this->db->row();
-		
-				if( count($result) > 0 )
-					$valueDB = $result[0]['config_value'];
-			}
-
-			$OrgDB = explode(",", $valueDB);
-			$newValue = ""; 
-
-			foreach($OrgDB as $tmp)
-			{
-				if( strtolower(trim($tmp)) != strtolower(trim($organization)))
-					$newValue .= $tmp . ",";
-			}
-			
-			$newValue = substr($newValue, 0,(strlen($newValue) -1 ));
-			
-			if( trim($newValue) != "")
-				$query = "UPDATE phpgw_config SET config_value = '".$newValue."' WHERE config_app = 'phpgwapi' AND config_name = 'conf_organization_jabberit'";
-			else
-				$query = "DELETE from phpgw_config where config_name = 'conf_organization_jabberit'";
-				
-			if( $this->db->query($query))
-				return true;
-			else
-				return false;
-		}
-		return false;	
-	}
+
 }
 ?>
