Merge pull request #8491 from seamuslee001/CRM-18752
[civicrm-core.git] / CRM / Mailing / Event / BAO / TrackableURLOpen.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33class CRM_Mailing_Event_BAO_TrackableURLOpen extends CRM_Mailing_Event_DAO_TrackableURLOpen {
34
35 /**
fe482240 36 * Class constructor.
6a488035 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40 }
41
42 /**
ad37ac8e 43 * Track a click-through and return the URL to redirect.
44 *
45 * If the numbers don't match up, return the base url.
6a488035 46 *
90c8230e
TO
47 * @param int $queue_id
48 * The Queue Event ID of the clicker.
49 * @param int $url_id
50 * The ID of the trackable URL.
6a488035 51 *
a6c01b45
CW
52 * @return string
53 * The redirection url, or base url on failure.
6a488035
TO
54 */
55 public static function track($queue_id, $url_id) {
56
57 $search = new CRM_Mailing_BAO_TrackableURL();
58
25606795 59 // To find the url, we also join on the queue and job tables. This
3d9b94a1 60 // prevents foreign key violations.
353ffa53
TO
61 $job = CRM_Mailing_BAO_MailingJob::getTableName();
62 $eq = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035
TO
63 $turl = CRM_Mailing_BAO_TrackableURL::getTableName();
64
65 if (!$queue_id) {
66 $search->query("SELECT $turl.url as url from $turl
67 WHERE $turl.id = " . CRM_Utils_Type::escape($url_id, 'Integer')
68 );
69 if (!$search->fetch()) {
70 return CRM_Utils_System::baseURL();
71 }
72 return $search->url;
73 }
74
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')
79 );
80
81 if (!$search->fetch()) {
9506b36d
ACD
82 // Can't find either the URL or the queue. If we can find the URL then
83 // return the URL without tracking. Otherwise return the base URL.
6a488035 84
9506b36d
ACD
85 $search->query("SELECT $turl.url as url from $turl
86 WHERE $turl.id = " . CRM_Utils_Type::escape($url_id, 'Integer')
87 );
88 if (!$search->fetch()) {
89 return CRM_Utils_System::baseURL();
90 }
91 return $search->url;
6a488035
TO
92 }
93
94 $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
95 $open->event_queue_id = $queue_id;
96 $open->trackable_url_id = $url_id;
97 $open->time_stamp = date('YmdHis');
98 $open->save();
99
100 return $search->url;
101 }
102
103 /**
fe482240 104 * Get row count for the event selector.
6a488035 105 *
90c8230e
TO
106 * @param int $mailing_id
107 * ID of the mailing.
108 * @param int $job_id
109 * Optional ID of a job to filter on.
110 * @param bool $is_distinct
111 * Group by queue ID?.
112 * @param int $url_id
113 * Optional ID of a url to filter on.
6a488035 114 *
ad37ac8e 115 * @param string $toDate
116 *
a6c01b45
CW
117 * @return int
118 * Number of rows in result set
6a488035 119 */
a3d7e8ee
TO
120 public static function getTotalCount(
121 $mailing_id, $job_id = NULL,
7811a84b 122 $is_distinct = FALSE, $url_id = NULL, $toDate = NULL
6a488035
TO
123 ) {
124 $dao = new CRM_Core_DAO();
125
353ffa53
TO
126 $click = self::getTableName();
127 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035 128 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
353ffa53 129 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
130
131 $query = "
132 SELECT COUNT($click.id) as opened
133 FROM $click
134 INNER JOIN $queue
135 ON $click.event_queue_id = $queue.id
136 INNER JOIN $job
137 ON $queue.job_id = $job.id
138 INNER JOIN $mailing
139 ON $job.mailing_id = $mailing.id
140 AND $job.is_test = 0
141 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
142
7811a84b 143 if (!empty($toDate)) {
144 $query .= " AND $click.time_stamp <= $toDate";
145 }
146
6a488035
TO
147 if (!empty($job_id)) {
148 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
149 }
150
151 if (!empty($url_id)) {
152 $query .= " AND $click.trackable_url_id = " . CRM_Utils_Type::escape($url_id, 'Integer');
153 }
154
155 if ($is_distinct) {
156 $query .= " GROUP BY $queue.id ";
157 }
158
159 // query was missing
160 $dao->query($query);
161
162 if ($dao->fetch()) {
163 return $dao->opened;
164 }
165
166 return NULL;
167 }
168
de1cbb7c 169 /**
ad37ac8e 170 * Get tracked url count for each mailing for a given set of mailing IDs.
171 *
de1cbb7c 172 * CRM-12814
de1cbb7c 173 *
ad37ac8e 174 * @param array $mailingIDs
da6b46f4 175 *
a6c01b45
CW
176 * @return array
177 * trackable url count per mailing ID
de1cbb7c
BS
178 */
179 public static function getMailingTotalCount($mailingIDs) {
180 $dao = new CRM_Core_DAO();
181 $clickCount = array();
182
183 $click = self::getTableName();
184 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
9da8dc8c 185 $job = CRM_Mailing_BAO_MailingJob::getTableName();
de1cbb7c
BS
186 $mailingIDs = implode(',', $mailingIDs);
187
188 $query = "
189 SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened
190 FROM $click
191 INNER JOIN $queue
192 ON $click.event_queue_id = $queue.id
193 INNER JOIN $job
194 ON $queue.job_id = $job.id
195 AND $job.is_test = 0
196 WHERE $job.mailing_id IN ({$mailingIDs})
197 GROUP BY civicrm_mailing_job.mailing_id
198 ";
199
200 $dao->query($query);
201
481a74f4 202 while ($dao->fetch()) {
de1cbb7c
BS
203 $clickCount[$dao->mailingID] = $dao->opened;
204 }
205 return $clickCount;
206 }
207
6b62f1bb 208 /**
fe482240 209 * Get tracked url count for each mailing for a given set of mailing IDs.
6b62f1bb 210 *
90c8230e
TO
211 * @param int $mailingIDs
212 * IDs of the mailing (comma separated).
213 * @param int $contactID
214 * ID of the contact.
6b62f1bb 215 *
a6c01b45
CW
216 * @return array
217 * Count per mailing ID
6b62f1bb
DG
218 */
219 public static function getMailingContactCount($mailingIDs, $contactID) {
220 $dao = new CRM_Core_DAO();
221 $clickCount = array();
222
223 $click = self::getTableName();
224 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
225 $job = CRM_Mailing_BAO_MailingJob::getTableName();
226 $mailingIDs = implode(',', $mailingIDs);
227
228 $query = "
229 SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened
230 FROM $click
231 INNER JOIN $queue
232 ON $click.event_queue_id = $queue.id
233 AND $queue.contact_id = $contactID
234 INNER JOIN $job
235 ON $queue.job_id = $job.id
236 AND $job.is_test = 0
237 WHERE $job.mailing_id IN ({$mailingIDs})
238 GROUP BY civicrm_mailing_job.mailing_id
239 ";
240
241 $dao->query($query);
242
481a74f4 243 while ($dao->fetch()) {
6b62f1bb
DG
244 $clickCount[$dao->mailingID] = $dao->opened;
245 }
246
247 return $clickCount;
248 }
249
6a488035 250 /**
fe482240 251 * Get rows for the event browser.
6a488035 252 *
90c8230e
TO
253 * @param int $mailing_id
254 * ID of the mailing.
255 * @param int $job_id
256 * Optional ID of the job.
257 * @param bool $is_distinct
258 * Group by queue id?.
259 * @param int $url_id
260 * Optional ID of a trackable URL to filter on.
261 * @param int $offset
262 * Offset.
263 * @param int $rowCount
264 * Number of rows.
265 * @param array $sort
266 * Sort array.
267 * @param int $contact_id
268 * Optional contact ID.
6a488035 269 *
a6c01b45
CW
270 * @return array
271 * Result set
6a488035 272 */
a3d7e8ee
TO
273 public static function &getRows(
274 $mailing_id, $job_id = NULL,
6a488035
TO
275 $is_distinct = FALSE, $url_id,
276 $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
277 ) {
278
279 $dao = new CRM_Core_Dao();
280
353ffa53
TO
281 $click = self::getTableName();
282 $url = CRM_Mailing_BAO_TrackableURL::getTableName();
283 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
6a488035 284 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
353ffa53 285 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035 286 $contact = CRM_Contact_BAO_Contact::getTableName();
353ffa53 287 $email = CRM_Core_BAO_Email::getTableName();
6a488035
TO
288
289 $query = "
290 SELECT $contact.display_name as display_name,
291 $contact.id as contact_id,
292 $email.email as email,
293 $click.time_stamp as date,
294 $url.url as url
295 FROM $contact
296 INNER JOIN $queue
297 ON $queue.contact_id = $contact.id
298 INNER JOIN $email
299 ON $queue.email_id = $email.id
300 INNER JOIN $click
301 ON $click.event_queue_id = $queue.id
302 INNER JOIN $url
303 ON $click.trackable_url_id = $url.id
304 INNER JOIN $job
305 ON $queue.job_id = $job.id
306 INNER JOIN $mailing
307 ON $job.mailing_id = $mailing.id
308 AND $job.is_test = 0
309 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
310
311 if (!empty($contact_id)) {
312 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
313 }
314
315 if (!empty($job_id)) {
316 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
317 }
318
319 if (!empty($url_id)) {
320 $query .= " AND $url.id = " . CRM_Utils_Type::escape($url_id, 'Integer');
321 }
322
323 if ($is_distinct) {
324 $query .= " GROUP BY $queue.id ";
325 }
326
327 $orderBy = "sort_name ASC, {$click}.time_stamp DESC";
328 if ($sort) {
329 if (is_string($sort)) {
21d32567 330 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
331 $orderBy = $sort;
332 }
333 else {
334 $orderBy = trim($sort->orderBy());
335 }
336 }
337
338 $query .= " ORDER BY {$orderBy} ";
339
340 if ($offset || $rowCount) {
341 //Added "||$rowCount" to avoid displaying all records on first page
342 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
343 }
344
345 $dao->query($query);
346
347 $results = array();
348
349 while ($dao->fetch()) {
350 $url = CRM_Utils_System::url('civicrm/contact/view',
351 "reset=1&cid={$dao->contact_id}"
352 );
353 $results[] = array(
354 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
355 'email' => $dao->email,
356 'url' => $dao->url,
357 'date' => CRM_Utils_Date::customFormat($dao->date),
358 );
359 }
360 return $results;
361 }
96025800 362
6a488035 363}