Merge pull request #15933 from civicrm/5.20
[civicrm-core.git] / CRM / Activity / Tokens.php
CommitLineData
46f5566c
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
46f5566c 6 | |
bc77d7c0
TO
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 |
46f5566c
TO
10 +--------------------------------------------------------------------+
11 */
12
7808aae6
SB
13/**
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
7808aae6
SB
16 */
17
46f5566c
TO
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 */
30class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
31
8246bca4 32 /**
33 * CRM_Activity_Tokens constructor.
34 */
46f5566c 35 public function __construct() {
4e9b6a62 36 parent::__construct('activity', array_merge(
86420016 37 $this->getBasicTokens(),
38 $this->getCustomFieldTokens()
46f5566c
TO
39 ));
40 }
41
70599df6 42 /**
298795cd 43 * @inheritDoc
70599df6 44 */
46f5566c
TO
45 public function checkActive(\Civi\Token\TokenProcessor $processor) {
46 // Extracted from scheduled-reminders code. See the class description.
62d3ee27 47 return !empty($processor->context['actionMapping'])
46f5566c
TO
48 && $processor->context['actionMapping']->getEntity() === 'civicrm_activity';
49 }
50
298795cd
TO
51 /**
52 * @inheritDoc
53 */
f9ec2da6
TO
54 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
55 if ($e->mapping->getEntity() !== 'civicrm_activity') {
56 return;
57 }
58
7808aae6
SB
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(...)`?
f9ec2da6
TO
62 $e->query->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0');
63
0d48f1cc
TO
64 // FIXME: seems too broad.
65 $e->query->select('e.*');
f9ec2da6
TO
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
46f5566c 79 /**
298795cd 80 * @inheritDoc
46f5566c
TO
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 }
8640061b 91 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
92 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
4e9b6a62 93 }
46f5566c
TO
94 else {
95 $row->tokens($entity, $field, '');
96 }
97 }
98
86420016 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
46f5566c 122}