Merge pull request #11396 from mukeshcompucorp/fix-page-structure
[civicrm-core.git] / api / v3 / Activity.php
index 1b675c265e4357c4a076c089834caa8a270f39fe..45b39dea0f582fd37fa2579e9428faa5caa000e0 100644 (file)
@@ -43,6 +43,7 @@
  *   API result array
  */
 function civicrm_api3_activity_create($params) {
+  $isNew = empty($params['id']);
 
   if (empty($params['id'])) {
     // an update does not require any mandatory parameters
@@ -91,7 +92,7 @@ function civicrm_api3_activity_create($params) {
   }
   if (!empty($params['case_id'])) {
     $case_id = $params['case_id'];
-    if (!empty($params['id'])) {
+    if (!empty($params['id']) && Civi::settings()->get('civicaseActivityRevisions')) {
       $oldActivityParams = array('id' => $params['id']);
       if (!$oldActivityValues) {
         CRM_Activity_BAO_Activity::retrieve($oldActivityParams, $oldActivityValues);
@@ -158,7 +159,7 @@ function civicrm_api3_activity_create($params) {
   $activityBAO = CRM_Activity_BAO_Activity::create($params);
 
   if (isset($activityBAO->id)) {
-    if ($case_id && !$createRevision) {
+    if ($case_id && $isNew && !$createRevision) {
       // If this is a brand new case activity, add to case(s)
       foreach ((array) $case_id as $singleCaseId) {
         $caseActivityParams = array('activity_id' => $activityBAO->id, 'case_id' => $singleCaseId);
@@ -250,6 +251,7 @@ function _civicrm_api3_activity_get_spec(&$params) {
     'type' => CRM_Utils_Type::T_INT,
     'FKClassName' => 'CRM_Case_DAO_Case',
     'FKApiName' => 'Case',
+    'supports_joins' => TRUE,
   );
   $params['contact_id'] = array(
     'title' => 'Activity Contact ID',
@@ -480,11 +482,16 @@ function _civicrm_api3_activity_get_formatResult($params, $activities, $options)
   }
 
   $tagGet = array('tag_id', 'entity_id');
+  $caseGet = $caseIds = array();
   foreach (array_keys($returns) as $key) {
     if (strpos($key, 'tag_id.') === 0) {
       $tagGet[] = $key;
       $returns['tag_id'] = 1;
     }
+    if (strpos($key, 'case_id.') === 0) {
+      $caseGet[] = str_replace('case_id.', '', $key);
+      $returns['case_id'] = 1;
+    }
   }
 
   foreach ($returns as $n => $v) {
@@ -547,6 +554,7 @@ function _civicrm_api3_activity_get_formatResult($params, $activities, $options)
           array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)));
         while ($dao->fetch()) {
           $activities[$dao->activity_id]['case_id'][] = $dao->case_id;
+          $caseIds[$dao->case_id] = $dao->case_id;
         }
         break;
 
@@ -563,6 +571,29 @@ function _civicrm_api3_activity_get_formatResult($params, $activities, $options)
     }
   }
 
+  // Fetch case fields via the join syntax
+  // Note this is limited to the first case if the activity belongs to more than one
+  if ($caseGet && $caseIds) {
+    $cases = civicrm_api3('Case', 'get', array(
+      'id' => array('IN' => $caseIds),
+      'options' => array('limit' => 0),
+      'check_permissions' => !empty($params['check_permissions']),
+      'return' => $caseGet,
+    ));
+    foreach ($activities as &$activity) {
+      if (!empty($activity['case_id'])) {
+        $case = CRM_Utils_Array::value($activity['case_id'][0], $cases['values']);
+        if ($case) {
+          foreach ($case as $key => $value) {
+            if ($key != 'id') {
+              $activity['case_id.' . $key] = $value;
+            }
+          }
+        }
+      }
+    }
+  }
+
   // Legacy extras
   if (!empty($params['contact_id'])) {
     $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get');