Merge pull request #13909 from colemanw/apiShortSyntax
[civicrm-core.git] / Civi / Token / AbstractTokenSubscriber.php
CommitLineData
46f5566c
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
46f5566c 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
46f5566c
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28namespace Civi\Token;
29
f9ec2da6 30use Civi\ActionSchedule\Event\MailingQueryEvent;
46f5566c
TO
31use Civi\Token\Event\TokenRegisterEvent;
32use Civi\Token\Event\TokenValueEvent;
33use Symfony\Component\EventDispatcher\EventSubscriberInterface;
34
35/**
36 * Class AbstractTokenSubscriber
37 * @package Civi\Token
38 *
39 * AbstractTokenSubscriber is a base class which may be extended to
40 * implement tokens in a somewhat more concise fashion.
41 *
42 * To implement a new token handler based on this:
43 * 1. Create a subclass.
44 * 2. Override the constructor and set values for $entity and $tokenNames.
45 * 3. Implement the evaluateToken() method.
f9ec2da6
TO
46 * 4. Optionally, override others:
47 * + checkActive()
eb969099 48 * + getActiveTokens()
f9ec2da6
TO
49 * + prefetch()
50 * + alterActionScheduleMailing()
51 * 5. Register the new class with the event-dispatcher.
46f5566c
TO
52 *
53 * Note: There's no obligation to use this base class. You could implement
54 * your own class anew -- just subscribe the proper events.
55 */
56abstract class AbstractTokenSubscriber implements EventSubscriberInterface {
57
58 public static function getSubscribedEvents() {
59 return array(
60 Events::TOKEN_REGISTER => 'registerTokens',
61 Events::TOKEN_EVALUATE => 'evaluateTokens',
f9ec2da6 62 \Civi\ActionSchedule\Events::MAILING_QUERY => 'alterActionScheduleQuery',
46f5566c
TO
63 );
64 }
65
66 /**
67 * @var string
68 * Ex: 'contact' or profile' or 'employer'
69 */
70 public $entity;
71
72 /**
73 * @var array
52883911
AS
74 * List of tokens provided by this class
75 * Array(string $fieldName => string $label).
46f5566c
TO
76 */
77 public $tokenNames;
78
52883911
AS
79 /**
80 * @var array
81 * List of active tokens - tokens provided by this class and used in the message
82 * Array(string $tokenName);
83 */
84 public $activeTokens;
85
46f5566c
TO
86 /**
87 * @param $entity
88 * @param array $tokenNames
52883911 89 * Array(string $tokenName => string $label).
46f5566c
TO
90 */
91 public function __construct($entity, $tokenNames = array()) {
92 $this->entity = $entity;
93 $this->tokenNames = $tokenNames;
94 }
95
96 /**
97 * Determine whether this token-handler should be used with
98 * the given processor.
99 *
100 * To short-circuit token-processing in irrelevant contexts,
101 * override this.
102 *
103 * @param \Civi\Token\TokenProcessor $processor
104 * @return bool
105 */
106 public function checkActive(\Civi\Token\TokenProcessor $processor) {
107 return TRUE;
108 }
109
110 /**
111 * Register the declared tokens.
112 *
113 * @param TokenRegisterEvent $e
114 * The registration event. Add new tokens using register().
115 */
116 public function registerTokens(TokenRegisterEvent $e) {
117 if (!$this->checkActive($e->getTokenProcessor())) {
118 return;
119 }
120 foreach ($this->tokenNames as $name => $label) {
121 $e->register(array(
122 'entity' => $this->entity,
123 'field' => $name,
124 'label' => $label,
125 ));
126 }
127 }
128
f9ec2da6
TO
129 /**
130 * Alter the query which prepopulates mailing data
131 * for scheduled reminders.
132 *
133 * This is method is not always appropriate, but if you're specifically
134 * focused on scheduled reminders, it can be convenient.
135 *
136 * @param MailingQueryEvent $e
137 * The pending query which may be modified. See discussion on
138 * MailingQueryEvent::$query.
139 */
140 public function alterActionScheduleQuery(MailingQueryEvent $e) {
141 }
142
46f5566c
TO
143 /**
144 * Populate the token data.
145 *
146 * @param TokenValueEvent $e
147 * The event, which includes a list of rows and tokens.
148 */
149 public function evaluateTokens(TokenValueEvent $e) {
150 if (!$this->checkActive($e->getTokenProcessor())) {
151 return;
152 }
e0c75385 153
52883911
AS
154 $this->activeTokens = $this->getActiveTokens($e);
155 if (!$this->activeTokens) {
e0c75385
TO
156 return;
157 }
46f5566c 158 $prefetch = $this->prefetch($e);
e0c75385 159
46f5566c 160 foreach ($e->getRows() as $row) {
52883911 161 foreach ($this->activeTokens as $field) {
46f5566c
TO
162 $this->evaluateToken($row, $this->entity, $field, $prefetch);
163 }
164 }
165 }
166
eb969099
AS
167 /**
168 * To handle variable tokens, override this function and return the active tokens.
169 *
170 * @param \Civi\Token\Event\TokenValueEvent $e
171 *
172 * @return mixed
173 */
174 public function getActiveTokens(TokenValueEvent $e) {
175 $messageTokens = $e->getTokenProcessor()->getMessageTokens();
176 if (!isset($messageTokens[$this->entity])) {
177 return FALSE;
178 }
179 return array_intersect($messageTokens[$this->entity], array_keys($this->tokenNames));
180 }
181
46f5566c
TO
182 /**
183 * To perform a bulk lookup before rendering tokens, override this
184 * function and return the prefetched data.
185 *
54957108 186 * @param \Civi\Token\Event\TokenValueEvent $e
187 *
46f5566c
TO
188 * @return mixed
189 */
190 public function prefetch(TokenValueEvent $e) {
191 return NULL;
192 }
193
194 /**
195 * Evaluate the content of a single token.
196 *
197 * @param TokenRow $row
198 * The record for which we want token values.
199 * @param string $entity
200 * The name of the token entity.
201 * @param string $field
202 * The name of the token field.
203 * @param mixed $prefetch
204 * Any data that was returned by the prefetch().
205 * @return mixed
206 */
207 public abstract function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL);
208
209}