Index: trunk/workflow/inc/class.so_userinterface.inc.php
===================================================================
--- trunk/workflow/inc/class.so_userinterface.inc.php	(revision 2056)
+++ trunk/workflow/inc/class.so_userinterface.inc.php	(revision 2085)
@@ -343,5 +343,5 @@
 
 	/**
-	 * Search Employee
+	 * Search Employee by Name
 	 * @param int $searchTerm term to search
 	 * @param int $organizationID  Id of organization
@@ -349,11 +349,7 @@
 	 * @access public
 	 */
-	function searchEmployee($searchTerm, $organizationID)
-	{
-		/* initialize some variables */
-		$output = array('employees' => array(), 'bygroup' => array( ) );
-		$ldapResult = array();
-
-		/* search for the $searchTerm in the LDAP */
+	function searchEmployeeByName($searchTerm, $organizationID)
+	{
+		/* get ldap connection */
 		$ldap = &$GLOBALS['workflow']['workflowObjects']->getLDAP();
 
@@ -361,85 +357,209 @@
 		$fullSearch = false;
 
-		if(count($searchTermExploded) > 0){
-			for($i=1; $i<count($searchTermExploded); $i++){
-				if(strlen($searchTermExploded[$i]) > 2){
+		if (count($searchTermExploded) > 0){
+			for ($i=1; $i<count($searchTermExploded); $i++) {
+				if (strlen($searchTermExploded[$i]) > 2) {
 					$fullSearch = true;
 				}
 			}
 
-			if($fullSearch){
+			if ($fullSearch){
 				$searchTerm = implode("*", $searchTermExploded);
 			}
 		}
 
+		/* searching employees by name in the ldap server */
 		$list = @ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(cn=*' . $searchTerm . '*)(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber'));
 		if ($list === false)
 			return false;
 
+		/* parsing ldap result */
 		$entries = ldap_get_entries($ldap, $list);
-		$employeesID = array();
+		$ldapResult = array();
+
 		for ($i = 0; $i < $entries['count']; $i++)
 			$ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]);
 
+		/* no records found. bye. */
+		if (count($ldapResult) == 0)
+			return array();
+
 		$uids = implode( ',', array_keys( $ldapResult ) );
 
-		/* load employee information */
-		$query = 'SELECT f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area';
-		$query .= ' FROM funcionario f, area a, funcionario_status s';
-		$query .= " WHERE (f.area_id = a.area_id) AND (f.organizacao_id = {$organizationID})";
-		$query .= " AND (" . ( ( ! empty( $uids ) ) ? "f.funcionario_id IN ({$uids}) OR " : '' );
-		$query .= " a.sigla LIKE UPPER(?))";
-		$query .= " AND (f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')";
-		$query .= " ORDER BY a.sigla asc";
-
-		$result = $this->db->query($query, array( "%{$searchTerm}%" ) );
-
-		if ( ! $result )
-			return array( );
-
-		$uids = array( );
-
-		$employees = $result->GetArray(-1);
-		for ($i = 0; $i < count($employees); $i++)
-		{
-			for ($j = 0; $j < $result->_numOfFields; $j++)
-				unset($employees[$i][$j]);
-
-			if ( array_key_exists( $employees[$i]['funcionario_id'], $ldapResult ) )
-			{
-				$employees[$i]['cn'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['cn']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['cn'];
-				$employees[$i]['telephoneNumber'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber'];
-			}
-			else
-			{
-				$employees[$i]['cn'] = $employees[$i]['funcionario_id'];
-				$employees[$i]['telephoneNumber'] = '';
-
-				$output['bygroup'][ $employees[$i]['funcionario_id'] ] = $employees[ $i ];
-				array_splice( $employees, $i--, 1 );
-			}
-		}
-
-		if ( count( $output['bygroup'] ) )
-		{
-			$list = ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(|(uidNumber=' . implode( ')(uidNumber=', array_keys( $output['bygroup'] ) ) . '))(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber'));
-
-			if ( $list !== false )
-			{
-				$entries = ldap_get_entries($ldap, $list);
-
-				for ( $i = 0; $i < $entries[ 'count' ]; $i++ )
-				{
-					$output[ 'bygroup' ][ $entries[$i]['uidnumber'][0] ][ 'cn' ] = $entries[$i]['cn'][0];
-					$output[ 'bygroup' ][ $entries[$i]['uidnumber'][0] ][ 'telephoneNumber' ] = $entries[$i]['telephonenumber'][0];
-				}
-			}
-		}
-
-		$output[ 'bygroup' ] = array_values( $output[ 'bygroup' ] );
-		$output[ 'employees' ] = $employees;
-
-		return $output;
-	}
+		/* searching for aditional employee information */
+		$query  = "SELECT ";
+		$query .= "		f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area ";
+		$query .= " FROM ";
+		$query .= "			funcionario f ";
+		$query .= "		INNER JOIN ";
+		$query .= "			area a USING (area_id) ";
+		$query .= "		INNER JOIN ";
+		$query .= "			funcionario_status s ON ((f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')) ";
+		$query .= " WHERE ";
+		$query .= "		(f.organizacao_id = ?) ";
+		$query .= " 	AND ";
+		$query .= "		(f.funcionario_id IN ({$uids})) ";
+
+		$result = $this->db->query($query, array($organizationID))->GetArray(-1);
+		$employees = array();
+
+		/* filling return array with employee's information */
+		for ($i = 0; $i < count($result); $i++) {
+			$employees []= array(
+					'area'	 			=> $result[$i]['area'],
+					'area_id' 			=> $result[$i]['area_id'],
+					'funcionario_id'	=> $result[$i]['funcionario_id'],
+					'cn'			 	=> $ldapResult[$result[$i]['funcionario_id']]['cn'],
+					'telephoneNumber'	=> empty($ldapResult[$result[$i]['funcionario_id']]['telephoneNumber']) ? '': $ldapResult[$result[$i]['funcionario_id']]['telephoneNumber']
+			);
+		}
+
+		/* sorting by name (cn) */
+        $sort_function = create_function('$a, $b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);');
+        usort($employees, $sort_function );
+
+		return $employees;
+	}
+
+	/**
+	 * Search Employee by Area
+	 * @param int $searchTerm term to search
+	 * @param int $organizationID  Id of organization
+	 * @return array employee data information
+	 * @access public
+	 */
+	function searchEmployeeByArea($searchTerm, $organizationID)
+	{
+		/* get ldap connection */
+		$ldap = &$GLOBALS['workflow']['workflowObjects']->getLDAP();
+
+		/* makes no sense search for an area if the string has more than one word */
+		if (count(explode(" ", $searchTerm)) > 1)
+			return array();
+
+		/* searching for employees in areas that match 'searchTerm' */
+		$query  = "SELECT ";
+		$query .= "		f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area ";
+		$query .= " FROM ";
+		$query .= "			funcionario f ";
+		$query .= "		INNER JOIN ";
+		$query .= "			area a USING (area_id) ";
+		$query .= "		INNER JOIN ";
+		$query .= "			funcionario_status s ON ((f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')) ";
+		$query .= " WHERE ";
+		$query .= "		(f.organizacao_id = ?) ";
+		$query .= " 	AND ";
+		$query .= "		(UPPER(a.sigla) LIKE UPPER(?)) ";
+
+		$result = $this->db->query($query, array($organizationID, '%'.$searchTerm.'%'))->GetArray(-1);
+	
+		/* no records found. bye */
+		if (count($result) == 0)
+			return array();
+
+		/* creating the ldap query */
+		$ldap_query = '(&(|';
+		for ($i = 0; $i < count($result); $i++) {
+			$ldap_query .= '(uidNumber=' . $result[$i]['funcionario_id'] . ')';
+		}
+		$ldap_query .= ')(phpgwAccountType=u))';
+
+		/* executing it */
+		$list = @ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), $ldap_query, array('uidNumber', 'cn', 'telephoneNumber'));
+		$entries = ldap_get_entries($ldap, $list);
+
+		/* parsing result */
+		$ldapResult = array();
+		for ($i = 0; $i < $entries['count']; $i++)
+			$ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]);
+
+		/* filling return array with employee's information */
+		$employees = array();
+		for ($i = 0; $i < count($result); $i++) {
+			$employees []= array(
+					'area'	 			=> $result[$i]['area'],
+					'area_id'		 	=> $result[$i]['area_id'],
+					'funcionario_id' 	=> $result[$i]['funcionario_id'],
+					'cn'				=> empty($ldapResult[$result[$i]['funcionario_id']]['cn']) ? $result[$i]['funcionario_id'] : $ldapResult[$result[$i]['funcionario_id']]['cn'],
+					'telephoneNumber'	=> empty($ldapResult[$result[$i]['funcionario_id']]['telephoneNumber']) ? '': $ldapResult[$result[$i]['funcionario_id']]['telephoneNumber']
+			);
+		}
+
+		/* sorting by name (cn) */
+        $sort_function = create_function('$a, $b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);');
+        usort($employees, $sort_function );
+
+		return $employees;
+	}
+
+	/**
+	 * Search Employee by Telephone
+	 * @param int $searchTerm term to search
+	 * @param int $organizationID  Id of organization
+	 * @return array employee data information
+	 * @access public
+	 */
+	function searchEmployeeByTelephone($searchTerm, $organizationID)
+	{
+		/* we will just excute it if we just get numbers and '-' */
+		if (!preg_match('/^[0-9-]+$/', $searchTerm))
+			return array();
+
+		/* get ldap connection */
+		$ldap = &$GLOBALS['workflow']['workflowObjects']->getLDAP();
+
+		/* searching employees by telephoneNumber in the ldap server */
+		$list = @ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(telephoneNumber=*' . $searchTerm . '*)(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber'));
+
+		if (!$list) return false;
+
+		/* parsing ldap result */
+		$entries = ldap_get_entries($ldap, $list);
+		$ldapResult = array();
+
+		for ($i = 0; $i < $entries['count']; $i++)
+			$ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]);
+
+		/* no records found. bye. */
+		if (count($ldapResult) == 0)
+			return array();
+
+		$uids = implode( ',', array_keys( $ldapResult ) );
+
+		/* searching for aditional employee information */
+		$query  = "SELECT ";
+		$query .= "		f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area ";
+		$query .= " FROM ";
+		$query .= "			funcionario f ";
+		$query .= "		INNER JOIN ";
+		$query .= "			area a USING (area_id) ";
+		$query .= "		INNER JOIN ";
+		$query .= "			funcionario_status s ON ((f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')) ";
+		$query .= " WHERE ";
+		$query .= "		(f.organizacao_id = ?) ";
+		$query .= " 	AND ";
+		$query .= "		(f.funcionario_id IN ({$uids})) ";
+
+		$result = $this->db->query($query, array($organizationID))->GetArray(-1);
+		$employees = array();
+
+		/* filling return array with employee's information */
+		for ($i = 0; $i < count($result); $i++) {
+			$employees []= array(
+					'area'	 			=> $result[$i]['area'],
+					'area_id'	 		=> $result[$i]['area_id'],
+					'funcionario_id'	=> $result[$i]['funcionario_id'],
+					'cn'			 	=> $ldapResult[$result[$i]['funcionario_id']]['cn'],
+					'telephoneNumber'	=> empty($ldapResult[$result[$i]['funcionario_id']]['telephoneNumber']) ? '': $ldapResult[$result[$i]['funcionario_id']]['telephoneNumber']
+			);
+		}
+
+		/* sorting by name (cn) */
+        $sort_function = create_function('$a, $b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);');
+        usort($employees, $sort_function );
+
+		return $employees;
+	}
+
 
 	/**
Index: trunk/workflow/inc/class.bo_userinterface.inc.php
===================================================================
--- trunk/workflow/inc/class.bo_userinterface.inc.php	(revision 1470)
+++ trunk/workflow/inc/class.bo_userinterface.inc.php	(revision 2085)
@@ -927,14 +927,18 @@
 			return $checkWarnings;
 
-		$result = $this->so->searchEmployee($params['searchTerm'], $this->organizationInfo['organizacao_id']);
-		$this->disconnect_all();
-
-		if ($result === false)
+		$result = array();
+
+		/* do the search */
+		$result['bytelephone'] = $this->so->searchEmployeeByTelephone($params['searchTerm'], $this->organizationInfo['organizacao_id']);
+		$result['employees'] = $this->so->searchEmployeeByName($params['searchTerm'], $this->organizationInfo['organizacao_id']);
+		$result['bygroup'] = $this->so->searchEmployeeByArea($params['searchTerm'], $this->organizationInfo['organizacao_id']);
+
+		$this->disconnect_all();
+
+		/* if all searches returned false */
+		if (!is_array($result['employees']) and
+			!is_array($result['bygroup']) and
+			!is_array($result['bytelephone']))
 			return array('error' => 'O sistema de busca não pode ser utilizado para sua organização');
-
-		$sort_function = create_function('$a,$b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);');
-
-		usort($result['employees'], $sort_function );
-		usort($result['bygroup'], $sort_function );
 
 		return $result;
Index: trunk/workflow/js/userinterface/orgchart.js
===================================================================
--- trunk/workflow/js/userinterface/orgchart.js	(revision 2042)
+++ trunk/workflow/js/userinterface/orgchart.js	(revision 2085)
@@ -45,5 +45,5 @@
 	content += '</select></a></li>';
 	content += '<li><a href="#" onclick="window.open(\'' + enderecoImagem + '\', \'extwindow\'); return false;"><img src="templateFile.php?file=images/Process.gif">&nbsp;&nbsp;Gráfico</a></li>';
-	content += '<li><a><input type="text" name="search_term" id="search_term" onkeypress="if (((event.which) ? event.which : event.keyCode) == 13) $(\'search_span\').onclick(); return true;" /><span id="search_span" style="cursor: pointer;" onclick="tmp = $$(\'div#orgchartAreas a.destaque\'); if (tmp[0]) tmp[0].removeClassName(\'destaque\'); orgchartSearchEmployee($F(\'search_term\')); return false;">&nbsp;busca</span></a></li>';
+	content += '<li><a><input type="text" name="search_term" id="search_term" onkeypress="if (((event.which) ? event.which : event.keyCode) == 13) $(\'search_span\').onclick(); return true;" /><span id="search_span" style="cursor: pointer;" onclick="tmp = $$(\'div#orgchartAreas a.destaque\'); if (tmp[0]) tmp[0].removeClassName(\'destaque\'); orgchartSearchEmployee($F(\'search_term\')); return false;">&nbsp;busca&nbsp;</span><img src="templateFile.php?file=images/help.png" title="Informe o nome, área ou telefone a ser pesquisado." style="cursor: help" /></a></li>';
 	content += '<li><a href="#" onclick="printArea(); return false;"><img src="templateFile.php?file=images/imprimir.png" width="16">&nbsp;&nbsp;Imprimir</a></li>';
 	content += '</ul>';
@@ -296,5 +296,7 @@
 
 	/* no employee to list */
-	if ( ( ! data['employees'] || data['employees'].length == 0 ) && ( ! data['bygroup'] || data['bygroup'].length == 0 ) )
+	if ( ( ! data['employees'] || data['employees'].length == 0 ) && 
+		 ( ! data['bygroup'] || data['bygroup'].length == 0 ) &&
+		 ( ! data['bytelephone'] || data['bytelephone'].length == 0 ) )
 	{
 		div.innerHTML = '<br/><br/><center><strong>Nenhum registro encontrado</strong></center>';
@@ -479,4 +481,5 @@
 			div.innerHTML += '</center><span style="color:red">Busca pelo nome: ' + searchTerm.toUpperCase( ) + '</span>' + content;
 
+		// printing records found by group
 		if ( data['bygroup'] && data['bygroup'].length )
 		{
@@ -486,4 +489,14 @@
 			if ( content )
 				div.innerHTML += '<br/><br/><span style="color:red">Busca pelo setor: ' + searchTerm.toUpperCase( ) + '</span><br/>' + content;
+		}
+
+		// printing records found by telephoneNumber
+		if ( data['bytelephone'] && data['bytelephone'].length )
+		{
+			employees = [ ];
+			employees[ 'employees' ] = data['bytelephone'];
+			content = printEmployeesHandler( employees, 'returnResult' );
+			if ( content )
+				div.innerHTML += '<br/><br/><span style="color:red">Busca pelo telefone: ' + searchTerm.toUpperCase( ) + '</span><br/>' + content;
 		}
 
