Merge pull request #6458 from totten/4.6-toxic-list
[civicrm-core.git] / CRM / Contact / BAO / GroupContact.php
index 863ad13a4aadefa17efbf6e7f34c9f24bca7af7d..8a4bb40b9e98b669c862d26caa0e2ea13fc9b118 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @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
@@ -51,8 +51,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
-   * @return CRM_Contact_BAO_Group object
-   * @static
+   * @return CRM_Contact_BAO_Group
    */
   public static function add(&$params) {
 
@@ -69,13 +68,12 @@ 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.
    *
-   * @return boolean
-   * @static
+   * @return bool
    */
   public static function dataExists(&$params) {
     // return if no data present
@@ -95,8 +93,8 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param array $values
    *   Output values of the object.
    *
-   * @return array (reference)   the values that could be potentially assigned to smarty
-   * @static
+   * @return array
+   *   (reference)   the values that could be potentially assigned to smarty
    */
   public static function getValues(&$params, &$values) {
     if (empty($params)) {
@@ -128,28 +126,21 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param string $status
    * @param int $tracking
    *
-   * @return array             (total, added, notAdded) count of contacts added to group
-   * @static
+   * @return array
+   *   (total, added, notAdded) count of contacts added to group
    */
-  static function addContactsToGroup(
+  public static function addContactsToGroup(
     $contactIds,
     $groupId,
-    $method   = 'Admin',
-    $status   = 'Added',
+    $method = 'Admin',
+    $status = 'Added',
     $tracking = NULL
   ) {
 
-
     CRM_Utils_Hook::pre('create', 'GroupContact', $groupId, $contactIds);
 
-    list($numContactsAdded,
-      $numContactsNotAdded
-    ) = self::bulkAddContactsToGroup($contactIds,
-      $groupId,
-      $method,
-      $status,
-      $tracking
-    );
+    list($numContactsAdded, $numContactsNotAdded)
+      = self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking);
 
     // also reset the acl cache
     $config = CRM_Core_Config::singleton();
@@ -178,14 +169,14 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param string $status
    * @param NULL $tracking
    *
-   * @return array             (total, removed, notRemoved) count of contacts removed to group
-   * @static
+   * @return array
+   *   (total, removed, notRemoved) count of contacts removed to group
    */
-  static function removeContactsFromGroup(
+  public static function removeContactsFromGroup(
     &$contactIds,
     $groupId,
-    $method   = 'Admin',
-    $status   = 'Removed',
+    $method = 'Admin',
+    $status = 'Removed',
     $tracking = NULL
   ) {
     if (!is_array($contactIds)) {
@@ -211,8 +202,8 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
 
     foreach ($contactIds as $contactId) {
       if ($status == 'Deleted') {
-        $query         = "DELETE FROM civicrm_group_contact WHERE contact_id=$contactId AND group_id=$groupId";
-        $dao           = CRM_Core_DAO::executeQuery($query);
+        $query = "DELETE FROM civicrm_group_contact WHERE contact_id=$contactId AND group_id=$groupId";
+        $dao = CRM_Core_DAO::executeQuery($query);
         $historyParams = array(
           'group_id' => $groupId,
           'contact_id' => $contactId,
@@ -269,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.
@@ -277,8 +268,8 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param bool $visibility
    *
    *
-   * @return array $values this array has key-> group id and value group title
-   * @static
+   * @return array
+   *   this array has key-> group id and value group title
    */
   public static function getGroupList($contactId = 0, $visibility = FALSE) {
     $group = new CRM_Contact_DAO_Group();
@@ -286,8 +277,8 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
     $select = $from = $where = '';
 
     $select = 'SELECT DISTINCT civicrm_group.id, civicrm_group.title ';
-    $from   = ' FROM civicrm_group ';
-    $where  = " WHERE civicrm_group.is_active = 1 ";
+    $from = ' FROM civicrm_group ';
+    $where = " WHERE civicrm_group.is_active = 1 ";
     if ($contactId) {
       $from .= ' , civicrm_group_contact ';
       $where .= " AND civicrm_group.id = civicrm_group_contact.group_id
@@ -312,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.
@@ -331,20 +322,19 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    *
    * @param bool $excludeHidden
    *
-   * @return array (reference )|int $values the relevant data object values for the contact or
-   *                                 the total count when $count is TRUE
-   *
-   * $access public
+   * @return array (reference)|int $values
+   *   the relevant data object values for the contact or
+   *   the total count when $count is TRUE
    */
-  static function &getContactGroup(
+  public static function &getContactGroup(
     $contactId,
-    $status           = NULL,
-    $numGroupContact  = NULL,
-    $count            = FALSE,
+    $status = NULL,
+    $numGroupContact = NULL,
+    $count = FALSE,
     $ignorePermission = FALSE,
     $onlyPublicGroups = FALSE,
-    $excludeHidden    = TRUE,
-    $groupId          = NULL
+    $excludeHidden = TRUE,
+    $groupId = NULL
   ) {
     if ($count) {
       $select = 'SELECT count(DISTINCT civicrm_group_contact.id)';
@@ -361,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 ";
@@ -391,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) {
@@ -449,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.
@@ -458,8 +452,8 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param string $method
    *   If we want the subscription history details for a specific method.
    *
-   * @return object of group contact
-   * @static
+   * @return object
+   *   of group contact
    */
   public static function getMembershipDetail($contactId, $groupID, $method = 'Email') {
     $leftJoin = $where = $orderBy = NULL;
@@ -493,14 +487,13 @@ SELECT    *
   }
 
   /**
-   * Method to get Group Id
+   * Method to get Group Id.
    *
    * @param int $groupContactID
    *   Id of a perticuler group.
    *
    *
    * @return groupID
-   * @static
    */
   public static function getGroupId($groupContactID) {
     $dao = new CRM_Contact_DAO_GroupContact();
@@ -523,7 +516,6 @@ SELECT    *
    * @param string $method
    *
    * @return void
-   * @static
    */
   public static function create(&$params, $contactId, $visibility = FALSE, $method = 'Admin') {
     $contactIds = array();
@@ -599,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
@@ -611,11 +603,12 @@ SELECT    *
    *
    * TODO: use the 3rd $sqls param to append sql statements rather than executing them here
    *
-   * @return void.
-   * @static
+   * @return void
+   *
    */
   public static function mergeGroupContact($mainContactId, $otherContactId) {
-    $params = array(1 => array($mainContactId, 'Integer'),
+    $params = array(
+      1 => array($mainContactId, 'Integer'),
       2 => array($otherContactId, 'Integer'),
     );
 
@@ -719,14 +712,14 @@ AND       group_id IN ( $groupIDString )
    * @param string $status
    * @param NULL $tracking
    *
-   * @return array             (total, added, notAdded) count of contacts added to group
-   * @static
+   * @return array
+   *   (total, added, notAdded) count of contacts added to group
    */
-  static function bulkAddContactsToGroup(
+  public static function bulkAddContactsToGroup(
     $contactIDs,
     $groupID,
-    $method   = 'Admin',
-    $status   = 'Added',
+    $method = 'Admin',
+    $status = 'Added',
     $tracking = NULL
   ) {
 
@@ -757,7 +750,8 @@ WHERE  group_id = %1
 AND    status = %2
 AND    contact_id IN ( $contactStr )
 ";
-      $params = array(1 => array($groupID, 'Integer'),
+      $params = array(
+        1 => array($groupID, 'Integer'),
         2 => array($status, 'String'),
       );
 
@@ -798,11 +792,11 @@ AND    contact_id IN ( $contactStr )
    *
    * @param string $fieldName
    * @param string $context
-   *   : @see CRM_Core_DAO::buildOptionsContext.
+   * @see CRM_Core_DAO::buildOptionsContext
    * @param array $props
-   *   whatever is known about this dao object.
+   *   whatever is known about this dao object.
    *
-   * @return Array|bool
+   * @return array|bool
    */
   public static function buildOptions($fieldName, $context = NULL, $props = array()) {
     $params = array();
@@ -817,4 +811,5 @@ AND    contact_id IN ( $contactStr )
 
     return $options;
   }
+
 }