Merge pull request #4764 from rohankatkar/CRM-15615
[civicrm-core.git] / CRM / Mailing / Event / BAO / Delivered.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Mailing_Event_BAO_Delivered extends CRM_Mailing_Event_DAO_Delivered {
36
37 /**
38 * Class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Create a new delivery event
46 *
47 * @param array $params Associative array of delivery event values
48 *
49 * @return void
50 * @access public
51 * @static
52 */
53 public static function &create(&$params) {
54 $q = &CRM_Mailing_Event_BAO_Queue::verify($params['job_id'],
55 $params['event_queue_id'],
56 $params['hash']
57 );
58
59 if (!$q) {
60 return NULL;
61 }
62 $q->free();
63
64 $delivered = new CRM_Mailing_Event_BAO_Delivered();
65 $delivered->time_stamp = date('YmdHis');
66 $delivered->copyValues($params);
67 $delivered->save();
68
69 $queue = new CRM_Mailing_Event_BAO_Queue();
70 $queue->id = $params['event_queue_id'];
71 $queue->find(TRUE);
72
73 while ($queue->fetch()) {
74 $email = new CRM_Core_BAO_Email();
75 $email->id = $queue->email_id;
76 $email->hold_date = '';
77 $email->reset_date = date('YmdHis');
78 $email->save();
79 }
80
81 return $delivered;
82 }
83
84 /**
85 * Get row count for the event selector
86 *
87 * @param int $mailing_id ID of the mailing
88 * @param int $job_id Optional ID of a job to filter on
89 * @param boolean $is_distinct Group by queue ID?
90 *
91 * @return int Number of rows in result set
92 * @access public
93 * @static
94 */
95 public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
96 $dao = new CRM_Core_DAO();
97
98 $delivered = self::getTableName();
99 $bounce = CRM_Mailing_Event_BAO_Bounce::getTableName();
100 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
101 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
102 $job = CRM_Mailing_BAO_MailingJob::getTableName();
103
104 $query = "
105 SELECT COUNT($delivered.id) as delivered
106 FROM $delivered
107 INNER JOIN $queue
108 ON $delivered.event_queue_id = $queue.id
109 LEFT JOIN $bounce
110 ON $delivered.event_queue_id = $bounce.event_queue_id
111 INNER JOIN $job
112 ON $queue.job_id = $job.id
113 AND $job.is_test = 0
114 INNER JOIN $mailing
115 ON $job.mailing_id = $mailing.id
116 WHERE $bounce.id IS null
117 AND $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
118
119 if (!empty($toDate)) {
120 $query .= " AND $delivered.time_stamp <= $toDate";
121 }
122
123 if (!empty($job_id)) {
124 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
125 }
126
127 if ($is_distinct) {
128 $query .= " GROUP BY $queue.id ";
129 }
130
131 // query was missing
132 $dao->query($query);
133
134 if ($dao->fetch()) {
135 return $dao->delivered;
136 }
137
138 return NULL;
139 }
140
141 /**
142 * Get rows for the event browser
143 *
144 * @param int $mailing_id ID of the mailing
145 * @param int $job_id optional ID of the job
146 * @param boolean $is_distinct Group by queue id?
147 * @param int $offset Offset
148 * @param int $rowCount Number of rows
149 * @param array $sort sort array
150 *
151 * @return array Result set
152 * @access public
153 * @static
154 */
155 public static function &getRows($mailing_id, $job_id = NULL,
156 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $is_test = 0
157 ) {
158
159 $dao = new CRM_Core_Dao();
160
161 $delivered = self::getTableName();
162 $bounce = CRM_Mailing_Event_BAO_Bounce::getTableName();
163 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
164 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
165 $job = CRM_Mailing_BAO_MailingJob::getTableName();
166 $contact = CRM_Contact_BAO_Contact::getTableName();
167 $email = CRM_Core_BAO_Email::getTableName();
168
169 $query = "
170 SELECT $delivered.id as id,
171 $contact.display_name as display_name,
172 $contact.id as contact_id,
173 $email.email as email,
174 $delivered.time_stamp as date
175 FROM $contact
176 INNER JOIN $queue
177 ON $queue.contact_id = $contact.id
178 INNER JOIN $email
179 ON $queue.email_id = $email.id
180 INNER JOIN $delivered
181 ON $delivered.event_queue_id = $queue.id
182 LEFT JOIN $bounce
183 ON $bounce.event_queue_id = $queue.id
184 INNER JOIN $job
185 ON $queue.job_id = $job.id
186 AND $job.is_test = $is_test
187 INNER JOIN $mailing
188 ON $job.mailing_id = $mailing.id
189 WHERE $bounce.id IS null
190 AND $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
191
192 if (!empty($job_id)) {
193 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
194 }
195
196 if ($is_distinct) {
197 $query .= " GROUP BY $queue.id ";
198 }
199
200 $orderBy = "sort_name ASC, {$delivered}.time_stamp DESC";
201 if ($sort) {
202 if (is_string($sort)) {
203 $sort = CRM_Utils_Type::escape($sort, 'String');
204 $orderBy = $sort;
205 }
206 else {
207 $orderBy = trim($sort->orderBy());
208 }
209 }
210
211 $query .= " ORDER BY {$orderBy} ";
212
213 if ($offset || $rowCount) {
214 //Added "||$rowCount" to avoid displaying all records on first page
215 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
216 }
217
218 $dao->query($query);
219
220 $results = array();
221
222 while ($dao->fetch()) {
223 $url = CRM_Utils_System::url('civicrm/contact/view',
224 "reset=1&cid={$dao->contact_id}"
225 );
226 $results[$dao->id] = array(
227 'contact_id' => $dao->contact_id,
228 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
229 'email' => $dao->email,
230 'date' => CRM_Utils_Date::customFormat($dao->date),
231 );
232 }
233 return $results;
234 }
235
236 /**
237 * @param $eventQueueIDs
238 * @param null $time
239 */
240 static function bulkCreate($eventQueueIDs, $time = NULL) {
241 if (!$time) {
242 $time = date('YmdHis');
243 }
244
245 // construct a bulk insert statement
246 $values = array();
247 foreach ($eventQueueIDs as $eqID) {
248 $values[] = "( $eqID, '{$time}' )";
249 }
250
251 while (!empty($values)) {
252 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
253 $str = implode(',', $input);
254 $sql = "INSERT INTO civicrm_mailing_event_delivered ( event_queue_id, time_stamp ) VALUES $str;";
255 CRM_Core_DAO::executeQuery($sql);
256 }
257 }
258
259 /**
260 * Since we never no when a mailing really bounces (hard bounce == NOW, soft bounce == NOW to NOW + 3 days?)
261 * we cannot decide when an email address last got an email.
262 *
263 * We want to avoid putting on hold an email address which had a few bounces (mbox full) and then got quite a few
264 * successfull deliveries before starting the bounce again. The current code does not set the resetDate and hence
265 * the above scenario results in the email being put on hold
266 *
267 * This function rectifies that by considering all non-test mailing jobs which have completed between $minDays and $maxDays
268 * and setting the resetDate to the date that an email was delivered
269 *
270 * @param integer $minDays consider mailings that were completed at least $minDays ago
271 * @param integer $maxDays consider mailings that were completed not more than $maxDays ago
272 *
273 * @return void
274 * @static
275 **/
276 public static function updateEmailResetDate($minDays = 3, $maxDays = 7) {
277 $dao = new CRM_Core_Dao();
278
279 $query = "
280 CREATE TEMPORARY TABLE civicrm_email_temp_values (
281 id int primary key,
282 reset_date datetime
283 ) ENGINE = HEAP;
284 ";
285 CRM_Core_DAO::executeQuery($query);
286
287 $query = "
288 INSERT INTO civicrm_email_temp_values (id, reset_date)
289 SELECT civicrm_email.id as email_id,
290 max(civicrm_mailing_event_delivered.time_stamp) as reset_date
291 FROM civicrm_mailing_event_queue
292 INNER JOIN civicrm_email ON civicrm_mailing_event_queue.email_id = civicrm_email.id
293 INNER JOIN civicrm_mailing_event_delivered ON civicrm_mailing_event_delivered.event_queue_id = civicrm_mailing_event_queue.id
294 LEFT JOIN civicrm_mailing_event_bounce ON civicrm_mailing_event_bounce.event_queue_id = civicrm_mailing_event_queue.id
295 INNER JOIN civicrm_mailing_job ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id AND civicrm_mailing_job.is_test = 0
296 WHERE civicrm_mailing_event_bounce.id IS NULL
297 AND civicrm_mailing_job.status = 'Complete'
298 AND civicrm_mailing_job.end_date BETWEEN DATE_SUB(NOW(), INTERVAL $maxDays day) AND DATE_SUB(NOW(), INTERVAL $minDays day)
299 AND (civicrm_email.reset_date IS NULL OR civicrm_email.reset_date < civicrm_mailing_job.start_date)
300 GROUP BY civicrm_email.id
301 ";
302
303 $query = "
304 UPDATE civicrm_email e
305 INNER JOIN civicrm_email_temp_values et ON e.id = et.id
306 SET e.on_hold = 0,
307 e.hold_date = NULL,
308 e.reset_date = et.reset_date
309 ";
310 CRM_Core_DAO::executeQuery($query);
311 }
312
313 }