-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathomnimail.php
189 lines (176 loc) · 5.9 KB
/
omnimail.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
<?php
require_once 'omnimail.civix.php';
use Civi\Api4\CustomGroup;
use Civi\Api4\Email;
use Civi\Api4\Omnicontact;
use CRM_Omnimail_ExtensionUtil as E;
// checking if the file exists allows compilation elsewhere if desired.
if (file_exists( __DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function omnimail_civicrm_config(&$config) {
_omnimail_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function omnimail_civicrm_install() {
_omnimail_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function omnimail_civicrm_enable() {
_omnimail_civix_civicrm_enable();
}
/**
* Add mailing event tab to contact summary screen
* @param string $tabsetName
* @param array $tabs
* @param array $context
*/
function omnimail_civicrm_tabset($tabsetName, &$tabs, $context) {
if ($tabsetName == 'civicrm/contact/view') {
$contactID = $context['contact_id'];
$url = CRM_Utils_System::url('civicrm/contact/mailings/view', "reset=1&snippet=json&force=1&cid=$contactID");
$tabs[] = [
'title' => ts('Mailing Events'),
'id' => 'omnimail',
'icon' => 'crm-i fa-envelope-open-o',
'url' => $url,
'weight' => 51, // Somewhere near the activities tab
'class' => 'livePage',
'count' => civicrm_api3('MailingProviderData', 'getcount', ['contact_id' => $contactID])
];
}
}
/**
* Keep mailing provider data out of log tables.
*
* @param array $logTableSpec
*/
function omnimail_civicrm_alterLogTables(&$logTableSpec) {
unset($logTableSpec['civicrm_mailing_provider_data'], $logTableSpec['civicrm_omnimail_job_progress']);
}
/**
* Ensure any missed omnimail dedupes are sorted before a contact is permanently deleted.
*
* Note this is required because we were not updating the contact id when merging contacts in the past.
*
* Doing it via a pre-hook on delete does not fix all the missed moves of this data - but it ensures we don't lose
* our last chance to do so & leaves the data just a bit better.
*
* Later we might have fixed it all & not need this.
*
* @param \CRM_Core_DAO $op
* @param string $objectName
* @param int|null $id
* @param array $params
*
* @throws \CRM_Core_Exception
*/
function omnimail_civicrm_pre($op, $objectName, $id, &$params) {
if ($op === 'delete' && in_array($objectName, ['Individual', 'Organization', 'Household', 'Contact'])) {
// Argh on prod contact_id is a varchar - put in quotes - aim to change to an int.
if (CRM_Core_DAO::singleValueQuery('SELECT 1 FROM civicrm_mailing_provider_data WHERE contact_id = "' . (int) $id . '"')) {
$mergedTo = civicrm_api3('Contact', 'getmergedto', ['contact_id' => $id])['values'];
if (!empty($mergedTo)) {
CRM_Core_DAO::executeQuery('
UPDATE civicrm_mailing_provider_data SET contact_id = "' . (int) key($mergedTo)
. '" WHERE contact_id = "' . (int) $id . '"'
);
}
}
}
}
function omnimail_civicrm_custom($op, $groupID, $entityID, &$params) {
// Early return if not the relevant group.
if ((int) $groupID !== _omnimail_civicrm_get_snooze_group_id()
|| !in_array($op, ['edit', 'create'])
// This static is a placeholder for later functionality where we want to
// update from Acoustic & we don't want this hook to fire to tell Acoustic about
// it's own data.
|| !empty(\Civi::$statics['omnimail']['is_batch_snooze_update'])
) {
return;
}
foreach ($params as $customFieldValue) {
if ($customFieldValue['column_name'] === 'snooze_date') {
$snoozeDate = $customFieldValue['value'];
if (!empty($snoozeDate)) {
try {
$snoozeDateObject = new DateTime($snoozeDate);
}
catch (Exception $e) {
Civi::log('wmf')->warning("Bad date format for email snooze field: '$snoozeDate'");
return;
}
// Only send snooze to mailing provider if the snooze date has not passed.
if ($snoozeDateObject > (new DateTime())) {
$fields = Email::get(FALSE)
->addWhere('id', '=', $customFieldValue['entity_id'])
->addWhere('is_primary', '=', TRUE)
->addSelect('email', 'contact_id')
->execute()->first();
$email = $fields['email'];
$contact_id = $fields['contact_id'];
if ($email) {
Omnicontact::snooze(FALSE)
->setEmail($email)
->setContactID($contact_id)
->setSnoozeDate($snoozeDate)->execute();
}
}
}
}
}
}
function _omnimail_civicrm_get_snooze_group_id(): int {
if (!isset(\Civi::$statics[__FUNCTION__])) {
\Civi::$statics[__FUNCTION__] = CustomGroup::get(FALSE)
->addWhere('name', '=', 'email_settings')
->execute()->first()['id'];
}
return (int) \Civi::$statics[__FUNCTION__];
}
/**
* Queue wrapper for api function.
*
* I'm hoping to get this or a similar fix upstreamed - so this
* should be temporary - it adds a function that calls the v4 api,
* ignoring the ctx - which doesn't serialise well...
*
* @param $ctx
* @param $entity
* @param $action
* @param $params
*
* @return true
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\NotImplementedException
*/
function civicrm_api4_queue($ctx, $entity, $action, $params): bool {
try {
civicrm_api4($entity, $action, $params);
}
catch (CRM_Core_Exception $e) {
\Civi::log('wmf')->error('queued action failed {entity} {action} {params} {message} {exception}', [
'entity' => $entity,
'action' => $action,
'params' => $params,
'message' => $e->getMessage(),
'exception' => $e,
]);
return FALSE;
}
return TRUE;
}