Merge pull request #8998 from yashodha/CRM-19271
[civicrm-core.git] / CRM / Mailing / Event / BAO / Bounce.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce {
34
35 /**
fe482240 36 * Class constructor.
6a488035 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40 }
41
42 /**
43 * Create a new bounce event, update the email address if necessary
ab432335
EM
44 *
45 * @param $params
46 *
47 * @return bool|null
6a488035 48 */
00be9182 49 public static function &create(&$params) {
6a488035
TO
50 $q = &CRM_Mailing_Event_BAO_Queue::verify($params['job_id'],
51 $params['event_queue_id'],
52 $params['hash']
53 );
54 $success = NULL;
55
56 if (!$q) {
57 return $success;
58 }
59
353ffa53
TO
60 $transaction = new CRM_Core_Transaction();
61 $bounce = new CRM_Mailing_Event_BAO_Bounce();
6a488035
TO
62 $bounce->time_stamp = date('YmdHis');
63
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
67 // CRM-4814
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');
74 }
75 }
76
77 // CRM-11989
78 $params['bounce_reason'] = substr($params['bounce_reason'], 0, 254);
79
80 $bounce->copyValues($params);
81 $bounce->save();
82 $success = TRUE;
83
84 $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
353ffa53
TO
85 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
86 $emailTable = CRM_Core_BAO_Email::getTableName();
87 $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035
TO
88
89 $bounce->reset();
90 // might want to put distinct inside the count
91 $query = "SELECT count($bounceTable.id) as bounces,
92 $bounceType.hold_threshold as threshold
93 FROM $bounceTable
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";
105
106 $bounce->query($query);
107
108 while ($bounce->fetch()) {
109 if ($bounce->bounces >= $bounce->threshold) {
353ffa53
TO
110 $email = new CRM_Core_BAO_Email();
111 $email->id = $q->email_id;
112 $email->on_hold = TRUE;
6a488035
TO
113 $email->hold_date = date('YmdHis');
114 $email->save();
115 break;
116 }
117 }
118 $transaction->commit();
119
120 return $success;
121 }
122
123 /**
fe482240 124 * Get row count for the event selector.
6a488035 125 *
90c8230e
TO
126 * @param int $mailing_id
127 * ID of the mailing.
128 * @param int $job_id
129 * Optional ID of a job to filter on.
130 * @param bool $is_distinct
131 * Group by queue ID?.
6a488035 132 *
ad37ac8e 133 * @param string|null $toDate
134 *
a6c01b45
CW
135 * @return int
136 * Number of rows in result set
6a488035 137 */
7811a84b 138 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
6a488035
TO
139 $dao = new CRM_Core_DAO();
140
353ffa53
TO
141 $bounce = self::getTableName();
142 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035 143 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
353ffa53 144 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
145
146 $query = "
147 SELECT COUNT($bounce.id) as bounce
148 FROM $bounce
149 INNER JOIN $queue
150 ON $bounce.event_queue_id = $queue.id
151 INNER JOIN $job
152 ON $queue.job_id = $job.id
153 INNER JOIN $mailing
154 ON $job.mailing_id = $mailing.id
155 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
156
7811a84b 157 if (!empty($toDate)) {
158 $query .= " AND $bounce.time_stamp <= $toDate";
159 }
160
6a488035
TO
161 if (!empty($job_id)) {
162 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
163 }
164
165 if ($is_distinct) {
166 $query .= " GROUP BY $queue.id ";
167 }
168
169 // query was missing
170 $dao->query($query);
171
172 if ($dao->fetch()) {
173 return $dao->bounce;
174 }
175
176 return NULL;
177 }
178
179 /**
fe482240 180 * Get rows for the event browser.
6a488035 181 *
90c8230e
TO
182 * @param int $mailing_id
183 * ID of the mailing.
184 * @param int $job_id
185 * Optional ID of the job.
186 * @param bool $is_distinct
187 * Group by queue id?.
188 * @param int $offset
189 * Offset.
190 * @param int $rowCount
191 * Number of rows.
192 * @param array $sort
193 * Sort array.
6a488035 194 *
a6c01b45
CW
195 * @return array
196 * Result set
6a488035 197 */
a3d7e8ee
TO
198 public static function &getRows(
199 $mailing_id, $job_id = NULL,
6a488035
TO
200 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
201 ) {
202
203 $dao = new CRM_Core_Dao();
204
353ffa53 205 $bounce = self::getTableName();
6a488035 206 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
353ffa53
TO
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();
6a488035
TO
212
213 $query = "
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
220 FROM $contact
221 INNER JOIN $queue
222 ON $queue.contact_id = $contact.id
223 INNER JOIN $email
224 ON $queue.email_id = $email.id
225 INNER JOIN $bounce
226 ON $bounce.event_queue_id = $queue.id
227 LEFT JOIN $bounceType
228 ON $bounce.bounce_type_id = $bounceType.id
229 INNER JOIN $job
230 ON $queue.job_id = $job.id
231 AND $job.is_test = 0
232 INNER JOIN $mailing
233 ON $job.mailing_id = $mailing.id
234 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
235
236 if (!empty($job_id)) {
237 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
238 }
239
240 if ($is_distinct) {
241 $query .= " GROUP BY $queue.id ";
242 }
243
244 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
245 if ($sort) {
246 if (is_string($sort)) {
21d32567 247 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
248 $orderBy = $sort;
249 }
250 else {
251 $orderBy = trim($sort->orderBy());
252 }
253 }
254 $query .= " ORDER BY {$orderBy} ";
255
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');
259 }
260
261 $dao->query($query);
262
263 $results = array();
264
265 while ($dao->fetch()) {
266 $url = CRM_Utils_System::url('civicrm/contact/view',
267 "reset=1&cid={$dao->contact_id}"
268 );
269 $results[] = array(
270 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
271 'email' => $dao->email,
272 // FIXME: translate this
389bcebf 273 'type' => (empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type
6a488035
TO
274 ),
275 'reason' => $dao->reason,
276 'date' => CRM_Utils_Date::customFormat($dao->date),
277 );
278 }
279 return $results;
280 }
96025800 281
6a488035 282}