Merge pull request #6548 from eileenmcnaughton/CRM-16512
[civicrm-core.git] / CRM / Contact / BAO / Group.php
index 1ad8a313b8bdee676b598fcd1675e36a07d30b49..d63caf2650e435a4039446257731094dc970dd0c 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$
  *
  */
@@ -42,11 +42,9 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Takes a bunch of params that are needed to match certain criteria and
-   * retrieves the relevant objects. Typically the valid params are only
-   * group_id. We'll tweak this function to be more full featured over a period
-   * of time. This is the inverse function of create. It also stores all the retrieved
-   * values in the default array
+   * Retrieve DB object based on input parameters.
+   *
+   * It also stores all the retrieved values in the default array.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
@@ -72,8 +70,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @param int $id
    *   Group id.
-   *
-   * @return NULL
    */
   public static function discard($id) {
     CRM_Utils_Hook::pre('delete', 'Group', $id, CRM_Core_DAO::$_nullArray);
@@ -150,7 +146,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Get the count of a members in a group with the specific status
+   * Get the count of a members in a group with the specific status.
    *
    * @param int $id
    *   Group id.
@@ -198,7 +194,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Get the list of member for a group id
+   * Get the list of member for a group id.
    *
    * @param int $groupID
    * @param bool $useCache
@@ -238,7 +234,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * @todo other BAO functions that use returnProperties (e.g. Query Objects) receive the array flipped & filled with 1s and
    * add in essential fields (e.g. id). This should follow a regular pattern like the others
    */
-  static function getGroups(
+  public static function getGroups(
     $params = NULL,
     $returnProperties = NULL,
     $sort = NULL,
@@ -302,7 +298,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Make sure that the user has permission to access this group
+   * Make sure that the user has permission to access this group.
    *
    * @param int $id
    *   The id of the object.
@@ -341,7 +337,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Create a new group
+   * Create a new group.
    *
    * @param array $params
    *
@@ -517,12 +513,10 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
         $this->where_tables = serialize($whereTables);
       }
     }
-
-    return;
   }
 
   /**
-   * Defines a new smart group
+   * Defines a new smart group.
    *
    * @param array $params
    *   Associative array of parameters.
@@ -550,7 +544,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Update the is_active flag in the db
+   * Update the is_active flag in the db.
    *
    * @param int $id
    *   Id of the database record.
@@ -604,7 +598,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Get permission relevant clauses
+   * Get permission relevant clauses.
    * CRM-12209
    *
    * @param bool $force
@@ -708,7 +702,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * wrapper for ajax group selector
+   * wrapper for ajax group selector.
    *
    * @param array $params
    *   Associated array for params record id.
@@ -778,7 +772,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * This function to get list of groups
+   * This function to get list of groups.
    *
    * @param array $params
    *   Associated array for params.
@@ -801,6 +795,11 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     $orderBy = ' ORDER BY groups.title asc';
     if (!empty($params['sort'])) {
       $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String');
+
+      // CRM-16905 - Sort by count cannot be done with sql
+      if (strpos($params['sort'], 'count') === 0) {
+        $orderBy = $limit = '';
+      }
     }
 
     $select = $from = $where = "";
@@ -867,6 +866,9 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
           'count' => '0',
         );
         CRM_Core_DAO::storeValues($object, $values[$object->id]);
+        // Wrap with crm-editable. Not an ideal solution.
+        $values[$object->id]['title'] = '<span class="crm-editable crmf-title">' . $values[$object->id]['title'] . '</span>';
+
         if ($object->saved_search_id) {
           $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')';
           // check if custom search, if so fix view link
@@ -965,18 +967,31 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       }
     }
 
-    // Get group counts
+    // Get group counts - executes one query for regular groups and another for smart groups
     foreach ($groupsToCount as $table => $groups) {
-      $where = "group_id IN (" . implode(',', $groups) . ")";
+      $where = "g.group_id IN (" . implode(',', $groups) . ")";
       if ($table == 'civicrm_group_contact') {
-        $where .= " AND status = 'Added'";
+        $where .= " AND g.status = 'Added'";
       }
-      $dao = CRM_Core_DAO::executeQuery("SELECT group_id, COUNT(id) as `count` FROM $table WHERE $where GROUP BY group_id");
+      // Exclude deleted contacts
+      $where .= " and c.id = g.contact_id AND c.is_deleted = 0";
+      $dao = CRM_Core_DAO::executeQuery("SELECT g.group_id, COUNT(g.id) as `count` FROM $table g, civicrm_contact c WHERE $where GROUP BY g.group_id");
       while ($dao->fetch()) {
         $values[$dao->group_id]['count'] = $dao->count;
       }
     }
 
+    // CRM-16905 - Sort by count cannot be done with sql
+    if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) {
+      usort($values, function($a, $b) {
+        return $a['count'] - $b['count'];
+      });
+      if (strpos($params['sort'], 'desc')) {
+        $values = array_reverse($values, TRUE);
+      }
+      return array_slice($values, $params['offset'], $params['rowCount']);
+    }
+
     return $values;
   }
 
@@ -992,7 +1007,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @return array
    */
-  static function getGroupsHierarchy(
+  public static function getGroupsHierarchy(
     $groupIDs,
     $parents = NULL,
     $spacer = '<span class="child-indent"></span>',
@@ -1135,7 +1150,7 @@ WHERE {$whereClause}";
   }
 
   /**
-   * Generate permissioned where clause for group search
+   * Generate permissioned where clause for group search.
    * @param array $params
    * @param bool $sortBy
    * @param bool $excludeHidden
@@ -1222,12 +1237,11 @@ WHERE {$whereClause}";
 
     $clauses[] = self::getPermissionClause();
 
-
     return implode(' AND ', $clauses);
   }
 
   /**
-   * Define action links
+   * Define action links.
    *
    * @return array
    *   array of action links
@@ -1284,4 +1298,5 @@ WHERE {$whereClause}";
 
     return CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
   }
+
 }