Merge pull request #15670 from eileenmcnaughton/dupe_locks
[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
TO
28 'id' => ts('Mailing ID'),
29 'name' => ts('Mailing Name'),
30 'group' => ts('Mailing Group(s)'),
31 'subject' => ts('Mailing Subject'),
32 'viewUrl' => ts('Mailing URL (View)'),
33 'editUrl' => ts('Mailing URL (Edit)'),
34 'scheduleUrl' => ts('Mailing URL (Schedule)'),
35 'html' => ts('Mailing HTML'),
36 'approvalStatus' => ts('Mailing Approval Status'),
37 'approvalNote' => ts('Mailing Approval Note'),
38 'approveUrl' => ts('Mailing Approval URL'),
39 'creator' => ts('Mailing Creator (Name)'),
40 'creatorEmail' => ts('Mailing Creator (Email)'),
be2fb01f 41 ]);
56df2d06
TO
42 }
43
44 /**
0ba33ffb 45 * @inheritDoc
56df2d06
TO
46 */
47 public function checkActive(\Civi\Token\TokenProcessor $processor) {
fa373cad
TO
48 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
49 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
56df2d06
TO
50 }
51
8246bca4 52 /**
53 * Prefetch tokens.
54 *
55 * @param \Civi\Token\Event\TokenValueEvent $e
56 *
57 * @return array
58 * @throws \Exception
59 */
56df2d06
TO
60 public function prefetch(\Civi\Token\Event\TokenValueEvent $e) {
61 $processor = $e->getTokenProcessor();
62 $mailing = isset($processor->context['mailing'])
63 ? $processor->context['mailing']
64 : CRM_Mailing_BAO_Mailing::findById($processor->context['mailingId']);
65
be2fb01f 66 return [
56df2d06 67 'mailing' => $mailing,
be2fb01f 68 ];
56df2d06
TO
69 }
70
71 /**
0ba33ffb 72 * @inheritDoc
56df2d06
TO
73 */
74 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
75 $row->format('text/plain')->tokens($entity, $field,
76 (string) CRM_Utils_Token::getMailingTokenReplacement($field, $prefetch['mailing']));
77 }
78
79}