3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 // if we dont have a valid bounce type, we should set it
65 // to bounce_type_id 11 which is Syntax error. this allows such email
66 // addresses to be bounce a few more time before being put on hold
68 // we changed this behavior since this bounce type might be due to some issue
69 // with the connection or smtp server etc
70 if (empty($params['bounce_type_id'])) {
71 $params['bounce_type_id'] = 11;
72 if (empty($params['bounce_reason'])) {
73 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
78 $params['bounce_reason'] = substr($params['bounce_reason'], 0, 254);
80 $bounce->copyValues($params);
84 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
85 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
86 $emailTable = CRM_Core_BAO_Email
::getTableName();
87 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
90 // might want to put distinct inside the count
91 $query = "SELECT count($bounceTable.id) as bounces,
92 $bounceType.hold_threshold as threshold
94 INNER JOIN $bounceType
95 ON $bounceTable.bounce_type_id = $bounceType.id
96 INNER JOIN $queueTable
97 ON $bounceTable.event_queue_id = $queueTable.id
98 INNER JOIN $emailTable
99 ON $queueTable.email_id = $emailTable.id
100 WHERE $emailTable.id = {$q->email_id}
101 AND ($emailTable.reset_date IS NULL
102 OR $bounceTable.time_stamp >= $emailTable.reset_date)
103 GROUP BY $bounceTable.bounce_type_id
104 ORDER BY threshold, bounces desc";
106 $bounce->query($query);
108 while ($bounce->fetch()) {
109 if ($bounce->bounces
>= $bounce->threshold
) {
110 $email = new CRM_Core_BAO_Email();
111 $email->id
= $q->email_id
;
112 $email->on_hold
= TRUE;
113 $email->hold_date
= date('YmdHis');
118 $transaction->commit();
124 * Get row count for the event selector.
126 * @param int $mailing_id
129 * Optional ID of a job to filter on.
130 * @param bool $is_distinct
131 * Group by queue ID?.
133 * @param string|null $toDate
136 * Number of rows in result set
138 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
139 $dao = new CRM_Core_DAO();
141 $bounce = self
::getTableName();
142 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
143 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
144 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
147 SELECT COUNT($bounce.id) as bounce
150 ON $bounce.event_queue_id = $queue.id
152 ON $queue.job_id = $job.id
154 ON $job.mailing_id = $mailing.id
155 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
157 if (!empty($toDate)) {
158 $query .= " AND $bounce.time_stamp <= $toDate";
161 if (!empty($job_id)) {
162 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
166 $query .= " GROUP BY $queue.id ";
180 * Get rows for the event browser.
182 * @param int $mailing_id
185 * Optional ID of the job.
186 * @param bool $is_distinct
187 * Group by queue id?.
190 * @param int $rowCount
198 public static function &getRows(
199 $mailing_id, $job_id = NULL,
200 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
203 $dao = new CRM_Core_Dao();
205 $bounce = self
::getTableName();
206 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
207 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
208 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
209 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
210 $contact = CRM_Contact_BAO_Contact
::getTableName();
211 $email = CRM_Core_BAO_Email
::getTableName();
214 SELECT $contact.display_name as display_name,
215 $contact.id as contact_id,
216 $email.email as email,
217 $bounce.time_stamp as date,
218 $bounce.bounce_reason as reason,
219 $bounceType.name as bounce_type
222 ON $queue.contact_id = $contact.id
224 ON $queue.email_id = $email.id
226 ON $bounce.event_queue_id = $queue.id
227 LEFT JOIN $bounceType
228 ON $bounce.bounce_type_id = $bounceType.id
230 ON $queue.job_id = $job.id
233 ON $job.mailing_id = $mailing.id
234 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
236 if (!empty($job_id)) {
237 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
241 $query .= " GROUP BY $queue.id ";
244 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
246 if (is_string($sort)) {
247 $sort = CRM_Utils_Type
::escape($sort, 'String');
251 $orderBy = trim($sort->orderBy());
254 $query .= " ORDER BY {$orderBy} ";
256 if ($offset ||
$rowCount) {
257 //Added "||$rowCount" to avoid displaying all records on first page
258 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
265 while ($dao->fetch()) {
266 $url = CRM_Utils_System
::url('civicrm/contact/view',
267 "reset=1&cid={$dao->contact_id}"
270 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
271 'email' => $dao->email
,
272 // FIXME: translate this
273 'type' => (empty($dao->bounce_type
) ?
ts('Unknown') : $dao->bounce_type
275 'reason' => $dao->reason
,
276 'date' => CRM_Utils_Date
::customFormat($dao->date
),