Merge pull request #12285 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Mailing / Tokens.php
CommitLineData
56df2d06
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
fee14197 5 | CiviCRM version 5 |
56df2d06 6 +--------------------------------------------------------------------+
8c9251b3 7 | Copyright CiviCRM LLC (c) 2004-2018 |
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() {
43 parent::__construct('mailing', array(
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)'),
57 ));
58 }
59
60 /**
0ba33ffb 61 * @inheritDoc
56df2d06
TO
62 */
63 public function checkActive(\Civi\Token\TokenProcessor $processor) {
64 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing']);
65 }
66
8246bca4 67 /**
68 * Prefetch tokens.
69 *
70 * @param \Civi\Token\Event\TokenValueEvent $e
71 *
72 * @return array
73 * @throws \Exception
74 */
56df2d06
TO
75 public function prefetch(\Civi\Token\Event\TokenValueEvent $e) {
76 $processor = $e->getTokenProcessor();
77 $mailing = isset($processor->context['mailing'])
78 ? $processor->context['mailing']
79 : CRM_Mailing_BAO_Mailing::findById($processor->context['mailingId']);
80
81 return array(
82 'mailing' => $mailing,
83 );
84 }
85
86 /**
0ba33ffb 87 * @inheritDoc
56df2d06
TO
88 */
89 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
90 $row->format('text/plain')->tokens($entity, $field,
91 (string) CRM_Utils_Token::getMailingTokenReplacement($field, $prefetch['mailing']));
92 }
93
94}