Merge pull request #5513 from mallezie/contact-select-file-16178
[civicrm-core.git] / CRM / Utils / Token.php
index f6f60ab92d3a4ee8dc62f1e395043f310d7eeaf4..b549e4aa9f1f6ae764e6456ca3b73bb8fd975f0f 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id: $
  *
  */
@@ -90,18 +90,11 @@ class CRM_Utils_Token {
     'welcome' => array('group'),
   );
 
+
   /**
-   * Check a string (mailing body) for required tokens.
-   *
-   * @param string $str
-   *   The message.
-   *
-   * @return bool|array
-   *    true if all required tokens are found,
-   *    else an array of the missing tokens
-   * @static
+   * @return array
    */
-  public static function requiredTokens(&$str) {
+  public static function getRequiredTokens() {
     if (self::$_requiredTokens == NULL) {
       self::$_requiredTokens = array(
         'domain.address' => ts("Domain address - displays your organization's postal address."),
@@ -113,9 +106,24 @@ class CRM_Utils_Token {
         ),
       );
     }
+    return self::$_requiredTokens;
+  }
+
+  /**
+   * Check a string (mailing body) for required tokens.
+   *
+   * @param string $str
+   *   The message.
+   *
+   * @return bool|array
+   *    true if all required tokens are found,
+   *    else an array of the missing tokens
+   */
+  public static function requiredTokens(&$str) {
+    $requiredTokens = self::getRequiredTokens();
 
     $missing = array();
-    foreach (self::$_requiredTokens as $token => $value) {
+    foreach ($requiredTokens as $token => $value) {
       if (!is_array($value)) {
         if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) {
           $missing[$token] = $value;
@@ -143,7 +151,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Wrapper for token matching
+   * Wrapper for token matching.
    *
    * @param string $type
    *   The token type (domain,mailing,contact,action).
@@ -152,9 +160,8 @@ class CRM_Utils_Token {
    * @param string $str
    *   The string to search.
    *
-   * @return boolean
+   * @return bool
    *   Was there a match
-   * @static
    */
   public static function token_match($type, $var, &$str) {
     $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}');
@@ -162,7 +169,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Wrapper for token replacing
+   * Wrapper for token replacing.
    *
    * @param string $type
    *   The token type.
@@ -176,7 +183,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) {
     $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}');
@@ -198,21 +204,19 @@ class CRM_Utils_Token {
    *
    * @return string
    *   regular expression sutiable for using in preg_replace
-   * @static
    */
   private static function tokenRegex($token_type) {
     return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(\-[\w\s]+)?)\}(?!\})/';
   }
 
   /**
-   * Escape the string so a malicious user cannot inject smarty code into the template
+   * Escape the string so a malicious user cannot inject smarty code into the template.
    *
    * @param string $string
    *   A string that needs to be escaped from smarty parsing.
    *
    * @return string
    *   the escaped string
-   * @static
    */
   private static function tokenEscapeSmarty($string) {
     // need to use negative look-behind, as both str_replace() and preg_replace() are sequential
@@ -234,7 +238,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceDomainTokens(
     $str,
@@ -336,11 +339,10 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
-    self::$_tokens['org'] =
-      array_merge(
+    self::$_tokens['org']
+      array_merge(
         array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')),
         array('address', 'display_name', 'checksum', 'contact_id')
       );
@@ -420,7 +422,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed sstring
-   * @static
    */
   public static function &replaceMailingTokens(
     $str,
@@ -555,7 +556,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceActionTokens(
     $str,
@@ -601,7 +601,7 @@ class CRM_Utils_Token {
     $escapeSmarty = FALSE
   ) {
     /* If the token is an email action, use it.  Otherwise, find the
-         * appropriate URL */
+     * appropriate URL */
 
     if (!in_array($token, self::$_tokens['action'])) {
       $value = "{action.$token}";
@@ -649,7 +649,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceContactTokens(
     $str,
@@ -663,8 +662,8 @@ class CRM_Utils_Token {
     if (self::$_tokens[$key] == NULL) {
       /* This should come from UF */
 
-      self::$_tokens[$key] =
-        array_merge(
+      self::$_tokens[$key]
+        array_merge(
           array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
           array('checksum', 'contact_id')
         );
@@ -709,8 +708,8 @@ class CRM_Utils_Token {
     if (self::$_tokens['contact'] == NULL) {
       /* This should come from UF */
 
-      self::$_tokens['contact'] =
-        array_merge(
+      self::$_tokens['contact']
+        array_merge(
           array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
           array('checksum', 'contact_id')
         );
@@ -772,7 +771,9 @@ class CRM_Utils_Token {
 
     if ($escapeSmarty
       && !($returnBlankToken && $noReplace)
-    ) { // $returnBlankToken means the caller wants to do further attempts at processing unreplaced tokens -- so don't escape them yet in this case.
+    ) {
+      // $returnBlankToken means the caller wants to do further attempts at
+      // processing unreplaced tokens -- so don't escape them yet in this case.
       $value = self::tokenEscapeSmarty($value);
     }
 
@@ -795,7 +796,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceHookTokens(
     $str,
@@ -817,7 +817,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Parse html through Smarty resolving any smarty functions
+   * Parse html through Smarty resolving any smarty functions.
    * @param string $tokenHtml
    * @param array $entity
    * @param string $entityType
@@ -872,15 +872,13 @@ class CRM_Utils_Token {
    * @param $str ref to the string that will be scanned and modified
    * @return void
    *   this function works directly on the string that is passed
-   * @access public
-   * @static
    */
   public static function unescapeTokens(&$str) {
     $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
   }
 
   /**
-   * Replace unsubscribe tokens
+   * Replace unsubscribe tokens.
    *
    * @param string $str
    *   The string with tokens to be replaced.
@@ -892,11 +890,10 @@ class CRM_Utils_Token {
    *   Replace tokens with html or plain text.
    * @param int $contact_id
    *   The contact ID.
-   * @param string hash The security hash of the unsub event
+   * @param string $hash The security hash of the unsub event
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceUnsubscribeTokens(
     $str,
@@ -927,7 +924,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Replace resubscribe tokens
+   * Replace resubscribe tokens.
    *
    * @param string $str
    *   The string with tokens to be replaced.
@@ -939,11 +936,10 @@ class CRM_Utils_Token {
    *   Replace tokens with html or plain text.
    * @param int $contact_id
    *   The contact ID.
-   * @param string hash The security hash of the resub event
+   * @param string $hash The security hash of the resub event
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceResubscribeTokens(
     $str, &$domain, &$groups, $html,
@@ -971,7 +967,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceSubscribeTokens($str, $group, $url, $html) {
     if (self::token_match('subscribe', 'group', $str)) {
@@ -991,7 +986,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceSubscribeInviteTokens($str) {
     if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
@@ -1039,7 +1033,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceWelcomeTokens($str, $group, $html) {
     if (self::token_match('welcome', 'group', $str)) {
@@ -1056,7 +1049,6 @@ class CRM_Utils_Token {
    *
    * @return array
    *   Array of tokens that weren't replaced
-   * @static
    */
   public static function &unmatchedTokens(&$str) {
     //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
@@ -1065,7 +1057,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Find and replace tokens for each component
+   * Find and replace tokens for each component.
    *
    * @param string $str
    *   The string to search.
@@ -1079,7 +1071,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
     if (!is_array($components) || empty($contact)) {
@@ -1105,15 +1096,13 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Get array of string tokens
+   * Get array of string tokens.
    *
    * @param string $string
    *   The input string to parse for tokens.
    *
    * @return array
    *   array of tokens mentioned in field
-   * @access public
-   * @static
    */
   public static function getTokens($string) {
     $matches = array();
@@ -1172,9 +1161,9 @@ class CRM_Utils_Token {
    * @param $contactIDs
    * @param array $returnProperties
    *   Of required properties.
-   * @param bool $skipOnHoldDon 't return on_hold contact info also.
+   * @param bool $skipOnHold Don't return on_hold contact info also.
    *   Don't return on_hold contact info also.
-   * @param bool $skipDeceasedDon 't return deceased contact info.
+   * @param bool $skipDeceased Don't return deceased contact info.
    *   Don't return deceased contact info.
    * @param array $extraParams
    *   Extra params.
@@ -1185,9 +1174,8 @@ class CRM_Utils_Token {
    *   The mailing list jobID - this is a legacy param.
    *
    * @return array
-   * @static
    */
-  static function getTokenDetails(
+  public static function getTokenDetails(
     $contactIDs,
     $returnProperties = NULL,
     $skipOnHold = TRUE,
@@ -1285,7 +1273,7 @@ class CRM_Utils_Token {
         foreach (array(
                    'email_greeting',
                    'postal_greeting',
-                   'addressee'
+                   'addressee',
                  ) as $val) {
           if (!empty($contactDetails[$contactID][$val])) {
             $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
@@ -1310,7 +1298,7 @@ class CRM_Utils_Token {
    *
    * @param array $contactIDs
    *   This should always be array(0) or its not anonymous - left to keep signature same.
-   * as main fn
+   *   as main fn
    * @param string $returnProperties
    * @param bool $skipOnHold
    * @param bool $skipDeceased
@@ -1323,7 +1311,7 @@ class CRM_Utils_Token {
    *   contactDetails with hooks swapped out
    */
   public function getAnonymousTokenDetails($contactIDs = array(
-      0
+      0,
     ),
                                            $returnProperties = NULL,
                                            $skipOnHold = TRUE,
@@ -1357,16 +1345,16 @@ class CRM_Utils_Token {
    * @param string $className
    *
    * @return array
-   * @static
    */
-  static function getContributionTokenDetails(
+  public static function getContributionTokenDetails(
     $contributionIDs,
     $returnProperties = NULL,
     $extraParams = NULL,
     $tokens = array(),
     $className = NULL
   ) {
-    //@todo - this function basically replications calling civicrm_api3('contribution', 'get', array('id' => array('IN' => array())
+    //@todo - this function basically replicates calling
+    //civicrm_api3('contribution', 'get', array('id' => array('IN' => array())
     if (empty($contributionIDs)) {
       // putting a fatal here so we can track if/when this happens
       CRM_Core_Error::fatal();
@@ -1411,14 +1399,14 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Get Membership Token Details
+   * Get Membership Token Details.
    * @param array $membershipIDs
    *   Array of membership IDS.
    */
   public static function getMembershipTokenDetails($membershipIDs) {
     $memberships = civicrm_api3('membership', 'get', array(
         'options' => array('limit' => 200000),
-        'membership_id' => array('IN' => (array) $membershipIDs)
+        'membership_id' => array('IN' => (array) $membershipIDs),
       ));
     return $memberships['values'];
   }
@@ -1444,7 +1432,8 @@ class CRM_Utils_Token {
       // check if there are any unevaluated tokens
       $greetingTokens = self::getTokens($tokenString);
 
-      // $greetingTokens not empty, means there are few tokens which are not evaluated, like custom data etc
+      // $greetingTokens not empty, means there are few tokens which are not
+      // evaluated, like custom data etc
       // so retrieve it from database
       if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) {
         $greetingsReturnProperties = array_flip(CRM_Utils_Array::value('contact', $greetingTokens));
@@ -1509,7 +1498,7 @@ class CRM_Utils_Token {
     foreach (array(
                'html',
                'text',
-               'subject'
+               'subject',
              ) as $prop) {
       if (!isset($tokens[$prop])) {
         continue;
@@ -1538,7 +1527,6 @@ class CRM_Utils_Token {
    *
    * @return string
    *   The processed string
-   * @static
    */
   public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
     $key = 'user';
@@ -1598,7 +1586,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Store membership tokens on the static _tokens array
+   * Store membership tokens on the static _tokens array.
    */
   protected static function _buildMembershipTokens() {
     $key = 'membership';
@@ -1613,7 +1601,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Replace tokens for an entity
+   * Replace tokens for an entity.
    * @param string $entity
    * @param array $entityArray
    *   (e.g. in format from api).
@@ -1641,7 +1629,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Replace Contribution tokens in html
+   * Replace Contribution tokens in html.
    *
    * @param string $str
    * @param array $contribution
@@ -1738,7 +1726,7 @@ class CRM_Utils_Token {
         try {
           $value = civicrm_api3('membership_type', 'getvalue', array(
               'id' => $membership['membership_type_id'],
-              'return' => 'minimum_fee'
+              'return' => 'minimum_fee',
             ));
         }
         catch (CiviCRM_API3_Exception $e) {
@@ -1820,6 +1808,7 @@ class CRM_Utils_Token {
 
   /**
    * Formats a token list for the select2 widget
+   *
    * @param $tokens
    * @return array
    */
@@ -1857,4 +1846,5 @@ class CRM_Utils_Token {
 
     return $output;
   }
+
 }