Commit 6e823542 authored by Rich's avatar Rich

Tokens old, tokens new

parent 2e53f0c7
......@@ -166,6 +166,10 @@ function actionlinks_civicrm_container($container) {
array(\Civi\Token\Events::TOKEN_EVALUATE, 'actionlinks_evaluate_tokens')
);
}
/**
* Add tokens the new way.
*
*/
function actionlinks_register_tokens(\Civi\Token\Event\TokenRegisterEvent $e) {
$contact = $e->entity('contact');
......@@ -232,7 +236,9 @@ function actionlinks_evaluate_tokens(\Civi\Token\Event\TokenValueEvent $e) {
}
/**
* Declare tokens the old skool way.
* Declare tokens the old skool way. This works with message templates.
*
* Tokens are like {actionlink.N} where N is the link ID.
*/
function actionlinks_civicrm_tokens(&$tokens) {
// Get list of all active Action Links, add tokens for them like actionLinkN where N is the ID.
......@@ -242,31 +248,33 @@ function actionlinks_civicrm_tokens(&$tokens) {
return;
}
while ($bao->fetch()) {
$tokens['contact']["contact.actionLink$bao->id"] = ts('Action Link "%1"', [1 => $bao->name]);
$tokens['actionlink']["actionlink.$bao->id"] = ts('Action Link "%1"', [1 => $bao->name]);
}
}
/**
* Provide tokens the old skool way.
* Provide token values the old skool way. This works with message templates.
*
* Tokens are like {actionlink.N} where N is the link ID.
*/
function actionlinks_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
if (!isset($tokens['contact'])) {
if (!isset($tokens['actionlink'])) {
return;
}
// $tokens is sometimes like: { 'contact': { 'foo': 1 } } and sometimes like { 'contact': ['foo'] }
if (is_numeric(key($tokens['contact']))) {
if (is_numeric(key($tokens['actionlink']))) {
// We have the 2nd form.
$_ = array_values($tokens['contact']);
$_ = array_values($tokens['actionlink']);
}
else {
// tokens are keys.
$_ = array_keys($tokens['contact']);
$_ = array_keys($tokens['actionlink']);
}
$actionLinkIDs = [];
foreach ($_ as $token) {
$matches = [];
if (preg_match('/^actionLink(\d+)$/', $token, $matches)) {
if (preg_match('/^(\d+)$/', $token, $matches)) {
$actionLinkIDs[] = (int) $matches[1];
}
}
......@@ -293,26 +301,28 @@ function actionlinks_civicrm_tokenValues(&$values, $cids, $job = null, $tokens =
->execute();
foreach ($result as $actionLinkRow) {
$values[$actionLinkRow['contactID']]["contact.actionLink$actionLinkID"] = $actionLinkRow['link'];
$values[$actionLinkRow['contactID']]["actionlink.$actionLinkID"] = $actionLinkRow['link'];
}
}
}
/* test/debugging code for tokens - kept because I suspect this is not over...
function actionlinx() {
// ------------------------- this sends a message tpl ---------------
// it needs old type tokens like {actionlink.1}
$from = CRM_Sos_Utils::getFromEmailAddress();
$params = [
'id' => 178,
'id' => 178, // put in a valid message template ID.
'from' => $from,
'to_email' => 'forums@artfulrobot.uk',
'contact_id' => 9497,
//'template_params' => []
//'bcc' => "forums@artfulrobot.uk", // so I can keep an eye.
'to_email' => 'youremail@localhost',
'contact_id' => 9497, // Put in a valid ID
];
$result = civicrm_api3('MessageTemplate', 'send', $params);
print_r($result);
}
/* test/debugging code for tokens
// ------------------------- this outputs new style stuff ---------
// it needs new type tokens like {contact.actionLink1}
$p = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), array(
'controller' => 'foo',
'smarty' => FALSE,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment