phpcs - Fix error, "Visibility must be declared on method"
[civicrm-core.git] / CRM / Mailing / Event / BAO / TrackableURLOpen.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Mailing_Event_BAO_TrackableURLOpen extends CRM_Mailing_Event_DAO_TrackableURLOpen {
36
37 /**
38 * Class constructor
39 */
40 public 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_MailingJob::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, $toDate = 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_MailingJob::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($toDate)) {
133 $query .= " AND $click.time_stamp <= $toDate";
134 }
135
136 if (!empty($job_id)) {
137 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
138 }
139
140 if (!empty($url_id)) {
141 $query .= " AND $click.trackable_url_id = " . CRM_Utils_Type::escape($url_id, 'Integer');
142 }
143
144 if ($is_distinct) {
145 $query .= " GROUP BY $queue.id ";
146 }
147
148 // query was missing
149 $dao->query($query);
150
151 if ($dao->fetch()) {
152 return $dao->opened;
153 }
154
155 return NULL;
156 }
157
158 /**
159 * CRM-12814
160 * Get tracked url count for each mailing for a given set of mailing IDs
161 *
162 * @param $mailingIDs
163 *
164 * @return array trackable url count per mailing ID
165 * @access public
166 * @static
167 */
168 public static function getMailingTotalCount($mailingIDs) {
169 $dao = new CRM_Core_DAO();
170 $clickCount = array();
171
172 $click = self::getTableName();
173 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
174 $job = CRM_Mailing_BAO_MailingJob::getTableName();
175 $mailingIDs = implode(',', $mailingIDs);
176
177 $query = "
178 SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened
179 FROM $click
180 INNER JOIN $queue
181 ON $click.event_queue_id = $queue.id
182 INNER JOIN $job
183 ON $queue.job_id = $job.id
184 AND $job.is_test = 0
185 WHERE $job.mailing_id IN ({$mailingIDs})
186 GROUP BY civicrm_mailing_job.mailing_id
187 ";
188
189 $dao->query($query);
190
191 while ( $dao->fetch() ) {
192 $clickCount[$dao->mailingID] = $dao->opened;
193 }
194 return $clickCount;
195 }
196
197 /**
198 * Get tracked url count for each mailing for a given set of mailing IDs
199 *
200 * @param int $mailingIDs IDs of the mailing (comma separated)
201 * @param int $contactID ID of the contact
202 *
203 * @return array Count per mailing ID
204 * @access public
205 * @static
206 */
207 public static function getMailingContactCount($mailingIDs, $contactID) {
208 $dao = new CRM_Core_DAO();
209 $clickCount = array();
210
211 $click = self::getTableName();
212 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
213 $job = CRM_Mailing_BAO_MailingJob::getTableName();
214 $mailingIDs = implode(',', $mailingIDs);
215
216 $query = "
217 SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened
218 FROM $click
219 INNER JOIN $queue
220 ON $click.event_queue_id = $queue.id
221 AND $queue.contact_id = $contactID
222 INNER JOIN $job
223 ON $queue.job_id = $job.id
224 AND $job.is_test = 0
225 WHERE $job.mailing_id IN ({$mailingIDs})
226 GROUP BY civicrm_mailing_job.mailing_id
227 ";
228
229 $dao->query($query);
230
231 while ( $dao->fetch() ) {
232 $clickCount[$dao->mailingID] = $dao->opened;
233 }
234
235 return $clickCount;
236 }
237
238 /**
239 * Get rows for the event browser
240 *
241 * @param int $mailing_id ID of the mailing
242 * @param int $job_id optional ID of the job
243 * @param boolean $is_distinct Group by queue id?
244 * @param int $url_id optional ID of a trackable URL to filter on
245 * @param int $offset Offset
246 * @param int $rowCount Number of rows
247 * @param array $sort sort array
248 * @param int $contact_id optional contact ID
249 *
250 * @return array Result set
251 * @access public
252 * @static
253 */
254 public static function &getRows($mailing_id, $job_id = NULL,
255 $is_distinct = FALSE, $url_id,
256 $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL
257 ) {
258
259 $dao = new CRM_Core_Dao();
260
261 $click = self::getTableName();
262 $url = CRM_Mailing_BAO_TrackableURL::getTableName();
263 $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
264 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
265 $job = CRM_Mailing_BAO_MailingJob::getTableName();
266 $contact = CRM_Contact_BAO_Contact::getTableName();
267 $email = CRM_Core_BAO_Email::getTableName();
268
269 $query = "
270 SELECT $contact.display_name as display_name,
271 $contact.id as contact_id,
272 $email.email as email,
273 $click.time_stamp as date,
274 $url.url as url
275 FROM $contact
276 INNER JOIN $queue
277 ON $queue.contact_id = $contact.id
278 INNER JOIN $email
279 ON $queue.email_id = $email.id
280 INNER JOIN $click
281 ON $click.event_queue_id = $queue.id
282 INNER JOIN $url
283 ON $click.trackable_url_id = $url.id
284 INNER JOIN $job
285 ON $queue.job_id = $job.id
286 INNER JOIN $mailing
287 ON $job.mailing_id = $mailing.id
288 AND $job.is_test = 0
289 WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
290
291 if (!empty($contact_id)) {
292 $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer');
293 }
294
295 if (!empty($job_id)) {
296 $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
297 }
298
299 if (!empty($url_id)) {
300 $query .= " AND $url.id = " . CRM_Utils_Type::escape($url_id, 'Integer');
301 }
302
303 if ($is_distinct) {
304 $query .= " GROUP BY $queue.id ";
305 }
306
307 $orderBy = "sort_name ASC, {$click}.time_stamp DESC";
308 if ($sort) {
309 if (is_string($sort)) {
310 $sort = CRM_Utils_Type::escape($sort, 'String');
311 $orderBy = $sort;
312 }
313 else {
314 $orderBy = trim($sort->orderBy());
315 }
316 }
317
318 $query .= " ORDER BY {$orderBy} ";
319
320 if ($offset || $rowCount) {
321 //Added "||$rowCount" to avoid displaying all records on first page
322 $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
323 }
324
325 $dao->query($query);
326
327 $results = array();
328
329 while ($dao->fetch()) {
330 $url = CRM_Utils_System::url('civicrm/contact/view',
331 "reset=1&cid={$dao->contact_id}"
332 );
333 $results[] = array(
334 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
335 'email' => $dao->email,
336 'url' => $dao->url,
337 'date' => CRM_Utils_Date::customFormat($dao->date),
338 );
339 }
340 return $results;
341 }
342 }