Merge pull request #14249 from yashodha/959_dev
[civicrm-core.git] / CRM / Mailing / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
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 */
37 class CRM_Mailing_Tokens extends \Civi\Token\AbstractTokenSubscriber {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 parent::__construct('mailing', [
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 /**
61 * @inheritDoc
62 */
63 public function checkActive(\Civi\Token\TokenProcessor $processor) {
64 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
65 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
66 }
67
68 /**
69 * Prefetch tokens.
70 *
71 * @param \Civi\Token\Event\TokenValueEvent $e
72 *
73 * @return array
74 * @throws \Exception
75 */
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
82 return [
83 'mailing' => $mailing,
84 ];
85 }
86
87 /**
88 * @inheritDoc
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 }