Index: trunk/prototype/modules/calendar/interceptors/DBMapping.php
===================================================================
--- trunk/prototype/modules/calendar/interceptors/DBMapping.php	(revision 8045)
+++ trunk/prototype/modules/calendar/interceptors/DBMapping.php	(revision 8058)
@@ -1634,26 +1634,64 @@
     //TODO - Criar conceito separado para participantes externos e remover o criterio notExternal
     public function findExternalPaticipants(&$uri, &$result, &$criteria, $original) {
-	if (Config::me('uidNumber') && !isset($criteria['notExternal'])) {
-	    $newuri['concept'] = 'user';
-	    $newuri['service'] = 'PostgreSQL';
-
-	    $newCriteria = $original['criteria'];
-	    $valid = true;
-
-	    $newCriteria['filter'] = array('AND', $newCriteria['filter'], array('=', 'owner', Config::me('uidNumber')));
-	    $externalUsers = Controller::find($newuri, $original['properties'] ? $original['properties'] : null, $newCriteria);
-
-	    if (is_array($externalUsers)) {
-		foreach ($externalUsers as $i => $v)
-		    $externalUsers[$i]['isExternal'] = '1';
-	    }
-	    else
-		$externalUsers = array();
-
-	    if (!is_array($result))
-		$result = array();
-
-	    return array_merge($result, $externalUsers);
-	}
+        if (Config::me('uidNumber') && !isset($criteria['notExternal'])) {
+            $newuri['concept'] = 'user';
+            $newuri['service'] = 'PostgreSQL';
+
+            $newCriteria = $original['criteria'];
+            $valid = true;
+
+            $newCriteria['filter'] = array('AND', $newCriteria['filter'], array('=', 'owner', Config::me('uidNumber')));
+            $externalUsers = Controller::find($newuri, $original['properties'] ? $original['properties'] : null, $newCriteria );
+
+            if (!is_array($result))
+                $result = array();
+
+            if (is_array($externalUsers)) {
+                foreach ($externalUsers as $i => $v)
+                    $externalUsers[$i]['isExternal'] = '1';
+            }
+            else
+                $externalUsers = array();
+
+            $result = array_merge($result, $externalUsers);
+
+
+            if(isset($original['criteria']['externalCatalogs']) &&  $original['criteria']['externalCatalogs'] == true)
+            {
+                $externalCatalogs = self::findExternalCatalogContacts($original['criteria']['filter'][2]);
+
+                foreach($externalCatalogs as $i => $v)
+                {
+                    $exist = false;
+                    foreach($result as $vv)
+                    {
+                        if($v['mail'] == $vv['mail'] )
+                            $exist = true;
+                    }
+                    if(!$exist)
+                        $result[] = $v;
+                }
+            }
+
+
+            if(isset($original['criteria']['personalContacts']) &&  $original['criteria']['personalContacts'] == true)
+            {
+                $personalContacts = self::findPersonalContacts($original['criteria']['filter'][2]);
+
+                foreach($personalContacts as $i => $v)
+                {
+                    $exist = false;
+                    foreach($result as $vv)
+                    {
+                        if($v['mail'] == $vv['mail'] )
+                            $exist = true;
+                    }
+                    if(!$exist)
+                        $result[] = $v;
+                }
+            }
+
+            return $result ;
+        }
     }
 
@@ -1774,4 +1812,245 @@
             && ($timestamp >= ~PHP_INT_MAX);
     }
+
+    static function findExternalCatalogContacts( $search )
+    {
+            $result = array();
+            $external_srcs = array();
+            $external_mappings = array();
+
+            include_once dirname(__DIR__) .'/../../../contactcenter/setup/external_catalogs.inc.php';
+
+            $search = str_replace(' ', '*', $search) ;
+            $search = '*' . $search . '*';
+
+            foreach($external_srcs as $i => $v)
+            {
+                $con = ldap_connect($v['host']);
+
+                ldap_set_option( $con , LDAP_OPT_PROTOCOL_VERSION , 3 );
+
+                if( isset( $v['acc'] ) && isset( $v['pw'] ) )
+                    ldap_bind( $con, $v['acc'], $v['pw'] );
+
+                $fields = array();
+                $fields[] = $external_mappings[$i]['contact.names_ordered'][0];
+                $fields[] = $external_mappings[$i]['contact.connection.typeof_connection.contact_connection_type_name']['email'][0];
+
+                $ldapFilter = '(&(objectClass='.$v['obj'].')(|('.$fields[0].'='.$search.')('.$fields[1].'='.$search.')))';
+                $sr =  ldap_search( $con, utf8_encode($v['dn']) , $ldapFilter , $fields );
+                if($sr)
+                {
+                    $search = ldap_get_entries($con, $sr);
+
+                    for ($j = 0; $j < $search["count"]; ++$j) {
+                        $tmp = array();
+                        $tmp['name'] = $search[$j][$fields[0]][0];
+                        $tmp['mail'] = $search[$j][$fields[1]][0];
+                        $tmp['isExternal'] = '1';
+
+                        $result[] = $tmp;
+                    }
+                }
+            }
+            return $result;
+    }
+
+    static function findPersonalContacts($search_for)
+    {
+
+              $query = 'select'
+
+                . ' C.id_connection,'
+                . ' A.id_contact,'
+                . ' A.names_ordered,'
+                . ' A.alias,'
+                . ' A.birthdate,'
+                . ' A.sex,'
+                . ' A.pgp_key,'
+                . ' A.notes,'
+                . ' A.web_page,'
+                . ' A.corporate_name,'
+                . ' A.job_title,'
+                . ' A.department,'
+                . ' C.connection_name,'
+                . ' C.connection_value,'
+                . ' B.id_typeof_contact_connection,'
+                . ' phpgw_cc_contact_addrs.id_typeof_contact_address,'
+                . ' phpgw_cc_addresses.address1,'
+                . ' phpgw_cc_addresses.address2,'
+                . ' phpgw_cc_addresses.complement,'
+                . ' phpgw_cc_addresses.postal_code,'
+                . ' phpgw_cc_city.city_name,'
+                . ' phpgw_cc_state.state_name,'
+                . ' phpgw_cc_addresses.id_country'
+            ;
+
+            $query .= ' from'
+                . ' phpgw_cc_contact A'
+                . ' inner join phpgw_cc_contact_conns B on ( A.id_contact = B.id_contact )'
+                . ' inner join phpgw_cc_connections C on ( B.id_connection = C.id_connection )'
+                . ' left join phpgw_cc_contact_addrs on ( A.id_contact = phpgw_cc_contact_addrs.id_contact )'
+                . ' left join phpgw_cc_addresses on ( phpgw_cc_contact_addrs.id_address = phpgw_cc_addresses.id_address )'
+                . ' left join phpgw_cc_city on ( phpgw_cc_addresses.id_city = phpgw_cc_city.id_city )'
+                . ' left join phpgw_cc_state on ( phpgw_cc_addresses.id_state = phpgw_cc_state.id_state)'
+            ;
+
+            $query .= ' where '
+                . 'A.id_owner=' . Config::me('uidNumber')
+                . ' and lower(translate(names_ordered, \'áàâãäéèêëíìïóòôõöúùûüÁÀÂÃÄÉÈÊËÍÌÏÓÒÔÕÖÚÙÛÜçÇñÑ\',\'aaaaaeeeeiiiooooouuuuAAAAAEEEEIIIOOOOOUUUUcCnN\'))'
+                . ' LIKE lower(translate(\'%' . $search_for . '%\', \'áàâãäéèêëíìïóòôõöúùûüÁÀÂÃÄÉÈÊËÍÌÏÓÒÔÕÖÚÙÛÜçÇñÑ\',\'aaaaaeeeeiiiooooouuuuAAAAAEEEEIIIOOOOOUUUUcCnN\'))';
+
+            //Se não existir parametro na busca, limita os usuarios no resultado da pesquisa.
+            if(!$search_for){
+                $query .= 'LIMIT 11';
+            }
+
+            $r = Controller::service('PostgreSQL')->execResultSql($query);
+
+
+            $all_contacts = array();
+            foreach( $r as $i => $object )
+            {
+                if ( ! array_key_exists( $object[ 'id_contact' ], $all_contacts ) )
+                    $all_contacts[ $object[ 'id_contact' ] ] = array(
+                        'connection_value' => '',
+                        'telephonenumber' => '',
+                        'mobile' => '',
+                        'cn' => '',
+                        'id_contact' => '',
+                        'id_connection' => '',
+                        'alias' => '',
+                        'birthdate' => '',
+                        'sex' => '',
+                        'pgp_key' => '',
+                        'notes' => '',
+                        'web_page' => '',
+                        'corporate_name' => '',
+                        'job_title' => '',
+                        'department' => '',
+                        'mail' => '',
+                        'aternative-mail' => '',
+                        'business-phone' => '',
+                        'business-address' => '',
+                        'business-complement' => '',
+                        'business-postal_code' => '',
+                        'business-city_name' => '',
+                        'business-state_name' => '',
+                        'business-id_country' => '',
+                        'business-fax' => '',
+                        'business-pager' => '',
+                        'business-mobile' => '',
+                        'business-address-2' => '',
+                        'home-phone' => '',
+                        'home-address' => '',
+                        'home-complement' => '',
+                        'home-postal_code' => '',
+                        'home-city_name' => '',
+                        'home-state_name' => '',
+                        'home-fax' => '',
+                        'home-pager' => '',
+                        'home-address-2' => ''
+
+
+                    );
+
+                switch( $object[ 'id_typeof_contact_connection' ] )
+                {
+                    case 1 :
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'connection_value' ] = $object[ 'connection_value' ];
+                        switch ( strtolower( $object[ 'connection_name' ] ) )
+                        {
+                            case 'alternativo' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'alternative-mail' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'principal' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'mail' ] = $object[ 'connection_value' ];
+                                break;
+                        }
+                        break;
+                    case 2 :
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'telephonenumber' ] = $object[ 'connection_value' ];
+                        switch ( strtolower( $object[ 'connection_name' ] ) )
+                        {
+                            case 'casa' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-phone' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'celular' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'mobile' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'trabalho' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-phone' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'fax' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-fax' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'pager' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'home-pager' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'celular corporativo' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-mobile' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'pager corporativo' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-pager' ] = $object[ 'connection_value' ];
+                                break;
+                            case 'fax corporativo' :
+                                $all_contacts[ $object[ 'id_contact' ] ][ 'business-fax' ] = $object[ 'connection_value' ];
+                                break;
+                        }
+                        break;
+                }
+
+                $all_contacts[ $object[ 'id_contact' ] ][ 'cn' ] = utf8_encode($object[ 'names_ordered' ]);
+                $all_contacts[ $object[ 'id_contact' ] ][ 'id_contact' ]    = $object[ 'id_contact' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'id_connection' ] = $object[ 'id_connection' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'alias' ]         = $object[ 'alias' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'birthdate' ] 	= $object[ 'birthdate' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'sex' ]    		= $object[ 'sex' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'pgp_key' ] 		= $object[ 'pgp_key' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'notes' ]         = $object[ 'notes' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'web_page' ] 		= $object[ 'web_page' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'corporate_name' ]= $object[ 'corporate_name' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'job_title' ] 	= $object[ 'job_title' ];
+                $all_contacts[ $object[ 'id_contact' ] ][ 'department' ]    = $object[ 'department' ];
+
+                switch( $object[ 'id_typeof_contact_address' ] )
+                {
+                    case 1 :
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-address' ]     = $object[ 'address1' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-address-2' ]   = $object[ 'address2' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-complement' ]  = $object[ 'complement' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-postal_code' ] = $object[ 'postal_code' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-city_name' ]   = $object[ 'city_name' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-state_name' ]  = $object[ 'state_name' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'business-id_country' ]  = $object[ 'id_country' ];
+                        break;
+                    case 2 :
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-address' ]     = $object[ 'address1' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-address-2' ]   = $object[ 'address2' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-complement' ]  = $object[ 'complement' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-postal_code' ] = $object[ 'postal_code' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-city_name' ]   = $object[ 'city_name' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-state_name' ]  = $object[ 'state_name' ];
+                        $all_contacts[ $object[ 'id_contact' ] ][ 'home-id_country' ]  = $object[ 'id_country' ];
+                        break;
+                }
+            }
+            $all = array_values($all_contacts);
+
+            $result = array();
+            foreach($all as $i => $v)
+            {
+                if(!$v['mail']) continue;
+
+                $tmp = array();
+                $tmp['mail'] = $v['mail'];
+                $tmp['name'] = $v['cn'];
+                $tmp['isExternal'] = '1';
+                $result[] = $tmp;
+            }
+
+            return $result;
+
+    }
 }
 
Index: trunk/prototype/modules/calendar/js/helpers.js
===================================================================
--- trunk/prototype/modules/calendar/js/helpers.js	(revision 8052)
+++ trunk/prototype/modules/calendar/js/helpers.js	(revision 8058)
@@ -1583,6 +1583,6 @@
 blkAddAtendee.find('.add-attendee-search input').keydown(function(event) {
 
-    if(event.keyCode == '13' || typeof(event.keyCode) == 'undefined') {			
-	var result = DataLayer.get('user', ["*", "name", $(this).val()], true);
+    if(event.keyCode == '13' || typeof(event.keyCode) == 'undefined') {
+	var result = DataLayer.get('user', { 'filter' :  ["*", "name", $(this).val()], criteria: { 'externalCatalogs' : true , 'personalContacts' : true }  }, true);
 
 	/**
@@ -1602,6 +1602,18 @@
 	    if ($(event.target).is('input')) {
 		old_item = $(event.target).parents('li');
+
+        var  userId = old_item.find('.id').html();
+
+        if(userId == '')
+        {
+           var  userId = DataLayer.put('user', {
+                name: old_item.find('.name').html(),
+                mail: old_item.find('.mail').html(),
+                isExternal: '1'
+            });
+        }
+
 		newAttendeeId = DataLayer.put('participant', {
-		    user: old_item.find('.id').html(), 
+		    user: userId,
 		    isExternal: old_item.find('.isExternal').html()
 		});
