Merge pull request #4860 from totten/master-ext-cache
[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 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Register an open event
46 *
47 * @param int $queue_id
48 * The Queue Event ID of the recipient.
49 *
50 * @return void
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
75 * ID of the mailing.
76 * @param int $job_id
77 * Optional ID of a job to filter on.
78 * @param bool $is_distinct
79 * Group by queue ID?.
80 *
81 * @return int Number of rows in result set
82 * @static
83 */
84 public static function getTotalCount(
85 $mailing_id,
86 $job_id = NULL,
87 $is_distinct = FALSE,
88 $toDate = NULL
89 ) {
90 $dao = new CRM_Core_DAO();
91
92 $open = self::getTableName();
93 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
94 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
95 $job = CRM_Mailing_BAO_MailingJob::getTableName();
96
97 $query = "
98 SELECT COUNT($open.id) as opened
99 FROM $open
100 INNER JOIN $queue
101 ON $open.event_queue_id = $queue.id
102 INNER JOIN $job
103 ON $queue.job_id = $job.id
104 INNER JOIN $mailing
105 ON $job.mailing_id = $mailing.id
106 AND $job.is_test = 0
107 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
108
109 if (!empty($toDate)) {
110 $query .= " AND $open.time_stamp <= $toDate";
111 }
112
113 if (!empty($job_id)) {
114 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
115 }
116
117 if ($is_distinct) {
118 $query .= " GROUP BY $queue.id ";
119 }
120
121 $dao->query($query);
122 $dao->fetch();
123 if ($is_distinct) {
124 return $dao->N;
125 }
126 else {
127 return $dao->opened ? $dao->opened : 0;
128 }
129 }
130
131 /**
132 * CRM-12814
133 * Get opened count for each mailing for a given set of mailing IDs
134 *
135 * @param $mailingIDs
136 *
137 * @return array Opened count per mailing ID
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
173 * IDs of the mailing (comma separated).
174 * @param int $contactID
175 * ID of the contact.
176 *
177 * @return array Count per mailing ID
178 * @static
179 */
180 public static function getMailingContactCount($mailingIDs, $contactID) {
181 $dao = new CRM_Core_DAO();
182 $openedCount = array();
183
184 $open = self::getTableName();
185 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
186 $job = CRM_Mailing_BAO_MailingJob::getTableName();
187 $mailingIDs = implode(',', $mailingIDs);
188
189 $query = "
190 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
191 FROM $open
192 INNER JOIN $queue
193 ON $open.event_queue_id = $queue.id
194 AND $queue.contact_id = $contactID
195 INNER JOIN $job
196 ON $queue.job_id = $job.id
197 AND $job.is_test = 0
198 WHERE $job.mailing_id IN ({$mailingIDs})
199 GROUP BY civicrm_mailing_job.mailing_id
200 ";
201
202 $dao->query($query);
203
204 while ($dao->fetch()) {
205 $openedCount[$dao->mailingID] = $dao->opened;
206 }
207
208 return $openedCount;
209 }
210
211 /**
212 * Get rows for the event browser
213 *
214 * @param int $mailing_id
215 * ID of the mailing.
216 * @param int $job_id
217 * Optional ID of the job.
218 * @param bool $is_distinct
219 * Group by queue id?.
220 * @param int $offset
221 * Offset.
222 * @param int $rowCount
223 * Number of rows.
224 * @param array $sort
225 * Sort array.
226 *
227 * @param int $contact_id
228 *
229 * @return array Result set
230 * @static
231 */
232 public static function &getRows(
233 $mailing_id, $job_id = NULL,
234 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
235 ) {
236 $dao = new CRM_Core_Dao();
237
238 $open = self::getTableName();
239 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
240 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
241 $job = CRM_Mailing_BAO_MailingJob::getTableName();
242 $contact = CRM_Contact_BAO_Contact::getTableName();
243 $email = CRM_Core_BAO_Email::getTableName();
244
245 $query = "
246 SELECT $contact.display_name as display_name,
247 $contact.id as contact_id,
248 $email.email as email,
249 $open.time_stamp as date
250 FROM $contact
251 INNER JOIN $queue
252 ON $queue.contact_id = $contact.id
253 INNER JOIN $email
254 ON $queue.email_id = $email.id
255 INNER JOIN $open
256 ON $open.event_queue_id = $queue.id
257 INNER JOIN $job
258 ON $queue.job_id = $job.id
259 INNER JOIN $mailing
260 ON $job.mailing_id = $mailing.id
261 AND $job.is_test = 0
262 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
263
264 if (!empty($job_id)) {
265 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
266 }
267
268 if (!empty($contact_id)) {
269 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
270 }
271
272 if ($is_distinct) {
273 $query .= " GROUP BY $queue.id ";
274 }
275
276 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
277 if ($sort) {
278 if (is_string($sort)) {
279 $sort = CRM_Utils_Type::escape($sort, 'String');
280 $orderBy = $sort;
281 }
282 else {
283 $orderBy = trim($sort->orderBy());
284 }
285 }
286
287 $query .= " ORDER BY {$orderBy} ";
288
289 if ($offset || $rowCount) {
290 //Added "||$rowCount" to avoid displaying all records on first page
291 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
292 }
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 }