Merge pull request #2350 from eileenmcnaughton/CRM-14012
[civicrm-core.git] / CRM / Utils / Token.php
index 6ca9aa722eda8fb872c5b018e321bb8b796bd385..2d659e5edd47a31e31c2a093a7272fdc07ec6f4e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -644,6 +644,9 @@ class CRM_Utils_Token {
 
     $value = NULL;
 
+    // Support legacy tokens
+    $token = CRM_Utils_Array::value($token, self::legacyContactTokens(), $token);
+
     // check if the token we were passed is valid
     // we have to do this because this function is
     // called only when we find a token in the string
@@ -663,6 +666,18 @@ class CRM_Utils_Token {
     }
     else {
       $value = CRM_Utils_Array::retrieveValueRecursive($contact, $token);
+
+      // FIXME: for some pseudoconstants we get array ( 0 => id, 1 => label )
+      if (is_array($value)) {
+        $value = $value[1];
+      }
+      // Convert pseudoconstants using metadata
+      elseif ($value && is_numeric($value)) {
+        $allFields = CRM_Contact_BAO_Contact::exportableFields('All');
+        if (!empty($allFields[$token]['pseudoconstant'])) {
+          $value = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $token, $value);
+        }
+      }
     }
 
     if (!$html) {
@@ -712,6 +727,22 @@ class CRM_Utils_Token {
     return $str;
   }
 
+  /**
+   * Parse html through Smarty resolving any smarty functions
+   * @param string $tokenHtml
+   * @param array $entity
+   * @param string $entityType
+   * @return string html parsed through smarty
+   */
+  public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
+    if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
+      $smarty = CRM_Core_Smarty::singleton();
+      // also add the tokens to the template
+      $smarty->assign_by_ref($entityType, $entity);
+      $tokenHtml = $smarty->fetch("string:$tokenHtml");
+    }
+    return $tokenHtml;
+  }
   public static function getHookTokenReplacement(
     $token,
     &$contact,
@@ -1155,6 +1186,12 @@ class CRM_Utils_Token {
           $details[$dao->id]['campaign'] = $campaigns[$campaignId];
         }
 
+        if (CRM_Utils_Array::value('financial_type_id', $details[$dao->id])) {
+          $financialtypeId = $details[$dao->id]['financial_type_id'];
+          $ftis = CRM_Contribute_PseudoConstant::financialType();
+          $details[$dao->id]['financial_type'] = $ftis[$financialtypeId];
+        }
+
         // TODO: call a hook to get token contribution details
       }
     }
@@ -1162,6 +1199,14 @@ class CRM_Utils_Token {
     return $details;
   }
 
+  /**
+   * Get Membership Token Details
+   * @param array $membershipIDs array of membership IDS
+   */
+  static function getMembershipTokenDetails($membershipIDs) {
+    $memberships = civicrm_api3('membership', 'get', array('options' => array('limit' => 200000), 'membership_id' => array('IN' => (array) $membershipIDs)));
+    return $memberships['values'];
+  }
   /**
    * replace greeting tokens exists in message/subject
    *
@@ -1289,6 +1334,45 @@ class CRM_Utils_Token {
     }
   }
 
+  /**
+   * store membership tokens on the static _tokens array
+   */
+  protected static function _buildMembershipTokens() {
+    $key = 'membership';
+    if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) {
+      $membershipTokens = array();
+      $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
+   * @param array $entityArray (e.g. in format from api)
+   * @param string $str string to replace in
+   * @param array $knownTokens array of tokens present
+   * @param boolean $escapeSmarty
+   * @return string string with replacements made
+   */
+  public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = array(), $escapeSmarty = FALSE) {
+    if (!$knownTokens || !CRM_Utils_Array::value($entity, $knownTokens)) {
+      return $str;
+    }
+
+    $fn = 'get' . ucFirst($entity) . 'tokenReplacement';
+    //since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace
+    foreach ($knownTokens[$entity] as $token) {
+      $replaceMent = CRM_Utils_Token::$fn($token, $entityArray, $escapeSmarty);
+      $str = str_replace('{' . $entity . '.' . $token . '}', $replaceMent, $str);
+    }
+    $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
+    return $str;
+  }
+
   public static function &replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
     self::_buildContributionTokens();
 
@@ -1313,6 +1397,52 @@ class CRM_Utils_Token {
     return $str;
   }
 
+  /**
+   * 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
+   * @param string $token
+   * @param array $membership an api result array for a single membership
+   * @param boolean $escapeSmarty
+   * @return string token replacement
+   */
+  public static function getMembershipTokenReplacement($token, $membership, $escapeSmarty = FALSE) {
+    $entity = 'membership';
+    self::_buildMembershipTokens();
+   switch ($token) {
+     case 'type':
+       $value = $membership['membership_name'];
+       break;
+     case 'status':
+       $statuses = CRM_Member_BAO_Membership::buildOptions('status_id');
+       $value = $statuses[$membership['status_id']];
+       break;
+     case 'fee':
+       try{
+         $value = civicrm_api3('membership_type', 'getvalue', array('id' => $membership['membership_type_id'], 'return' => 'minimum_fee'));
+       }
+       catch (CiviCRM_API3_Exception $e) {
+         // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the
+         // api handles NULL (4.4)
+         $value = 0;
+       }
+       break;
+     default:
+       if (in_array($token, self::$_tokens[$entity])) {
+         $value = $membership[$token];
+       }
+       else {
+         //ie unchanged
+         $value = "{$entity}.{$token}";
+       }
+       break;
+    }
+
+    if ($escapeSmarty) {
+      $value = self::tokenEscapeSmarty($value);
+    }
+    return $value;
+  }
+
   public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) {
     self::_buildContributionTokens();
 
@@ -1349,4 +1479,16 @@ class CRM_Utils_Token {
   function getPermissionEmails($permissionName) {}
 
   function getRoleEmails($roleName) {}
+
+  /**
+   * @return array: legacy_token => new_token
+   */
+  static function legacyContactTokens() {
+    return array(
+      'individual_prefix' => 'prefix_id',
+      'individual_suffix' => 'suffix_id',
+      'gender' => 'gender_id',
+    );
+  }
+
 }