Merge pull request #12003 from vinuvarshith/dev/core/69-fix-state-province-name-token...
[civicrm-core.git] / CRM / Mailing / Event / BAO / Bounce.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
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 */
cdc5c450 49 public static function create(&$params) {
50 $q = CRM_Mailing_Event_BAO_Queue::verify($params['job_id'],
6a488035
TO
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
532e83d3 64 $action = empty($params['id']) ? 'create' : 'edit';
65 CRM_Utils_Hook::pre($action, 'MailingEventBounce', CRM_Utils_Array::value('id', $params), $params);
66
6a488035
TO
67 // if we dont have a valid bounce type, we should set it
68 // to bounce_type_id 11 which is Syntax error. this allows such email
69 // addresses to be bounce a few more time before being put on hold
70 // CRM-4814
71 // we changed this behavior since this bounce type might be due to some issue
72 // with the connection or smtp server etc
73 if (empty($params['bounce_type_id'])) {
74 $params['bounce_type_id'] = 11;
75 if (empty($params['bounce_reason'])) {
76 $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
77 }
78 }
79
80 // CRM-11989
307aac0a 81 $params['bounce_reason'] = mb_strcut($params['bounce_reason'], 0, 254);
6a488035
TO
82
83 $bounce->copyValues($params);
84 $bounce->save();
6a488035 85
532e83d3 86 CRM_Utils_Hook::post($action, 'MailingEventBounce', $bounce->id, $bounce);
87
d15a97f4 88 if ($q->email_id) {
89 self::putEmailOnHold($q->email_id);
6a488035
TO
90 }
91 $transaction->commit();
92
d15a97f4 93 return TRUE;
6a488035
TO
94 }
95
96 /**
fe482240 97 * Get row count for the event selector.
6a488035 98 *
90c8230e
TO
99 * @param int $mailing_id
100 * ID of the mailing.
101 * @param int $job_id
102 * Optional ID of a job to filter on.
103 * @param bool $is_distinct
104 * Group by queue ID?.
6a488035 105 *
ad37ac8e 106 * @param string|null $toDate
107 *
a6c01b45
CW
108 * @return int
109 * Number of rows in result set
6a488035 110 */
7811a84b 111 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
6a488035
TO
112 $dao = new CRM_Core_DAO();
113
353ffa53
TO
114 $bounce = self::getTableName();
115 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035 116 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
353ffa53 117 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
118
119 $query = "
120 SELECT COUNT($bounce.id) as bounce
121 FROM $bounce
122 INNER JOIN $queue
123 ON $bounce.event_queue_id = $queue.id
124 INNER JOIN $job
125 ON $queue.job_id = $job.id
126 INNER JOIN $mailing
127 ON $job.mailing_id = $mailing.id
128 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
129
7811a84b 130 if (!empty($toDate)) {
131 $query .= " AND $bounce.time_stamp <= $toDate";
132 }
133
6a488035
TO
134 if (!empty($job_id)) {
135 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
136 }
137
138 if ($is_distinct) {
139 $query .= " GROUP BY $queue.id ";
140 }
141
142 // query was missing
143 $dao->query($query);
144
145 if ($dao->fetch()) {
146 return $dao->bounce;
147 }
148
149 return NULL;
150 }
151
152 /**
fe482240 153 * Get rows for the event browser.
6a488035 154 *
90c8230e
TO
155 * @param int $mailing_id
156 * ID of the mailing.
157 * @param int $job_id
158 * Optional ID of the job.
159 * @param bool $is_distinct
160 * Group by queue id?.
161 * @param int $offset
162 * Offset.
163 * @param int $rowCount
164 * Number of rows.
165 * @param array $sort
166 * Sort array.
6a488035 167 *
a6c01b45
CW
168 * @return array
169 * Result set
6a488035 170 */
a3d7e8ee
TO
171 public static function &getRows(
172 $mailing_id, $job_id = NULL,
6a488035
TO
173 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
174 ) {
175
176 $dao = new CRM_Core_Dao();
177
353ffa53 178 $bounce = self::getTableName();
6a488035 179 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
353ffa53
TO
180 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
181 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
182 $job = CRM_Mailing_BAO_MailingJob::getTableName();
183 $contact = CRM_Contact_BAO_Contact::getTableName();
184 $email = CRM_Core_BAO_Email::getTableName();
6a488035
TO
185
186 $query = "
187 SELECT $contact.display_name as display_name,
188 $contact.id as contact_id,
189 $email.email as email,
190 $bounce.time_stamp as date,
191 $bounce.bounce_reason as reason,
192 $bounceType.name as bounce_type
193 FROM $contact
194 INNER JOIN $queue
195 ON $queue.contact_id = $contact.id
196 INNER JOIN $email
197 ON $queue.email_id = $email.id
198 INNER JOIN $bounce
199 ON $bounce.event_queue_id = $queue.id
200 LEFT JOIN $bounceType
201 ON $bounce.bounce_type_id = $bounceType.id
202 INNER JOIN $job
203 ON $queue.job_id = $job.id
204 AND $job.is_test = 0
205 INNER JOIN $mailing
206 ON $job.mailing_id = $mailing.id
207 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
208
209 if (!empty($job_id)) {
210 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
211 }
212
213 if ($is_distinct) {
0ee5581e 214 $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name ";
6a488035
TO
215 }
216
217 $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
218 if ($sort) {
219 if (is_string($sort)) {
21d32567 220 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
221 $orderBy = $sort;
222 }
223 else {
224 $orderBy = trim($sort->orderBy());
225 }
226 }
227 $query .= " ORDER BY {$orderBy} ";
228
229 if ($offset || $rowCount) {
230 //Added "||$rowCount" to avoid displaying all records on first page
231 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
232 }
233
234 $dao->query($query);
235
236 $results = array();
237
238 while ($dao->fetch()) {
239 $url = CRM_Utils_System::url('civicrm/contact/view',
240 "reset=1&cid={$dao->contact_id}"
241 );
242 $results[] = array(
243 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
244 'email' => $dao->email,
245 // FIXME: translate this
389bcebf 246 'type' => (empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type
6a488035
TO
247 ),
248 'reason' => $dao->reason,
249 'date' => CRM_Utils_Date::customFormat($dao->date),
250 );
251 }
252 return $results;
253 }
96025800 254
d15a97f4 255 /**
256 * Put the email on hold if it has met the threshold.
257 *
258 * @param int $email_id
259 */
260 protected static function putEmailOnHold($email_id) {
261
262 $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
263 $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
264 $emailTable = CRM_Core_BAO_Email::getTableName();
265 $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
266
267 // might want to put distinct inside the count
268 $query = "SELECT count($bounceTable.id) as bounces,
269 $bounceType.hold_threshold as threshold
270 FROM $bounceTable
271 INNER JOIN $bounceType
272 ON $bounceTable.bounce_type_id = $bounceType.id
273 INNER JOIN $queueTable
274 ON $bounceTable.event_queue_id = $queueTable.id
275 INNER JOIN $emailTable
276 ON $queueTable.email_id = $emailTable.id
277 WHERE $emailTable.id = $email_id
278 AND ($emailTable.reset_date IS NULL
279 OR $bounceTable.time_stamp >= $emailTable.reset_date)
280 GROUP BY $bounceTable.bounce_type_id
281 ORDER BY threshold, bounces desc";
282
283 $dao = CRM_Core_DAO::executeQuery($query);
284
285 while ($dao->fetch()) {
286 if ($dao->bounces >= $dao->threshold) {
287 $email = new CRM_Core_BAO_Email();
288 $email->id = $email_id;
289 $email->on_hold = TRUE;
290 $email->hold_date = date('YmdHis');
291 $email->save();
292 break;
293 }
294 }
295 }
296
6a488035 297}