Remove now-obsolete getPseudoTokens
[civicrm-core.git] / CRM / Utils / Token.php
index 6f065a1e147c9e830fdbfdd94b9d248f0dbfb5b7..6a8ff52d44d7239489b2f78580e977c798e919ba 100644 (file)
@@ -257,7 +257,7 @@ class CRM_Utils_Token {
    * @return null|string
    */
   public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE): ?string {
-    $tokens = self::getDomainTokens($domain->id, $html);
+    $tokens = CRM_Core_DomainTokens::getDomainTokenValues($domain->id, $html);
     $value = $tokens[$token] ?? "{domain.$token}";
     if ($escapeSmarty) {
       $value = self::tokenEscapeSmarty($value);
@@ -296,6 +296,7 @@ class CRM_Utils_Token {
    *   The processed string
    */
   public static function replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
+    CRM_Core_Error::deprecatedFunctionWarning('token processor');
     self::$_tokens['org']
       = array_merge(
         array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')),
@@ -776,6 +777,7 @@ class CRM_Utils_Token {
    * @param string $entityType
    * @return string
    *   html parsed through smarty
+   * @deprecated
    */
   public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
     if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
@@ -822,6 +824,8 @@ class CRM_Utils_Token {
    *
    * this routine will remove the extra backslashes and braces
    *
+   * @deprecated
+   *
    * @param $str ref to the string that will be scanned and modified
    */
   public static function unescapeTokens(&$str) {
@@ -1021,8 +1025,11 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
+   *
+   * @deprecated
    */
-  public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
+  public static function replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
+    CRM_Core_Error::deprecatedFunctionWarning('use the token processor');
     if (!is_array($components) || empty($contact)) {
       return $str;
     }
@@ -1276,6 +1283,8 @@ class CRM_Utils_Token {
    * Get Membership Token Details.
    * @param array $membershipIDs
    *   Array of membership IDS.
+   *
+   * @deprecated
    */
   public static function getMembershipTokenDetails($membershipIDs) {
     $memberships = civicrm_api3('membership', 'get', [
@@ -1514,15 +1523,32 @@ class CRM_Utils_Token {
     return $value;
   }
 
+  /**
+   * @deprecated
+   *
+   * Do not use this function - it still needs full removal from active code
+   * in CRM_Contribute_Form_Task_PDFLetter.
+   */
   protected static function _buildContributionTokens() {
     $key = 'contribution';
 
     if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$key])) {
-      $processor = new CRM_Contribute_Tokens();
       $tokens = array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
         ['campaign' => [], 'financial_type' => [], 'payment_instrument' => []],
         self::getCustomFieldTokens('Contribution'),
-        $processor->getPseudoTokens()
+        [
+          'financial_type_id:label',
+          'financial_type_id:name',
+          'contribution_page_id:label',
+          'contribution_page_id:name',
+          'payment_instrument_id:label',
+          'payment_instrument_id:name',
+          'is_test:label',
+          'is_pay_later:label',
+          'contribution_status_id:label',
+          'contribution_status_id:name',
+          'is_template:label',
+        ]
       );
       foreach ($tokens as $token) {
         if (!empty($token['name'])) {
@@ -1534,21 +1560,6 @@ class CRM_Utils_Token {
     self::$_tokens[$key] = Civi::$statics[__CLASS__][__FUNCTION__][$key];
   }
 
-  /**
-   * Store membership tokens on the static _tokens array.
-   */
-  protected static function _buildMembershipTokens() {
-    $key = 'membership';
-    if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) {
-      $membershipTokens = [];
-      $tokens = CRM_Core_SelectValues::membershipTokens();
-      foreach ($tokens as $token => $dontCare) {
-        $membershipTokens[] = substr($token, (strpos($token, '.') + 1), -1);
-      }
-      self::$_tokens[$key] = $membershipTokens;
-    }
-  }
-
   /**
    * Replace tokens for an entity.
    * @param string $entity
@@ -1586,6 +1597,8 @@ class CRM_Utils_Token {
   }
 
   /**
+   * @deprecated
+   *
    * @param int $caseId
    * @param string $str
    * @param array $knownTokens
@@ -1607,6 +1620,8 @@ class CRM_Utils_Token {
   /**
    * Generic function for formatting token replacement for an api field
    *
+   * @deprecated
+   *
    * @param string $entity
    * @param string $token
    * @param array $entityArray
@@ -1633,13 +1648,15 @@ class CRM_Utils_Token {
       return implode(', ', $ret);
     }
     // Format date fields
-    elseif ($entityArray[$token] && $fieldType == CRM_Utils_Type::T_DATE) {
+    elseif ($entityArray[$token] && in_array($fieldType, [CRM_Utils_Type::T_DATE, CRM_Utils_Type::T_TIMESTAMP, (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)])) {
       return CRM_Utils_Date::customFormat($entityArray[$token]);
     }
     return implode(', ', (array) $entityArray[$token]);
   }
 
   /**
+   * Do not use - unused in core.
+   *
    * Replace Contribution tokens in html.
    *
    * @param string $str
@@ -1648,6 +1665,8 @@ class CRM_Utils_Token {
    * @param string $knownTokens
    * @param bool|string $escapeSmarty
    *
+   * @deprecated
+   *
    * @return mixed
    */
   public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
@@ -1689,9 +1708,12 @@ class CRM_Utils_Token {
    * @param array $contributions
    * @param array $knownTokens
    *
+   * @deprecated
+   *
    * @return string
    */
   public static function replaceMultipleContributionTokens(string $separator, string $str, array $contributions, array $knownTokens): string {
+    CRM_Core_Error::deprecatedFunctionWarning('no alternative');
     foreach ($knownTokens['contribution'] ?? [] as $token) {
       $resolvedTokens = [];
       foreach ($contributions as $contribution) {
@@ -1705,6 +1727,14 @@ class CRM_Utils_Token {
   /**
    * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
    * - this is used by the pdfLetter task from membership search
+   *
+   * This is called via replaceEntityTokens.
+   *
+   * In the near term it will not be called at all from core as
+   * the pdf letter task is updated to use the processor.
+   *
+   * @deprecated
+   *
    * @param string $entity
    *   should always be "membership"
    * @param string $token
@@ -1714,13 +1744,29 @@ class CRM_Utils_Token {
    * @return string token replacement
    */
   public static function getMembershipTokenReplacement($entity, $token, $membership) {
-    self::_buildMembershipTokens();
+    $supportedTokens = [
+      'id',
+      'status',
+      'status_id',
+      'type',
+      'membership_type_id',
+      'start_date',
+      'join_date',
+      'end_date',
+      'fee',
+    ];
     switch ($token) {
       case 'type':
+        // membership_type_id would only be requested if the calling
+        // class is mapping it to '{membership:membership_type_id:label'}
+      case 'membership_type_id':
         $value = $membership['membership_name'];
         break;
 
       case 'status':
+        // status_id would only be requested if the calling
+        // class is mapping it to '{membership:status_id:label'}
+      case 'status_id':
         $statuses = CRM_Member_BAO_Membership::buildOptions('status_id');
         $value = $statuses[$membership['status_id']];
         break;
@@ -1741,7 +1787,7 @@ class CRM_Utils_Token {
         break;
 
       default:
-        if (in_array($token, self::$_tokens[$entity])) {
+        if (in_array($token, $supportedTokens)) {
           $value = $membership[$token];
           if (CRM_Utils_String::endsWith($token, '_date')) {
             $value = CRM_Utils_Date::customFormat($value);
@@ -1758,15 +1804,19 @@ class CRM_Utils_Token {
   }
 
   /**
+   * Do not use - unused in core.
+   *
    * @param $token
    * @param $contribution
    * @param bool $html
    * @param bool $escapeSmarty
    *
+   * @deprecated
+   *
    * @return mixed|string
    * @throws \CRM_Core_Exception
    */
-  public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) {
+  public static function getContributionTokenReplacement($token, $contribution, $html = FALSE, $escapeSmarty = FALSE) {
     self::_buildContributionTokens();
 
     switch ($token) {
@@ -1787,6 +1837,10 @@ class CRM_Utils_Token {
         $value = CRM_Utils_Date::customFormat($value, $config->dateformatDatetime);
         break;
 
+      case 'source':
+        $value = CRM_Utils_Array::retrieveValueRecursive($contribution, 'contribution_source');
+        break;
+
       default:
         if (!in_array($token, self::$_tokens['contribution'])) {
           $value = "{contribution.$token}";
@@ -1820,18 +1874,14 @@ class CRM_Utils_Token {
    * Get all custom field tokens of $entity
    *
    * @param string $entity
-   * @param bool $usedForTokenWidget
-   *
    * @return array
    *   return custom field tokens in array('custom_N' => 'label') format
    */
-  public static function getCustomFieldTokens($entity, $usedForTokenWidget = FALSE) {
+  public static function getCustomFieldTokens($entity) {
     $customTokens = [];
-    $tokenName = $usedForTokenWidget ? "{contribution.custom_%d}" : "custom_%d";
     foreach (CRM_Core_BAO_CustomField::getFields($entity) as $id => $info) {
-      $customTokens[sprintf($tokenName, $id)] = $info['label'];
+      $customTokens['custom_' . $id] = $info['label'] . ' :: ' . $info['groupTitle'];
     }
-
     return $customTokens;
   }
 
@@ -1917,44 +1967,4 @@ class CRM_Utils_Token {
     ];
   }
 
-  /**
-   * Get the tokens available for the domain.
-   *
-   * @param int $domainID
-   * @param bool $html
-   *
-   * @return array
-   * @throws \CRM_Core_Exception
-   */
-  protected static function getDomainTokens(int $domainID, bool $html): array {
-    $cacheKey = __CLASS__ . 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n::getLocale();
-    if (!Civi::cache('metadata')->has($cacheKey)) {
-      if (CRM_Core_Config::domainID() === $domainID) {
-        $domain = CRM_Core_BAO_Domain::getDomain();
-      }
-      else {
-        $domain = new CRM_Core_BAO_Domain();
-        $domain->find(TRUE);
-      }
-      $tokens = [
-        'name' => $domain->name,
-        'id' => $domain->id,
-        'description' => $domain->description,
-      ];
-      $loc = $domain->getLocationValues();
-      if ($html) {
-        $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ?? ''));
-      }
-      else {
-        $tokens['address'] = $loc['address'][1]['display_text'] ?? '';
-      }
-      $phone = reset($loc['phone']);
-      $email = reset($loc['email']);
-      $tokens['phone'] = $phone['phone'] ?? '';
-      $tokens['email'] = $email['email'] ?? '';
-      Civi::cache('metadata')->set($cacheKey, $tokens);
-    }
-    return Civi::cache('metadata')->get($cacheKey);
-  }
-
 }