CRM-13554 - validate values of limit and offset
[civicrm-core.git] / CRM / Mailing / Event / BAO / TrackableURLOpen.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
63 $job = CRM_Mailing_BAO_Job::getTableName();
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();
118 $job = CRM_Mailing_BAO_Job::getTableName();
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
154 /**
155 * Get rows for the event browser
156 *
157 * @param int $mailing_id ID of the mailing
158 * @param int $job_id optional ID of the job
159 * @param boolean $is_distinct Group by queue id?
160 * @param int $url_id optional ID of a trackable URL to filter on
161 * @param int $offset Offset
162 * @param int $rowCount Number of rows
163 * @param array $sort sort array
164 * @param int $contact_id optional contact ID
165 *
166 * @return array Result set
167 * @access public
168 * @static
169 */
170 public static function &getRows($mailing_id, $job_id = NULL,
171 $is_distinct = FALSE, $url_id,
172 $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
173 ) {
174
175 $dao = new CRM_Core_Dao();
176
177 $click = self::getTableName();
178 $url = CRM_Mailing_BAO_TrackableURL::getTableName();
179 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
180 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
181 $job = CRM_Mailing_BAO_Job::getTableName();
182 $contact = CRM_Contact_BAO_Contact::getTableName();
183 $email = CRM_Core_BAO_Email::getTableName();
184
185 $query = "
186 SELECT $contact.display_name as display_name,
187 $contact.id as contact_id,
188 $email.email as email,
189 $click.time_stamp as date,
190 $url.url as url
191 FROM $contact
192 INNER JOIN $queue
193 ON $queue.contact_id = $contact.id
194 INNER JOIN $email
195 ON $queue.email_id = $email.id
196 INNER JOIN $click
197 ON $click.event_queue_id = $queue.id
198 INNER JOIN $url
199 ON $click.trackable_url_id = $url.id
200 INNER JOIN $job
201 ON $queue.job_id = $job.id
202 INNER JOIN $mailing
203 ON $job.mailing_id = $mailing.id
204 AND $job.is_test = 0
205 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
206
207 if (!empty($contact_id)) {
208 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
209 }
210
211 if (!empty($job_id)) {
212 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
213 }
214
215 if (!empty($url_id)) {
216 $query .= " AND $url.id = " . CRM_Utils_Type::escape($url_id, 'Integer');
217 }
218
219 if ($is_distinct) {
220 $query .= " GROUP BY $queue.id ";
221 }
222
223 $orderBy = "sort_name ASC, {$click}.time_stamp DESC";
224 if ($sort) {
225 if (is_string($sort)) {
226 $orderBy = $sort;
227 }
228 else {
229 $orderBy = trim($sort->orderBy());
230 }
231 }
232
233 $query .= " ORDER BY {$orderBy} ";
234
235 if ($offset || $rowCount) {
236 //Added "||$rowCount" to avoid displaying all records on first page
237 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
238 }
239
240 $dao->query($query);
241
242 $results = array();
243
244 while ($dao->fetch()) {
245 $url = CRM_Utils_System::url('civicrm/contact/view',
246 "reset=1&cid={$dao->contact_id}"
247 );
248 $results[] = array(
249 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
250 'email' => $dao->email,
251 'url' => $dao->url,
252 'date' => CRM_Utils_Date::customFormat($dao->date),
253 );
254 }
255 return $results;
256 }
257}
258