Merge pull request #15697 from artfulrobot/payment-property-bag
[civicrm-core.git] / CRM / Activity / 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 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Member_Tokens
20 *
21 * Generate "activity.*" tokens.
22 *
23 * This TokenSubscriber was produced by refactoring the code from the
24 * scheduled-reminder system with the goal of making that system
25 * more flexible. The current implementation is still coupled to
26 * scheduled-reminders. It would be good to figure out a more generic
27 * implementation which is not tied to scheduled reminders, although
28 * that is outside the current scope.
29 */
30 class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
31
32 /**
33 * CRM_Activity_Tokens constructor.
34 */
35 public function __construct() {
36 parent::__construct('activity', array_merge(
37 $this->getBasicTokens(),
38 $this->getCustomFieldTokens()
39 ));
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public function checkActive(\Civi\Token\TokenProcessor $processor) {
46 // Extracted from scheduled-reminders code. See the class description.
47 return !empty($processor->context['actionMapping'])
48 && $processor->context['actionMapping']->getEntity() === 'civicrm_activity';
49 }
50
51 /**
52 * @inheritDoc
53 */
54 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
55 if ($e->mapping->getEntity() !== 'civicrm_activity') {
56 return;
57 }
58
59 // The joint expression for activities needs some extra nuance to handle.
60 // Multiple revisions of the activity.
61 // Q: Could we simplify & move the extra AND clauses into `where(...)`?
62 $e->query->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0');
63
64 // FIXME: seems too broad.
65 $e->query->select('e.*');
66 $e->query->select('ov.label as activity_type, e.id as activity_id');
67
68 $e->query->join("og", "!casMailingJoinType civicrm_option_group og ON og.name = 'activity_type'");
69 $e->query->join("ov", "!casMailingJoinType civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id");
70
71 // if CiviCase component is enabled, join for caseId.
72 $compInfo = CRM_Core_Component::getEnabledComponents();
73 if (array_key_exists('CiviCase', $compInfo)) {
74 $e->query->select("civicrm_case_activity.case_id as case_id");
75 $e->query->join('civicrm_case_activity', "LEFT JOIN `civicrm_case_activity` ON `e`.`id` = `civicrm_case_activity`.`activity_id`");
76 }
77 }
78
79 /**
80 * @inheritDoc
81 */
82 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
83 $actionSearchResult = $row->context['actionSearchResult'];
84
85 if (in_array($field, array('activity_date_time'))) {
86 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
87 }
88 elseif (isset($actionSearchResult->$field)) {
89 $row->tokens($entity, $field, $actionSearchResult->$field);
90 }
91 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
92 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
93 }
94 else {
95 $row->tokens($entity, $field, '');
96 }
97 }
98
99 /**
100 * Get the basic tokens provided.
101 *
102 * @return array token name => token label
103 */
104 protected function getBasicTokens() {
105 return [
106 'activity_id' => ts('Activity ID'),
107 'activity_type' => ts('Activity Type'),
108 'subject' => ts('Activity Subject'),
109 'details' => ts('Activity Details'),
110 'activity_date_time' => ts('Activity Date-Time'),
111 ];
112 }
113
114 /**
115 * Get the tokens for custom fields
116 * @return array token name => token label
117 */
118 protected function getCustomFieldTokens() {
119 return CRM_Utils_Token::getCustomFieldTokens('Activity');
120 }
121
122 }