CRM-19799, fixed contribution params to include line items
[civicrm-core.git] / CRM / Activity / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2016 |
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
29 /**
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
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 */
46 class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
47
48 public function __construct() {
49 parent::__construct('activity', array(
50 'activity_id' => ts('Activity ID'),
51 'activity_type' => ts('Activity Type'),
52 'subject' => ts('Activity Subject'),
53 'details' => ts('Activity Details'),
54 'activity_date_time' => ts('Activity Date-Time'),
55 ));
56 }
57
58 /**
59 * Check is active.
60 *
61 * @param \Civi\Token\TokenProcessor $processor
62 *
63 * @return bool
64 */
65 public function checkActive(\Civi\Token\TokenProcessor $processor) {
66 // Extracted from scheduled-reminders code. See the class description.
67 return
68 !empty($processor->context['actionMapping'])
69 && $processor->context['actionMapping']->getEntity() === 'civicrm_activity';
70 }
71
72 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
73 if ($e->mapping->getEntity() !== 'civicrm_activity') {
74 return;
75 }
76
77 // The joint expression for activities needs some extra nuance to handle.
78 // Multiple revisions of the activity.
79 // Q: Could we simplify & move the extra AND clauses into `where(...)`?
80 $e->query->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0');
81
82 $e->query->select('e.*'); // FIXME: seems too broad.
83 $e->query->select('ov.label as activity_type, e.id as activity_id');
84
85 $e->query->join("og", "!casMailingJoinType civicrm_option_group og ON og.name = 'activity_type'");
86 $e->query->join("ov", "!casMailingJoinType civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id");
87
88 // if CiviCase component is enabled, join for caseId.
89 $compInfo = CRM_Core_Component::getEnabledComponents();
90 if (array_key_exists('CiviCase', $compInfo)) {
91 $e->query->select("civicrm_case_activity.case_id as case_id");
92 $e->query->join('civicrm_case_activity', "LEFT JOIN `civicrm_case_activity` ON `e`.`id` = `civicrm_case_activity`.`activity_id`");
93 }
94 }
95
96 /**
97 * Evaluate the content of a single token.
98 *
99 * @param \Civi\Token\TokenRow $row
100 * The record for which we want token values.
101 * @param string $entity
102 * @param string $field
103 * The name of the token field.
104 * @param mixed $prefetch
105 * Any data that was returned by the prefetch().
106 *
107 * @return mixed
108 */
109 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
110 $actionSearchResult = $row->context['actionSearchResult'];
111
112 if (in_array($field, array('activity_date_time'))) {
113 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
114 }
115 elseif (isset($actionSearchResult->$field)) {
116 $row->tokens($entity, $field, $actionSearchResult->$field);
117 }
118 else {
119 $row->tokens($entity, $field, '');
120 }
121 }
122
123 }