Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class 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()) { | |
25606795 | 82 | // Whoops, error, don't track it. Return the base url. |
6a488035 TO |
83 | |
84 | return CRM_Utils_System::baseURL(); | |
85 | } | |
86 | ||
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'); | |
91 | $open->save(); | |
92 | ||
93 | return $search->url; | |
94 | } | |
95 | ||
96 | /** | |
fe482240 | 97 | * Get row count for the event selector. |
6a488035 | 98 | * |
90c8230e TO |
99 | * @param int $mailing_id |
100 | * ID of the mailing. | |
101 | * @param int $job_id | |
102 | * Optional ID of a job to filter on. | |
103 | * @param bool $is_distinct | |
104 | * Group by queue ID?. | |
105 | * @param int $url_id | |
106 | * Optional ID of a url to filter on. | |
6a488035 | 107 | * |
ad37ac8e | 108 | * @param string $toDate |
109 | * | |
a6c01b45 CW |
110 | * @return int |
111 | * Number of rows in result set | |
6a488035 | 112 | */ |
a3d7e8ee TO |
113 | public static function getTotalCount( |
114 | $mailing_id, $job_id = NULL, | |
7811a84b | 115 | $is_distinct = FALSE, $url_id = NULL, $toDate = NULL |
6a488035 TO |
116 | ) { |
117 | $dao = new CRM_Core_DAO(); | |
118 | ||
353ffa53 TO |
119 | $click = self::getTableName(); |
120 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
6a488035 | 121 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 122 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
123 | |
124 | $query = " | |
125 | SELECT COUNT($click.id) as opened | |
126 | FROM $click | |
127 | INNER JOIN $queue | |
128 | ON $click.event_queue_id = $queue.id | |
129 | INNER JOIN $job | |
130 | ON $queue.job_id = $job.id | |
131 | INNER JOIN $mailing | |
132 | ON $job.mailing_id = $mailing.id | |
133 | AND $job.is_test = 0 | |
134 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
135 | ||
7811a84b | 136 | if (!empty($toDate)) { |
137 | $query .= " AND $click.time_stamp <= $toDate"; | |
138 | } | |
139 | ||
6a488035 TO |
140 | if (!empty($job_id)) { |
141 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
142 | } | |
143 | ||
144 | if (!empty($url_id)) { | |
145 | $query .= " AND $click.trackable_url_id = " . CRM_Utils_Type::escape($url_id, 'Integer'); | |
146 | } | |
147 | ||
148 | if ($is_distinct) { | |
149 | $query .= " GROUP BY $queue.id "; | |
150 | } | |
151 | ||
152 | // query was missing | |
153 | $dao->query($query); | |
154 | ||
155 | if ($dao->fetch()) { | |
156 | return $dao->opened; | |
157 | } | |
158 | ||
159 | return NULL; | |
160 | } | |
161 | ||
de1cbb7c | 162 | /** |
ad37ac8e | 163 | * Get tracked url count for each mailing for a given set of mailing IDs. |
164 | * | |
de1cbb7c | 165 | * CRM-12814 |
de1cbb7c | 166 | * |
ad37ac8e | 167 | * @param array $mailingIDs |
da6b46f4 | 168 | * |
a6c01b45 CW |
169 | * @return array |
170 | * trackable url count per mailing ID | |
de1cbb7c BS |
171 | */ |
172 | public static function getMailingTotalCount($mailingIDs) { | |
173 | $dao = new CRM_Core_DAO(); | |
174 | $clickCount = array(); | |
175 | ||
176 | $click = self::getTableName(); | |
177 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
9da8dc8c | 178 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
de1cbb7c BS |
179 | $mailingIDs = implode(',', $mailingIDs); |
180 | ||
181 | $query = " | |
182 | SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened | |
183 | FROM $click | |
184 | INNER JOIN $queue | |
185 | ON $click.event_queue_id = $queue.id | |
186 | INNER JOIN $job | |
187 | ON $queue.job_id = $job.id | |
188 | AND $job.is_test = 0 | |
189 | WHERE $job.mailing_id IN ({$mailingIDs}) | |
190 | GROUP BY civicrm_mailing_job.mailing_id | |
191 | "; | |
192 | ||
193 | $dao->query($query); | |
194 | ||
481a74f4 | 195 | while ($dao->fetch()) { |
de1cbb7c BS |
196 | $clickCount[$dao->mailingID] = $dao->opened; |
197 | } | |
198 | return $clickCount; | |
199 | } | |
200 | ||
6b62f1bb | 201 | /** |
fe482240 | 202 | * Get tracked url count for each mailing for a given set of mailing IDs. |
6b62f1bb | 203 | * |
90c8230e TO |
204 | * @param int $mailingIDs |
205 | * IDs of the mailing (comma separated). | |
206 | * @param int $contactID | |
207 | * ID of the contact. | |
6b62f1bb | 208 | * |
a6c01b45 CW |
209 | * @return array |
210 | * Count per mailing ID | |
6b62f1bb DG |
211 | */ |
212 | public static function getMailingContactCount($mailingIDs, $contactID) { | |
213 | $dao = new CRM_Core_DAO(); | |
214 | $clickCount = array(); | |
215 | ||
216 | $click = self::getTableName(); | |
217 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
218 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); | |
219 | $mailingIDs = implode(',', $mailingIDs); | |
220 | ||
221 | $query = " | |
222 | SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened | |
223 | FROM $click | |
224 | INNER JOIN $queue | |
225 | ON $click.event_queue_id = $queue.id | |
226 | AND $queue.contact_id = $contactID | |
227 | INNER JOIN $job | |
228 | ON $queue.job_id = $job.id | |
229 | AND $job.is_test = 0 | |
230 | WHERE $job.mailing_id IN ({$mailingIDs}) | |
231 | GROUP BY civicrm_mailing_job.mailing_id | |
232 | "; | |
233 | ||
234 | $dao->query($query); | |
235 | ||
481a74f4 | 236 | while ($dao->fetch()) { |
6b62f1bb DG |
237 | $clickCount[$dao->mailingID] = $dao->opened; |
238 | } | |
239 | ||
240 | return $clickCount; | |
241 | } | |
242 | ||
6a488035 | 243 | /** |
fe482240 | 244 | * Get rows for the event browser. |
6a488035 | 245 | * |
90c8230e TO |
246 | * @param int $mailing_id |
247 | * ID of the mailing. | |
248 | * @param int $job_id | |
249 | * Optional ID of the job. | |
250 | * @param bool $is_distinct | |
251 | * Group by queue id?. | |
252 | * @param int $url_id | |
253 | * Optional ID of a trackable URL to filter on. | |
254 | * @param int $offset | |
255 | * Offset. | |
256 | * @param int $rowCount | |
257 | * Number of rows. | |
258 | * @param array $sort | |
259 | * Sort array. | |
260 | * @param int $contact_id | |
261 | * Optional contact ID. | |
6a488035 | 262 | * |
a6c01b45 CW |
263 | * @return array |
264 | * Result set | |
6a488035 | 265 | */ |
a3d7e8ee TO |
266 | public static function &getRows( |
267 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
268 | $is_distinct = FALSE, $url_id, |
269 | $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL | |
270 | ) { | |
271 | ||
272 | $dao = new CRM_Core_Dao(); | |
273 | ||
353ffa53 TO |
274 | $click = self::getTableName(); |
275 | $url = CRM_Mailing_BAO_TrackableURL::getTableName(); | |
276 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
6a488035 | 277 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
353ffa53 | 278 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 | 279 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
353ffa53 | 280 | $email = CRM_Core_BAO_Email::getTableName(); |
6a488035 TO |
281 | |
282 | $query = " | |
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, | |
287 | $url.url as url | |
288 | FROM $contact | |
289 | INNER JOIN $queue | |
290 | ON $queue.contact_id = $contact.id | |
291 | INNER JOIN $email | |
292 | ON $queue.email_id = $email.id | |
293 | INNER JOIN $click | |
294 | ON $click.event_queue_id = $queue.id | |
295 | INNER JOIN $url | |
296 | ON $click.trackable_url_id = $url.id | |
297 | INNER JOIN $job | |
298 | ON $queue.job_id = $job.id | |
299 | INNER JOIN $mailing | |
300 | ON $job.mailing_id = $mailing.id | |
301 | AND $job.is_test = 0 | |
302 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
303 | ||
304 | if (!empty($contact_id)) { | |
305 | $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer'); | |
306 | } | |
307 | ||
308 | if (!empty($job_id)) { | |
309 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
310 | } | |
311 | ||
312 | if (!empty($url_id)) { | |
313 | $query .= " AND $url.id = " . CRM_Utils_Type::escape($url_id, 'Integer'); | |
314 | } | |
315 | ||
316 | if ($is_distinct) { | |
317 | $query .= " GROUP BY $queue.id "; | |
318 | } | |
319 | ||
320 | $orderBy = "sort_name ASC, {$click}.time_stamp DESC"; | |
321 | if ($sort) { | |
322 | if (is_string($sort)) { | |
21d32567 | 323 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
324 | $orderBy = $sort; |
325 | } | |
326 | else { | |
327 | $orderBy = trim($sort->orderBy()); | |
328 | } | |
329 | } | |
330 | ||
331 | $query .= " ORDER BY {$orderBy} "; | |
332 | ||
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'); | |
336 | } | |
337 | ||
338 | $dao->query($query); | |
339 | ||
340 | $results = array(); | |
341 | ||
342 | while ($dao->fetch()) { | |
343 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
344 | "reset=1&cid={$dao->contact_id}" | |
345 | ); | |
346 | $results[] = array( | |
347 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
348 | 'email' => $dao->email, | |
349 | 'url' => $dao->url, | |
350 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
351 | ); | |
352 | } | |
353 | return $results; | |
354 | } | |
96025800 | 355 | |
6a488035 | 356 | } |