Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class 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 | ||
6b62f1bb DG |
193 | /** |
194 | * Get tracked url count for each mailing for a given set of mailing IDs | |
195 | * | |
196 | * @param int $mailingIDs IDs of the mailing (comma separated) | |
197 | * @param int $contactID ID of the contact | |
198 | * | |
199 | * @return array Count per mailing ID | |
200 | * @access public | |
201 | * @static | |
202 | */ | |
203 | public static function getMailingContactCount($mailingIDs, $contactID) { | |
204 | $dao = new CRM_Core_DAO(); | |
205 | $clickCount = array(); | |
206 | ||
207 | $click = self::getTableName(); | |
208 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
209 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); | |
210 | $mailingIDs = implode(',', $mailingIDs); | |
211 | ||
212 | $query = " | |
213 | SELECT $job.mailing_id as mailingID, COUNT($click.id) as opened | |
214 | FROM $click | |
215 | INNER JOIN $queue | |
216 | ON $click.event_queue_id = $queue.id | |
217 | AND $queue.contact_id = $contactID | |
218 | INNER JOIN $job | |
219 | ON $queue.job_id = $job.id | |
220 | AND $job.is_test = 0 | |
221 | WHERE $job.mailing_id IN ({$mailingIDs}) | |
222 | GROUP BY civicrm_mailing_job.mailing_id | |
223 | "; | |
224 | ||
225 | $dao->query($query); | |
226 | ||
227 | while ( $dao->fetch() ) { | |
228 | $clickCount[$dao->mailingID] = $dao->opened; | |
229 | } | |
230 | ||
231 | return $clickCount; | |
232 | } | |
233 | ||
6a488035 TO |
234 | /** |
235 | * Get rows for the event browser | |
236 | * | |
237 | * @param int $mailing_id ID of the mailing | |
238 | * @param int $job_id optional ID of the job | |
239 | * @param boolean $is_distinct Group by queue id? | |
240 | * @param int $url_id optional ID of a trackable URL to filter on | |
241 | * @param int $offset Offset | |
242 | * @param int $rowCount Number of rows | |
243 | * @param array $sort sort array | |
244 | * @param int $contact_id optional contact ID | |
245 | * | |
246 | * @return array Result set | |
247 | * @access public | |
248 | * @static | |
249 | */ | |
250 | public static function &getRows($mailing_id, $job_id = NULL, | |
251 | $is_distinct = FALSE, $url_id, | |
252 | $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id = NULL | |
253 | ) { | |
254 | ||
255 | $dao = new CRM_Core_Dao(); | |
256 | ||
257 | $click = self::getTableName(); | |
258 | $url = CRM_Mailing_BAO_TrackableURL::getTableName(); | |
259 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
260 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 261 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
262 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
263 | $email = CRM_Core_BAO_Email::getTableName(); | |
264 | ||
265 | $query = " | |
266 | SELECT $contact.display_name as display_name, | |
267 | $contact.id as contact_id, | |
268 | $email.email as email, | |
269 | $click.time_stamp as date, | |
270 | $url.url as url | |
271 | FROM $contact | |
272 | INNER JOIN $queue | |
273 | ON $queue.contact_id = $contact.id | |
274 | INNER JOIN $email | |
275 | ON $queue.email_id = $email.id | |
276 | INNER JOIN $click | |
277 | ON $click.event_queue_id = $queue.id | |
278 | INNER JOIN $url | |
279 | ON $click.trackable_url_id = $url.id | |
280 | INNER JOIN $job | |
281 | ON $queue.job_id = $job.id | |
282 | INNER JOIN $mailing | |
283 | ON $job.mailing_id = $mailing.id | |
284 | AND $job.is_test = 0 | |
285 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
286 | ||
287 | if (!empty($contact_id)) { | |
288 | $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer'); | |
289 | } | |
290 | ||
291 | if (!empty($job_id)) { | |
292 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
293 | } | |
294 | ||
295 | if (!empty($url_id)) { | |
296 | $query .= " AND $url.id = " . CRM_Utils_Type::escape($url_id, 'Integer'); | |
297 | } | |
298 | ||
299 | if ($is_distinct) { | |
300 | $query .= " GROUP BY $queue.id "; | |
301 | } | |
302 | ||
303 | $orderBy = "sort_name ASC, {$click}.time_stamp DESC"; | |
304 | if ($sort) { | |
305 | if (is_string($sort)) { | |
21d32567 | 306 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
307 | $orderBy = $sort; |
308 | } | |
309 | else { | |
310 | $orderBy = trim($sort->orderBy()); | |
311 | } | |
312 | } | |
313 | ||
314 | $query .= " ORDER BY {$orderBy} "; | |
315 | ||
316 | if ($offset || $rowCount) { | |
317 | //Added "||$rowCount" to avoid displaying all records on first page | |
318 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
319 | } | |
320 | ||
321 | $dao->query($query); | |
322 | ||
323 | $results = array(); | |
324 | ||
325 | while ($dao->fetch()) { | |
326 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
327 | "reset=1&cid={$dao->contact_id}" | |
328 | ); | |
329 | $results[] = array( | |
330 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
331 | 'email' => $dao->email, | |
332 | 'url' => $dao->url, | |
333 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
334 | ); | |
335 | } | |
336 | return $results; | |
337 | } | |
338 | } | |
339 |