forked from WeMoveEU/uk.co.compucorp.civicrm.mailjet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailjet.php
155 lines (142 loc) · 4.84 KB
/
mailjet.php
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
<?php
require_once 'mailjet.civix.php';
/**
* Implementation of hook_civicrm_alterMailParams( )
* To add Mailjet headers in mail
*/
function mailjet_civicrm_alterMailParams(&$params, $context) {
$jobId = CRM_Utils_Array::value('job_id', $params); //CiviCRM job ID
if (isset($jobId)) {
$key = 'mailjet-campaign-'. $jobId;
$mailjetCampaign = Civi::cache()->get($key);
if (!isset($mailjetCampaign)) {
$mailjetCampaign = CRM_Mailjet_BAO_Event::getMailjetCampaign($jobId);
Civi::cache()->set($key, $mailjetCampaign);
}
$params['headers']['X-Mailjet-Campaign'] = $mailjetCampaign;
$params['headers']['X-Mailjet-CustomValue'] = $mailjetCampaign;
$params['headers']['X-Mailjet-Prio'] = 1; // this has to go batch
}
else {
$params['headers']['X-Mailjet-Campaign'] = prepareTransactionalCampaign($params);
$params['headers']['X-Mailjet-CustomValue'] = prepareTransactionalCampaign($params);
$params['headers']['X-Mailjet-Prio'] = 2; // High priority queue
}
$params['headers']['X-MJ-EventPayload'] = prepareEventPayload($params);
if (array_key_exists('Subject',$params) && substr($params['Subject'], 0, 16) === "[CiviMail Draft]") {
$params['headers']['X-Mailjet-Prio'] = 3; // this has to go as fast as possible
}
}
/**
* Implementation of hook_civicrm_pageRun
*
* Handler for pageRun hook.
*/
/* function mailjet_civicrm_pageRun(&$page) {
if (get_class($page) == 'CRM_Mailing_Page_Report') {
$mailingId = $page->_mailing_id;
$mailingJobs = civicrm_api3('MailingJob', 'get', $params = array('mailing_id' => $mailingId));
$stats = array(
'BlockedCount' => 0,
'BouncedCount' => 0,
'ClickedCount' => 0,
'DeliveredCount' => 0,
'OpenedCount' => 0,
'ProcessedCount' => 0,
'QueuedCount' => 0,
'SpamComplaintCount' => 0,
'UnsubscribedCount' => 0,
);
foreach ($mailingJobs['values'] as $key => $job) {
if ($job['job_type'] == 'child') {
$jobId = $key;
require_once('packages/mailjet-0.3/php-mailjet-v3-simple.class.php');
// Create a new Mailjet Object
$mj = new Mailjet(MAILJET_API_KEY, MAILJET_SECRET_KEY);
$mj->debug = 0;
$mailJetParams = array(
'method' => 'VIEW',
'unique' => CRM_Mailjet_BAO_Event::getMailjetCustomCampaignId($jobId),
);
$response = $mj->campaign($mailJetParams);
$page->assign('mailjet_params', $mailJetParams);
if (!empty($response)) {
if ($response->Count == 1) {
$campaign = $response->Data[0];
$mailJetParams = array(
'method' => 'VIEW',
'unique' => $campaign->ID,
);
$response = $mj->campaignstatistics($mailJetParams);
if ($response->Count == 1) {
$stats = sumUpStats($stats, get_object_vars($response->Data[0]));
}
}
}
}
}
$page->assign('mailjet_stats', $stats);
CRM_Core_Region::instance('page-header')->add(array(
'template' => 'CRM/Mailjet/Page/Report.tpl',
));
}
} */
/**
* Implementation of hook_civicrm_config
*/
function mailjet_civicrm_config(&$config) {
_mailjet_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_install
*/
function mailjet_civicrm_install() {
return _mailjet_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_enable
*/
function mailjet_civicrm_enable() {
return _mailjet_civix_civicrm_enable();
}
function sumUpStats($base, $newStats) {
$keys = array(
'BlockedCount',
'BouncedCount',
'ClickedCount',
'DeliveredCount',
'OpenedCount',
'ProcessedCount',
'QueuedCount',
'SpamComplaintCount',
'UnsubscribedCount',
);
foreach ($keys as $key) {
if (array_key_exists($key, $base) && array_key_exists($key, $newStats)) {
$base[$key] += $newStats[$key];
}
}
return $base;
}
function prepareTransactionalCampaign($params) {
$activityId = (int) CRM_Utils_Array::value('custom-activity-id', $params);
$campaignId = (int) CRM_Utils_Array::value('custom-campaign-id', $params);
$from = CRM_Utils_Array::value('from', $params);
if ($activityId || $campaignId) {
return 'TRANS-ACTIVITY-' . $activityId . '-CAMPAIGN-' . $campaignId;
}
return 'TRANS-FROM-' . $from;
}
function prepareEventPayload($params) {
$current_payload = [];
if (isset($params['headers']['X-MJ-EventPayload'])) {
// decode current payload
$current_payload = json_decode($params['headers']['X-MJ-EventPayload'], TRUE);
}
// add Event Payload
$current_payload['jobId'] = (int) CRM_Utils_Array::value('job_id', $params);
$current_payload['activityId'] = (int) CRM_Utils_Array::value('custom-activity-id', $params);
$current_payload['campaignId'] = (int) CRM_Utils_Array::value('custom-campaign-id', $params);
// return merged payload
return json_encode($current_payload);
}