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_Opened extends CRM_Mailing_Event_DAO_Opened { | |
36 | ||
37 | /** | |
38 | * class constructor | |
39 | */ | |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Register an open event | |
46 | * | |
47 | * @param int $queue_id The Queue Event ID of the recipient | |
48 | * | |
49 | * @return void | |
50 | * @access public | |
51 | * @static | |
52 | */ | |
53 | public static function open($queue_id) { | |
54 | /* First make sure there's a matching queue event */ | |
55 | ||
56 | $success = FALSE; | |
57 | ||
58 | $q = new CRM_Mailing_Event_BAO_Queue(); | |
59 | $q->id = $queue_id; | |
60 | if ($q->find(TRUE)) { | |
61 | $oe = new CRM_Mailing_Event_BAO_Opened(); | |
62 | $oe->event_queue_id = $queue_id; | |
63 | $oe->time_stamp = date('YmdHis'); | |
64 | $oe->save(); | |
65 | $success = TRUE; | |
66 | } | |
67 | ||
68 | return $success; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Get row count for the event selector | |
73 | * | |
74 | * @param int $mailing_id ID of the mailing | |
75 | * @param int $job_id Optional ID of a job to filter on | |
76 | * @param boolean $is_distinct Group by queue ID? | |
77 | * | |
78 | * @return int Number of rows in result set | |
79 | * @access public | |
80 | * @static | |
81 | */ | |
82 | public static function getTotalCount($mailing_id, | |
83 | $job_id = NULL, | |
84 | $is_distinct = FALSE | |
85 | ) { | |
86 | $dao = new CRM_Core_DAO(); | |
87 | ||
88 | $open = self::getTableName(); | |
89 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
90 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 91 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
92 | |
93 | $query = " | |
94 | SELECT COUNT($open.id) as opened | |
95 | FROM $open | |
96 | INNER JOIN $queue | |
97 | ON $open.event_queue_id = $queue.id | |
98 | INNER JOIN $job | |
99 | ON $queue.job_id = $job.id | |
100 | INNER JOIN $mailing | |
101 | ON $job.mailing_id = $mailing.id | |
102 | AND $job.is_test = 0 | |
103 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
104 | ||
105 | if (!empty($job_id)) { | |
106 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
107 | } | |
108 | ||
109 | if ($is_distinct) { | |
110 | $query .= " GROUP BY $queue.id "; | |
111 | } | |
112 | ||
113 | $dao->query($query); | |
114 | $dao->fetch(); | |
115 | if ($is_distinct) { | |
116 | return $dao->N; | |
117 | } | |
118 | else { | |
119 | return $dao->opened ? $dao->opened : 0; | |
120 | } | |
121 | } | |
122 | ||
de1cbb7c BS |
123 | /** |
124 | * CRM-12814 | |
125 | * Get opened count for each mailing for a given set of mailing IDs | |
126 | * | |
dd244018 EM |
127 | * @param $mailingIDs |
128 | * | |
129 | * @internal param int $contactID ID of the mailing | |
de1cbb7c BS |
130 | * |
131 | * @return array Opened count per mailing ID | |
132 | * @access public | |
133 | * @static | |
134 | */ | |
135 | public static function getMailingTotalCount($mailingIDs) { | |
136 | $dao = new CRM_Core_DAO(); | |
137 | $openedCount = array(); | |
138 | ||
139 | $open = self::getTableName(); | |
140 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
9da8dc8c | 141 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
de1cbb7c BS |
142 | $mailingIDs = implode(',', $mailingIDs); |
143 | ||
144 | $query = " | |
145 | SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened | |
146 | FROM $open | |
147 | INNER JOIN $queue | |
148 | ON $open.event_queue_id = $queue.id | |
149 | INNER JOIN $job | |
150 | ON $queue.job_id = $job.id | |
151 | AND $job.is_test = 0 | |
152 | WHERE $job.mailing_id IN ({$mailingIDs}) | |
153 | GROUP BY civicrm_mailing_job.mailing_id | |
154 | "; | |
155 | ||
156 | $dao->query($query); | |
157 | ||
158 | while ( $dao->fetch() ) { | |
159 | $openedCount[$dao->mailingID] = $dao->opened; | |
160 | } | |
161 | return $openedCount; | |
162 | } | |
163 | ||
6b62f1bb DG |
164 | /** |
165 | * Get opened count for each mailing for a given set of mailing IDs and a specific contact | |
166 | * | |
167 | * @param int $mailingIDs IDs of the mailing (comma separated) | |
168 | * @param int $contactID ID of the contact | |
169 | * | |
170 | * @return array Count per mailing ID | |
171 | * @access public | |
172 | * @static | |
173 | */ | |
174 | public static function getMailingContactCount($mailingIDs, $contactID) { | |
175 | $dao = new CRM_Core_DAO(); | |
176 | $openedCount = array(); | |
177 | ||
178 | $open = self::getTableName(); | |
179 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
180 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); | |
181 | $mailingIDs = implode(',', $mailingIDs); | |
182 | ||
183 | $query = " | |
184 | SELECT $job.mailing_id as mailingID, COUNT($open.id) as opened | |
185 | FROM $open | |
186 | INNER JOIN $queue | |
187 | ON $open.event_queue_id = $queue.id | |
188 | AND $queue.contact_id = $contactID | |
189 | INNER JOIN $job | |
190 | ON $queue.job_id = $job.id | |
191 | AND $job.is_test = 0 | |
192 | WHERE $job.mailing_id IN ({$mailingIDs}) | |
193 | GROUP BY civicrm_mailing_job.mailing_id | |
194 | "; | |
195 | ||
196 | $dao->query($query); | |
197 | ||
198 | while ( $dao->fetch() ) { | |
199 | $openedCount[$dao->mailingID] = $dao->opened; | |
200 | } | |
201 | ||
202 | return $openedCount; | |
203 | } | |
204 | ||
6a488035 TO |
205 | /** |
206 | * Get rows for the event browser | |
207 | * | |
da6b46f4 EM |
208 | * @param int $mailing_id ID of the mailing |
209 | * @param int $job_id optional ID of the job | |
210 | * @param boolean $is_distinct Group by queue id? | |
211 | * @param int $offset Offset | |
212 | * @param int $rowCount Number of rows | |
213 | * @param array $sort sort array | |
214 | * | |
215 | * @param null $contact_id | |
6a488035 TO |
216 | * |
217 | * @return array Result set | |
218 | * @access public | |
219 | * @static | |
220 | */ | |
221 | public static function &getRows($mailing_id, $job_id = NULL, | |
222 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $contact_id= NULL | |
223 | ) { | |
224 | ||
225 | $dao = new CRM_Core_Dao(); | |
226 | ||
227 | $open = self::getTableName(); | |
228 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
229 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 230 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
231 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
232 | $email = CRM_Core_BAO_Email::getTableName(); | |
233 | ||
234 | $query = " | |
235 | SELECT $contact.display_name as display_name, | |
236 | $contact.id as contact_id, | |
237 | $email.email as email, | |
238 | $open.time_stamp as date | |
239 | FROM $contact | |
240 | INNER JOIN $queue | |
241 | ON $queue.contact_id = $contact.id | |
242 | INNER JOIN $email | |
243 | ON $queue.email_id = $email.id | |
244 | INNER JOIN $open | |
245 | ON $open.event_queue_id = $queue.id | |
246 | INNER JOIN $job | |
247 | ON $queue.job_id = $job.id | |
248 | INNER JOIN $mailing | |
249 | ON $job.mailing_id = $mailing.id | |
250 | AND $job.is_test = 0 | |
251 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
252 | ||
253 | if (!empty($job_id)) { | |
254 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
255 | } | |
03e04002 | 256 | |
6a488035 TO |
257 | if (!empty($contact_id)) { |
258 | $query .= " AND $contact.id = " . CRM_Utils_Type::escape($contact_id, 'Integer'); | |
259 | } | |
03e04002 | 260 | |
6a488035 TO |
261 | if ($is_distinct) { |
262 | $query .= " GROUP BY $queue.id "; | |
263 | } | |
264 | ||
265 | $orderBy = "sort_name ASC, {$open}.time_stamp DESC"; | |
266 | if ($sort) { | |
267 | if (is_string($sort)) { | |
21d32567 | 268 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
269 | $orderBy = $sort; |
270 | } | |
271 | else { | |
272 | $orderBy = trim($sort->orderBy()); | |
273 | } | |
274 | } | |
275 | ||
276 | $query .= " ORDER BY {$orderBy} "; | |
277 | ||
278 | if ($offset || $rowCount) { | |
279 | //Added "||$rowCount" to avoid displaying all records on first page | |
280 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
281 | } | |
282 | ||
283 | $dao->query($query); | |
284 | ||
285 | $results = array(); | |
286 | ||
287 | while ($dao->fetch()) { | |
288 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
289 | "reset=1&cid={$dao->contact_id}" | |
290 | ); | |
291 | $results[] = array( | |
292 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
293 | 'email' => $dao->email, | |
294 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
295 | ); | |
296 | } | |
297 | return $results; | |
298 | } | |
299 | } | |
300 |