3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Mailing_Event_BAO_TrackableURLOpen
extends CRM_Mailing_Event_DAO_TrackableURLOpen
{
38 public function __construct() {
39 parent
::__construct();
43 * Track a click-through and return the URL to redirect.
45 * If the numbers don't match up, return the base url.
47 * @param int $queue_id
48 * The Queue Event ID of the clicker.
50 * The ID of the trackable URL.
53 * The redirection url, or base url on failure.
55 public static function track($queue_id, $url_id) {
57 $search = new CRM_Mailing_BAO_TrackableURL();
59 // To find the url, we also join on the queue and job tables. This
60 // prevents foreign key violations.
61 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
62 $eq = CRM_Mailing_Event_BAO_Queue
::getTableName();
63 $turl = CRM_Mailing_BAO_TrackableURL
::getTableName();
66 $search->query("SELECT $turl.url as url from $turl
67 WHERE $turl.id = " . CRM_Utils_Type
::escape($url_id, 'Integer')
69 if (!$search->fetch()) {
70 return CRM_Utils_System
::baseURL();
75 $search->query("SELECT $turl.url as url from $turl
76 INNER JOIN $job ON $turl.mailing_id = $job.mailing_id
77 INNER JOIN $eq ON $job.id = $eq.job_id
78 WHERE $eq.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer') . " AND $turl.id = " . CRM_Utils_Type
::escape($url_id, 'Integer')
81 if (!$search->fetch()) {
82 // Whoops, error, don't track it. Return the base url.
84 return CRM_Utils_System
::baseURL();
87 $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
88 $open->event_queue_id
= $queue_id;
89 $open->trackable_url_id
= $url_id;
90 $open->time_stamp
= date('YmdHis');
97 * Get row count for the event selector.
99 * @param int $mailing_id
102 * Optional ID of a job to filter on.
103 * @param bool $is_distinct
104 * Group by queue ID?.
106 * Optional ID of a url to filter on.
108 * @param string $toDate
111 * Number of rows in result set
113 public static function getTotalCount(
114 $mailing_id, $job_id = NULL,
115 $is_distinct = FALSE, $url_id = NULL, $toDate = NULL
117 $dao = new CRM_Core_DAO();
119 $click = self
::getTableName();
120 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
121 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
122 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
125 SELECT COUNT($click.id) as opened
128 ON $click.event_queue_id = $queue.id
130 ON $queue.job_id = $job.id
132 ON $job.mailing_id = $mailing.id
134 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
136 if (!empty($toDate)) {
137 $query .= " AND $click.time_stamp <= $toDate";
140 if (!empty($job_id)) {
141 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
144 if (!empty($url_id)) {
145 $query .= " AND $click.trackable_url_id = " . CRM_Utils_Type
::escape($url_id, 'Integer');
149 $query .= " GROUP BY $queue.id ";
163 * Get tracked url count for each mailing for a given set of mailing IDs.
167 * @param array $mailingIDs
170 * trackable url count per mailing ID
172 public static function getMailingTotalCount($mailingIDs) {
173 $dao = new CRM_Core_DAO();
174 $clickCount = array();
176 $click = 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($click.id) as opened
185 ON $click.event_queue_id = $queue.id
187 ON $queue.job_id = $job.id
189 WHERE $job.mailing_id IN ({$mailingIDs})
190 GROUP BY civicrm_mailing_job.mailing_id
195 while ($dao->fetch()) {
196 $clickCount[$dao->mailingID
] = $dao->opened
;
202 * Get tracked url count for each mailing for a given set of mailing IDs.
204 * @param int $mailingIDs
205 * IDs of the mailing (comma separated).
206 * @param int $contactID
210 * Count per mailing ID
212 public static function getMailingContactCount($mailingIDs, $contactID) {
213 $dao = new CRM_Core_DAO();
214 $clickCount = array();
216 $click = self
::getTableName();
217 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
218 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
219 $mailingIDs = implode(',', $mailingIDs);
222 SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened
225 ON $click.event_queue_id = $queue.id
226 AND $queue.contact_id = $contactID
228 ON $queue.job_id = $job.id
230 WHERE $job.mailing_id IN ({$mailingIDs})
231 GROUP BY civicrm_mailing_job.mailing_id
236 while ($dao->fetch()) {
237 $clickCount[$dao->mailingID
] = $dao->opened
;
244 * Get rows for the event browser.
246 * @param int $mailing_id
249 * Optional ID of the job.
250 * @param bool $is_distinct
251 * Group by queue id?.
253 * Optional ID of a trackable URL to filter on.
256 * @param int $rowCount
260 * @param int $contact_id
261 * Optional contact ID.
266 public static function &getRows(
267 $mailing_id, $job_id = NULL,
268 $is_distinct = FALSE, $url_id,
269 $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
272 $dao = new CRM_Core_Dao();
274 $click = self
::getTableName();
275 $url = CRM_Mailing_BAO_TrackableURL
::getTableName();
276 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
277 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
278 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
279 $contact = CRM_Contact_BAO_Contact
::getTableName();
280 $email = CRM_Core_BAO_Email
::getTableName();
283 SELECT $contact.display_name as display_name,
284 $contact.id as contact_id,
285 $email.email as email,
286 $click.time_stamp as date,
290 ON $queue.contact_id = $contact.id
292 ON $queue.email_id = $email.id
294 ON $click.event_queue_id = $queue.id
296 ON $click.trackable_url_id = $url.id
298 ON $queue.job_id = $job.id
300 ON $job.mailing_id = $mailing.id
302 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
304 if (!empty($contact_id)) {
305 $query .= " AND $contact.id = " . CRM_Utils_Type
::escape($contact_id, 'Integer');
308 if (!empty($job_id)) {
309 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
312 if (!empty($url_id)) {
313 $query .= " AND $url.id = " . CRM_Utils_Type
::escape($url_id, 'Integer');
317 $query .= " GROUP BY $queue.id ";
320 $orderBy = "sort_name ASC, {$click}.time_stamp DESC";
322 if (is_string($sort)) {
323 $sort = CRM_Utils_Type
::escape($sort, 'String');
327 $orderBy = trim($sort->orderBy());
331 $query .= " ORDER BY {$orderBy} ";
333 if ($offset ||
$rowCount) {
334 //Added "||$rowCount" to avoid displaying all records on first page
335 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
342 while ($dao->fetch()) {
343 $url = CRM_Utils_System
::url('civicrm/contact/view',
344 "reset=1&cid={$dao->contact_id}"
347 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
348 'email' => $dao->email
,
350 'date' => CRM_Utils_Date
::customFormat($dao->date
),