Commit | Line | Data |
---|---|---|
46f5566c TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
3435af9a | 5 | | CiviCRM version 4.7 | |
46f5566c | 6 | +--------------------------------------------------------------------+ |
0f03f337 | 7 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | ||
29 | /** | |
30 | * Class CRM_Event_Tokens | |
31 | * | |
32 | * Generate "event.*" tokens. | |
33 | * | |
34 | * This TokenSubscriber was produced by refactoring the code from the | |
35 | * scheduled-reminder system with the goal of making that system | |
36 | * more flexible. The current implementation is still coupled to | |
37 | * scheduled-reminders. It would be good to figure out a more generic | |
38 | * implementation which is not tied to scheduled reminders, although | |
39 | * that is outside the current scope. | |
40 | */ | |
41 | class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { | |
42 | ||
70599df6 | 43 | /** |
44 | * Class constructor. | |
45 | */ | |
46f5566c TO |
46 | public function __construct() { |
47 | parent::__construct('event', array( | |
48 | 'event_type' => ts('Event Type'), | |
49 | 'title' => ts('Event Title'), | |
50 | 'event_id' => ts('Event ID'), | |
51 | 'start_date' => ts('Event Start Date'), | |
52 | 'end_date' => ts('Event End Date'), | |
53 | 'summary' => ts('Event Summary'), | |
54 | 'description' => ts('Event Description'), | |
55 | 'location' => ts('Event Location'), | |
56 | 'info_url' => ts('Event Info URL'), | |
57 | 'registration_url' => ts('Event Registration URL'), | |
58 | 'fee_amount' => ts('Event Fee'), | |
59 | 'contact_email' => ts('Event Contact (Email)'), | |
60 | 'contact_phone' => ts('Event Contact (Phone)'), | |
61 | 'balance' => ts('Event Balance'), | |
62 | )); | |
63 | } | |
64 | ||
70599df6 | 65 | /** |
298795cd | 66 | * @inheritDoc |
70599df6 | 67 | */ |
46f5566c TO |
68 | public function checkActive(\Civi\Token\TokenProcessor $processor) { |
69 | // Extracted from scheduled-reminders code. See the class description. | |
70 | return | |
71 | !empty($processor->context['actionMapping']) | |
72 | && $processor->context['actionMapping']->getEntity() === 'civicrm_participant'; | |
73 | } | |
74 | ||
f9ec2da6 TO |
75 | public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) { |
76 | if ($e->mapping->getEntity() !== 'civicrm_participant') { | |
77 | return; | |
78 | } | |
79 | ||
80 | $e->query->select('e.*'); // FIXME: seems too broad. | |
81 | $e->query->select('ov.label as event_type, ev.title, ev.id as event_id, ev.start_date, ev.end_date, ev.summary, ev.description, address.street_address, address.city, address.state_province_id, address.postal_code, email.email as contact_email, phone.phone as contact_phone'); | |
82 | $e->query->join('participant_stuff', " | |
83 | !casMailingJoinType civicrm_event ev ON e.event_id = ev.id | |
84 | !casMailingJoinType civicrm_option_group og ON og.name = 'event_type' | |
85 | !casMailingJoinType civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id | |
86 | LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id | |
87 | LEFT JOIN civicrm_address address ON address.id = lb.address_id | |
88 | LEFT JOIN civicrm_email email ON email.id = lb.email_id | |
89 | LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id | |
90 | "); | |
91 | } | |
92 | ||
46f5566c | 93 | /** |
298795cd | 94 | * @inheritDoc |
46f5566c TO |
95 | */ |
96 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { | |
97 | $actionSearchResult = $row->context['actionSearchResult']; | |
98 | ||
99 | if ($field == 'location') { | |
100 | $loc = array(); | |
101 | $stateProvince = \CRM_Core_PseudoConstant::stateProvince(); | |
102 | $loc['street_address'] = $actionSearchResult->street_address; | |
103 | $loc['city'] = $actionSearchResult->city; | |
104 | $loc['state_province'] = \CRM_Utils_Array::value($actionSearchResult->state_province_id, $stateProvince); | |
105 | $loc['postal_code'] = $actionSearchResult->postal_code; | |
106 | //$entityTokenParams[$tokenEntity][$field] = \CRM_Utils_Address::format($loc); | |
107 | $row->tokens($entity, $field, \CRM_Utils_Address::format($loc)); | |
108 | } | |
109 | elseif ($field == 'info_url') { | |
110 | $row | |
111 | ->tokens($entity, $field, \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $actionSearchResult->event_id, TRUE, NULL, FALSE)); | |
112 | } | |
113 | elseif ($field == 'registration_url') { | |
114 | $row | |
115 | ->tokens($entity, $field, \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $actionSearchResult->event_id, TRUE, NULL, FALSE)); | |
116 | } | |
117 | elseif (in_array($field, array('start_date', 'end_date'))) { | |
118 | $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field)); | |
119 | } | |
120 | elseif ($field == 'balance') { | |
121 | if ($actionSearchResult->entityTable == 'civicrm_contact') { | |
122 | $balancePay = 'N/A'; | |
123 | } | |
124 | elseif (!empty($actionSearchResult->entityID)) { | |
125 | $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($actionSearchResult->entityID, 'event'); | |
126 | $balancePay = \CRM_Utils_Array::value('balance', $info); | |
127 | $balancePay = \CRM_Utils_Money::format($balancePay); | |
128 | } | |
129 | $row->tokens($entity, $field, $balancePay); | |
130 | } | |
131 | elseif ($field == 'fee_amount') { | |
132 | $row->tokens($entity, $field, \CRM_Utils_Money::format($actionSearchResult->$field)); | |
133 | } | |
134 | elseif (isset($actionSearchResult->$field)) { | |
135 | $row->tokens($entity, $field, $actionSearchResult->$field); | |
136 | } | |
137 | else { | |
138 | $row->tokens($entity, $field, ''); | |
139 | } | |
140 | } | |
141 | ||
142 | } |