Index: openacs-4/packages/contacts/lib/contacts.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/contacts/lib/contacts.tcl,v
diff -u -r1.68 -r1.69
--- openacs-4/packages/contacts/lib/contacts.tcl	26 Jun 2006 06:36:51 -0000	1.68
+++ openacs-4/packages/contacts/lib/contacts.tcl	8 Sep 2006 12:10:18 -0000	1.69
@@ -174,6 +174,7 @@
             }
             # set default_attr_extend [parameter::get -parameter "DefaultPersonAttributeExtension"]
 	    set party_column "persons.person_id"
+	    set item_column "persons.person_id"
 	}
 	organization { 
 	    set page_query_name "organization_pagination"
@@ -182,26 +183,29 @@
 	    }
             # set default_attr_extend [parameter::get -parameter "DefaultOrganizationAttributeExtension"]
 	    set party_column "organizations.organization_id"
+	    set item_column "organizations.organization_id"
 	}
 	party { 
 	    set page_query_name "contacts_pagination"
             # set default_attr_extend [parameter::get -parameter "DefaultPersonOrganAttributeExtension"]
 	    set party_column "parties.party_id"
+	    set item_column "parties.party_id"
 	}
         employee {
 	    set actual_object_type "organization"
-	    set party_column "acs_rels.object_id_two"
+	    set party_column "acs_rels.object_id_one"
+	    set item_column "acs_rels.object_id_two"
 	    set page_query_name "employee_pagination"
 	}
     }
     set search_clause 	[contact::search_clause -and -search_id $search_id -query $query -party_id $party_column -revision_id "cr_items.live_revision" -limit_type_p "0"]
     if { $orderby eq "last_modified,desc" } {
 	# we need the cr_items and cr_revisions table since we need the
         # cr_revisions.publish date
-	append cr_where " and $party_column = cr_items.item_id and cr_items.live_revision = cr_revisions.revision_id"
+	append cr_where " and $item_column = cr_items.item_id and cr_items.live_revision = cr_revisions.revision_id"
         append cr_from " cr_items, cr_revisions,"
     } elseif {[lsearch -exact $condition_type_list "attribute"] > -1 || [lsearch -exact $condition_type_list "contact"] > -1 } {
-	set cr_where "and cr_items.item_id = $party_column"
+	set cr_where "and cr_items.item_id = $item_column"
 	set cr_from "cr_items,"
     } else {
 	# We don't need to search for attributes so we don't need to join
@@ -223,6 +227,7 @@
     set actual_object_type "party"
     set page_query_name "contacts_pagination"
     set search_clause "[contact::search_clause -and -query $query -search_id "" -party_id "parties.party_id" -limit_type_p "0"]"
+
     if { $orderby eq "last_modified,desc" } {
 	set cr_from "cr_items, cr_revisions,"
 	set cr_where "and parties.party_id = cr_items.item_id and cr_items.live_revision = cr_revisions.revision_id"
Index: openacs-4/packages/contacts/tcl/contact-search-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/contacts/tcl/contact-search-procs.tcl,v
diff -u -r1.36 -r1.37
--- openacs-4/packages/contacts/tcl/contact-search-procs.tcl	30 Jun 2006 16:00:08 -0000	1.36
+++ openacs-4/packages/contacts/tcl/contact-search-procs.tcl	8 Sep 2006 12:10:19 -0000	1.37
@@ -248,25 +248,35 @@
     if { [exists_and_not_null search_id] } {
 	# Get the results depening on the object_type
 	set object_type [db_string get_object_type {} -default "party"]
+
+	# The party column is the column of the object we look for
+	# The item column is the column of the item which has the attributes
+	# This allows to search for the attributes of an organization, but have the party
+	# in a special search (employee search)
+	
 	switch $object_type {
 	    party { 
 		set party_column "parties.party_id"
+		set item_column "parties.party_id"
 	    }
 	    organization {
 		set party_column "organizations.organization_id"
+		set item_column "organizations.organization_id"
 	    } 
 	    person {
 		set party_column "persons.person_id"
+		set item_column "persons.person_id"
 	    } 
             employee {
-		set party_column "acs_rels.object_id_two"
+		set party_column "acs_rels.object_id_one"
+		set item_column "acs_rels.object_id_two"
 	    }
 	}
 	set search_clause [contact::search_clause -and -search_id $search_id -query $query -party_id $party_column -revision_id "cr_items.live_revision" -limit_type_p "0"]
 	
 	set condition_types [db_list get_condition_types {}]
 	if { [lsearch -exact $condition_types "attribute"] > -1 || [lsearch -exact $condition_types "contact"] > -1 } {
-	    set cr_where "and cr_items.item_id = $party_column"
+	    set cr_where "and cr_items.item_id = $item_column"
 	    set cr_from "cr_items,"
 	} else {
 	    # We don't need to search for attributes so we don't need to join
@@ -302,38 +312,77 @@
 }
 
 ad_proc -private contact::party_id_in_sub_search_clause {
-    {-search_id}
+    {-search_id:required}
     {-party_id "party_id"}
     {-not:boolean}
+    {-package_id ""}
 } {
 } {
-    # If we do not have a search_id, limit the list to only users in the default group.
+
+    if { $package_id eq ""} {
+	set package_id [ad_conn package_id]
+    }
+
+    # Get the results depening on the object_type
+    set object_type [db_string get_object_type {} -default "party"]
     
-    if {[exists_and_not_null search_id]} {
-        set group_where_clause ""
+    # The party column is the column of the object we look for
+    # The item column is the column of the item which has the attributes
+    # This allows to search for the attributes of an organization, but have the party
+    # in a special search (employee search)
+    
+    switch $object_type {
+	party { 
+	    set party_column "parties.party_id"
+	    set item_column "parties.party_id"
+	}
+	organization {
+	    set party_column "organizations.organization_id"
+	    set item_column "organizations.organization_id"
+	} 
+	person {
+	    set party_column "persons.person_id"
+	    set item_column "persons.person_id"
+	} 
+	employee {
+	    set party_column "acs_rels.object_id_one"
+	    set item_column "acs_rels.object_id_two"
+	}
+    }
+    set search_clause [contact::search_clause -and -search_id $search_id -party_id $party_column -revision_id "cr_items.live_revision" -limit_type_p "0"]
+    
+    set condition_types [db_list get_condition_types {}]
+    if { [lsearch -exact $condition_types "attribute"] > -1 || [lsearch -exact $condition_types "contact"] > -1 } {
+	set cr_where "and cr_items.item_id = $item_column"
+	set cr_from "cr_items,"
     } else {
-        set group_where_clause "and group_distinct_member_map.group_id in ([template::util::tcl_to_sql_list [contacts::default_groups]])"
-#        set group_where_clause "and group_distinct_member_map.group_id = [contacts::default_group]"
+	# We don't need to search for attributes so we don't need to join
+	# on the cr_items table. This should speed things up. This assumes
+	# that packages other than contacts that add search condition
+	# types do not need the revision_id column, and only needs the
+	# party_id column. If this is not the case we may want to add a
+	# callback here to check if another package needs the revisions 
+	# table.
+	#
+	# If this needs to change you should also update the
+	# contacts/lib/contacts.tcl file which behave the same way.
+	set cr_where ""
+	set cr_from ""
     }
 
-    set query "
-    select parties.party_id
-      from parties left join cr_items on (parties.party_id = cr_items.item_id) left join cr_revisions on (cr_items.latest_revision = cr_revisions.revision_id ),
-           group_distinct_member_map
-     where parties.party_id = group_distinct_member_map.member_id
-     $group_where_clause
-    [contact::search_clause -and -search_id $search_id -query "" -party_id "parties.party_id" -revision_id "revision_id"]
-    "
-
-
-
-
+    set results ""
+#    if { [catch {
+	set query [db_list select_${object_type} {}]
+#    } errmsg] } {
+#	ns_log Error "contact::search::results_count_not_cached contact search $search_id had a problem \n\n$errmsg"
+#    }
+    
     if { [exists_and_not_null query] } {
         set result ${party_id}
         if { $not_p } {
             append result " not"
         }
-        append result " in ( $query )"
+        append result " in ( [template::util::tcl_to_sql_list $query] )"
     } else {
         set result ""
     } 
@@ -462,7 +511,6 @@
 
     set query_clauses [list]
     set callback_query_clauses [callback contact::search::query_clauses -query $query -party_id $party_id]
-
     if { [llength $callback_query_clauses] > 0 } {
 	# the callback returns a list of the lists from the callbacks
 	foreach callback_clauses $callback_query_clauses {
Index: openacs-4/packages/contacts/tcl/contact-search-procs.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/contacts/tcl/contact-search-procs.xql,v
diff -u -r1.20 -r1.21
--- openacs-4/packages/contacts/tcl/contact-search-procs.xql	26 Jun 2006 07:02:39 -0000	1.20
+++ openacs-4/packages/contacts/tcl/contact-search-procs.xql	8 Sep 2006 12:10:19 -0000	1.21
@@ -117,6 +117,72 @@
   </querytext>
 </fullquery>
 
+<fullquery name="contact::party_id_in_sub_search_clause.get_object_type">
+  <querytext>
+    select object_type
+      from contact_searches
+     where search_id = :search_id
+  </querytext>
+</fullquery>
+
+
+<fullquery name="contact::party_id_in_sub_search_clause.get_condition_types">
+  <querytext>
+    select type 
+      from contact_search_conditions
+     where search_id = :search_id
+  </querytext>
+</fullquery>
+
+<fullquery name="contact::party_id_in_sub_search_clause.select_party">
+    <querytext>
+    select distinct parties.party_id
+      from parties, $cr_from group_approved_member_map
+     where parties.party_id = group_approved_member_map.member_id
+	$cr_where
+       and group_approved_member_map.group_id in ([template::util::tcl_to_sql_list [contacts::default_groups -package_id $package_id]])
+	$search_clause
+    </querytext>
+</fullquery>
+
+<fullquery name="contact::party_id_in_sub_search_clause.select_person">
+    <querytext>
+        select distinct persons.person_id as party_id
+      from persons, $cr_from group_approved_member_map
+     where persons.person_id = group_approved_member_map.member_id
+       and group_approved_member_map.group_id in ([template::util::tcl_to_sql_list [contacts::default_groups -package_id $package_id]])
+	$cr_where
+	$search_clause
+  </querytext>
+</fullquery>
+
+<fullquery name="contact::party_id_in_sub_search_clause.select_organization">
+    <querytext>
+        select distinct organizations.organization_id as party_id, 
+      from organizations, $cr_from
+           group_approved_member_map
+     where organizations.organization_id = group_approved_member_map.member_id
+       and group_approved_member_map.group_id in ([template::util::tcl_to_sql_list [contacts::default_groups -package_id $package_id]])
+	$cr_where
+	$search_clause
+  </querytext>
+</fullquery>
+
+<fullquery name="contact::party_id_in_sub_search_clause.select_employee">
+    <querytext>
+        select distinct persons.person_id as party_id
+      from persons, $cr_from
+           group_approved_member_map,
+           acs_rels
+     where persons.person_id = group_approved_member_map.member_id
+       and group_approved_member_map.group_id in ([template::util::tcl_to_sql_list [contacts::default_groups -package_id $package_id]])
+       and persons.person_id = acs_rels.object_id_one
+       and acs_rels.rel_type = 'contact_rels_employment'
+        $cr_where
+        $search_clause
+  </querytext>
+</fullquery>
+
 <fullquery name="contact::search::party_p_not_cached.party_in_search_p">
   <querytext>
     select 1