Merge pull request #16837 from tunbola/case-api-case-clients
[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 'key' => ts('Mailing Key'),
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)'),
42 ]);
43 }
44
45 /**
46 * @inheritDoc
47 */
48 public function checkActive(\Civi\Token\TokenProcessor $processor) {
49 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
50 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
51 }
52
53 /**
54 * Prefetch tokens.
55 *
56 * @param \Civi\Token\Event\TokenValueEvent $e
57 *
58 * @return array
59 * @throws \Exception
60 */
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
67 return [
68 'mailing' => $mailing,
69 ];
70 }
71
72 /**
73 * @inheritDoc
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 }