Skip to content

Commit

Permalink
alternative way to handle custom field token
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Jan 12, 2017
1 parent bba4f3f commit 4e9b6a6
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 74 deletions.
18 changes: 12 additions & 6 deletions CRM/Activity/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
));
}

Expand Down Expand Up @@ -107,6 +110,9 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe
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, '');
}
Expand Down
4 changes: 4 additions & 0 deletions CRM/Contribute/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function __construct() {
$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);
}

Expand Down Expand Up @@ -135,6 +136,9 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe
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);
}
Expand Down
36 changes: 21 additions & 15 deletions CRM/Event/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
));
}

Expand Down Expand Up @@ -139,6 +142,9 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe
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, '');
}
Expand Down
22 changes: 14 additions & 8 deletions CRM/Member/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
));
}

Expand Down Expand Up @@ -95,6 +98,9 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe
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, '');
}
Expand Down
50 changes: 16 additions & 34 deletions Civi/Token/AbstractTokenSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ public function registerTokens(TokenRegisterEvent $e) {
}
}

/**
* 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.
Expand Down Expand Up @@ -148,50 +164,16 @@ public function evaluateTokens(TokenValueEvent $e) {
}

$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.
Expand Down
17 changes: 6 additions & 11 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,12 @@ public function onEvaluate(TokenValueEvent $e) {
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);
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions Civi/Token/TokenRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ public function tokens($a = NULL, $b = NULL, $c = NULL) {
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.
*
Expand Down

0 comments on commit 4e9b6a6

Please sign in to comment.