Merge pull request #6496 from totten/master-regen-leak
[civicrm-core.git] / CRM / Contact / BAO / GroupContact.php
index 8616165c39eaea49d552f3546746370f66817687..da7ebc36040a4ac9af093f45d19b4f22c4b1db1d 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
 
   /**
-   * Class constructor
+   * Class constructor.
    */
   public function __construct() {
     parent::__construct();
   }
 
   /**
-   * Takes an associative array and creates a groupContact object
+   * Takes an associative array and creates a groupContact object.
    *
    * the function extract all the params it needs to initialize the create a
    * group object. the params array could contain additional unused name/value
@@ -68,7 +68,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
   }
 
   /**
-   * Check if there is data to create the object
+   * Check if there is data to create the object.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
@@ -260,7 +260,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
   }
 
   /**
-   * Get list of all the groups and groups for a contact
+   * Get list of all the groups and groups for a contact.
    *
    * @param int $contactId
    *   Contact id.
@@ -303,7 +303,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
   }
 
   /**
-   * Get the list of groups for contact based on status of group membership
+   * Get the list of groups for contact based on status of group membership.
    *
    * @param int $contactId
    *   Contact id.
@@ -351,7 +351,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
                     civicrm_subscription_history.method as method';
     }
 
-    $where = " WHERE contact_a.id = %1 AND civicrm_group.is_active = 1 ";
+    $where = " WHERE contact_a.id = %1 AND civicrm_group.is_active = 1";
 
     if ($excludeHidden) {
       $where .= " AND civicrm_group.is_hidden = 0 ";
@@ -381,6 +381,10 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
 
     $from = CRM_Contact_BAO_Query::fromClause($tables);
 
+    //CRM-16945: seems hackish but as per CRM-16483 of using group criteria for Search Builder it is mandatory
+    //to include group_contact_cache clause when group table is present, so following code remove duplicacy
+    $from = str_replace("OR civicrm_group.id = civicrm_group_contact_cache.group_id", 'AND civicrm_group.saved_search_id IS NULL', $from);
+
     $where .= " AND $permission ";
 
     if ($onlyPublicGroups) {
@@ -439,7 +443,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
   }
 
   /**
-   * Returns membership details of a contact for a group
+   * Returns membership details of a contact for a group.
    *
    * @param int $contactId
    *   Id of the contact.
@@ -483,7 +487,7 @@ SELECT    *
   }
 
   /**
-   * Method to get Group Id
+   * Method to get Group Id.
    *
    * @param int $groupContactID
    *   Id of a perticuler group.
@@ -587,7 +591,7 @@ SELECT    *
   }
 
   /**
-   * Function merges the groups from otherContactID to mainContactID
+   * Function merges the groups from otherContactID to mainContactID.
    * along with subscription history
    *
    * @param int $mainContactId