3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Mailing_Event_BAO_Bounce
extends CRM_Mailing_Event_DAO_Bounce
{
38 public function __construct() {
39 parent
::__construct();
43 * Create a new bounce event, update the email address if necessary
49 public static function create(&$params) {
50 $q = CRM_Mailing_Event_BAO_Queue
::verify($params['job_id'],
51 $params['event_queue_id'],
60 $transaction = new CRM_Core_Transaction();
61 $bounce = new CRM_Mailing_Event_BAO_Bounce();
62 $bounce->time_stamp
= date('YmdHis');
64 $action = empty($params['id']) ?
'create' : 'edit';
65 CRM_Utils_Hook
::pre($action, 'MailingEventBounce', CRM_Utils_Array
::value('id', $params), $params);
67 // if we dont have a valid bounce type, we should set it
68 // to bounce_type_id 11 which is Syntax error. this allows such email
69 // addresses to be bounce a few more time before being put on hold
71 // we changed this behavior since this bounce type might be due to some issue
72 // with the connection or smtp server etc
73 if (empty($params['bounce_type_id'])) {
74 $params['bounce_type_id'] = 11;
75 if (empty($params['bounce_reason'])) {
76 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
81 $params['bounce_reason'] = mb_strcut($params['bounce_reason'], 0, 254);
83 $bounce->copyValues($params);
86 CRM_Utils_Hook
::post($action, 'MailingEventBounce', $bounce->id
, $bounce);
89 self
::putEmailOnHold($q->email_id
);
91 $transaction->commit();
97 * Get row count for the event selector.
99 * @param int $mailing_id
102 * Optional ID of a job to filter on.
103 * @param bool $is_distinct
104 * Group by queue ID?.
106 * @param string|null $toDate
109 * Number of rows in result set
111 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
112 $dao = new CRM_Core_DAO();
114 $bounce = self
::getTableName();
115 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
116 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
117 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
120 SELECT COUNT($bounce.id) as bounce
123 ON $bounce.event_queue_id = $queue.id
125 ON $queue.job_id = $job.id
127 ON $job.mailing_id = $mailing.id
128 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
130 if (!empty($toDate)) {
131 $query .= " AND $bounce.time_stamp <= $toDate";
134 if (!empty($job_id)) {
135 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
139 $query .= " GROUP BY $queue.id ";
153 * Get rows for the event browser.
155 * @param int $mailing_id
158 * Optional ID of the job.
159 * @param bool $is_distinct
160 * Group by queue id?.
163 * @param int $rowCount
171 public static function &getRows(
172 $mailing_id, $job_id = NULL,
173 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
176 $dao = new CRM_Core_Dao();
178 $bounce = self
::getTableName();
179 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
180 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
181 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
182 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
183 $contact = CRM_Contact_BAO_Contact
::getTableName();
184 $email = CRM_Core_BAO_Email
::getTableName();
187 SELECT $contact.display_name as display_name,
188 $contact.id as contact_id,
189 $email.email as email,
190 $bounce.time_stamp as date,
191 $bounce.bounce_reason as reason,
192 $bounceType.name as bounce_type
195 ON $queue.contact_id = $contact.id
197 ON $queue.email_id = $email.id
199 ON $bounce.event_queue_id = $queue.id
200 LEFT JOIN $bounceType
201 ON $bounce.bounce_type_id = $bounceType.id
203 ON $queue.job_id = $job.id
206 ON $job.mailing_id = $mailing.id
207 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
209 if (!empty($job_id)) {
210 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
214 $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name ";
217 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
219 if (is_string($sort)) {
220 $sort = CRM_Utils_Type
::escape($sort, 'String');
224 $orderBy = trim($sort->orderBy());
227 $query .= " ORDER BY {$orderBy} ";
229 if ($offset ||
$rowCount) {
230 //Added "||$rowCount" to avoid displaying all records on first page
231 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
238 while ($dao->fetch()) {
239 $url = CRM_Utils_System
::url('civicrm/contact/view',
240 "reset=1&cid={$dao->contact_id}"
243 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
244 'email' => $dao->email
,
245 // FIXME: translate this
246 'type' => (empty($dao->bounce_type
) ?
ts('Unknown') : $dao->bounce_type
248 'reason' => $dao->reason
,
249 'date' => CRM_Utils_Date
::customFormat($dao->date
),
256 * Put the email on hold if it has met the threshold.
258 * @param int $email_id
260 protected static function putEmailOnHold($email_id) {
262 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
263 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
264 $emailTable = CRM_Core_BAO_Email
::getTableName();
265 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
267 // might want to put distinct inside the count
268 $query = "SELECT count($bounceTable.id) as bounces,
269 $bounceType.hold_threshold as threshold
271 INNER JOIN $bounceType
272 ON $bounceTable.bounce_type_id = $bounceType.id
273 INNER JOIN $queueTable
274 ON $bounceTable.event_queue_id = $queueTable.id
275 INNER JOIN $emailTable
276 ON $queueTable.email_id = $emailTable.id
277 WHERE $emailTable.id = $email_id
278 AND ($emailTable.reset_date IS NULL
279 OR $bounceTable.time_stamp >= $emailTable.reset_date)
280 GROUP BY $bounceTable.bounce_type_id
281 ORDER BY threshold, bounces desc";
283 $dao = CRM_Core_DAO
::executeQuery($query);
285 while ($dao->fetch()) {
286 if ($dao->bounces
>= $dao->threshold
) {
287 $email = new CRM_Core_BAO_Email();
288 $email->id
= $email_id;
289 $email->on_hold
= TRUE;
290 $email->hold_date
= date('YmdHis');