3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Mailing_Event_BAO_Opened
extends CRM_Mailing_Event_DAO_Opened
{
40 function __construct() {
41 parent
::__construct();
45 * Register an open event
47 * @param int $queue_id 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 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?
78 * @return int Number of rows in result set
82 public static function getTotalCount($mailing_id,
86 $dao = new CRM_Core_DAO();
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();
94 SELECT COUNT($open.id) as opened
97 ON $open.event_queue_id = $queue.id
99 ON $queue.job_id = $job.id
101 ON $job.mailing_id = $mailing.id
103 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
105 if (!empty($job_id)) {
106 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
110 $query .= " GROUP BY $queue.id ";
119 return $dao->opened ?
$dao->opened
: 0;
125 * Get opened count for each mailing for a given set of mailing IDs
127 * @param int $contactID ID of the mailing
129 * @return array Opened count per mailing ID
133 public static function getMailingTotalCount($mailingIDs) {
134 $dao = new CRM_Core_DAO();
135 $openedCount = array();
137 $open = self
::getTableName();
138 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
139 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
140 $mailingIDs = implode(',', $mailingIDs);
143 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
146 ON $open.event_queue_id = $queue.id
148 ON $queue.job_id = $job.id
150 WHERE $job.mailing_id IN ({$mailingIDs})
151 GROUP BY civicrm_mailing_job.mailing_id
156 while ( $dao->fetch() ) {
157 $openedCount[$dao->mailingID
] = $dao->opened
;
163 * Get opened count for each mailing for a given set of mailing IDs and a specific contact
165 * @param int $mailingIDs IDs of the mailing (comma separated)
166 * @param int $contactID ID of the contact
168 * @return array Count per mailing ID
172 public static function getMailingContactCount($mailingIDs, $contactID) {
173 $dao = new CRM_Core_DAO();
174 $openedCount = array();
176 $open = self
::getTableName();
177 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
178 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
179 $mailingIDs = implode(',', $mailingIDs);
182 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
185 ON $open.event_queue_id = $queue.id
186 AND $queue.contact_id = $contactID
188 ON $queue.job_id = $job.id
190 WHERE $job.mailing_id IN ({$mailingIDs})
191 GROUP BY civicrm_mailing_job.mailing_id
196 while ( $dao->fetch() ) {
197 $openedCount[$dao->mailingID
] = $dao->opened
;
204 * Get rows for the event browser
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
213 * @return array Result set
217 public static function &getRows($mailing_id, $job_id = NULL,
218 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id= NULL
221 $dao = new CRM_Core_Dao();
223 $open = self
::getTableName();
224 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
225 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
226 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
227 $contact = CRM_Contact_BAO_Contact
::getTableName();
228 $email = CRM_Core_BAO_Email
::getTableName();
231 SELECT $contact.display_name as display_name,
232 $contact.id as contact_id,
233 $email.email as email,
234 $open.time_stamp as date
237 ON $queue.contact_id = $contact.id
239 ON $queue.email_id = $email.id
241 ON $open.event_queue_id = $queue.id
243 ON $queue.job_id = $job.id
245 ON $job.mailing_id = $mailing.id
247 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
249 if (!empty($job_id)) {
250 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
253 if (!empty($contact_id)) {
254 $query .= " AND $contact.id = " . CRM_Utils_Type
::escape($contact_id, 'Integer');
258 $query .= " GROUP BY $queue.id ";
261 $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
263 if (is_string($sort)) {
264 $sort = CRM_Utils_Type
::escape($sort, 'String');
268 $orderBy = trim($sort->orderBy());
272 $query .= " ORDER BY {$orderBy} ";
274 if ($offset ||
$rowCount) {
275 //Added "||$rowCount" to avoid displaying all records on first page
276 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
283 while ($dao->fetch()) {
284 $url = CRM_Utils_System
::url('civicrm/contact/view',
285 "reset=1&cid={$dao->contact_id}"
288 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
289 'email' => $dao->email
,
290 'date' => CRM_Utils_Date
::customFormat($dao->date
),