From: deb.monish Date: Thu, 12 Jan 2017 14:29:17 +0000 (+0530) Subject: alternative way to handle custom field token X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4e9b6a623b3acc106ec16b1e2c4a1ad580f7119c;p=civicrm-core.git alternative way to handle custom field token --- diff --git a/CRM/Activity/Tokens.php b/CRM/Activity/Tokens.php index ed487e2f11..94cbd4807c 100644 --- a/CRM/Activity/Tokens.php +++ b/CRM/Activity/Tokens.php @@ -49,12 +49,15 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber { * CRM_Activity_Tokens constructor. */ public function __construct() { - parent::__construct('activity', array( - 'activity_id' => ts('Activity ID'), - 'activity_type' => ts('Activity Type'), - 'subject' => ts('Activity Subject'), - 'details' => ts('Activity Details'), - 'activity_date_time' => ts('Activity Date-Time'), + parent::__construct('activity', array_merge( + array( + 'activity_id' => ts('Activity ID'), + 'activity_type' => ts('Activity Type'), + 'subject' => ts('Activity Subject'), + 'details' => ts('Activity Details'), + 'activity_date_time' => ts('Activity Date-Time'), + ), + $this->getCustomTokens('Activity') )); } @@ -107,6 +110,9 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber { elseif (isset($actionSearchResult->$field)) { $row->tokens($entity, $field, $actionSearchResult->$field); } + elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $field, $actionSearchResult->entity_id); + } else { $row->tokens($entity, $field, ''); } diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index c7fe1fdbda..62dcbe5e51 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -85,6 +85,7 @@ class CRM_Contribute_Tokens extends \Civi\Token\AbstractTokenSubscriber { $tokens['source'] = ts('Contribution Source'); $tokens['status'] = ts('Contribution Status'); $tokens['type'] = ts('Financial Type'); + $tokens = array_merge($tokens, $this->getCustomTokens('Contribution')); parent::__construct('contribution', $tokens); } @@ -135,6 +136,9 @@ class CRM_Contribute_Tokens extends \Civi\Token\AbstractTokenSubscriber { elseif (isset($aliasTokens[$field])) { $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue); } + elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $field, $actionSearchResult->entity_id); + } else { $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue); } diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php index 2a50255440..923a5414d5 100644 --- a/CRM/Event/Tokens.php +++ b/CRM/Event/Tokens.php @@ -44,21 +44,24 @@ class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { * Class constructor. */ public function __construct() { - parent::__construct('event', array( - 'event_type' => ts('Event Type'), - 'title' => ts('Event Title'), - 'event_id' => ts('Event ID'), - 'start_date' => ts('Event Start Date'), - 'end_date' => ts('Event End Date'), - 'summary' => ts('Event Summary'), - 'description' => ts('Event Description'), - 'location' => ts('Event Location'), - 'info_url' => ts('Event Info URL'), - 'registration_url' => ts('Event Registration URL'), - 'fee_amount' => ts('Event Fee'), - 'contact_email' => ts('Event Contact (Email)'), - 'contact_phone' => ts('Event Contact (Phone)'), - 'balance' => ts('Event Balance'), + parent::__construct('event', array_merge( + array( + 'event_type' => ts('Event Type'), + 'title' => ts('Event Title'), + 'event_id' => ts('Event ID'), + 'start_date' => ts('Event Start Date'), + 'end_date' => ts('Event End Date'), + 'summary' => ts('Event Summary'), + 'description' => ts('Event Description'), + 'location' => ts('Event Location'), + 'info_url' => ts('Event Info URL'), + 'registration_url' => ts('Event Registration URL'), + 'fee_amount' => ts('Event Fee'), + 'contact_email' => ts('Event Contact (Email)'), + 'contact_phone' => ts('Event Contact (Phone)'), + 'balance' => ts('Event Balance'), + ), + $this->getCustomTokens('Event') )); } @@ -139,6 +142,9 @@ LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id elseif (isset($actionSearchResult->$field)) { $row->tokens($entity, $field, $actionSearchResult->$field); } + elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $field, $actionSearchResult->entity_id); + } else { $row->tokens($entity, $field, ''); } diff --git a/CRM/Member/Tokens.php b/CRM/Member/Tokens.php index 983ae10035..26adb29f48 100644 --- a/CRM/Member/Tokens.php +++ b/CRM/Member/Tokens.php @@ -44,14 +44,17 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber { * Class constructor. */ public function __construct() { - parent::__construct('membership', array( - 'fee' => ts('Membership Fee'), - 'id' => ts('Membership ID'), - 'join_date' => ts('Membership Join Date'), - 'start_date' => ts('Membership Start Date'), - 'end_date' => ts('Membership End Date'), - 'status' => ts('Membership Status'), - 'type' => ts('Membership Type'), + parent::__construct('membership', array_merge( + array( + 'fee' => ts('Membership Fee'), + 'id' => ts('Membership ID'), + 'join_date' => ts('Membership Join Date'), + 'start_date' => ts('Membership Start Date'), + 'end_date' => ts('Membership End Date'), + 'status' => ts('Membership Status'), + 'type' => ts('Membership Type'), + ), + $this->getCustomTokens('Membership') )); } @@ -95,6 +98,9 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber { elseif (isset($actionSearchResult->$field)) { $row->tokens($entity, $field, $actionSearchResult->$field); } + elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $field, $actionSearchResult->entity_id); + } else { $row->tokens($entity, $field, ''); } diff --git a/Civi/Token/AbstractTokenSubscriber.php b/Civi/Token/AbstractTokenSubscriber.php index e0f712f427..1164032de8 100644 --- a/Civi/Token/AbstractTokenSubscriber.php +++ b/Civi/Token/AbstractTokenSubscriber.php @@ -117,6 +117,22 @@ abstract class AbstractTokenSubscriber implements EventSubscriberInterface { } } + /** + * Get all custom field tokens of $entity + * + * @param string $entity + * @return array $customTokens + * return custom field tokens in array('custom_N' => 'label') format + */ + public function getCustomTokens($entity) { + $customTokens = array(); + foreach (\CRM_Core_BAO_CustomField::getFields($entity) as $id => $info) { + $customTokens["custom_$id"] = $info['label']; + } + + return $customTokens; + } + /** * Alter the query which prepopulates mailing data * for scheduled reminders. @@ -148,50 +164,16 @@ abstract class AbstractTokenSubscriber implements EventSubscriberInterface { } $activeTokens = array_intersect($messageTokens[$this->entity], array_keys($this->tokenNames)); - $extraTokens = array_diff($messageTokens[$this->entity], array_keys($this->tokenNames)); $prefetch = $this->prefetch($e); foreach ($e->getRows() as $row) { - $this->evaluateExtraToken($row, $this->entity, $extraTokens); foreach ((array) $activeTokens as $field) { $this->evaluateToken($row, $this->entity, $field, $prefetch); } } } - /** - * Populate the custom field and other remaining entity token data. - * - * @param TokenRow $row - * The record for which we want token values. - * @param string $entity - * The entity for which we want the token values - * @param array $tokens - * The array of tokens whose data need to be fetched - */ - public function evaluateExtraToken(TokenRow $row, $entity, $tokens) { - if (empty($tokens)) { - return; - } - - $actionSearchResult = $row->context['actionSearchResult']; - - try { - $result = civicrm_api3($entity, 'getsingle', array( - 'id' => $actionSearchResult->entity_id, - 'return' => $tokens, - )); - } - catch (CiviCRM_API3_Exception $e) { - return; - } - - foreach ($tokens as $token) { - $row->tokens($entity, $token, \CRM_Utils_Array::value($token, $result, '')); - } - } - /** * To perform a bulk lookup before rendering tokens, override this * function and return the prefetched data. diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index 48d7984911..7a2ce239a7 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -62,17 +62,12 @@ class TokenCompatSubscriber implements EventSubscriberInterface { throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']); } - $contactTokens = \CRM_Utils_Array::value('contact', $messageTokens); - if (!empty($contactTokens)) { - try { - $result = civicrm_api3('Contact', 'getsingle', array( - 'id' => $contactId, - 'return' => $contactTokens, - )); - $contact = array_merge($contact, $result); - } - catch (CiviCRM_API3_Exception $e) { - //do nothing + //update value of custom field token + if (!empty($messageTokens['contact'])) { + foreach ($messageTokens['contact'] as $token) { + if (\CRM_Core_BAO_CustomField::getKeyID($token)) { + $row->customToken('Contact', $token, $contactId); + } } } } diff --git a/Civi/Token/TokenRow.php b/Civi/Token/TokenRow.php index 3c77f31d12..1a3033a9ac 100644 --- a/Civi/Token/TokenRow.php +++ b/Civi/Token/TokenRow.php @@ -126,6 +126,22 @@ class TokenRow { return $this; } + /** + * Update the value of a custom field token. + * + * @param string $entity + * @param string $fieldName + * @param string $entityID + * @return TokenRow + */ + public function customToken($entity, $fieldName, $entityID) { + $fieldValue = civicrm_api3($entity, 'getvalue', array( + 'return' => $fieldName, + 'id' => $entityID, + )); + return $this->tokens($entity, $fieldName, $fieldValue); + } + /** * Update the value of a token. Apply formatting based on DB schema. *