Merge pull request #2495 from civicrm/4.4
[civicrm-core.git] / CRM / Mailing / Event / BAO / TrackableURLOpen.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Mailing_Event_BAO_TrackableURLOpen extends CRM_Mailing_Event_DAO_TrackableURLOpen {
36
37 /**
38 * class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Track a click-through and return the URL to redirect. If the numbers
46 * don't match up, return the base url.
47 *
48 * @param int $queue_id The Queue Event ID of the clicker
49 * @param int $url_id The ID of the trackable URL
50 *
51 * @return string $url The redirection url, or base url on failure.
52 * @access public
53 * @static
54 */
55 public static function track($queue_id, $url_id) {
56
57 $search = new CRM_Mailing_BAO_TrackableURL();
58
59 /* To find the url, we also join on the queue and job tables. This
60 * prevents foreign key violations. */
61
62
9da8dc8c 63 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
64 $eq = CRM_Mailing_Event_BAO_Queue::getTableName();
65 $turl = CRM_Mailing_BAO_TrackableURL::getTableName();
66
67 if (!$queue_id) {
68 $search->query("SELECT $turl.url as url from $turl
69 WHERE $turl.id = " . CRM_Utils_Type::escape($url_id, 'Integer')
70 );
71 if (!$search->fetch()) {
72 return CRM_Utils_System::baseURL();
73 }
74 return $search->url;
75 }
76
77 $search->query("SELECT $turl.url as url from $turl
78 INNER JOIN $job ON $turl.mailing_id = $job.mailing_id
79 INNER JOIN $eq ON $job.id = $eq.job_id
80 WHERE $eq.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') . " AND $turl.id = " . CRM_Utils_Type::escape($url_id, 'Integer')
81 );
82
83 if (!$search->fetch()) {
84 /* Whoops, error, don't track it. Return the base url. */
85
86 return CRM_Utils_System::baseURL();
87 }
88
89 $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
90 $open->event_queue_id = $queue_id;
91 $open->trackable_url_id = $url_id;
92 $open->time_stamp = date('YmdHis');
93 $open->save();
94
95 return $search->url;
96 }
97
98 /**
99 * Get row count for the event selector
100 *
101 * @param int $mailing_id ID of the mailing
102 * @param int $job_id Optional ID of a job to filter on
103 * @param boolean $is_distinct Group by queue ID?
104 * @param int $url_id Optional ID of a url to filter on
105 *
106 * @return int Number of rows in result set
107 * @access public
108 * @static
109 */
110 public static function getTotalCount($mailing_id, $job_id = NULL,
111 $is_distinct = FALSE, $url_id = NULL
112 ) {
113 $dao = new CRM_Core_DAO();
114
115 $click = self::getTableName();
116 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
117 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 118 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
119
120 $query = "
121 SELECT COUNT($click.id) as opened
122 FROM $click
123 INNER JOIN $queue
124 ON $click.event_queue_id = $queue.id
125 INNER JOIN $job
126 ON $queue.job_id = $job.id
127 INNER JOIN $mailing
128 ON $job.mailing_id = $mailing.id
129 AND $job.is_test = 0
130 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
131
132 if (!empty($job_id)) {
133 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
134 }
135
136 if (!empty($url_id)) {
137 $query .= " AND $click.trackable_url_id = " . CRM_Utils_Type::escape($url_id, 'Integer');
138 }
139
140 if ($is_distinct) {
141 $query .= " GROUP BY $queue.id ";
142 }
143
144 // query was missing
145 $dao->query($query);
146
147 if ($dao->fetch()) {
148 return $dao->opened;
149 }
150
151 return NULL;
152 }
153
de1cbb7c
BS
154 /**
155 * CRM-12814
156 * Get tracked url count for each mailing for a given set of mailing IDs
157 *
158 * @param int $contactID ID of the mailing
159 *
160 * @return array trackable url count per mailing ID
161 * @access public
162 * @static
163 */
164 public static function getMailingTotalCount($mailingIDs) {
165 $dao = new CRM_Core_DAO();
166 $clickCount = array();
167
168 $click = self::getTableName();
169 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
9da8dc8c 170 $job = CRM_Mailing_BAO_MailingJob::getTableName();
de1cbb7c
BS
171 $mailingIDs = implode(',', $mailingIDs);
172
173 $query = "
174 SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened
175 FROM $click
176 INNER JOIN $queue
177 ON $click.event_queue_id = $queue.id
178 INNER JOIN $job
179 ON $queue.job_id = $job.id
180 AND $job.is_test = 0
181 WHERE $job.mailing_id IN ({$mailingIDs})
182 GROUP BY civicrm_mailing_job.mailing_id
183 ";
184
185 $dao->query($query);
186
187 while ( $dao->fetch() ) {
188 $clickCount[$dao->mailingID] = $dao->opened;
189 }
190 return $clickCount;
191 }
192
6a488035
TO
193 /**
194 * Get rows for the event browser
195 *
196 * @param int $mailing_id ID of the mailing
197 * @param int $job_id optional ID of the job
198 * @param boolean $is_distinct Group by queue id?
199 * @param int $url_id optional ID of a trackable URL to filter on
200 * @param int $offset Offset
201 * @param int $rowCount Number of rows
202 * @param array $sort sort array
203 * @param int $contact_id optional contact ID
204 *
205 * @return array Result set
206 * @access public
207 * @static
208 */
209 public static function &getRows($mailing_id, $job_id = NULL,
210 $is_distinct = FALSE, $url_id,
211 $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
212 ) {
213
214 $dao = new CRM_Core_Dao();
215
216 $click = self::getTableName();
217 $url = CRM_Mailing_BAO_TrackableURL::getTableName();
218 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
219 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 220 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
221 $contact = CRM_Contact_BAO_Contact::getTableName();
222 $email = CRM_Core_BAO_Email::getTableName();
223
224 $query = "
225 SELECT $contact.display_name as display_name,
226 $contact.id as contact_id,
227 $email.email as email,
228 $click.time_stamp as date,
229 $url.url as url
230 FROM $contact
231 INNER JOIN $queue
232 ON $queue.contact_id = $contact.id
233 INNER JOIN $email
234 ON $queue.email_id = $email.id
235 INNER JOIN $click
236 ON $click.event_queue_id = $queue.id
237 INNER JOIN $url
238 ON $click.trackable_url_id = $url.id
239 INNER JOIN $job
240 ON $queue.job_id = $job.id
241 INNER JOIN $mailing
242 ON $job.mailing_id = $mailing.id
243 AND $job.is_test = 0
244 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
245
246 if (!empty($contact_id)) {
247 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
248 }
249
250 if (!empty($job_id)) {
251 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
252 }
253
254 if (!empty($url_id)) {
255 $query .= " AND $url.id = " . CRM_Utils_Type::escape($url_id, 'Integer');
256 }
257
258 if ($is_distinct) {
259 $query .= " GROUP BY $queue.id ";
260 }
261
262 $orderBy = "sort_name ASC, {$click}.time_stamp DESC";
263 if ($sort) {
264 if (is_string($sort)) {
21d32567 265 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
266 $orderBy = $sort;
267 }
268 else {
269 $orderBy = trim($sort->orderBy());
270 }
271 }
272
273 $query .= " ORDER BY {$orderBy} ";
274
275 if ($offset || $rowCount) {
276 //Added "||$rowCount" to avoid displaying all records on first page
277 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
278 }
279
280 $dao->query($query);
281
282 $results = array();
283
284 while ($dao->fetch()) {
285 $url = CRM_Utils_System::url('civicrm/contact/view',
286 "reset=1&cid={$dao->contact_id}"
287 );
288 $results[] = array(
289 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
290 'email' => $dao->email,
291 'url' => $dao->url,
292 'date' => CRM_Utils_Date::customFormat($dao->date),
293 );
294 }
295 return $results;
296 }
297}
298