Set version to 5.20.beta1
[civicrm-core.git] / CRM / Mailing / Tokens.php
CommitLineData
56df2d06
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
fee14197 5 | CiviCRM version 5 |
56df2d06 6 +--------------------------------------------------------------------+
6b83d5bd 7 | Copyright CiviCRM LLC (c) 2004-2019 |
56df2d06
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 * Class CRM_Mailing_Tokens
31 *
32 * Generate "mailing.*" tokens.
33 *
34 * To activate these tokens, the TokenProcessor context must specify either
35 * "mailingId" (int) or "mailing" (CRM_Mailing_BAO_Mailing).
36 */
37class CRM_Mailing_Tokens extends \Civi\Token\AbstractTokenSubscriber {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
be2fb01f 43 parent::__construct('mailing', [
56df2d06
TO
44 'id' => ts('Mailing ID'),
45 'name' => ts('Mailing Name'),
46 'group' => ts('Mailing Group(s)'),
47 'subject' => ts('Mailing Subject'),
48 'viewUrl' => ts('Mailing URL (View)'),
49 'editUrl' => ts('Mailing URL (Edit)'),
50 'scheduleUrl' => ts('Mailing URL (Schedule)'),
51 'html' => ts('Mailing HTML'),
52 'approvalStatus' => ts('Mailing Approval Status'),
53 'approvalNote' => ts('Mailing Approval Note'),
54 'approveUrl' => ts('Mailing Approval URL'),
55 'creator' => ts('Mailing Creator (Name)'),
56 'creatorEmail' => ts('Mailing Creator (Email)'),
be2fb01f 57 ]);
56df2d06
TO
58 }
59
60 /**
0ba33ffb 61 * @inheritDoc
56df2d06
TO
62 */
63 public function checkActive(\Civi\Token\TokenProcessor $processor) {
fa373cad
TO
64 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
65 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
56df2d06
TO
66 }
67
8246bca4 68 /**
69 * Prefetch tokens.
70 *
71 * @param \Civi\Token\Event\TokenValueEvent $e
72 *
73 * @return array
74 * @throws \Exception
75 */
56df2d06
TO
76 public function prefetch(\Civi\Token\Event\TokenValueEvent $e) {
77 $processor = $e->getTokenProcessor();
78 $mailing = isset($processor->context['mailing'])
79 ? $processor->context['mailing']
80 : CRM_Mailing_BAO_Mailing::findById($processor->context['mailingId']);
81
be2fb01f 82 return [
56df2d06 83 'mailing' => $mailing,
be2fb01f 84 ];
56df2d06
TO
85 }
86
87 /**
0ba33ffb 88 * @inheritDoc
56df2d06
TO
89 */
90 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
91 $row->format('text/plain')->tokens($entity, $field,
92 (string) CRM_Utils_Token::getMailingTokenReplacement($field, $prefetch['mailing']));
93 }
94
95}