Merge pull request #4686 from lcdservices/CRM-15698-b
[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 ) {
86 $dao = new CRM_Core_DAO();
87
88 $open = self::getTableName();
89 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
90 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
91 $job = CRM_Mailing_BAO_MailingJob::getTableName();
92
93 $query = "
94 SELECT COUNT($open.id) as opened
95 FROM $open
96 INNER JOIN $queue
97 ON $open.event_queue_id = $queue.id
98 INNER JOIN $job
99 ON $queue.job_id = $job.id
100 INNER JOIN $mailing
101 ON $job.mailing_id = $mailing.id
102 AND $job.is_test = 0
103 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
104
105 if (!empty($job_id)) {
106 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
107 }
108
109 if ($is_distinct) {
110 $query .= " GROUP BY $queue.id ";
111 }
112
113 $dao->query($query);
114 $dao->fetch();
115 if ($is_distinct) {
116 return $dao->N;
117 }
118 else {
119 return $dao->opened ? $dao->opened : 0;
120 }
121 }
122
123 /**
124 * CRM-12814
125 * Get opened count for each mailing for a given set of mailing IDs
126 *
127 * @param $mailingIDs
128 *
129 * @return array Opened count per mailing ID
130 * @access public
131 * @static
132 */
133 public static function getMailingTotalCount($mailingIDs) {
134 $dao = new CRM_Core_DAO();
135 $openedCount = array();
136
137 $open = self::getTableName();
138 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
139 $job = CRM_Mailing_BAO_MailingJob::getTableName();
140 $mailingIDs = implode(',', $mailingIDs);
141
142 $query = "
143 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
144 FROM $open
145 INNER JOIN $queue
146 ON $open.event_queue_id = $queue.id
147 INNER JOIN $job
148 ON $queue.job_id = $job.id
149 AND $job.is_test = 0
150 WHERE $job.mailing_id IN ({$mailingIDs})
151 GROUP BY civicrm_mailing_job.mailing_id
152 ";
153
154 $dao->query($query);
155
156 while ( $dao->fetch() ) {
157 $openedCount[$dao->mailingID] = $dao->opened;
158 }
159 return $openedCount;
160 }
161
162 /**
163 * Get opened count for each mailing for a given set of mailing IDs and a specific contact
164 *
165 * @param int $mailingIDs IDs of the mailing (comma separated)
166 * @param int $contactID ID of the contact
167 *
168 * @return array Count per mailing ID
169 * @access public
170 * @static
171 */
172 public static function getMailingContactCount($mailingIDs, $contactID) {
173 $dao = new CRM_Core_DAO();
174 $openedCount = array();
175
176 $open = self::getTableName();
177 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
178 $job = CRM_Mailing_BAO_MailingJob::getTableName();
179 $mailingIDs = implode(',', $mailingIDs);
180
181 $query = "
182 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
183 FROM $open
184 INNER JOIN $queue
185 ON $open.event_queue_id = $queue.id
186 AND $queue.contact_id = $contactID
187 INNER JOIN $job
188 ON $queue.job_id = $job.id
189 AND $job.is_test = 0
190 WHERE $job.mailing_id IN ({$mailingIDs})
191 GROUP BY civicrm_mailing_job.mailing_id
192 ";
193
194 $dao->query($query);
195
196 while ( $dao->fetch() ) {
197 $openedCount[$dao->mailingID] = $dao->opened;
198 }
199
200 return $openedCount;
201 }
202
203 /**
204 * Get rows for the event browser
205 *
206 * @param int $mailing_id ID of the mailing
207 * @param int $job_id optional ID of the job
208 * @param boolean $is_distinct Group by queue id?
209 * @param int $offset Offset
210 * @param int $rowCount Number of rows
211 * @param array $sort sort array
212 *
213 * @param int $contact_id
214 *
215 * @return array Result set
216 * @access public
217 * @static
218 */
219 public static function &getRows($mailing_id, $job_id = NULL,
220 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id= NULL
221 ) {
222
223 $dao = new CRM_Core_Dao();
224
225 $open = self::getTableName();
226 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
227 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
228 $job = CRM_Mailing_BAO_MailingJob::getTableName();
229 $contact = CRM_Contact_BAO_Contact::getTableName();
230 $email = CRM_Core_BAO_Email::getTableName();
231
232 $query = "
233 SELECT $contact.display_name as display_name,
234 $contact.id as contact_id,
235 $email.email as email,
236 $open.time_stamp as date
237 FROM $contact
238 INNER JOIN $queue
239 ON $queue.contact_id = $contact.id
240 INNER JOIN $email
241 ON $queue.email_id = $email.id
242 INNER JOIN $open
243 ON $open.event_queue_id = $queue.id
244 INNER JOIN $job
245 ON $queue.job_id = $job.id
246 INNER JOIN $mailing
247 ON $job.mailing_id = $mailing.id
248 AND $job.is_test = 0
249 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
250
251 if (!empty($job_id)) {
252 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
253 }
254
255 if (!empty($contact_id)) {
256 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
257 }
258
259 if ($is_distinct) {
260 $query .= " GROUP BY $queue.id ";
261 }
262
263 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
264 if ($sort) {
265 if (is_string($sort)) {
266 $sort = CRM_Utils_Type::escape($sort, 'String');
267 $orderBy = $sort;
268 }
269 else {
270 $orderBy = trim($sort->orderBy());
271 }
272 }
273
274 $query .= " ORDER BY {$orderBy} ";
275
276 if ($offset || $rowCount) {
277 //Added "||$rowCount" to avoid displaying all records on first page
278 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
279 }
280
281 $dao->query($query);
282
283 $results = array();
284
285 while ($dao->fetch()) {
286 $url = CRM_Utils_System::url('civicrm/contact/view',
287 "reset=1&cid={$dao->contact_id}"
288 );
289 $results[] = array(
290 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
291 'email' => $dao->email,
292 'date' => CRM_Utils_Date::customFormat($dao->date),
293 );
294 }
295 return $results;
296 }
297 }
298