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