Format details for case custom data in a human readable format
authorMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Fri, 28 Dec 2018 20:19:12 +0000 (20:19 +0000)
committerMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Fri, 4 Oct 2019 13:41:03 +0000 (15:41 +0200)
CRM/Case/Form/CustomData.php

index 46f3754149736544a4bfb53f1e8bc86d948954a9..04723f6609eaabbccb0560936f3ab52b41c081b4 100644 (file)
@@ -136,7 +136,7 @@ class CRM_Case_Form_CustomData extends CRM_Core_Form {
       'subject' => $this->_customTitle . " : change data",
       'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
       'target_contact_id' => $this->_contactID,
-      'details' => json_encode($this->_defaults),
+      'details' => $this->formatCustomDataChangesForDetail($params),
       'activity_date_time' => date('YmdHis'),
     ];
     $activity = CRM_Activity_BAO_Activity::create($activityParams);
@@ -150,4 +150,47 @@ class CRM_Case_Form_CustomData extends CRM_Core_Form {
     $transaction->commit();
   }
 
+  /**
+   * Format the custom data changes as [label]: [old value] => [new value]
+   *
+   * @param array $params New custom field values from form
+   *
+   * @return string
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function formatCustomDataChangesForDetail($params) {
+    $formattedDetails = [];
+    foreach ($params as $customField => $newCustomValue) {
+      if (substr($customField, 0, 7) == 'custom_') {
+        if ($this->_defaults[$customField] == $newCustomValue) {
+          // Don't show values that did not change
+          continue;
+        }
+        // We need custom field ID from custom_XX_1
+        list($_, $customFieldId, $_) = explode('_', $customField);
+
+        if (!empty($customFieldId) && is_numeric($customFieldId)) {
+          // Got a custom field ID
+          $label = civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldId, 'return' => 'label']);
+          $oldValue = civicrm_api3('CustomValue', 'getdisplayvalue', [
+            'custom_field_id' => $customFieldId,
+            'entity_id' => $this->_entityID,
+            'custom_field_value' => $this->_defaults[$customField],
+          ]);
+          $oldValue = $oldValue['values'][$customFieldId]['display'];
+          $newValue = civicrm_api3('CustomValue', 'getdisplayvalue', [
+            'custom_field_id' => $customFieldId,
+            'entity_id' => $this->_entityID,
+            'custom_field_value' => $newCustomValue,
+          ]);
+          $newValue = $newValue['values'][$customFieldId]['display'];
+          $formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue;
+        }
+
+      }
+    }
+
+    return implode('<br/>', $formattedDetails);
+  }
+
 }