Commit 2e53f0c7 authored by Rich's avatar Rich

WIP

parent 6cab7c2b
......@@ -230,3 +230,105 @@ function actionlinks_evaluate_tokens(\Civi\Token\Event\TokenValueEvent $e) {
}
}
}
/**
* Declare tokens the old skool way.
*/
function actionlinks_civicrm_tokens(&$tokens) {
// Get list of all active Action Links, add tokens for them like actionLinkN where N is the ID.
$bao = new CRM_Actionlinks_BAO_ActionLink();
$bao->is_active = 1;
if (!$bao->find()) {
return;
}
while ($bao->fetch()) {
$tokens['contact']["contact.actionLink$bao->id"] = ts('Action Link "%1"', [1 => $bao->name]);
}
}
/**
* Provide tokens the old skool way.
*/
function actionlinks_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
if (!isset($tokens['contact'])) {
return;
}
// $tokens is sometimes like: { 'contact': { 'foo': 1 } } and sometimes like { 'contact': ['foo'] }
if (is_numeric(key($tokens['contact']))) {
// We have the 2nd form.
$_ = array_values($tokens['contact']);
}
else {
// tokens are keys.
$_ = array_keys($tokens['contact']);
}
$actionLinkIDs = [];
foreach ($_ as $token) {
$matches = [];
if (preg_match('/^actionLink(\d+)$/', $token, $matches)) {
$actionLinkIDs[] = (int) $matches[1];
}
}
if (!$actionLinkIDs) {
// No actionLink tokens
return;
}
// get contactIDs.
$contactIDs = [];
foreach ($cids as $contactID) {
if (((int) ($contactID ?? 0)) > 0) {
$contactIDs[] = (int) $contactID;
}
}
// Create links
$tokens = [];
foreach ($actionLinkIDs as $actionLinkID) {
$result = \Civi\Api4\ActionLink::getLink()
->setActionLinkID($actionLinkID)
->setContactIDs($contactIDs)
->execute();
foreach ($result as $actionLinkRow) {
$values[$actionLinkRow['contactID']]["contact.actionLink$actionLinkID"] = $actionLinkRow['link'];
}
}
}
function actionlinx() {
$from = CRM_Sos_Utils::getFromEmailAddress();
$params = [
'id' => 178,
'from' => $from,
'to_email' => 'forums@artfulrobot.uk',
'contact_id' => 9497,
//'template_params' => []
//'bcc' => "forums@artfulrobot.uk", // so I can keep an eye.
];
$result = civicrm_api3('MessageTemplate', 'send', $params);
print_r($result);
}
/* test/debugging code for tokens
$p = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), array(
'controller' => 'foo',
'smarty' => FALSE,
));
// Fill the processor with a batch of data.
$p->addMessage('body_text', 'Hello {contact.display_name} link: {contact.actionLink1}\n', 'text/plain');
$p->addRow()->context('contact_id', 9497);
$p->addRow()->context('contact_id', 123);
// Lookup/compose any tokens which are referenced in the message.
$p->evaluate();
// Display mail-merge data.
foreach ($p->getRows() as $row) {
echo $row->render('body_text');
}
}
*/
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