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
82 * Number of rows in result set
85 public static function getTotalCount(
91 $dao = new CRM_Core_DAO();
93 $open = self
::getTableName();
94 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
95 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
96 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
99 SELECT COUNT($open.id) as opened
102 ON $open.event_queue_id = $queue.id
104 ON $queue.job_id = $job.id
106 ON $job.mailing_id = $mailing.id
108 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
110 if (!empty($toDate)) {
111 $query .= " AND $open.time_stamp <= $toDate";
114 if (!empty($job_id)) {
115 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
119 $query .= " GROUP BY $queue.id ";
128 return $dao->opened ?
$dao->opened
: 0;
134 * Get opened count for each mailing for a given set of mailing IDs
139 * Opened count per mailing ID
142 public static function getMailingTotalCount($mailingIDs) {
143 $dao = new CRM_Core_DAO();
144 $openedCount = array();
146 $open = self
::getTableName();
147 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
148 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
149 $mailingIDs = implode(',', $mailingIDs);
152 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
155 ON $open.event_queue_id = $queue.id
157 ON $queue.job_id = $job.id
159 WHERE $job.mailing_id IN ({$mailingIDs})
160 GROUP BY civicrm_mailing_job.mailing_id
165 while ($dao->fetch()) {
166 $openedCount[$dao->mailingID
] = $dao->opened
;
172 * Get opened count for each mailing for a given set of mailing IDs and a specific contact
174 * @param int $mailingIDs
175 * IDs of the mailing (comma separated).
176 * @param int $contactID
180 * Count per mailing ID
183 public static function getMailingContactCount($mailingIDs, $contactID) {
184 $dao = new CRM_Core_DAO();
185 $openedCount = array();
187 $open = self
::getTableName();
188 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
189 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
190 $mailingIDs = implode(',', $mailingIDs);
193 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
196 ON $open.event_queue_id = $queue.id
197 AND $queue.contact_id = $contactID
199 ON $queue.job_id = $job.id
201 WHERE $job.mailing_id IN ({$mailingIDs})
202 GROUP BY civicrm_mailing_job.mailing_id
207 while ($dao->fetch()) {
208 $openedCount[$dao->mailingID
] = $dao->opened
;
215 * Get rows for the event browser
217 * @param int $mailing_id
220 * Optional ID of the job.
221 * @param bool $is_distinct
222 * Group by queue id?.
225 * @param int $rowCount
230 * @param int $contact_id
236 public static function &getRows(
237 $mailing_id, $job_id = NULL,
238 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
240 $dao = new CRM_Core_Dao();
242 $open = self
::getTableName();
243 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
244 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
245 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
246 $contact = CRM_Contact_BAO_Contact
::getTableName();
247 $email = CRM_Core_BAO_Email
::getTableName();
250 SELECT $contact.display_name as display_name,
251 $contact.id as contact_id,
252 $email.email as email,
253 $open.time_stamp as date
256 ON $queue.contact_id = $contact.id
258 ON $queue.email_id = $email.id
260 ON $open.event_queue_id = $queue.id
262 ON $queue.job_id = $job.id
264 ON $job.mailing_id = $mailing.id
266 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
268 if (!empty($job_id)) {
269 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
272 if (!empty($contact_id)) {
273 $query .= " AND $contact.id = " . CRM_Utils_Type
::escape($contact_id, 'Integer');
277 $query .= " GROUP BY $queue.id ";
280 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
282 if (is_string($sort)) {
283 $sort = CRM_Utils_Type
::escape($sort, 'String');
287 $orderBy = trim($sort->orderBy());
291 $query .= " ORDER BY {$orderBy} ";
293 if ($offset ||
$rowCount) {
294 //Added "||$rowCount" to avoid displaying all records on first page
295 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
301 while ($dao->fetch()) {
302 $url = CRM_Utils_System
::url('civicrm/contact/view',
303 "reset=1&cid={$dao->contact_id}"
306 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
307 'email' => $dao->email
,
308 'date' => CRM_Utils_Date
::customFormat($dao->date
),