Merge pull request #6468 from eileenmcnaughton/CRM-17010
[civicrm-core.git] / CRM / Contact / BAO / Group.php
index e645f4c096084d1fa269881d54fd92becdcbfe1d..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.                                    |
   |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @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.
@@ -148,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.
@@ -196,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
@@ -300,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.
@@ -339,7 +337,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Create a new group
+   * Create a new group.
    *
    * @param array $params
    *
@@ -518,7 +516,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
   }
 
   /**
-   * Defines a new smart group
+   * Defines a new smart group.
    *
    * @param array $params
    *   Associative array of parameters.
@@ -546,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.
@@ -600,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
@@ -704,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.
@@ -774,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.
@@ -797,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 = "";
@@ -863,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
@@ -975,6 +981,17 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       }
     }
 
+    // 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;
   }
 
@@ -1133,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
@@ -1224,7 +1241,7 @@ WHERE {$whereClause}";
   }
 
   /**
-   * Define action links
+   * Define action links.
    *
    * @return array
    *   array of action links