3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Mailing_Event_BAO_Bounce
extends CRM_Mailing_Event_DAO_Bounce
{
40 function __construct() {
41 parent
::__construct();
45 * Create a new bounce event, update the email address if necessary
47 static function &create(&$params) {
48 $q = &CRM_Mailing_Event_BAO_Queue
::verify($params['job_id'],
49 $params['event_queue_id'],
58 $transaction = new CRM_Core_Transaction();
59 $bounce = new CRM_Mailing_Event_BAO_Bounce();
60 $bounce->time_stamp
= date('YmdHis');
62 // if we dont have a valid bounce type, we should set it
63 // to bounce_type_id 11 which is Syntax error. this allows such email
64 // addresses to be bounce a few more time before being put on hold
66 // we changed this behavior since this bounce type might be due to some issue
67 // with the connection or smtp server etc
68 if (empty($params['bounce_type_id'])) {
69 $params['bounce_type_id'] = 11;
70 if (empty($params['bounce_reason'])) {
71 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
76 $params['bounce_reason'] = substr($params['bounce_reason'], 0, 254);
78 $bounce->copyValues($params);
82 $bounceTable = CRM_Mailing_Event_BAO_Bounce
::getTableName();
83 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
84 $emailTable = CRM_Core_BAO_Email
::getTableName();
85 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
88 // might want to put distinct inside the count
89 $query = "SELECT count($bounceTable.id) as bounces,
90 $bounceType.hold_threshold as threshold
92 INNER JOIN $bounceType
93 ON $bounceTable.bounce_type_id = $bounceType.id
94 INNER JOIN $queueTable
95 ON $bounceTable.event_queue_id = $queueTable.id
96 INNER JOIN $emailTable
97 ON $queueTable.email_id = $emailTable.id
98 WHERE $emailTable.id = {$q->email_id}
99 AND ($emailTable.reset_date IS NULL
100 OR $bounceTable.time_stamp >= $emailTable.reset_date)
101 GROUP BY $bounceTable.bounce_type_id
102 ORDER BY threshold, bounces desc";
104 $bounce->query($query);
106 while ($bounce->fetch()) {
107 if ($bounce->bounces
>= $bounce->threshold
) {
108 $email = new CRM_Core_BAO_Email();
109 $email->id
= $q->email_id
;
110 $email->on_hold
= TRUE;
111 $email->hold_date
= date('YmdHis');
116 $transaction->commit();
122 * Get row count for the event selector
124 * @param int $mailing_id ID of the mailing
125 * @param int $job_id Optional ID of a job to filter on
126 * @param boolean $is_distinct Group by queue ID?
128 * @return int Number of rows in result set
132 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
133 $dao = new CRM_Core_DAO();
135 $bounce = self
::getTableName();
136 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
137 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
138 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
141 SELECT COUNT($bounce.id) as bounce
144 ON $bounce.event_queue_id = $queue.id
146 ON $queue.job_id = $job.id
148 ON $job.mailing_id = $mailing.id
149 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
151 if (!empty($toDate)) {
152 $query .= " AND $bounce.time_stamp <= $toDate";
155 if (!empty($job_id)) {
156 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
160 $query .= " GROUP BY $queue.id ";
174 * Get rows for the event browser
176 * @param int $mailing_id ID of the mailing
177 * @param int $job_id optional ID of the job
178 * @param boolean $is_distinct Group by queue id?
179 * @param int $offset Offset
180 * @param int $rowCount Number of rows
181 * @param array $sort sort array
183 * @return array Result set
187 public static function &getRows($mailing_id, $job_id = NULL,
188 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
191 $dao = new CRM_Core_Dao();
193 $bounce = self
::getTableName();
194 $bounceType = CRM_Mailing_DAO_BounceType
::getTableName();
195 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
196 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
197 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
198 $contact = CRM_Contact_BAO_Contact
::getTableName();
199 $email = CRM_Core_BAO_Email
::getTableName();
202 SELECT $contact.display_name as display_name,
203 $contact.id as contact_id,
204 $email.email as email,
205 $bounce.time_stamp as date,
206 $bounce.bounce_reason as reason,
207 $bounceType.name as bounce_type
210 ON $queue.contact_id = $contact.id
212 ON $queue.email_id = $email.id
214 ON $bounce.event_queue_id = $queue.id
215 LEFT JOIN $bounceType
216 ON $bounce.bounce_type_id = $bounceType.id
218 ON $queue.job_id = $job.id
221 ON $job.mailing_id = $mailing.id
222 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
224 if (!empty($job_id)) {
225 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
229 $query .= " GROUP BY $queue.id ";
232 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
234 if (is_string($sort)) {
235 $sort = CRM_Utils_Type
::escape($sort, 'String');
239 $orderBy = trim($sort->orderBy());
242 $query .= " ORDER BY {$orderBy} ";
244 if ($offset ||
$rowCount) {
245 //Added "||$rowCount" to avoid displaying all records on first page
246 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
253 while ($dao->fetch()) {
254 $url = CRM_Utils_System
::url('civicrm/contact/view',
255 "reset=1&cid={$dao->contact_id}"
258 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
259 'email' => $dao->email
,
260 // FIXME: translate this
261 'type' => (empty($dao->bounce_type
)
262 ?
ts('Unknown') : $dao->bounce_type
264 'reason' => $dao->reason
,
265 'date' => CRM_Utils_Date
::customFormat($dao->date
),