4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Member_Tokens
21 * Generate "activity.*" tokens.
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.
30 class CRM_Activity_Tokens
extends \Civi\Token\AbstractTokenSubscriber
{
33 * CRM_Activity_Tokens constructor.
35 public function __construct() {
36 parent
::__construct('activity', array_merge(
37 $this->getBasicTokens(),
38 $this->getCustomFieldTokens()
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';
54 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent
$e) {
55 if ($e->mapping
->getEntity() !== 'civicrm_activity') {
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');
64 // FIXME: seems too broad.
65 $e->query
->select('e.*');
66 $e->query
->select('ov.label as activity_type, e.id as activity_id');
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");
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`");
82 public function evaluateToken(\Civi\Token\TokenRow
$row, $entity, $field, $prefetch = NULL) {
83 $actionSearchResult = $row->context
['actionSearchResult'];
85 if (in_array($field, array('activity_date_time'))) {
86 $row->tokens($entity, $field, \CRM_Utils_Date
::customFormat($actionSearchResult->$field));
88 elseif (isset($actionSearchResult->$field)) {
89 $row->tokens($entity, $field, $actionSearchResult->$field);
91 elseif ($cfID = \CRM_Core_BAO_CustomField
::getKeyID($field)) {
92 $row->customToken($entity, $cfID, $actionSearchResult->entity_id
);
95 $row->tokens($entity, $field, '');
100 * Get the basic tokens provided.
102 * @return array token name => token label
104 protected function getBasicTokens() {
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'),
115 * Get the tokens for custom fields
116 * @return array token name => token label
118 protected function getCustomFieldTokens() {
119 return CRM_Utils_Token
::getCustomFieldTokens('Activity');