+--------------------------------------------------------------------+
| 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: $
*
*/
'welcome' => array('group'),
);
+
/**
- * Check a string (mailing body) for required tokens.
- *
- * @param string $str
- * The message.
- *
- * @return true|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."),
),
);
}
+ 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;
}
/**
- * Wrapper for token matching
+ * Wrapper for token matching.
*
* @param string $type
* The token type (domain,mailing,contact,action).
* @param string $str
* The string to search.
*
- * @return boolean Was there a match
- * @static
+ * @return bool
+ * Was there a match
*/
public static function token_match($type, $var, &$str) {
$token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}');
}
/**
- * Wrapper for token replacing
+ * Wrapper for token replacing.
*
* @param string $type
* The token type.
*
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) {
$token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}');
* @param string $token_type
* A string indicating the the type of token to be used in the expression.
*
- * @return string regular expression sutiable for using in preg_replace
- * @static
+ * @return string
+ * regular expression sutiable for using in preg_replace
*/
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
+ * @return string
+ * the escaped string
*/
private static function tokenEscapeSmarty($string) {
// need to use negative look-behind, as both str_replace() and preg_replace() are sequential
* @param null $knownTokens
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceDomainTokens(
$str,
) {
$key = 'domain';
if (
- !$knownTokens || empty($knownTokens[$key])) {
+ !$knownTokens || empty($knownTokens[$key])
+ ) {
return $str;
}
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use(&$domain, $html, $escapeSmarty) {
- return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
+ function ($matches) use (&$domain, $html, $escapeSmarty) {
+ return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
},
$str
);
*
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
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')
);
* @param null $knownTokens
* @param bool $escapeSmarty
*
- * @return string The processed sstring
- * @static
+ * @return string
+ * The processed sstring
*/
public static function &replaceMailingTokens(
$str,
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use(&$mailing, $escapeSmarty) {
- return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
+ function ($matches) use (&$mailing, $escapeSmarty) {
+ return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
},
$str
);
*
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceActionTokens(
$str,
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use(&$addresses, &$urls, $html, $escapeSmarty) {
- return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
+ function ($matches) use (&$addresses, &$urls, $html, $escapeSmarty) {
+ return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
},
$str
);
$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}";
*
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceContactTokens(
$str,
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')
);
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use(&$contact, $html, $returnBlankToken, $escapeSmarty) {
- return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
+ function ($matches) use (&$contact, $html, $returnBlankToken, $escapeSmarty) {
+ return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
},
$str
);
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')
);
}
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 && $noReplace)
+ ) {
+ // $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);
}
*
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceHookTokens(
$str,
foreach ($categories as $key) {
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use(&$contact, $key, $html, $escapeSmarty) {
- return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
+ function ($matches) use (&$contact, $key, $html, $escapeSmarty) {
+ return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
},
$str
);
}
/**
- * 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
- * @return string html parsed through smarty
+ * @return string
+ * html parsed through smarty
*/
public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
* this routine will remove the extra backslashes and braces
*
* @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
+ * @return void
+ * this function works directly on the string that is passed
*/
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.
* 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
+ * @return string
+ * The processed string
*/
public static function &replaceUnsubscribeTokens(
$str,
}
/**
- * Replace resubscribe tokens
+ * Replace resubscribe tokens.
*
* @param string $str
* The string with tokens to be replaced.
* 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
+ * @return string
+ * The processed string
*/
public static function &replaceResubscribeTokens(
$str, &$domain, &$groups, $html,
* @param bool $html
* Replace tokens with html or plain text.
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceSubscribeTokens($str, $group, $url, $html) {
if (self::token_match('subscribe', 'group', $str)) {
* @param string $str
* The string with tokens to be replaced.
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceSubscribeInviteTokens($str) {
if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) {
foreach ($matches as $key => $value) {
- $gid = substr($value, 18, -1);
- $config = CRM_Core_Config::singleton();
- $domain = CRM_Core_BAO_MailSettings::defaultDomain();
+ $gid = substr($value, 18, -1);
+ $config = CRM_Core_Config::singleton();
+ $domain = CRM_Core_BAO_MailSettings::defaultDomain();
$localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
// we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash)
$str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str);
* @param bool $html
* Replace tokens with html or plain text.
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceWelcomeTokens($str, $group, $html) {
if (self::token_match('welcome', 'group', $str)) {
* @param string $str
* The string to search.
*
- * @return array Array of tokens that weren't replaced
- * @static
+ * @return array
+ * Array of tokens that weren't replaced
*/
public static function &unmatchedTokens(&$str) {
//preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
}
/**
- * Find and replace tokens for each component
+ * Find and replace tokens for each component.
*
* @param string $str
* The string to search.
* @param bool $escapeSmarty
* @param bool $returnEmptyToken
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
if (!is_array($components) || empty($contact)) {
}
/**
- * Get array of string tokens
+ * Get array of string tokens.
*
* @param string $string
* The input string to parse for tokens.
*
- * @return array $tokens array of tokens mentioned in field@access public
- * @static
+ * @return array
+ * array of tokens mentioned in field
*/
public static function getTokens($string) {
$matches = array();
* Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function
* and getTokens appears to be historical rather than intentional and should be reviewed
* @param $string
- * @return array fields to pass in as return properties when populating token
- *
+ * @return array
+ * fields to pass in as return properties when populating token
*/
public static function getReturnProperties(&$string) {
$returnProperties = array();
$matches = array();
preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
- $string,
- $matches,
- PREG_PATTERN_ORDER
- );
+ $string,
+ $matches,
+ PREG_PATTERN_ORDER
+ );
if ($matches[1]) {
foreach ($matches[1] as $token) {
list($type, $name) = preg_split('/\./', $token, 2);
* @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.
* The mailing list jobID - this is a legacy param.
*
* @return array
- * @static
*/
- static function getTokenDetails(
+ public static function getTokenDetails(
$contactIDs,
$returnProperties = NULL,
$skipOnHold = TRUE,
foreach ($contactIDs as $key => $contactID) {
$params[] = array(
CRM_Core_Form::CB_PREFIX . $contactID,
- '=', 1, 0, 0,
+ '=',
+ 1,
+ 0,
+ 0,
);
}
//special case for greeting replacement
foreach (array(
- 'email_greeting', 'postal_greeting', 'addressee') as $val) {
+ 'email_greeting',
+ 'postal_greeting',
+ 'addressee',
+ ) as $val) {
if (!empty($contactDetails[$contactID][$val])) {
$contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
}
*
* @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
* @param string $className
* Sent as context to the hook.
* @param string $jobID
- * @return array contactDetails with hooks swapped out
+ * @return array
+ * contactDetails with hooks swapped out
*/
public function getAnonymousTokenDetails($contactIDs = array(
- 0),
- $returnProperties = NULL,
- $skipOnHold = TRUE,
- $skipDeceased = TRUE,
- $extraParams = NULL,
- $tokens = array(),
- $className = NULL,
- $jobID = NULL) {
+ 0,
+ ),
+ $returnProperties = NULL,
+ $skipOnHold = TRUE,
+ $skipDeceased = TRUE,
+ $extraParams = NULL,
+ $tokens = array(),
+ $className = NULL,
+ $jobID = NULL) {
$details = array(0 => array());
// also call a hook and get token details
CRM_Utils_Hook::tokenValues($details[0],
* @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();
}
/**
- * 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)));
+ $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
- *
*/
public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
// 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));
$flattenTokens = array();
foreach (array(
- 'html', 'text', 'subject') as $prop) {
+ 'html',
+ 'text',
+ 'subject',
+ ) as $prop) {
if (!isset($tokens[$prop])) {
continue;
}
* @param null $knownTokens
* @param bool $escapeSmarty
*
- * @return string The processed string
- * @static
+ * @return string
+ * The processed string
*/
public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
$key = 'user';
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use($escapeSmarty) {
- return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty);
+ function ($matches) use ($escapeSmarty) {
+ return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty);
},
$str
);
}
/**
- *
*/
protected static function _buildContributionTokens() {
$key = 'contribution';
if (self::$_tokens[$key] == NULL) {
self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
- array('campaign', 'financial_type')
- ));
+ array('campaign', 'financial_type')
+ ));
}
}
/**
- * Store membership tokens on the static _tokens array
+ * Store membership tokens on the static _tokens array.
*/
protected static function _buildMembershipTokens() {
$key = 'membership';
}
/**
- * Replace tokens for an entity
+ * Replace tokens for an entity.
* @param string $entity
* @param array $entityArray
* (e.g. in format from api).
* @param array $knownTokens
* Array of tokens present.
* @param bool $escapeSmarty
- * @return string string with replacements made
+ * @return string
+ * string with replacements made
*/
public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = array(), $escapeSmarty = FALSE) {
if (!$knownTokens || empty($knownTokens[$entity])) {
}
/**
- * Replace Contribution tokens in html
+ * Replace Contribution tokens in html.
*
* @param string $str
* @param array $contribution
* @param string $knownTokens
* @param bool|string $escapeSmarty
*
- * @return unknown|Ambigous <string, mixed>|mixed
+ * @return mixed
*/
public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
$key = 'contribution';
$str = preg_replace_callback(
self::tokenRegex($key),
- function ($matches) use(&$contribution, $html, $escapeSmarty) {
- return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
+ function ($matches) use (&$contribution, $html, $escapeSmarty) {
+ return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
},
$str
);
* @param array $membership
* An api result array for a single membership.
* @param bool $escapeSmarty
- * @return string token replacement
+ * @return string
+ * token replacement
*/
public static function getMembershipTokenReplacement($token, $membership, $escapeSmarty = FALSE) {
$entity = 'membership';
case 'fee':
try {
- $value = civicrm_api3('membership_type', 'getvalue', array('id' => $membership['membership_type_id'], 'return' => 'minimum_fee'));
+ $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
}
/**
- * @return array: legacy_token => new_token
+ * @return array
+ * legacy_token => new_token
*/
public static function legacyContactTokens() {
return array(
/**
* Formats a token list for the select2 widget
+ *
* @param $tokens
* @return array
*/
return $output;
}
+
}