3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Mailing_Event_BAO_Opened
extends CRM_Mailing_Event_DAO_Opened
{
38 public function __construct() {
39 parent
::__construct();
43 * Register an open event.
45 * @param int $queue_id
46 * The Queue Event ID of the recipient.
50 public static function open($queue_id) {
51 // First make sure there's a matching queue event.
55 $q = new CRM_Mailing_Event_BAO_Queue();
58 $oe = new CRM_Mailing_Event_BAO_Opened();
59 $oe->event_queue_id
= $queue_id;
60 $oe->time_stamp
= date('YmdHis');
69 * Get row count for the event selector.
71 * @param int $mailing_id
74 * Optional ID of a job to filter on.
75 * @param bool $is_distinct
78 * @param string $toDate
81 * Number of rows in result set
83 public static function getTotalCount(
89 $dao = new CRM_Core_DAO();
91 $open = self
::getTableName();
92 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
93 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
94 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
97 SELECT COUNT($open.id) as opened
100 ON $open.event_queue_id = $queue.id
102 ON $queue.job_id = $job.id
104 ON $job.mailing_id = $mailing.id
106 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
108 if (!empty($toDate)) {
109 $query .= " AND $open.time_stamp <= $toDate";
112 if (!empty($job_id)) {
113 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
117 $query .= " GROUP BY $queue.id ";
126 return $dao->opened ?
$dao->opened
: 0;
132 * Get opened count for each mailing for a given set of mailing IDs
137 * Opened count per mailing ID
139 public static function getMailingTotalCount($mailingIDs) {
140 $dao = new CRM_Core_DAO();
141 $openedCount = array();
143 $open = self
::getTableName();
144 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
145 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
146 $mailingIDs = implode(',', $mailingIDs);
149 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
152 ON $open.event_queue_id = $queue.id
154 ON $queue.job_id = $job.id
156 WHERE $job.mailing_id IN ({$mailingIDs})
157 GROUP BY civicrm_mailing_job.mailing_id
162 while ($dao->fetch()) {
163 $openedCount[$dao->mailingID
] = $dao->opened
;
169 * Get opened count for each mailing for a given set of mailing IDs and a specific contact.
171 * @param int $mailingIDs
172 * IDs of the mailing (comma separated).
173 * @param int $contactID
177 * Count per mailing ID
179 public static function getMailingContactCount($mailingIDs, $contactID) {
180 $dao = new CRM_Core_DAO();
181 $openedCount = array();
183 $open = self
::getTableName();
184 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
185 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
186 $mailingIDs = implode(',', $mailingIDs);
189 SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened
192 ON $open.event_queue_id = $queue.id
193 AND $queue.contact_id = $contactID
195 ON $queue.job_id = $job.id
197 WHERE $job.mailing_id IN ({$mailingIDs})
198 GROUP BY civicrm_mailing_job.mailing_id
203 while ($dao->fetch()) {
204 $openedCount[$dao->mailingID
] = $dao->opened
;
211 * Get rows for the event browser.
213 * @param int $mailing_id
216 * Optional ID of the job.
217 * @param bool $is_distinct
218 * Group by queue id?.
221 * @param int $rowCount
226 * @param int $contact_id
231 public static function &getRows(
232 $mailing_id, $job_id = NULL,
233 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
235 $dao = new CRM_Core_Dao();
237 $open = self
::getTableName();
238 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
239 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
240 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
241 $contact = CRM_Contact_BAO_Contact
::getTableName();
242 $email = CRM_Core_BAO_Email
::getTableName();
244 $selectClauses = array(
245 "$contact.display_name as display_name",
246 "$contact.id as contact_id",
247 "$email.email as email",
248 ($is_distinct) ?
"MIN({$open}.time_stamp) as date" : "{$open}.time_stamp as date",
252 $groupBy = " GROUP BY $queue.id ";
253 $select = CRM_Contact_BAO_Query
::appendAnyValueToSelect($selectClauses, "$queue.id");
257 $select = " SELECT " . implode(', ', $selectClauses);
264 ON $queue.contact_id = $contact.id
266 ON $queue.email_id = $email.id
268 ON $open.event_queue_id = $queue.id
270 ON $queue.job_id = $job.id
272 ON $job.mailing_id = $mailing.id
274 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
276 if (!empty($job_id)) {
277 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
280 if (!empty($contact_id)) {
281 $query .= " AND $contact.id = " . CRM_Utils_Type
::escape($contact_id, 'Integer');
286 $orderBy = "sort_name ASC";
288 $orderBy .= ", {$open}.time_stamp DESC";
291 if (is_string($sort)) {
292 $sort = CRM_Utils_Type
::escape($sort, 'String');
296 $orderBy = trim($sort->orderBy());
300 $query .= " ORDER BY {$orderBy} ";
302 if ($offset ||
$rowCount) {
303 //Added "||$rowCount" to avoid displaying all records on first page
304 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
311 while ($dao->fetch()) {
312 $url = CRM_Utils_System
::url('civicrm/contact/view',
313 "reset=1&cid={$dao->contact_id}"
316 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
317 'email' => $dao->email
,
318 'date' => CRM_Utils_Date
::customFormat($dao->date
),