INFRA-132 - Misc
[civicrm-core.git] / CRM / Contact / BAO / Group.php
index 57e727032dacdc0957aa461f787f12bae869d626..e645f4c096084d1fa269881d54fd92becdcbfe1d 100644 (file)
@@ -23,7 +23,7 @@
   | GNU Affero General Public License or the licensing of CiviCRM,     |
   | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
   +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -53,8 +53,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * @param array $defaults
    *   (reference ) an assoc array to hold the flattened values.
    *
-   * @return CRM_Contact_BAO_Group object
-   * @static
+   * @return CRM_Contact_BAO_Group
    */
   public static function retrieve(&$params, &$defaults) {
     $group = new CRM_Contact_DAO_Group();
@@ -73,9 +72,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @param int $id
    *   Group id.
-   *
-   * @return NULL
-   * @static
    */
   public static function discard($id) {
     CRM_Utils_Hook::pre('delete', 'Group', $id, CRM_Core_DAO::$_nullArray);
@@ -118,8 +114,9 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     CRM_Core_DAO::executeQuery($query, $params);
 
     if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
-        'is_enabled'
-      )) {
+      'is_enabled'
+    )
+    ) {
       // clear any descendant groups cache if exists
       CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org');
     }
@@ -155,8 +152,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @param int $id
    *   Group id.
-   * @param enum|string $status status of members in group
-   *
+   * @param string $status
+   *   status of members in group
    * @param bool $countChildGroups
    *
    * @return int
@@ -206,7 +203,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @return array
    *   this array contains the list of members for this group id
-   * @static
    */
   public static function &getMember($groupID, $useCache = TRUE) {
     $params = array(array('group', 'IN', array($groupID => 1), 0, 0));
@@ -228,20 +224,19 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *   Limits the set of groups returned.
    * @param array $returnProperties
    *   Which properties should be included in the returned group objects.
-   *                                       (member_count should be last element.)
-   *
-   * @param NULL $sort
-   * @param NULL $offset
-   * @param NULL $rowCount
+   *   (member_count should be last element.)
+   * @param string $sort
+   * @param int $offset
+   * @param int $rowCount
    *
    * @return array
-   *   of group objects.
+   *   Array of group objects.
    *
    *
    * @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,
@@ -312,7 +307,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @return string
    *   the permission that the user has (or NULL)
-   * @static
    */
   public static function checkPermission($id) {
     $allGroups = CRM_Core_PseudoConstant::allGroup();
@@ -348,10 +342,9 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * Create a new group
    *
    * @param array $params
-   *   Associative array of parameters.
    *
-   * @return object|NULL      The new group BAO (if created)
-   * @static
+   * @return CRM_Contact_BAO_Group|NULL
+   *   The new group BAO (if created)
    */
   public static function &create(&$params) {
 
@@ -372,8 +365,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     if (isset($params['group_type'])) {
       if (is_array($params['group_type'])) {
         $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
-          array_keys($params['group_type'])
-        ) . CRM_Core_DAO::VALUE_SEPARATOR;
+            array_keys($params['group_type'])
+          ) . CRM_Core_DAO::VALUE_SEPARATOR;
       }
     }
     else {
@@ -400,8 +393,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     // we need the $params one to be in it's original form & we are not sure what test coverage we have on that
     if (isset($group->parents) && is_array($group->parents)) {
       $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
-        array_keys($group->parents)
-      ) . CRM_Core_DAO::VALUE_SEPARATOR;
+          array_keys($group->parents)
+        ) . CRM_Core_DAO::VALUE_SEPARATOR;
     }
     if (empty($params['id']) &&
       !$nameParam
@@ -522,8 +515,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
         $this->where_tables = serialize($whereTables);
       }
     }
-
-    return;
   }
 
   /**
@@ -532,8 +523,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * @param array $params
    *   Associative array of parameters.
    *
-   * @return object|NULL      The new group BAO (if created)
-   * @static
+   * @return CRM_Contact_BAO_Group|NULL
+   *   The new group BAO (if created)
    */
   public static function createSmartGroup(&$params) {
     if (!empty($params['formValues'])) {
@@ -562,9 +553,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * @param bool $isActive
    *   Value we want to set the is_active field.
    *
-   * @return Object
-   *   DAO object on sucess, NULL otherwise
-   * @static
+   * @return CRM_Core_DAO|null
+   *   DAO object on success, NULL otherwise
    */
   public static function setIsActive($id, $isActive) {
     return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Group', $id, 'is_active', $isActive);
@@ -575,10 +565,9 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @param string $groupType
    *   Type of group(Access/Mailing) OR the key of the group.
-   * @param bool|\boolen $excludeHidden exclude hidden groups.
+   * @param bool $excludeHidden exclude hidden groups.
    *
    * @return string
-   * @static
    */
   public static function groupTypeCondition($groupType = NULL, $excludeHidden = TRUE) {
     $value = NULL;
@@ -652,7 +641,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @return array
    *   ( smartGroupId, ssId ) smart group id and saved search id
-   * @static
    */
   public static function createHiddenSmartGroup($params) {
     $ssId = CRM_Utils_Array::value('saved_search_id', $params);
@@ -663,10 +651,10 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       //save the mapping for search builder
       if (!$ssId) {
         //save record in mapping table
-        $temp          = array();
+        $temp = array();
         $mappingParams = array('mapping_type' => 'Search Builder');
-        $mapping       = CRM_Core_BAO_Mapping::add($mappingParams, $temp);
-        $mappingId     = $mapping->id;
+        $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp);
+        $mappingId = $mapping->id;
       }
       else {
         //get the mapping id from saved search
@@ -723,16 +711,16 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    *
    * @return array
    *   associated array of group list
-   *  -rp = rowcount
-   *  -page= offset
+   *   -rp = rowcount
+   *   -page= offset
    * @todo there seems little reason for the small number of functions that call this to pass in
    * params that then need to be translated in this function since they are coding them when calling
    */
   static public function getGroupListSelector(&$params) {
     // format the params
-    $params['offset']   = ($params['page'] - 1) * $params['rp'];
+    $params['offset'] = ($params['page'] - 1) * $params['rp'];
     $params['rowCount'] = $params['rp'];
-    $params['sort']     = CRM_Utils_Array::value('sortBy', $params);
+    $params['sort'] = CRM_Utils_Array::value('sortBy', $params);
 
     // get groups
     $groups = CRM_Contact_BAO_Group::getGroupList($params);
@@ -973,13 +961,15 @@ 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;
       }
@@ -1000,7 +990,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>',
@@ -1167,9 +1157,9 @@ WHERE {$whereClause}";
     if ($groupType) {
       $types = explode(',', $groupType);
       if (!empty($types)) {
-        $clauses[]  = 'groups.group_type LIKE %2';
+        $clauses[] = 'groups.group_type LIKE %2';
         $typeString = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $types) . CRM_Core_DAO::VALUE_SEPARATOR;
-        $params[2]  = array($typeString, 'String', TRUE);
+        $params[2] = array($typeString, 'String', TRUE);
       }
     }
 
@@ -1230,7 +1220,6 @@ WHERE {$whereClause}";
 
     $clauses[] = self::getPermissionClause();
 
-
     return implode(' AND ', $clauses);
   }
 
@@ -1292,4 +1281,5 @@ WHERE {$whereClause}";
 
     return CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
   }
+
 }