INFRA-132 - Civi - PHPStorm cleanup
[civicrm-core.git] / CRM / Mailing / Event / BAO / Bounce.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce {
36
37 /**
100fef9d 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
46 */
00be9182 47 public static function &create(&$params) {
6a488035
TO
48 $q = &CRM_Mailing_Event_BAO_Queue::verify($params['job_id'],
49 $params['event_queue_id'],
50 $params['hash']
51 );
52 $success = NULL;
53
54 if (!$q) {
55 return $success;
56 }
57
58 $transaction = new CRM_Core_Transaction();
59 $bounce = new CRM_Mailing_Event_BAO_Bounce();
60 $bounce->time_stamp = date('YmdHis');
61
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
65 // CRM-4814
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');
72 }
73 }
74
75 // CRM-11989
76 $params['bounce_reason'] = substr($params['bounce_reason'], 0, 254);
77
78 $bounce->copyValues($params);
79 $bounce->save();
80 $success = TRUE;
81
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();
86
87 $bounce->reset();
88 // might want to put distinct inside the count
89 $query = "SELECT count($bounceTable.id) as bounces,
90 $bounceType.hold_threshold as threshold
91 FROM $bounceTable
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";
103
104 $bounce->query($query);
105
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');
112 $email->save();
113 break;
114 }
115 }
116 $transaction->commit();
117
118 return $success;
119 }
120
121 /**
122 * Get row count for the event selector
123 *
90c8230e
TO
124 * @param int $mailing_id
125 * ID of the mailing.
126 * @param int $job_id
127 * Optional ID of a job to filter on.
128 * @param bool $is_distinct
129 * Group by queue ID?.
6a488035 130 *
a6c01b45
CW
131 * @return int
132 * Number of rows in result set
6a488035
TO
133 * @static
134 */
7811a84b 135 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
6a488035
TO
136 $dao = new CRM_Core_DAO();
137
138 $bounce = self::getTableName();
139 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
140 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 141 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
142
143 $query = "
144 SELECT COUNT($bounce.id) as bounce
145 FROM $bounce
146 INNER JOIN $queue
147 ON $bounce.event_queue_id = $queue.id
148 INNER JOIN $job
149 ON $queue.job_id = $job.id
150 INNER JOIN $mailing
151 ON $job.mailing_id = $mailing.id
152 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
153
7811a84b 154 if (!empty($toDate)) {
155 $query .= " AND $bounce.time_stamp <= $toDate";
156 }
157
6a488035
TO
158 if (!empty($job_id)) {
159 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
160 }
161
162 if ($is_distinct) {
163 $query .= " GROUP BY $queue.id ";
164 }
165
166 // query was missing
167 $dao->query($query);
168
169 if ($dao->fetch()) {
170 return $dao->bounce;
171 }
172
173 return NULL;
174 }
175
176 /**
177 * Get rows for the event browser
178 *
90c8230e
TO
179 * @param int $mailing_id
180 * ID of the mailing.
181 * @param int $job_id
182 * Optional ID of the job.
183 * @param bool $is_distinct
184 * Group by queue id?.
185 * @param int $offset
186 * Offset.
187 * @param int $rowCount
188 * Number of rows.
189 * @param array $sort
190 * Sort array.
6a488035 191 *
a6c01b45
CW
192 * @return array
193 * Result set
6a488035
TO
194 * @static
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
203 $bounce = self::getTableName();
204 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
205 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
206 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 207 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
208 $contact = CRM_Contact_BAO_Contact::getTableName();
209 $email = CRM_Core_BAO_Email::getTableName();
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
271 'type' => (empty($dao->bounce_type)
35f7561f 272 ? ts('Unknown') : $dao->bounce_type
6a488035
TO
273 ),
274 'reason' => $dao->reason,
275 'date' => CRM_Utils_Date::customFormat($dao->date),
276 );
277 }
278 return $results;
279 }
280}