Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce { | |
18 | ||
6a488035 TO |
19 | /** |
20 | * Create a new bounce event, update the email address if necessary | |
ab432335 | 21 | * |
fa3fdebc | 22 | * @param array $params |
ab432335 EM |
23 | * |
24 | * @return bool|null | |
6a488035 | 25 | */ |
cdc5c450 | 26 | public static function create(&$params) { |
27 | $q = CRM_Mailing_Event_BAO_Queue::verify($params['job_id'], | |
6a488035 TO |
28 | $params['event_queue_id'], |
29 | $params['hash'] | |
30 | ); | |
31 | $success = NULL; | |
32 | ||
33 | if (!$q) { | |
34 | return $success; | |
35 | } | |
36 | ||
353ffa53 TO |
37 | $transaction = new CRM_Core_Transaction(); |
38 | $bounce = new CRM_Mailing_Event_BAO_Bounce(); | |
6a488035 TO |
39 | $bounce->time_stamp = date('YmdHis'); |
40 | ||
532e83d3 | 41 | $action = empty($params['id']) ? 'create' : 'edit'; |
42 | CRM_Utils_Hook::pre($action, 'MailingEventBounce', CRM_Utils_Array::value('id', $params), $params); | |
43 | ||
6a488035 TO |
44 | // if we dont have a valid bounce type, we should set it |
45 | // to bounce_type_id 11 which is Syntax error. this allows such email | |
46 | // addresses to be bounce a few more time before being put on hold | |
47 | // CRM-4814 | |
48 | // we changed this behavior since this bounce type might be due to some issue | |
49 | // with the connection or smtp server etc | |
50 | if (empty($params['bounce_type_id'])) { | |
51 | $params['bounce_type_id'] = 11; | |
52 | if (empty($params['bounce_reason'])) { | |
53 | $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email'); | |
54 | } | |
55 | } | |
56 | ||
212eb1a8 TS |
57 | // replace any invalid unicode characters with replacement characters |
58 | $params['bounce_reason'] = mb_convert_encoding($params['bounce_reason'], 'UTF-8', 'UTF-8'); | |
59 | ||
b29b2a44 TS |
60 | // dev/mail#37 Replace 4-byte utf8 characaters with the unicode replacement character |
61 | // while CiviCRM does not support utf8mb4 for MySQL | |
62 | $params['bounce_reason'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $params['bounce_reason']); | |
63 | ||
6a488035 | 64 | // CRM-11989 |
307aac0a | 65 | $params['bounce_reason'] = mb_strcut($params['bounce_reason'], 0, 254); |
6a488035 TO |
66 | |
67 | $bounce->copyValues($params); | |
68 | $bounce->save(); | |
6a488035 | 69 | |
532e83d3 | 70 | CRM_Utils_Hook::post($action, 'MailingEventBounce', $bounce->id, $bounce); |
71 | ||
d15a97f4 | 72 | if ($q->email_id) { |
73 | self::putEmailOnHold($q->email_id); | |
6a488035 TO |
74 | } |
75 | $transaction->commit(); | |
76 | ||
d15a97f4 | 77 | return TRUE; |
6a488035 TO |
78 | } |
79 | ||
80 | /** | |
fe482240 | 81 | * Get row count for the event selector. |
6a488035 | 82 | * |
90c8230e TO |
83 | * @param int $mailing_id |
84 | * ID of the mailing. | |
85 | * @param int $job_id | |
86 | * Optional ID of a job to filter on. | |
6a488035 | 87 | * |
ad37ac8e | 88 | * @param string|null $toDate |
89 | * | |
a6c01b45 CW |
90 | * @return int |
91 | * Number of rows in result set | |
6a488035 | 92 | */ |
2f2240dc | 93 | public static function getTotalCount($mailing_id, $job_id = NULL, $toDate = NULL) { |
6a488035 TO |
94 | $dao = new CRM_Core_DAO(); |
95 | ||
353ffa53 TO |
96 | $bounce = self::getTableName(); |
97 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
6a488035 | 98 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 99 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
100 | |
101 | $query = " | |
102 | SELECT COUNT($bounce.id) as bounce | |
103 | FROM $bounce | |
104 | INNER JOIN $queue | |
105 | ON $bounce.event_queue_id = $queue.id | |
106 | INNER JOIN $job | |
107 | ON $queue.job_id = $job.id | |
108 | INNER JOIN $mailing | |
109 | ON $job.mailing_id = $mailing.id | |
110 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
111 | ||
7811a84b | 112 | if (!empty($toDate)) { |
113 | $query .= " AND $bounce.time_stamp <= $toDate"; | |
114 | } | |
115 | ||
6a488035 TO |
116 | if (!empty($job_id)) { |
117 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
118 | } | |
119 | ||
6a488035 TO |
120 | // query was missing |
121 | $dao->query($query); | |
122 | ||
123 | if ($dao->fetch()) { | |
124 | return $dao->bounce; | |
125 | } | |
126 | ||
127 | return NULL; | |
128 | } | |
129 | ||
130 | /** | |
fe482240 | 131 | * Get rows for the event browser. |
6a488035 | 132 | * |
90c8230e TO |
133 | * @param int $mailing_id |
134 | * ID of the mailing. | |
135 | * @param int $job_id | |
136 | * Optional ID of the job. | |
137 | * @param bool $is_distinct | |
138 | * Group by queue id?. | |
139 | * @param int $offset | |
140 | * Offset. | |
141 | * @param int $rowCount | |
142 | * Number of rows. | |
143 | * @param array $sort | |
144 | * Sort array. | |
6a488035 | 145 | * |
a6c01b45 CW |
146 | * @return array |
147 | * Result set | |
6a488035 | 148 | */ |
a3d7e8ee TO |
149 | public static function &getRows( |
150 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
151 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL |
152 | ) { | |
153 | ||
0fc59d7a | 154 | $dao = new CRM_Core_DAO(); |
6a488035 | 155 | |
353ffa53 | 156 | $bounce = self::getTableName(); |
6a488035 | 157 | $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); |
353ffa53 TO |
158 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); |
159 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
160 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); | |
161 | $contact = CRM_Contact_BAO_Contact::getTableName(); | |
162 | $email = CRM_Core_BAO_Email::getTableName(); | |
6a488035 TO |
163 | |
164 | $query = " | |
165 | SELECT $contact.display_name as display_name, | |
166 | $contact.id as contact_id, | |
167 | $email.email as email, | |
168 | $bounce.time_stamp as date, | |
169 | $bounce.bounce_reason as reason, | |
170 | $bounceType.name as bounce_type | |
171 | FROM $contact | |
172 | INNER JOIN $queue | |
173 | ON $queue.contact_id = $contact.id | |
174 | INNER JOIN $email | |
175 | ON $queue.email_id = $email.id | |
176 | INNER JOIN $bounce | |
177 | ON $bounce.event_queue_id = $queue.id | |
178 | LEFT JOIN $bounceType | |
179 | ON $bounce.bounce_type_id = $bounceType.id | |
180 | INNER JOIN $job | |
181 | ON $queue.job_id = $job.id | |
182 | AND $job.is_test = 0 | |
183 | INNER JOIN $mailing | |
184 | ON $job.mailing_id = $mailing.id | |
185 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
186 | ||
187 | if (!empty($job_id)) { | |
188 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
189 | } | |
190 | ||
191 | if ($is_distinct) { | |
0ee5581e | 192 | $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name "; |
6a488035 TO |
193 | } |
194 | ||
195 | $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC"; | |
196 | if ($sort) { | |
197 | if (is_string($sort)) { | |
21d32567 | 198 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
199 | $orderBy = $sort; |
200 | } | |
201 | else { | |
202 | $orderBy = trim($sort->orderBy()); | |
203 | } | |
204 | } | |
205 | $query .= " ORDER BY {$orderBy} "; | |
206 | ||
207 | if ($offset || $rowCount) { | |
208 | //Added "||$rowCount" to avoid displaying all records on first page | |
209 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
210 | } | |
211 | ||
212 | $dao->query($query); | |
213 | ||
affcc9d2 | 214 | $results = []; |
6a488035 TO |
215 | |
216 | while ($dao->fetch()) { | |
217 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
218 | "reset=1&cid={$dao->contact_id}" | |
219 | ); | |
220 | $results[] = array( | |
221 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
222 | 'email' => $dao->email, | |
223 | // FIXME: translate this | |
389bcebf | 224 | 'type' => (empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type |
6a488035 TO |
225 | ), |
226 | 'reason' => $dao->reason, | |
227 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
228 | ); | |
229 | } | |
230 | return $results; | |
231 | } | |
96025800 | 232 | |
d15a97f4 | 233 | /** |
234 | * Put the email on hold if it has met the threshold. | |
235 | * | |
236 | * @param int $email_id | |
237 | */ | |
238 | protected static function putEmailOnHold($email_id) { | |
239 | ||
240 | $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); | |
241 | $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); | |
242 | $emailTable = CRM_Core_BAO_Email::getTableName(); | |
243 | $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
244 | ||
245 | // might want to put distinct inside the count | |
246 | $query = "SELECT count($bounceTable.id) as bounces, | |
247 | $bounceType.hold_threshold as threshold | |
248 | FROM $bounceTable | |
249 | INNER JOIN $bounceType | |
250 | ON $bounceTable.bounce_type_id = $bounceType.id | |
251 | INNER JOIN $queueTable | |
252 | ON $bounceTable.event_queue_id = $queueTable.id | |
253 | INNER JOIN $emailTable | |
254 | ON $queueTable.email_id = $emailTable.id | |
255 | WHERE $emailTable.id = $email_id | |
256 | AND ($emailTable.reset_date IS NULL | |
257 | OR $bounceTable.time_stamp >= $emailTable.reset_date) | |
258 | GROUP BY $bounceTable.bounce_type_id | |
259 | ORDER BY threshold, bounces desc"; | |
260 | ||
261 | $dao = CRM_Core_DAO::executeQuery($query); | |
262 | ||
263 | while ($dao->fetch()) { | |
264 | if ($dao->bounces >= $dao->threshold) { | |
265 | $email = new CRM_Core_BAO_Email(); | |
266 | $email->id = $email_id; | |
267 | $email->on_hold = TRUE; | |
268 | $email->hold_date = date('YmdHis'); | |
269 | $email->save(); | |
270 | break; | |
271 | } | |
272 | } | |
273 | } | |
274 | ||
6a488035 | 275 | } |