CRM-14449 stdise group api so it gets custom fields & works with getlist
[civicrm-core.git] / api / v3 / Case.php
index 063d6de4f3262a5f9c517b699111508ab3c3d714..04e2d3f8bbd6ef385d755bc006421b2c7e53fb35 100644 (file)
@@ -2,9 +2,9 @@
 
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.4                                                |
+  | CiviCRM version 4.5                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2013                                |
+  | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -32,7 +32,7 @@
  *
  * @package CiviCRM_APIv3
  * @subpackage API_Case
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  *
  */
 
@@ -134,6 +134,7 @@ function civicrm_api3_case_create($params) {
 function _civicrm_api3_case_get_spec(&$params) {
   $params['contact_id']['api.aliases'] = array('client_id');
   $params['contact_id']['title'] = 'Case Client';
+  $params['creator_id']['api.default'] = 'user_contact_id';
 }
 
 /**
@@ -190,31 +191,8 @@ function _civicrm_api3_case_delete_spec(&$params) {
  * @todo Erik Hommel 16 dec 2010 check if all DB fields are returned
  */
 function civicrm_api3_case_get($params) {
-  civicrm_api3_verify_mandatory($params, NULL, array(
-    array('case_id', 'contact_id', 'activity_id', 'contact_id')
-  ));
-
   $options = _civicrm_api3_get_options_from_params($params);
 
-  // Get by id
-  $caseId = CRM_Utils_Array::value('id', $params);
-  if ($caseId) {
-    // Validate param
-    if (!is_numeric($caseId)) {
-      return civicrm_api3_create_error('Invalid parameter: case_id. Must provide a numeric value.');
-    }
-    // For historic reasons we always return these when an id is provided
-    $options['return'] = array('contacts' => 1, 'activities' => 1);
-    $case = _civicrm_api3_case_read($caseId, $options);
-
-    if ($case) {
-      return civicrm_api3_create_success(array($caseId => $case), $params, 'case', 'get');
-    }
-    else {
-      return civicrm_api3_create_success(array(), $params, 'case', 'get');
-    }
-  }
-
   //search by client
   if (!empty($params['contact_id'])) {
     $ids = array();
@@ -257,7 +235,7 @@ SELECT DISTINCT case_id
  WHERE (contact_id_a = $contact
     OR contact_id_b = $contact)
    AND case_id IS NOT NULL";
-    $dao = &CRM_Core_DAO::executeQuery($sql);
+    $dao = CRM_Core_DAO::executeQuery($sql);
 
     $cases = array();
     while ($dao->fetch()) {
@@ -265,6 +243,22 @@ SELECT DISTINCT case_id
     }
     return civicrm_api3_create_success($cases, $params, 'case', 'get');
   }
+
+  // For historic reasons we always return these when an id is provided
+  $caseId = CRM_Utils_Array::value('id', $params);
+  if ($caseId) {
+    $options['return'] = array('contacts' => 1, 'activities' => 1);
+  }
+
+  $foundcases =  _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case');
+  $cases = array();
+  foreach ($foundcases['values'] as $foundcase) {
+      if ($case = _civicrm_api3_case_read($foundcase['id'], $options)) {
+        $cases[$foundcase['id']] = $case;
+      }
+    }
+
+  return civicrm_api3_create_success($cases, $params, 'case', 'get');
 }
 
 /**
@@ -379,8 +373,11 @@ function civicrm_api3_case_delete($params) {
  *
  * @param int $caseId
  *
- * @return array (reference) case object
+ * @param $params
+ *
+ * @internal param $options
  *
+ * @return array (reference) case object
  */
 function _civicrm_api3_case_read($caseId, $options) {
   $return = CRM_Utils_Array::value('return', $options, array());
@@ -419,13 +416,6 @@ function _civicrm_api3_case_read($caseId, $options) {
  * Internal function to format create params for processing
  */
 function _civicrm_api3_case_format_params(&$params) {
-  if (!array_key_exists('creator_id', $params)) {
-    $session = CRM_Core_Session::singleton();
-    $params['creator_id'] = $session->get('userID');
-  }
-  if (empty($params['start_date'])) {
-    $params['start_date'] = date('YmdHis');
-  }
   // figure out case type id from case type and vice-versa
   $caseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE);
   if (empty($params['case_type_id'])) {