'registerTokens', Events::TOKEN_EVALUATE => 'evaluateTokens', \Civi\ActionSchedule\Events::MAILING_QUERY => 'alterActionScheduleQuery', ); } /** * @var string * Ex: 'contact' or profile' or 'employer' */ public $entity; /** * @var array * Ex: array('viewUrl', 'editUrl'). */ public $tokenNames; /** * @param $entity * @param array $tokenNames * Array(string $fieldName => string $label). */ public function __construct($entity, $tokenNames = array()) { $this->entity = $entity; $this->tokenNames = $tokenNames; } /** * Determine whether this token-handler should be used with * the given processor. * * To short-circuit token-processing in irrelevant contexts, * override this. * * @param \Civi\Token\TokenProcessor $processor * @return bool */ public function checkActive(\Civi\Token\TokenProcessor $processor) { return TRUE; } /** * Register the declared tokens. * * @param TokenRegisterEvent $e * The registration event. Add new tokens using register(). */ public function registerTokens(TokenRegisterEvent $e) { if (!$this->checkActive($e->getTokenProcessor())) { return; } foreach ($this->tokenNames as $name => $label) { $e->register(array( 'entity' => $this->entity, 'field' => $name, 'label' => $label, )); } } /** * Alter the query which prepopulates mailing data * for scheduled reminders. * * This is method is not always appropriate, but if you're specifically * focused on scheduled reminders, it can be convenient. * * @param MailingQueryEvent $e * The pending query which may be modified. See discussion on * MailingQueryEvent::$query. */ public function alterActionScheduleQuery(MailingQueryEvent $e) { } /** * Populate the token data. * * @param TokenValueEvent $e * The event, which includes a list of rows and tokens. */ public function evaluateTokens(TokenValueEvent $e) { if (!$this->checkActive($e->getTokenProcessor())) { return; } $messageTokens = $e->getTokenProcessor()->getMessageTokens(); if (!isset($messageTokens[$this->entity])) { return; } $activeTokens = array_intersect($messageTokens[$this->entity], array_keys($this->tokenNames)); if (empty($activeTokens)) { return; } $prefetch = $this->prefetch($e); foreach ($e->getRows() as $row) { foreach ($activeTokens as $field) { $this->evaluateToken($row, $this->entity, $field, $prefetch); } } } /** * To perform a bulk lookup before rendering tokens, override this * function and return the prefetched data. * * @param \Civi\Token\Event\TokenValueEvent $e * * @return mixed */ public function prefetch(TokenValueEvent $e) { return NULL; } /** * Evaluate the content of a single token. * * @param TokenRow $row * The record for which we want token values. * @param string $entity * The name of the token entity. * @param string $field * The name of the token field. * @param mixed $prefetch * Any data that was returned by the prefetch(). * @return mixed */ public abstract function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL); }