forked from JMAConsulting/biz.jmaconsulting.mte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmte.php
348 lines (319 loc) · 10.7 KB
/
mte.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* Mandrill Transactional Email extension integrates CiviCRM's non-bulk email
* with the Mandrill service
*
* Copyright (C) 2012 JMA Consulting
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Support: https://github.com/JMAConsulting/biz.jmaconsulting.mte/issues
*
* Contact: info@jmaconsulting.biz
* JMA Consulting
* 215 Spadina Ave, Ste 400
* Toronto, ON
* Canada M5T 2C7
*/
require_once 'mte.civix.php';
/**
* Implementation of hook_civicrm_config
*/
function mte_civicrm_config(&$config) {
_mte_civix_civicrm_config($config);
if ($config->userFramework == 'Joomla'
&& 'civicrm/ajax/mte/callback' == CRM_Utils_Array::value('task', $_REQUEST)) {
$_SESSION['mte_temp'] = 1;
}
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function mte_civicrm_xmlMenu(&$files) {
_mte_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function mte_civicrm_install() {
$mailingParams = array(
'subject' => '***All Transactional Emails***',
'url_tracking' => TRUE,
'forward_replies' => FALSE,
'auto_responder' => FALSE,
'open_tracking' => TRUE,
'is_completed' => FALSE,
);
//create entry in civicrm_mailing
$mailing = CRM_Mailing_BAO_Mailing::add($mailingParams, CRM_Core_DAO::$_nullArray);
//add entry in civicrm_mailing_job
//MTE-17
$config = CRM_Core_Config::singleton();
if (property_exists($config, 'civiVersion')) {
$civiVersion = $config->civiVersion;
}
else {
$civiVersion = CRM_Core_BAO_Domain::version();
}
$jobCLassName = 'CRM_Mailing_DAO_MailingJob';
if (version_compare('4.4alpha1', $civiVersion) > 0) {
$jobCLassName = 'CRM_Mailing_DAO_Job';
}
$changeENUM = FALSE;
if (version_compare('4.5alpha1', $civiVersion) > 0) {
$changeENUM = TRUE;
}
CRM_Core_Smarty::singleton()->assign('changeENUM', $changeENUM);
_mte_civix_civicrm_install();
$saveJob = new $jobCLassName();
$saveJob->start_date = $saveJob->end_date = date('YmdHis');
$saveJob->status = 'Complete';
$saveJob->job_type = "Special: All transactional emails being sent through Mandrill";
$saveJob->mailing_id = $mailing->id;
$saveJob->save();
// create mailing bounce type
$mailingBounceType = array(
'1' => array (
'name' => 'Mandrill Hard',
'description' => 'Mandrill hard bounce',
'hold_threshold' => 1,
),
'2' => array (
'name' => 'Mandrill Soft',
'description' => 'Mandrill soft bounce',
'hold_threshold' => 3,
),
'3' => array (
'name' => 'Mandrill Spam',
'description' => 'User marked a transactional email sent via Mandrill as spam',
'hold_threshold' => 1,
),
'4' => array (
'name' => 'Mandrill Reject',
'description' => 'Mandrill rejected delivery to this email address',
'hold_threshold' => 1,
),
);
foreach ($mailingBounceType as $value) {
$bounceType = new CRM_Mailing_DAO_BounceType();
$bounceType->copyValues($value);
if(!$bounceType->find(true)) {
$bounceType->save();
}
}
return TRUE;
}
/**
* Implementation of hook_civicrm_uninstall
*/
function mte_civicrm_uninstall() {
mte_enableDisableNavigationMenu(2);
return _mte_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function mte_civicrm_enable() {
mte_enableDisableNavigationMenu(1);
return _mte_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function mte_civicrm_disable() {
mte_enableDisableNavigationMenu(0);
return _mte_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function mte_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _mte_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function mte_civicrm_managed(&$entities) {
return _mte_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_alterMailer( )
* To alter mailer settings i.e use mandrill smtp settings for transactional mails
*/
function mte_civicrm_alterMailer(&$mailer, $driver, $params) {
$isTransactionalMail = CRM_Core_Smarty::singleton()->get_template_vars('isTransactionalMail', 1);
if ($isTransactionalMail) {
mte_getmailer($mailer, $params);
}
}
function mte_getmailer(&$mailer, &$params = array()) {
$mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
'mandrill_smtp_settings'
);
if (CRM_Utils_array::value('is_active', $mailingBackend)) {
$params['host'] = $mailingBackend['smtpServer'];
$params['port'] = $mailingBackend['smtpPort'];
$params['username'] = trim($mailingBackend['smtpUsername']);
$params['password'] = CRM_Utils_Crypt::decrypt($mailingBackend['smtpPassword']);
$params['auth'] = ($mailingBackend['smtpAuth']) ? TRUE : FALSE;
$mailer = Mail::factory('smtp', $params);
CRM_Core_Smarty::singleton()->assign('isTransactionalMail', 0);
}
}
/**
* Implementation of hook_civicrm_alterMailParams( )
* To send headers in mail and also create activity
*/
function mte_civicrm_alterMailParams(&$params, $context = NULL) {
if ($context == 'civimail') {
return FALSE;
}
$session = CRM_Core_Session::singleton();
$userID = $session->get('userID');
$activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
$params['toEmail'] = trim($params['toEmail']); // BRES-103 Prevent silent failure when emails with whitespaces are used.
if (!$userID) {
$config = CRM_Core_Config::singleton();
if (version_compare($config->civiVersion, '4.3.alpha1') < 0) {
//FIX: source id for version less that 4.3
$matches = array();
preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $params['from'], $matches);
if (!empty($matches)) {
$userID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $matches[0], 'contact_id', 'email');
if (!$userID) {
$userID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['toEmail'], 'contact_id', 'email');
}
}
}
else {
$userID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
}
}
$activityParams = array(
'source_contact_id' => $userID,
'activity_type_id' => array_search('Mandrill Email Sent', $activityTypes),
'subject' => CRM_Utils_Array::value('subject', $params) ? $params['subject'] : CRM_Utils_Array::value('Subject', $params),
'activity_date_time' => date('YmdHis'),
'status_id' => 1,
'priority_id' => 1,
'version' => 3,
'details' => $params['html'],
);
$result = civicrm_api( 'activity','create',$activityParams );
if(CRM_Utils_Array::value('id', $result)){
$params['activityId'] = $result['id'];
$params['headers']['X-MC-Metadata'] = '{"CiviCRM_Mandrill_id": "'.$result['id'].'" }';
CRM_Core_Smarty::singleton()->assign('isTransactionalMail', 1);
if (!method_exists(CRM_Utils_Hook::singleton(), 'alterMail')) {
$mailer = & CRM_Core_Config::getMailer();
mte_getmailer($mailer);
}
}
}
/**
* Implementation of hook_civicrm_postEmailSend( )
* To update the status of activity created in hook_civicrm_alterMailParams.
*/
function mte_civicrm_postEmailSend(&$params) {
if(CRM_Utils_Array::value('activityId', $params)){
$targetContactID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['toEmail'], 'contact_id', 'email');
if (!$targetContactID) {
$result = civicrm_api3('contact', 'create', array(
'contact_type' => 'Individual',
'email' => $params['toEmail'],
));
$targetContactID = $result['id'];
}
$activityParams = array(
'id' => $params['activityId'],
'status_id' => 2,
'version' => 3,
'target_contact_id' => $targetContactID,
);
$result = civicrm_api( 'activity','create',$activityParams );
}
}
/**
* MTE-18 and MTE-38
* function to disable/enable/delete navigation menu
*
* @param integer $action
*
*/
function mte_enableDisableNavigationMenu($action) {
$domainID = CRM_Core_Config::domainID();
if ($action < 2) {
CRM_Core_DAO::executeQuery(
"UPDATE civicrm_option_value cov
INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
SET cov.is_active = %1
WHERE cog.name IN ('activity_type', 'mandrill_secret')
AND cov.name IN('Mandrill Email Bounce', 'Mandrill Email Click', 'Mandrill Email Open', 'Mandrill Email Sent','Secret Code')",
array(
1 => array($action, 'Integer'),
)
);
CRM_Core_DAO::executeQuery(
"UPDATE civicrm_option_group
SET is_active = %1
WHERE name = 'mandrill_secret'",
array(
1 => array($action, 'Integer')
)
);
CRM_Core_DAO::executeQuery(
"UPDATE civicrm_navigation SET is_active = %2 WHERE name = 'mandrill_smtp_settings' AND domain_id = %1",
array(
1 => array($domainID, 'Integer'),
2 => array($action, 'Integer')
)
);
}
else {
CRM_Core_DAO::executeQuery(
"DELETE FROM civicrm_navigation WHERE name = 'mandrill_smtp_settings' AND domain_id = %1",
array(
1 => array($domainID, 'Integer')
)
);
}
}
/**
* Implementation of hook_civicrm_buildForm
*/
function mte_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Admin_Form_Options' && 'mandrill_secret' == $form->getVar('_gName')) {
$values = $form->getVar('_values');
if (CRM_Utils_Array::value('name', $values) != 'Secret Code') {
return FALSE;
}
$form->add('text',
'value',
ts('Value'),
CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'),
TRUE
);
}
}