* @return null|string
*/
public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE): ?string {
- // 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
-
- $loc = $domain->getLocationValues();
-
- if (!in_array($token, self::$_tokens['domain'])) {
- $value = "{domain.$token}";
- }
- elseif ($token === 'address') {
- $cacheKey = __CLASS__ . 'address_token_cache' . CRM_Core_Config::domainID();
- $addressCache = Civi::cache()->has($cacheKey) ? Civi::cache()->get($cacheKey) : [];
-
- $fieldKey = $html ? 'address-html' : 'address-text';
- if (array_key_exists($fieldKey, $addressCache)) {
- return $addressCache[$fieldKey];
- }
-
- $value = NULL;
- // Construct the address token
-
- if (!empty($loc[$token])) {
- if ($html) {
- $value = str_replace("\n", '<br />', $loc[$token][1]['display']);
- }
- else {
- $value = $loc[$token][1]['display_text'];
- }
- Civi::cache()->set($cacheKey, $addressCache);
- }
- }
- elseif ($token === 'name' || $token === 'id' || $token === 'description') {
- $value = $domain->$token;
- }
- elseif ($token === 'phone' || $token === 'email') {
- // Construct the phone and email tokens
-
- $value = NULL;
- if (!empty($loc[$token])) {
- foreach ($loc[$token] as $index => $entity) {
- $value = $entity[$token];
- break;
- }
- }
- }
-
+ $tokens = self::getDomainTokens($domain->id, $html);
+ $value = $tokens[$token] ?? "{domain.$token}";
if ($escapeSmarty) {
$value = self::tokenEscapeSmarty($value);
}
-
return $value;
}
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
$fn = is_callable(['CRM_Utils_Token', $fn]) ? $fn : 'getApiTokenReplacement';
// 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 = self::$fn($entity, $token, $entityArray);
+ // We are now supporting the syntax case_type_id:label
+ // so strip anything after the ':'
+ // (we aren't supporting 'name' at this stage, so we can assume 'label'
+ // test cover in TokenConsistencyTest.
+ $parts = explode(':', $token);
+ $replacement = self::$fn($entity, $parts[0], $entityArray);
if ($escapeSmarty) {
$replacement = self::tokenEscapeSmarty($replacement);
}
* @return string
* @throws \CiviCRM_API3_Exception
*/
- public static function replaceCaseTokens($caseId, $str, $knownTokens = [], $escapeSmarty = FALSE) {
- if (!$knownTokens || empty($knownTokens['case'])) {
+ public static function replaceCaseTokens($caseId, $str, $knownTokens = NULL, $escapeSmarty = FALSE): string {
+ if (strpos($str, '{case.') === FALSE) {
return $str;
}
+ if (!$knownTokens) {
+ $knownTokens = self::getTokens($str);
+ }
$case = civicrm_api3('case', 'getsingle', ['id' => $caseId]);
return self::replaceEntityTokens('case', $case, $str, $knownTokens, $escapeSmarty);
}
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]);
/**
* 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
* @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;
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);
$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[sprintf($tokenName, $id)] = $info['label'] . ' :: ' . $info['groupTitle'];
}
return $customTokens;
return $value;
}
+ /**
+ * Get token deprecation information.
+ *
+ * @return array
+ */
+ public static function getTokenDeprecations(): array {
+ return [
+ 'WorkFlowMessageTemplates' => [
+ 'contribution_invoice_receipt' => [
+ '$display_name' => 'contact.display_name',
+ ],
+ 'contribution_online_receipt' => [
+ '$contributeMode' => 'no longer available / relevant',
+ '$first_name' => 'contact.first_name',
+ '$last_name' => 'contact.last_name',
+ '$displayName' => 'contact.display_name',
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * 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);
+ }
+
}