Merge pull request #19359 from totten/master-dao-update
[civicrm-core.git] / CRM / Mailing / Tokens.php
CommitLineData
56df2d06
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
56df2d06 6 | |
bc77d7c0
TO
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
56df2d06
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 * Class CRM_Mailing_Tokens
15 *
16 * Generate "mailing.*" tokens.
17 *
18 * To activate these tokens, the TokenProcessor context must specify either
19 * "mailingId" (int) or "mailing" (CRM_Mailing_BAO_Mailing).
20 */
21class CRM_Mailing_Tokens extends \Civi\Token\AbstractTokenSubscriber {
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
be2fb01f 27 parent::__construct('mailing', [
56df2d06 28 'id' => ts('Mailing ID'),
b56b8b0e 29 'key' => ts('Mailing Key'),
56df2d06
TO
30 'name' => ts('Mailing Name'),
31 'group' => ts('Mailing Group(s)'),
32 'subject' => ts('Mailing Subject'),
33 'viewUrl' => ts('Mailing URL (View)'),
34 'editUrl' => ts('Mailing URL (Edit)'),
35 'scheduleUrl' => ts('Mailing URL (Schedule)'),
36 'html' => ts('Mailing HTML'),
37 'approvalStatus' => ts('Mailing Approval Status'),
38 'approvalNote' => ts('Mailing Approval Note'),
39 'approveUrl' => ts('Mailing Approval URL'),
40 'creator' => ts('Mailing Creator (Name)'),
41 'creatorEmail' => ts('Mailing Creator (Email)'),
be2fb01f 42 ]);
56df2d06
TO
43 }
44
45 /**
0ba33ffb 46 * @inheritDoc
56df2d06
TO
47 */
48 public function checkActive(\Civi\Token\TokenProcessor $processor) {
fa373cad
TO
49 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
50 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
56df2d06
TO
51 }
52
8246bca4 53 /**
54 * Prefetch tokens.
55 *
56 * @param \Civi\Token\Event\TokenValueEvent $e
57 *
58 * @return array
59 * @throws \Exception
60 */
56df2d06
TO
61 public function prefetch(\Civi\Token\Event\TokenValueEvent $e) {
62 $processor = $e->getTokenProcessor();
63 $mailing = isset($processor->context['mailing'])
64 ? $processor->context['mailing']
65 : CRM_Mailing_BAO_Mailing::findById($processor->context['mailingId']);
66
be2fb01f 67 return [
56df2d06 68 'mailing' => $mailing,
be2fb01f 69 ];
56df2d06
TO
70 }
71
72 /**
0ba33ffb 73 * @inheritDoc
56df2d06
TO
74 */
75 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
76 $row->format('text/plain')->tokens($entity, $field,
77 (string) CRM_Utils_Token::getMailingTokenReplacement($field, $prefetch['mailing']));
78 }
79
80}