Merge pull request #4726 from atif-shaikh/CRM-5039
[civicrm-core.git] / CRM / Mailing / Event / BAO / Opened.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_Opened extends CRM_Mailing_Event_DAO_Opened {
36
37 /**
38 * Class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Register an open event
46 *
47 * @param int $queue_id The Queue Event ID of the recipient
48 *
49 * @return void
50 * @access public
51 * @static
52 */
53 public static function open($queue_id) {
54 /* First make sure there's a matching queue event */
55
56 $success = FALSE;
57
58 $q = new CRM_Mailing_Event_BAO_Queue();
59 $q->id = $queue_id;
60 if ($q->find(TRUE)) {
61 $oe = new CRM_Mailing_Event_BAO_Opened();
62 $oe->event_queue_id = $queue_id;
63 $oe->time_stamp = date('YmdHis');
64 $oe->save();
65 $success = TRUE;
66 }
67
68 return $success;
69 }
70
71 /**
72 * Get row count for the event selector
73 *
74 * @param int $mailing_id ID of the mailing
75 * @param int $job_id Optional ID of a job to filter on
76 * @param boolean $is_distinct Group by queue ID?
77 *
78 * @return int Number of rows in result set
79 * @access public
80 * @static
81 */
82 public static function getTotalCount($mailing_id,
83 $job_id = NULL,
84 $is_distinct = FALSE,
85 $toDate = NULL
86 ) {
87 $dao = new CRM_Core_DAO();
88
89 $open = self::getTableName();
90 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
91 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
92 $job = CRM_Mailing_BAO_MailingJob::getTableName();
93
94 $query = "
95 SELECT COUNT($open.id) as opened
96 FROM $open
97 INNER JOIN $queue
98 ON $open.event_queue_id = $queue.id
99 INNER JOIN $job
100 ON $queue.job_id = $job.id
101 INNER JOIN $mailing
102 ON $job.mailing_id = $mailing.id
103 AND $job.is_test = 0
104 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
105
106 if (!empty($toDate)) {
107 $query .= " AND $open.time_stamp <= $toDate";
108 }
109
110 if (!empty($job_id)) {
111 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
112 }
113
114 if ($is_distinct) {
115 $query .= " GROUP BY $queue.id ";
116 }
117
118 $dao->query($query);
119 $dao->fetch();
120 if ($is_distinct) {
121 return $dao->N;
122 }
123 else {
124 return $dao->opened ? $dao->opened : 0;
125 }
126 }
127
128 /**
129 * CRM-12814
130 * Get opened count for each mailing for a given set of mailing IDs
131 *
132 * @param $mailingIDs
133 *
134 * @return array Opened count per mailing ID
135 * @access public
136 * @static
137 */
138 public static function getMailingTotalCount($mailingIDs) {
139 $dao = new CRM_Core_DAO();
140 $openedCount = array();
141
142 $open = self::getTableName();
143 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
144 $job = CRM_Mailing_BAO_MailingJob::getTableName();
145 $mailingIDs = implode(',', $mailingIDs);
146
147 $query = "
148 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
149 FROM $open
150 INNER JOIN $queue
151 ON $open.event_queue_id = $queue.id
152 INNER JOIN $job
153 ON $queue.job_id = $job.id
154 AND $job.is_test = 0
155 WHERE $job.mailing_id IN ({$mailingIDs})
156 GROUP BY civicrm_mailing_job.mailing_id
157 ";
158
159 $dao->query($query);
160
161 while ( $dao->fetch() ) {
162 $openedCount[$dao->mailingID] = $dao->opened;
163 }
164 return $openedCount;
165 }
166
167 /**
168 * Get opened count for each mailing for a given set of mailing IDs and a specific contact
169 *
170 * @param int $mailingIDs IDs of the mailing (comma separated)
171 * @param int $contactID ID of the contact
172 *
173 * @return array Count per mailing ID
174 * @access public
175 * @static
176 */
177 public static function getMailingContactCount($mailingIDs, $contactID) {
178 $dao = new CRM_Core_DAO();
179 $openedCount = array();
180
181 $open = self::getTableName();
182 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
183 $job = CRM_Mailing_BAO_MailingJob::getTableName();
184 $mailingIDs = implode(',', $mailingIDs);
185
186 $query = "
187 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
188 FROM $open
189 INNER JOIN $queue
190 ON $open.event_queue_id = $queue.id
191 AND $queue.contact_id = $contactID
192 INNER JOIN $job
193 ON $queue.job_id = $job.id
194 AND $job.is_test = 0
195 WHERE $job.mailing_id IN ({$mailingIDs})
196 GROUP BY civicrm_mailing_job.mailing_id
197 ";
198
199 $dao->query($query);
200
201 while ( $dao->fetch() ) {
202 $openedCount[$dao->mailingID] = $dao->opened;
203 }
204
205 return $openedCount;
206 }
207
208 /**
209 * Get rows for the event browser
210 *
211 * @param int $mailing_id ID of the mailing
212 * @param int $job_id optional ID of the job
213 * @param boolean $is_distinct Group by queue id?
214 * @param int $offset Offset
215 * @param int $rowCount Number of rows
216 * @param array $sort sort array
217 *
218 * @param int $contact_id
219 *
220 * @return array Result set
221 * @access public
222 * @static
223 */
224 public static function &getRows($mailing_id, $job_id = NULL,
225 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id= NULL
226 ) {
227 $dao = new CRM_Core_Dao();
228
229 $open = self::getTableName();
230 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
231 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
232 $job = CRM_Mailing_BAO_MailingJob::getTableName();
233 $contact = CRM_Contact_BAO_Contact::getTableName();
234 $email = CRM_Core_BAO_Email::getTableName();
235
236 $query = "
237 SELECT $contact.display_name as display_name,
238 $contact.id as contact_id,
239 $email.email as email,
240 $open.time_stamp as date
241 FROM $contact
242 INNER JOIN $queue
243 ON $queue.contact_id = $contact.id
244 INNER JOIN $email
245 ON $queue.email_id = $email.id
246 INNER JOIN $open
247 ON $open.event_queue_id = $queue.id
248 INNER JOIN $job
249 ON $queue.job_id = $job.id
250 INNER JOIN $mailing
251 ON $job.mailing_id = $mailing.id
252 AND $job.is_test = 0
253 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
254
255 if (!empty($job_id)) {
256 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
257 }
258
259 if (!empty($contact_id)) {
260 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
261 }
262
263 if ($is_distinct) {
264 $query .= " GROUP BY $queue.id ";
265 }
266
267 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
268 if ($sort) {
269 if (is_string($sort)) {
270 $sort = CRM_Utils_Type::escape($sort, 'String');
271 $orderBy = $sort;
272 }
273 else {
274 $orderBy = trim($sort->orderBy());
275 }
276 }
277
278 $query .= " ORDER BY {$orderBy} ";
279
280 if ($offset || $rowCount) {
281 //Added "||$rowCount" to avoid displaying all records on first page
282 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
283 }
284 $dao->query($query);
285
286 $results = array();
287
288 while ($dao->fetch()) {
289 $url = CRM_Utils_System::url('civicrm/contact/view',
290 "reset=1&cid={$dao->contact_id}"
291 );
292 $results[] = array(
293 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
294 'email' => $dao->email,
295 'date' => CRM_Utils_Date::customFormat($dao->date),
296 );
297 }
298 return $results;
299 }
300 }