Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Mailing / Event / BAO / Bounce.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce {
36
37 /**
fe482240 38 * Class constructor.
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
45 * Create a new bounce event, update the email address if necessary
ab432335
EM
46 *
47 * @param $params
48 *
49 * @return bool|null
6a488035 50 */
00be9182 51 public static function &create(&$params) {
6a488035
TO
52 $q = &CRM_Mailing_Event_BAO_Queue::verify($params['job_id'],
53 $params['event_queue_id'],
54 $params['hash']
55 );
56 $success = NULL;
57
58 if (!$q) {
59 return $success;
60 }
61
353ffa53
TO
62 $transaction = new CRM_Core_Transaction();
63 $bounce = new CRM_Mailing_Event_BAO_Bounce();
6a488035
TO
64 $bounce->time_stamp = date('YmdHis');
65
66 // if we dont have a valid bounce type, we should set it
67 // to bounce_type_id 11 which is Syntax error. this allows such email
68 // addresses to be bounce a few more time before being put on hold
69 // CRM-4814
70 // we changed this behavior since this bounce type might be due to some issue
71 // with the connection or smtp server etc
72 if (empty($params['bounce_type_id'])) {
73 $params['bounce_type_id'] = 11;
74 if (empty($params['bounce_reason'])) {
75 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
76 }
77 }
78
79 // CRM-11989
80 $params['bounce_reason'] = substr($params['bounce_reason'], 0, 254);
81
82 $bounce->copyValues($params);
83 $bounce->save();
84 $success = TRUE;
85
86 $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
353ffa53
TO
87 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
88 $emailTable = CRM_Core_BAO_Email::getTableName();
89 $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035
TO
90
91 $bounce->reset();
92 // might want to put distinct inside the count
93 $query = "SELECT count($bounceTable.id) as bounces,
94 $bounceType.hold_threshold as threshold
95 FROM $bounceTable
96 INNER JOIN $bounceType
97 ON $bounceTable.bounce_type_id = $bounceType.id
98 INNER JOIN $queueTable
99 ON $bounceTable.event_queue_id = $queueTable.id
100 INNER JOIN $emailTable
101 ON $queueTable.email_id = $emailTable.id
102 WHERE $emailTable.id = {$q->email_id}
103 AND ($emailTable.reset_date IS NULL
104 OR $bounceTable.time_stamp >= $emailTable.reset_date)
105 GROUP BY $bounceTable.bounce_type_id
106 ORDER BY threshold, bounces desc";
107
108 $bounce->query($query);
109
110 while ($bounce->fetch()) {
111 if ($bounce->bounces >= $bounce->threshold) {
353ffa53
TO
112 $email = new CRM_Core_BAO_Email();
113 $email->id = $q->email_id;
114 $email->on_hold = TRUE;
6a488035
TO
115 $email->hold_date = date('YmdHis');
116 $email->save();
117 break;
118 }
119 }
120 $transaction->commit();
121
122 return $success;
123 }
124
125 /**
fe482240 126 * Get row count for the event selector.
6a488035 127 *
90c8230e
TO
128 * @param int $mailing_id
129 * ID of the mailing.
130 * @param int $job_id
131 * Optional ID of a job to filter on.
132 * @param bool $is_distinct
133 * Group by queue ID?.
6a488035 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}