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