-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheventbrite.php
More file actions
285 lines (263 loc) · 10.2 KB
/
Copy patheventbrite.php
File metadata and controls
285 lines (263 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* TODOS:
* - use post hook to delete eventbritelink 'event' records on event.delete op.
*/
require_once 'eventbrite.civix.php';
use CRM_Eventbrite_ExtensionUtil as E;
/**
* Implements hook_civicrm_post().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post
*/
function eventbrite_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($op == 'delete') {
if (in_array($objectName, array('Participant', 'Contribution', 'Event'))) {
$result = _eventbrite_civicrmapi('EventbriteLink', 'get', array(
'civicrm_entity_type' => $objectName,
'civicrm_entity_id' => $objectId,
'api.EventbriteLink.delete' => array(),
));
}
if ($objectName == 'Participant') {
// Also delete PrimaryParticant links.
$result = _eventbrite_civicrmapi('EventbriteLink', 'get', array(
'civicrm_entity_type' => 'PrimaryParticipant',
'civicrm_entity_id' => $objectId,
'api.EventbriteLink.delete' => array(),
));
}
}
}
/**
* Implements hook_civicrm_pageRun().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pageRun
*/
function eventbrite_civicrm_pageRun(&$page) {
// If permission and configs are right, display EventbriteLink data for certain entities.
if (
CRM_Core_Permission::check('Administer CiviCRM')
&& _eventbrite_civicrmapi('Setting', 'getvalue', array('name' => "eventbrite_is_link_inspect"))
) {
$pageName = $page->getVar('_name');
$entityPerPage = array(
'CRM_Event_Page_Tab' => 'participant',
'CRM_Contribute_Page_Tab' => 'contribution',
'CRM_Contact_Page_View_Summary' => 'contact',
);
if ($entity = CRM_Utils_Array::value($pageName, $entityPerPage)) {
$ebToken = _eventbrite_civicrmapi('Setting', 'getvalue', array('name' => "eventbrite_api_token"));
switch ($entity) {
case 'contact':
$link = _eventbrite_civicrmapi('EventbriteLink', 'get', array(
'civicrm_entity_type' => 'event',
'eb_entity_type' => 'event',
'options' => array(
'limit' => 0,
),
));
$eventIds = crm_utils_array::collect('civicrm_entity_id', $link['values']);
$cid = $page->getVar('_contactId');
$participant = _eventbrite_civicrmapi('participant', 'get', array(
'contact_id' => $cid,
'event_id' => array('IN' => $eventIds),
'api.EventbriteLink.get' => array(
'civicrm_entity_type' => 'participant',
'eb_entity_type' => 'attendee',
'civicrm_entity_id' => '$value.id',
),
));
$participantAttendees = array();
foreach ($participant['values'] as $value) {
if (!empty($value['api.EventbriteLink.get']['values'][0])) {
$pid = $value['api.EventbriteLink.get']['values'][0]['civicrm_entity_id'];
$aid = $value['api.EventbriteLink.get']['values'][0]['eb_entity_id'];
$participantAttendees[$pid] = $aid;
}
}
if (!empty($participantAttendees)) {
$msg = E::ts('This contact is linked to Eventbrite Attendees throug participant records:') . '<ul>';
foreach ($participantAttendees as $participantId => $attendeeId) {
$tsParams = array(
'1' => $participantId,
'2' => $attendeeId,
'3' => CRM_Utils_System::url('civicrm/contact/view/participant', "reset=1&id=$participantId&cid=$cid&action=view&context=participant&selectedChild=event"),
'4' => "https://www.eventbriteapi.com/v3/attendees/{$attendeeId}/?token={$ebToken}",
);
$msg .= '<li>' . E::ts('<a href="%3">Participant %1</a> is linked to <a href="%4" target="_blank">Eventbrite Attendee %2</a>.', $tsParams) . '</li>';
}
$msg .= '</ul>';
CRM_Core_Session::setStatus($msg, E::ts('Eventbrite linked entity'), 'info');
}
break;
case 'participant':
if ($page->_id) {
$link = _eventbrite_civicrmapi('EventbriteLink', 'get', array(
'civicrm_entity_type' => 'participant',
'eb_entity_type' => 'attendee',
'civicrm_entity_id' => $page->_id,
'sequential' => 1,
));
if (!empty($link['values'][0])) {
$attendeeId = $link['values'][0]['eb_entity_id'];
$tsParams = array(
'1' => $attendeeId,
'2' => "https://www.eventbriteapi.com/v3/attendees/{$attendeeId}/?token={$ebToken}",
);
$msg = E::ts('This participant is linked to <a href="%2" target="_blank">Eventbrite Attendee: %1</a>.', $tsParams);
CRM_Core_Session::setStatus($msg, E::ts('Eventbrite linked entity'), 'info', array('expires' => 0));
}
}
break;
case 'contribution':
if ($page->_id) {
$link = _eventbrite_civicrmapi('EventbriteLink', 'get', array(
'civicrm_entity_type' => 'contribution',
'eb_entity_type' => 'order',
'civicrm_entity_id' => $page->_id,
'sequential' => 1,
));
if (!empty($link['values'][0])) {
$orderId = $link['values'][0]['eb_entity_id'];
$tsParams = array(
'1' => $orderId,
'2' => "https://www.eventbriteapi.com/v3/orders/{$orderId}/?token={$ebToken}&expand=attendees,attendee-answers",
);
$msg = E::ts('This contribution is linked to <a href="%2" target="_blank">Eventbrite Order: %1</a>.', $tsParams);
CRM_Core_Session::setStatus($msg, E::ts('Eventbrite linked entity'), 'info', array('expires' => 0));
}
}
break;
}
}
}
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function eventbrite_civicrm_config(&$config) {
_eventbrite_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function eventbrite_civicrm_install() {
_eventbrite_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function eventbrite_civicrm_enable() {
_eventbrite_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*/
// function eventbrite_civicrm_preProcess($formName, &$form) {
// } // */
/**
* For an array of menu items, recursively get the value of the greatest navID
* attribute.
* @param <type> $menu
* @param <type> $max_navID
*/
function _eventbrite_get_max_navID(&$menu, &$max_navID = NULL) {
foreach ($menu as $id => $item) {
if (!empty($item['attributes']['navID'])) {
$max_navID = max($max_navID, $item['attributes']['navID']);
}
if (!empty($item['child'])) {
_eventbrite_get_max_navID($item['child'], $max_navID);
}
}
}
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*/
function eventbrite_civicrm_navigationMenu(&$menu) {
_eventbrite_get_max_navID($menu, $max_navID);
_eventbrite_civix_insert_navigation_menu($menu, 'Administer/CiviEvent', array(
'label' => E::ts('Eventbrite Integration'),
'name' => 'Eventbrite Integration',
'url' => 'civicrm/admin/eventbrite',
'permission' => 'administer CiviCRM',
'operator' => 'AND',
'separator' => NULL,
'navID' => ++$max_navID,
));
_eventbrite_civix_insert_navigation_menu($menu, 'Administer/CiviEvent/Eventbrite Integration', array(
'label' => E::ts('Settings'),
'name' => 'Settings',
'url' => 'civicrm/admin/eventbrite/settings',
'permission' => 'administer CiviCRM',
'operator' => 'AND',
'separator' => NULL,
'navID' => ++$max_navID,
));
_eventbrite_civix_insert_navigation_menu($menu, 'Administer/CiviEvent/Eventbrite Integration', array(
'label' => E::ts('Events'),
'name' => 'Events',
'url' => 'civicrm/admin/eventbrite/manage/events?action=browse&reset=1',
'permission' => 'administer CiviCRM',
'operator' => 'AND',
'separator' => NULL,
'navID' => ++$max_navID,
));
_eventbrite_civix_navigationMenu($menu);
}
/**
* Log CiviCRM API errors to CiviCRM log.
*/
function _eventbrite_log_api_error(CiviCRM_API3_Exception $e, $entity, $action, $contextMessage = NULL, $params) {
$message = "CiviCRM API Error '{$entity}.{$action}': " . $e->getMessage() . '; ';
$message .= "API parameters when this error happened: " . json_encode($params) . '; ';
$bt = debug_backtrace();
$error_location = "{$bt[1]['file']}::{$bt[1]['line']}";
$message .= "Error API called from: $error_location";
CRM_Core_Error::debug_log_message($message);
$eventbriteLogMessage = $message;
if ($contextMessage) {
$eventbriteLogMessage .= "; Context: $contextMessage";
}
CRM_Eventbrite_BAO_EventbriteLog::create(array(
'message' => $eventbriteLogMessage,
'message_type_id' => CRM_Eventbrite_BAO_EventbriteLog::MESSAGE_TYPE_ID_CIVICRM_API_ERROR,
));
}
/**
* CiviCRM API wrapper. Wraps with try/catch, redirects errors to log, saves
* typing.
*
* @param string $entity as in civicrm_api3($ENTITY, ..., ...)
* @param string $action as in civicrm_api3(..., $ACTION, ...)
* @param array $params as in civicrm_api3(..., ..., $PARAMS)
* @param string $contextMessage Additional message for inclusion in EventbriteLog upon any failures.
* @param bool $silence_errors If TRUE, throw any exceptions we catch; otherwise don't.
*
* @return Array result of civicrm_api3()
* @throws CiviCRM_API3_Exception
*/
function _eventbrite_civicrmapi($entity, $action, $params, $contextMessage = NULL, $silence_errors = TRUE) {
try {
$result = civicrm_api3($entity, $action, $params);
}
catch (CiviCRM_API3_Exception $e) {
_eventbrite_log_api_error($e, $entity, $action, $contextMessage, $params);
if (!$silence_errors) {
throw $e;
}
}
return $result;
}