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 EM |
21 | * |
22 | * @param $params | |
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. | |
87 | * @param bool $is_distinct | |
88 | * Group by queue ID?. | |
6a488035 | 89 | * |
ad37ac8e | 90 | * @param string|null $toDate |
91 | * | |
a6c01b45 CW |
92 | * @return int |
93 | * Number of rows in result set | |
6a488035 | 94 | */ |
7811a84b | 95 | public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) { |
6a488035 TO |
96 | $dao = new CRM_Core_DAO(); |
97 | ||
353ffa53 TO |
98 | $bounce = self::getTableName(); |
99 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
6a488035 | 100 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 101 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
102 | |
103 | $query = " | |
104 | SELECT COUNT($bounce.id) as bounce | |
105 | FROM $bounce | |
106 | INNER JOIN $queue | |
107 | ON $bounce.event_queue_id = $queue.id | |
108 | INNER JOIN $job | |
109 | ON $queue.job_id = $job.id | |
110 | INNER JOIN $mailing | |
111 | ON $job.mailing_id = $mailing.id | |
112 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
113 | ||
7811a84b | 114 | if (!empty($toDate)) { |
115 | $query .= " AND $bounce.time_stamp <= $toDate"; | |
116 | } | |
117 | ||
6a488035 TO |
118 | if (!empty($job_id)) { |
119 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
120 | } | |
121 | ||
122 | if ($is_distinct) { | |
123 | $query .= " GROUP BY $queue.id "; | |
124 | } | |
125 | ||
126 | // query was missing | |
127 | $dao->query($query); | |
128 | ||
129 | if ($dao->fetch()) { | |
130 | return $dao->bounce; | |
131 | } | |
132 | ||
133 | return NULL; | |
134 | } | |
135 | ||
136 | /** | |
fe482240 | 137 | * Get rows for the event browser. |
6a488035 | 138 | * |
90c8230e TO |
139 | * @param int $mailing_id |
140 | * ID of the mailing. | |
141 | * @param int $job_id | |
142 | * Optional ID of the job. | |
143 | * @param bool $is_distinct | |
144 | * Group by queue id?. | |
145 | * @param int $offset | |
146 | * Offset. | |
147 | * @param int $rowCount | |
148 | * Number of rows. | |
149 | * @param array $sort | |
150 | * Sort array. | |
6a488035 | 151 | * |
a6c01b45 CW |
152 | * @return array |
153 | * Result set | |
6a488035 | 154 | */ |
a3d7e8ee TO |
155 | public static function &getRows( |
156 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
157 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL |
158 | ) { | |
159 | ||
0fc59d7a | 160 | $dao = new CRM_Core_DAO(); |
6a488035 | 161 | |
353ffa53 | 162 | $bounce = self::getTableName(); |
6a488035 | 163 | $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); |
353ffa53 TO |
164 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); |
165 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
166 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); | |
167 | $contact = CRM_Contact_BAO_Contact::getTableName(); | |
168 | $email = CRM_Core_BAO_Email::getTableName(); | |
6a488035 TO |
169 | |
170 | $query = " | |
171 | SELECT $contact.display_name as display_name, | |
172 | $contact.id as contact_id, | |
173 | $email.email as email, | |
174 | $bounce.time_stamp as date, | |
175 | $bounce.bounce_reason as reason, | |
176 | $bounceType.name as bounce_type | |
177 | FROM $contact | |
178 | INNER JOIN $queue | |
179 | ON $queue.contact_id = $contact.id | |
180 | INNER JOIN $email | |
181 | ON $queue.email_id = $email.id | |
182 | INNER JOIN $bounce | |
183 | ON $bounce.event_queue_id = $queue.id | |
184 | LEFT JOIN $bounceType | |
185 | ON $bounce.bounce_type_id = $bounceType.id | |
186 | INNER JOIN $job | |
187 | ON $queue.job_id = $job.id | |
188 | AND $job.is_test = 0 | |
189 | INNER JOIN $mailing | |
190 | ON $job.mailing_id = $mailing.id | |
191 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
192 | ||
193 | if (!empty($job_id)) { | |
194 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
195 | } | |
196 | ||
197 | if ($is_distinct) { | |
0ee5581e | 198 | $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name "; |
6a488035 TO |
199 | } |
200 | ||
201 | $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC"; | |
202 | if ($sort) { | |
203 | if (is_string($sort)) { | |
21d32567 | 204 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
205 | $orderBy = $sort; |
206 | } | |
207 | else { | |
208 | $orderBy = trim($sort->orderBy()); | |
209 | } | |
210 | } | |
211 | $query .= " ORDER BY {$orderBy} "; | |
212 | ||
213 | if ($offset || $rowCount) { | |
214 | //Added "||$rowCount" to avoid displaying all records on first page | |
215 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
216 | } | |
217 | ||
218 | $dao->query($query); | |
219 | ||
affcc9d2 | 220 | $results = []; |
6a488035 TO |
221 | |
222 | while ($dao->fetch()) { | |
223 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
224 | "reset=1&cid={$dao->contact_id}" | |
225 | ); | |
226 | $results[] = array( | |
227 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
228 | 'email' => $dao->email, | |
229 | // FIXME: translate this | |
389bcebf | 230 | 'type' => (empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type |
6a488035 TO |
231 | ), |
232 | 'reason' => $dao->reason, | |
233 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
234 | ); | |
235 | } | |
236 | return $results; | |
237 | } | |
96025800 | 238 | |
d15a97f4 | 239 | /** |
240 | * Put the email on hold if it has met the threshold. | |
241 | * | |
242 | * @param int $email_id | |
243 | */ | |
244 | protected static function putEmailOnHold($email_id) { | |
245 | ||
246 | $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); | |
247 | $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); | |
248 | $emailTable = CRM_Core_BAO_Email::getTableName(); | |
249 | $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
250 | ||
251 | // might want to put distinct inside the count | |
252 | $query = "SELECT count($bounceTable.id) as bounces, | |
253 | $bounceType.hold_threshold as threshold | |
254 | FROM $bounceTable | |
255 | INNER JOIN $bounceType | |
256 | ON $bounceTable.bounce_type_id = $bounceType.id | |
257 | INNER JOIN $queueTable | |
258 | ON $bounceTable.event_queue_id = $queueTable.id | |
259 | INNER JOIN $emailTable | |
260 | ON $queueTable.email_id = $emailTable.id | |
261 | WHERE $emailTable.id = $email_id | |
262 | AND ($emailTable.reset_date IS NULL | |
263 | OR $bounceTable.time_stamp >= $emailTable.reset_date) | |
264 | GROUP BY $bounceTable.bounce_type_id | |
265 | ORDER BY threshold, bounces desc"; | |
266 | ||
267 | $dao = CRM_Core_DAO::executeQuery($query); | |
268 | ||
269 | while ($dao->fetch()) { | |
270 | if ($dao->bounces >= $dao->threshold) { | |
271 | $email = new CRM_Core_BAO_Email(); | |
272 | $email->id = $email_id; | |
273 | $email->on_hold = TRUE; | |
274 | $email->hold_date = date('YmdHis'); | |
275 | $email->save(); | |
276 | break; | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
6a488035 | 281 | } |