comment fixes
[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 */
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 *
a6c01b45
CW
133 * @return int
134 * Number of rows in result set
6a488035 135 */
7811a84b 136 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
6a488035
TO
137 $dao = new CRM_Core_DAO();
138
353ffa53
TO
139 $bounce = self::getTableName();
140 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035 141 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
353ffa53 142 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
143
144 $query = "
145 SELECT COUNT($bounce.id) as bounce
146 FROM $bounce
147 INNER JOIN $queue
148 ON $bounce.event_queue_id = $queue.id
149 INNER JOIN $job
150 ON $queue.job_id = $job.id
151 INNER JOIN $mailing
152 ON $job.mailing_id = $mailing.id
153 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
154
7811a84b 155 if (!empty($toDate)) {
156 $query .= " AND $bounce.time_stamp <= $toDate";
157 }
158
6a488035
TO
159 if (!empty($job_id)) {
160 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
161 }
162
163 if ($is_distinct) {
164 $query .= " GROUP BY $queue.id ";
165 }
166
167 // query was missing
168 $dao->query($query);
169
170 if ($dao->fetch()) {
171 return $dao->bounce;
172 }
173
174 return NULL;
175 }
176
177 /**
fe482240 178 * Get rows for the event browser.
6a488035 179 *
90c8230e
TO
180 * @param int $mailing_id
181 * ID of the mailing.
182 * @param int $job_id
183 * Optional ID of the job.
184 * @param bool $is_distinct
185 * Group by queue id?.
186 * @param int $offset
187 * Offset.
188 * @param int $rowCount
189 * Number of rows.
190 * @param array $sort
191 * Sort array.
6a488035 192 *
a6c01b45
CW
193 * @return array
194 * Result set
6a488035 195 */
a3d7e8ee
TO
196 public static function &getRows(
197 $mailing_id, $job_id = NULL,
6a488035
TO
198 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
199 ) {
200
201 $dao = new CRM_Core_Dao();
202
353ffa53 203 $bounce = self::getTableName();
6a488035 204 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
353ffa53
TO
205 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
206 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
207 $job = CRM_Mailing_BAO_MailingJob::getTableName();
208 $contact = CRM_Contact_BAO_Contact::getTableName();
209 $email = CRM_Core_BAO_Email::getTableName();
6a488035
TO
210
211 $query = "
212 SELECT $contact.display_name as display_name,
213 $contact.id as contact_id,
214 $email.email as email,
215 $bounce.time_stamp as date,
216 $bounce.bounce_reason as reason,
217 $bounceType.name as bounce_type
218 FROM $contact
219 INNER JOIN $queue
220 ON $queue.contact_id = $contact.id
221 INNER JOIN $email
222 ON $queue.email_id = $email.id
223 INNER JOIN $bounce
224 ON $bounce.event_queue_id = $queue.id
225 LEFT JOIN $bounceType
226 ON $bounce.bounce_type_id = $bounceType.id
227 INNER JOIN $job
228 ON $queue.job_id = $job.id
229 AND $job.is_test = 0
230 INNER JOIN $mailing
231 ON $job.mailing_id = $mailing.id
232 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
233
234 if (!empty($job_id)) {
235 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
236 }
237
238 if ($is_distinct) {
239 $query .= " GROUP BY $queue.id ";
240 }
241
242 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
243 if ($sort) {
244 if (is_string($sort)) {
21d32567 245 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
246 $orderBy = $sort;
247 }
248 else {
249 $orderBy = trim($sort->orderBy());
250 }
251 }
252 $query .= " ORDER BY {$orderBy} ";
253
254 if ($offset || $rowCount) {
255 //Added "||$rowCount" to avoid displaying all records on first page
256 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
257 }
258
259 $dao->query($query);
260
261 $results = array();
262
263 while ($dao->fetch()) {
264 $url = CRM_Utils_System::url('civicrm/contact/view',
265 "reset=1&cid={$dao->contact_id}"
266 );
267 $results[] = array(
268 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
269 'email' => $dao->email,
270 // FIXME: translate this
389bcebf 271 'type' => (empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type
6a488035
TO
272 ),
273 'reason' => $dao->reason,
274 'date' => CRM_Utils_Date::customFormat($dao->date),
275 );
276 }
277 return $results;
278 }
96025800 279
6a488035 280}