3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Mailing_Event_BAO_Opened
extends CRM_Mailing_Event_DAO_Opened
{
40 public function __construct() {
41 parent
::__construct();
45 * Register an open event
47 * @param int $queue_id
48 * The Queue Event ID of the recipient.
53 public static function open($queue_id) {
54 /* First make sure there's a matching queue event */
58 $q = new CRM_Mailing_Event_BAO_Queue();
61 $oe = new CRM_Mailing_Event_BAO_Opened();
62 $oe->event_queue_id
= $queue_id;
63 $oe->time_stamp
= date('YmdHis');
72 * Get row count for the event selector
74 * @param int $mailing_id
77 * Optional ID of a job to filter on.
78 * @param bool $is_distinct
81 * @return int Number of rows in result set
84 public static function getTotalCount(
90 $dao = new CRM_Core_DAO();
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();
98 SELECT COUNT($open.id) as opened
101 ON $open.event_queue_id = $queue.id
103 ON $queue.job_id = $job.id
105 ON $job.mailing_id = $mailing.id
107 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
109 if (!empty($toDate)) {
110 $query .= " AND $open.time_stamp <= $toDate";
113 if (!empty($job_id)) {
114 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
118 $query .= " GROUP BY $queue.id ";
127 return $dao->opened ?
$dao->opened
: 0;
133 * Get opened count for each mailing for a given set of mailing IDs
137 * @return array Opened count per mailing ID
140 public static function getMailingTotalCount($mailingIDs) {
141 $dao = new CRM_Core_DAO();
142 $openedCount = array();
144 $open = self
::getTableName();
145 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
146 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
147 $mailingIDs = implode(',', $mailingIDs);
150 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
153 ON $open.event_queue_id = $queue.id
155 ON $queue.job_id = $job.id
157 WHERE $job.mailing_id IN ({$mailingIDs})
158 GROUP BY civicrm_mailing_job.mailing_id
163 while ($dao->fetch()) {
164 $openedCount[$dao->mailingID
] = $dao->opened
;
170 * Get opened count for each mailing for a given set of mailing IDs and a specific contact
172 * @param int $mailingIDs
173 * IDs of the mailing (comma separated).
174 * @param int $contactID
177 * @return array Count per mailing ID
180 public static function getMailingContactCount($mailingIDs, $contactID) {
181 $dao = new CRM_Core_DAO();
182 $openedCount = array();
184 $open = self
::getTableName();
185 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
186 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
187 $mailingIDs = implode(',', $mailingIDs);
190 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
193 ON $open.event_queue_id = $queue.id
194 AND $queue.contact_id = $contactID
196 ON $queue.job_id = $job.id
198 WHERE $job.mailing_id IN ({$mailingIDs})
199 GROUP BY civicrm_mailing_job.mailing_id
204 while ($dao->fetch()) {
205 $openedCount[$dao->mailingID
] = $dao->opened
;
212 * Get rows for the event browser
214 * @param int $mailing_id
217 * Optional ID of the job.
218 * @param bool $is_distinct
219 * Group by queue id?.
222 * @param int $rowCount
227 * @param int $contact_id
229 * @return array Result set
232 public static function &getRows(
233 $mailing_id, $job_id = NULL,
234 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
236 $dao = new CRM_Core_Dao();
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();
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
252 ON $queue.contact_id = $contact.id
254 ON $queue.email_id = $email.id
256 ON $open.event_queue_id = $queue.id
258 ON $queue.job_id = $job.id
260 ON $job.mailing_id = $mailing.id
262 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
264 if (!empty($job_id)) {
265 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
268 if (!empty($contact_id)) {
269 $query .= " AND $contact.id = " . CRM_Utils_Type
::escape($contact_id, 'Integer');
273 $query .= " GROUP BY $queue.id ";
276 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
278 if (is_string($sort)) {
279 $sort = CRM_Utils_Type
::escape($sort, 'String');
283 $orderBy = trim($sort->orderBy());
287 $query .= " ORDER BY {$orderBy} ";
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');
297 while ($dao->fetch()) {
298 $url = CRM_Utils_System
::url('civicrm/contact/view',
299 "reset=1&cid={$dao->contact_id}"
302 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
303 'email' => $dao->email
,
304 'date' => CRM_Utils_Date
::customFormat($dao->date
),