Merge pull request #15938 from civicrm/5.20
[civicrm-core.git] / CRM / Mailing / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 |
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 */
21 class CRM_Mailing_Tokens extends \Civi\Token\AbstractTokenSubscriber {
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 parent::__construct('mailing', [
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)'),
41 ]);
42 }
43
44 /**
45 * @inheritDoc
46 */
47 public function checkActive(\Civi\Token\TokenProcessor $processor) {
48 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
49 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
50 }
51
52 /**
53 * Prefetch tokens.
54 *
55 * @param \Civi\Token\Event\TokenValueEvent $e
56 *
57 * @return array
58 * @throws \Exception
59 */
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
66 return [
67 'mailing' => $mailing,
68 ];
69 }
70
71 /**
72 * @inheritDoc
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 }