Merge remote-tracking branch 'rajgo94/mailingui-new' into master-abtest
[civicrm-core.git] / CRM / Mailing / Event / BAO / Opened.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_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 * @internal param int $contactID ID of the mailing
135 *
136 * @return array Opened count per mailing ID
137 * @access public
138 * @static
139 */
140 public static function getMailingTotalCount($mailingIDs) {
141 $dao = new CRM_Core_DAO();
142 $openedCount = array();
143
144 $open = self::getTableName();
145 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
146 $job = CRM_Mailing_BAO_MailingJob::getTableName();
147 $mailingIDs = implode(',', $mailingIDs);
148
149 $query = "
150 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
151 FROM $open
152 INNER JOIN $queue
153 ON $open.event_queue_id = $queue.id
154 INNER JOIN $job
155 ON $queue.job_id = $job.id
156 AND $job.is_test = 0
157 WHERE $job.mailing_id IN ({$mailingIDs})
158 GROUP BY civicrm_mailing_job.mailing_id
159 ";
160
161 $dao->query($query);
162
163 while ( $dao->fetch() ) {
164 $openedCount[$dao->mailingID] = $dao->opened;
165 }
166 return $openedCount;
167 }
168
169 /**
170 * Get opened count for each mailing for a given set of mailing IDs and a specific contact
171 *
172 * @param int $mailingIDs IDs of the mailing (comma separated)
173 * @param int $contactID ID of the contact
174 *
175 * @return array Count per mailing ID
176 * @access public
177 * @static
178 */
179 public static function getMailingContactCount($mailingIDs, $contactID) {
180 $dao = new CRM_Core_DAO();
181 $openedCount = array();
182
183 $open = self::getTableName();
184 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
185 $job = CRM_Mailing_BAO_MailingJob::getTableName();
186 $mailingIDs = implode(',', $mailingIDs);
187
188 $query = "
189 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
190 FROM $open
191 INNER JOIN $queue
192 ON $open.event_queue_id = $queue.id
193 AND $queue.contact_id = $contactID
194 INNER JOIN $job
195 ON $queue.job_id = $job.id
196 AND $job.is_test = 0
197 WHERE $job.mailing_id IN ({$mailingIDs})
198 GROUP BY civicrm_mailing_job.mailing_id
199 ";
200
201 $dao->query($query);
202
203 while ( $dao->fetch() ) {
204 $openedCount[$dao->mailingID] = $dao->opened;
205 }
206
207 return $openedCount;
208 }
209
210 /**
211 * Get rows for the event browser
212 *
213 * @param int $mailing_id ID of the mailing
214 * @param int $job_id optional ID of the job
215 * @param boolean $is_distinct Group by queue id?
216 * @param int $offset Offset
217 * @param int $rowCount Number of rows
218 * @param array $sort sort array
219 *
220 * @param null $contact_id
221 *
222 * @return array Result set
223 * @access public
224 * @static
225 */
226 public static function &getRows($mailing_id, $job_id = NULL,
227 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id= NULL
228 ) {
229 CRM_Core_Error::debug_var('3',$offset);
230 CRM_Core_Error::debug_var('4',$rowCount);
231
232 $dao = new CRM_Core_Dao();
233
234 $open = self::getTableName();
235 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
236 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
237 $job = CRM_Mailing_BAO_MailingJob::getTableName();
238 $contact = CRM_Contact_BAO_Contact::getTableName();
239 $email = CRM_Core_BAO_Email::getTableName();
240
241 $query = "
242 SELECT $contact.display_name as display_name,
243 $contact.id as contact_id,
244 $email.email as email,
245 $open.time_stamp as date
246 FROM $contact
247 INNER JOIN $queue
248 ON $queue.contact_id = $contact.id
249 INNER JOIN $email
250 ON $queue.email_id = $email.id
251 INNER JOIN $open
252 ON $open.event_queue_id = $queue.id
253 INNER JOIN $job
254 ON $queue.job_id = $job.id
255 INNER JOIN $mailing
256 ON $job.mailing_id = $mailing.id
257 AND $job.is_test = 0
258 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
259
260 if (!empty($job_id)) {
261 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
262 }
263
264 if (!empty($contact_id)) {
265 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
266 }
267
268 if ($is_distinct) {
269 $query .= " GROUP BY $queue.id ";
270 }
271
272 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
273 if ($sort) {
274 if (is_string($sort)) {
275
276 $sort = CRM_Utils_Type::escape($sort, 'String');
277 CRM_Core_Error::debug_var('2',$sort);
278
279 $orderBy = $sort;
280 }
281 else {
282 $orderBy = trim($sort->orderBy());
283 }
284 }
285
286 $query .= " ORDER BY {$orderBy} ";
287
288 if ($offset || $rowCount) {
289 //Added "||$rowCount" to avoid displaying all records on first page
290 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
291 }
292 CRM_Core_Error::debug_var('1',$query);
293 $dao->query($query);
294
295 $results = array();
296
297 while ($dao->fetch()) {
298 $url = CRM_Utils_System::url('civicrm/contact/view',
299 "reset=1&cid={$dao->contact_id}"
300 );
301 $results[] = array(
302 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
303 'email' => $dao->email,
304 'date' => CRM_Utils_Date::customFormat($dao->date),
305 );
306 }
307 return $results;
308 }
309 }