Merge pull request #4851 from totten/master-createtest-bao
[civicrm-core.git] / api / v3 / Group.php
index 6cc7647ebcab2bdefdd7e066856a75a7995cb195..22f7e38eb07fd2b6cab675a0d414b8f72cace98b 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  * create/update group
  *
  * This API is used to create new group or update any of the existing
- * In case of updating existing group, id of that particular grop must
+ * In case of updating existing group, id of that particular group must
  * be in $params array. Either id or name is required field in the
  * $params array
  *
- * @param array $params  (referance) Associative array of property
+ * @param array $params
+ *   Associative array of property.
  *                       name/value pairs to insert in new 'group'
  *
- * @return array   returns id of the group created if success,
- *                 error message otherwise
+ * @return array  API result array
  *@example GroupCreate.php
  *{@getfields group_create}
  * @access public
  */
 function civicrm_api3_group_create($params) {
-
-  $group = CRM_Contact_BAO_Group::create($params);
-
-  if (is_null($group)) {
-    return civicrm_api3_create_error('Group not created');
-  }
-  else {
-    $values = array();
-    _civicrm_api3_object_to_array_unique_fields($group, $values[$group->id]);
-    return civicrm_api3_create_success($values, $params, 'group', 'create', $group);
-  }
+  return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Group');
 }
 
 /**
  * Adjust Metadata for Create action
  *
  * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
+ * @param array $params
+ *   Array or parameters determined by getfields.
  */
 function _civicrm_api3_group_create_spec(&$params) {
   $params['is_active']['api.default'] = 1;
@@ -80,7 +71,8 @@ function _civicrm_api3_group_create_spec(&$params) {
 /**
  * Returns array of groups  matching a set of one or more group properties
  *
- * @param array $params  (referance) Array of one or more valid
+ * @param array $params
+ *   Array of one or more valid.
  *                       property_name=>value pairs. If $params is set
  *                       as null, all groups will be returned
  *
@@ -90,34 +82,16 @@ function _civicrm_api3_group_create_spec(&$params) {
  * @access public
  */
 function civicrm_api3_group_get($params) {
-
-  $options          = _civicrm_api3_get_options_from_params($params, TRUE, 'group', 'get');
-  $sort             = CRM_Utils_Array::value('sort', $options, NULL);
-  $offset           = CRM_Utils_Array::value('offset', $options);
-  $rowCount         = CRM_Utils_Array::value('limit', $options);
-  $returnProperties = CRM_Utils_Array::value('return', $options, NULL);
-  $inputParams      = CRM_Utils_Array::value('input_params', $options, array());
-  if(is_array($returnProperties) && !empty($returnProperties)){
-    // group function takes $returnProperties in non standard format & doesn't add id
-    unset($returnProperties['group_id']);
-    $returnProperties['id'] = 1;
-    $returnProperties = array_keys($returnProperties);
-  }
-  if (!empty($inputParams['group_id'])) {
-    $inputParams['id'] = $inputParams['group_id'];
-  }
-  $groupObjects = CRM_Contact_BAO_Group::getGroups($inputParams, $returnProperties, $sort, $offset, $rowCount);
-  if (empty($groupObjects)) {
-    return civicrm_api3_create_success(FALSE);
-  }
-  $groups = array();
-  foreach ($groupObjects as $group) {
-    _civicrm_api3_object_to_array($group, $groups[$group->id]);
-    _civicrm_api3_custom_data_get($groups[$group->id], 'Group', $group->id);
+  $options = _civicrm_api3_get_options_from_params($params, TRUE, 'group', 'get');
+  if (empty($options['return']) || !in_array('member_count', $options['return'])) {
+    return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Group');
   }
 
-
-  return civicrm_api3_create_success($groups, $params, 'group', 'create');
+  $groups = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Group');
+  foreach ($groups as $id => $group) {
+    $groups[$id]['member_count'] = CRM_Contact_BAO_Group::memberCount($id);
+  }
+  return civicrm_api3_create_success($groups, $params, 'group', 'get');
 }
 
 /**
@@ -126,11 +100,11 @@ function civicrm_api3_group_get($params) {
  * This method is used to delete any existing group. id of the group
  * to be deleted is required field in $params array
  *
- * @param array $params  (referance) array containing id of the group
+ * @param array $params
+ *   Array containing id of the group.
  *                       to be deleted
  *
- * @return array  (referance) returns flag true if successfull, error
- *                message otherwise
+ * @return array API result array
  *@example GroupDelete.php
  *{@getfields group_delete}
  *