Merge pull request #14802 from eileenmcnaughton/ex4
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
index f06992ddfa4775c480edc835714f50a90270907b..2a0379a917f5a42327eb389e7feb3fdb0d40a504 100644 (file)
@@ -1612,6 +1612,43 @@ class CRM_Export_BAO_ExportProcessor {
     return $returnProperties;
   }
 
+  /**
+   * @param int $contactId
+   * @param array $exportParams
+   *
+   * @return array
+   */
+  public function replaceMergeTokens($contactId, $exportParams) {
+    $greetings = [];
+    $contact = NULL;
+
+    $greetingFields = [
+      'postal_greeting',
+      'addressee',
+    ];
+    foreach ($greetingFields as $greeting) {
+      if (!empty($exportParams[$greeting])) {
+        $greetingLabel = $exportParams[$greeting];
+        if (empty($contact)) {
+          $values = [
+            'id' => $contactId,
+            'version' => 3,
+          ];
+          $contact = civicrm_api('contact', 'get', $values);
+
+          if (!empty($contact['is_error'])) {
+            return $greetings;
+          }
+          $contact = $contact['values'][$contact['id']];
+        }
+
+        $tokens = ['contact' => $greetingLabel];
+        $greetings[$greeting] = CRM_Utils_Token::replaceContactTokens($greetingLabel, $contact, NULL, $tokens);
+      }
+    }
+    return $greetings;
+  }
+
   /**
    * The function unsets static part of the string, if token is the dynamic part.
    *