Skip to content

Commit

Permalink
CIVIELECT-56 #12 Replace hook_civicrm_alterMailParams with new CiviCR…
Browse files Browse the repository at this point in the history
…M tokens class
  • Loading branch information
agileware-justin committed Aug 9, 2024
1 parent 9ac7823 commit 3b10291
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 164 deletions.
94 changes: 94 additions & 0 deletions CRM/Elections/Tokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

use Civi\Token\Event\TokenRegisterEvent;
use Civi\Token\Event\TokenValueEvent;
use Civi\Token\TokenRow;
use CRM_Elections_ExtensionUtil as E;

class CRM_Elections_Tokens {
const TOKEN = 'election';

/**
* @param \Civi\Token\Event\TokenRegisterEvent $entity
* @param string $field Machine name for the token
* @param string $label the translated token label
*
* @return string
*/
protected static function registerCtx(TokenRegisterEvent $entity, string $field, string $label){
$entity->register($field, $label . ' :: ' . E::ts('Elections') );
}

public static function register(TokenRegisterEvent $e) {
$context = $e->getTokenProcessor()->context;
if(!is_array($context['schema'] ?? NULL))
return;

// Register Election tokens for Activity
if (in_array('activityId', $context['schema'])) {
$entity = $e->entity(self::TOKEN);

self::registerCtx($entity, 'election_name', E::ts('Election Name'));
self::registerCtx($entity, 'election_position', E::ts('Election Position'));
self::registerCtx($entity, 'nominator_name', E::ts('Nominator Name'));
self::registerCtx($entity, 'nominee_name', E::ts('Nominee Name'));
}
}

public static function evaluate(TokenValueEvent $e) {
foreach($e->getRows() as $row) {
self::evaluateRow($row);
}
}

protected static function evaluateRow(TokenRow $row) {
if (empty($row->context['activityId'])) {
return;
}
$row->format('text/html');
try {
$activityInfo = civicrm_api3('Activity', 'getsingle', [
'id' => $row->context['activityId'],
'return' => [
'source_record_id',
'activity_type_id.name',
'is_star',
],
]);

if (isset($activityInfo['activity_type_id.name']) && $activityInfo['activity_type_id.name'] === 'Nomination') {
$electionNominationInfo = civicrm_api3('ElectionNominationSeconder', 'getsingle', [
'id' => $activityInfo['source_record_id'],
'sequential' => TRUE,
'return' => [
'election_nomination_id.election_position_id.election_id.name',
'election_nomination_id.election_position_id.name',
'member_nominator.display_name',
'election_nomination_id.member_nominee.display_name',
],
]);

$row->tokens(self::TOKEN, 'election_name', $electionNominationInfo['election_nomination_id.election_position_id.election_id.name'] ?? '');
$row->tokens(self::TOKEN, 'election_position', $electionNominationInfo['election_nomination_id.election_position_id.name'] ?? '');
$row->tokens(self::TOKEN, 'nominator_name', $electionNominationInfo['member_nominator.display_name'] ?? '');
$row->tokens(self::TOKEN, 'nominee_name', $electionNominationInfo['election_nomination_id.member_nominee.display_name'] ?? '');
}

if (isset($activityInfo['activity_type_id.name']) && $activityInfo['activity_type_id.name'] === 'Vote') {
$electionInfo = civicrm_api3('Election', 'getsingle', [
'id' => $activityInfo['source_record_id'],
'sequential' => TRUE,
'return' => [
'name',
],
]);

$row->tokens(self::TOKEN, 'election_name', $electionInfo['name'] ?? '');
}
}
catch(CRM_Core_Exception $e) {
// Silence is golden
}
}

}
173 changes: 9 additions & 164 deletions elections.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
* Implements hook_civicrm_container()
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_container/
*
* @return void
*/
function elections_civicrm_container(ContainerBuilder $container) {
$container->addResource(new FileResource(E::path('CRM/Elections/Tokens.php')));
$dispatcher = $container->findDefinition('dispatcher');
$dispatcher->addMethodCall('addListener', ['civi.token.eval', ['CRM_Elections_Tokens', 'evaluate']]);
$dispatcher->addMethodCall('addListener', ['civi.token.list', ['CRM_Elections_Tokens', 'register']]);
$container->addCompilerPass(new Civi\Elections\CompilerPass());
}

Expand Down Expand Up @@ -197,7 +203,7 @@ function isElectionAdmin() {
*/
function throwUnauthorizedMessageIfRequired($formOrPage) {
if (!isElectionAdmin()) {
throwAccessDeniedException($formOrPage, 'You\'re not authorized to perform this action.');
throwAccessDeniedException($formOrPage, 'You are not authorized to perform this action.');
return TRUE;
}
return FALSE;
Expand Down Expand Up @@ -282,14 +288,14 @@ function isRequestUsingShortCode() {
*/
function findElectionById($electionId, $throwErrorIfNotFound = TRUE) {
if (!$electionId) {
throw new CRM_Extension_Exception('You\'re not authorized to access this page.');
throw new CRM_Extension_Exception('You are not authorised to access this page.');
}

$election = new CRM_Elections_BAO_Election();
$election->id = $electionId;

if (!$election->find(TRUE) && $throwErrorIfNotFound) {
throw new CRM_Extension_Exception('You\'re not authorized to access this page.');
throw new CRM_Extension_Exception('You are not authorised to access this page.');
}

$election->assignStatues();
Expand Down Expand Up @@ -428,61 +434,6 @@ function getLoggedInUserVoteDate($electionId) {
return $voteDate;
}

/**
* Define election tokens to be included in emails.
*
* @param $tokens
*/
function elections_civicrm_tokens(&$tokens) {
$tokensList = getElectionTokensList();
foreach ($tokensList as $tokenKey => $tokenItems) {
$tokens[$tokenKey] = $tokenItems;
}
}

/**
* Get election tokens list
*
* @return array
*/
function getElectionTokensList() {
$tokensList = array(
'election' => array(
'election.name' => "Name",
),
'electionposition' => array(
'electionposition.name' => "Name",
),
'nomination' => array(
'nomination.nominatorname' => "Nominator Name",
'nomination.nomineename' => "Nominee Name",
),
);
return $tokensList;
}

/**
* Implements hook_civicrm_tokenValues().
*
*/
function elections_civicrm_tokenValues(&$values, $cids, $job = NULL, $tokens = array(), $context = NULL) {
$customTokens = array_keys(getElectionTokensList());
foreach ($customTokens as $customToken) {
if (isset($tokens[$customToken])) {
addPlaceholderTokenValues($tokens, $customToken, $cids, $values);
}
}
}

function addPlaceholderTokenValues($tokens, $customToken, $cids, &$values) {
foreach ($tokens[$customToken] as $electionToken) {
foreach ($cids as $cid) {
$tokenKey = $customToken . '.' . $electionToken;
$values[$cid][$tokenKey] = '[' . $tokenKey . ']';
}
}
}

/**
* Implements hook_civicrm_permission().
*
Expand All @@ -498,112 +449,6 @@ function elections_civicrm_permission(&$permissions) {
];
}

/**
* Implements hook_civicrm_alterMailParams().
*
*/
function elections_civicrm_alterMailParams(&$params, $context) {
if ($params['groupName'] == 'Scheduled Reminder Sender' && $params['entity'] == 'action_schedule' && isset($params['token_params'])) {
$tokenValues = $params['token_params'];
if ($tokenValues['entity_table'] == 'civicrm_activity') {
$activityId = $tokenValues['entity_id'];
$activityInfo = civicrm_api3('Activity', 'getsingle', array(
'id' => $activityId,
'return' => array(
'source_record_id',
'activity_type_id.name',
'is_star',
),
));

$subject = $params['subject'];
$text = $params['text'];
$html = $params['html'];

if ($activityInfo['activity_type_id.name'] == 'Nomination') {
$electionNominationInfo = civicrm_api3("ElectionNominationSeconder", "getsingle", array(
'id' => $activityInfo['source_record_id'],
'sequential' => TRUE,
'return' => [
"member_nominator.display_name",
"election_nomination_id.member_nominee.display_name",
"election_nomination_id.election_position_id.name",
"election_nomination_id.election_position_id.election_id.name",
],
));

$placeHolders = getTokenPlaceholders();
foreach ($placeHolders as $placeHolder) {
$placeHolderValue = "";
if ($placeHolder == '[election.name]') {
$placeHolderValue = $electionNominationInfo['election_nomination_id.election_position_id.election_id.name'];
}
if ($placeHolder == '[electionposition.name]') {
$placeHolderValue = $electionNominationInfo['election_nomination_id.election_position_id.name'];
}
if ($placeHolder == '[nomination.nominatorname]') {
$placeHolderValue = $electionNominationInfo['member_nominator.display_name'];
}
if ($placeHolder == '[nomination.nomineename]') {
$placeHolderValue = $electionNominationInfo['election_nomination_id.member_nominee.display_name'];
}

replacePlaceholderValue($subject, $placeHolder, $placeHolderValue);
replacePlaceholderValue($text, $placeHolder, $placeHolderValue);
replacePlaceholderValue($html, $placeHolder, $placeHolderValue);
}
}

if ($activityInfo['activity_type_id.name'] == 'Vote') {
$electionInfo = civicrm_api3("Election", "getsingle", array(
'id' => $activityInfo['source_record_id'],
'sequential' => TRUE,
'return' => [
"name",
],
));

replacePlaceholderValue($subject, '[election.name]', $electionInfo['name']);
replacePlaceholderValue($text, '[election.name]', $electionInfo['name']);
replacePlaceholderValue($html, '[election.name]', $electionInfo['name']);
}

$params['html'] = $html;
$params['subject'] = $subject;
$params['text'] = $text;
}
}
}

/**
* Function replaces token placeholder with its actual value.
*
* @param $text
* @param $placeholder
* @param $placeholderValue
*/
function replacePlaceholderValue(&$text, $placeholder, $placeholderValue) {
$text = str_replace($placeholder, $placeholderValue, $text);
}

/**
* Function returns list of token placeholders.
*
* @return array
*/
function getTokenPlaceHolders() {
$electionTokens = getElectionTokensList();
$placeHolders = array();
foreach ($electionTokens as $tokenKey => $electionTokenItems) {
foreach ($electionTokenItems as $electionTokenItemKey => $electionTokenItem) {
$tokenPlaceholder = '[' . $electionTokenItemKey . ']';
$placeHolders[] = $tokenPlaceholder;
}
}

return $placeHolders;
}

/**
* Shuffle the array keeping the keys.
*
Expand Down

0 comments on commit 3b10291

Please sign in to comment.