Merge pull request #4931 from davecivicrm/INFRA-132
[civicrm-core.git] / CRM / Mailing / Selector / Event.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Mailing_Selector_Event extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
43
44 /**
100fef9d 45 * Array of supported links, currenly null
6a488035
TO
46 *
47 * @var array
6a488035
TO
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * What event type are we browsing?
6a488035
TO
53 */
54 private $_event;
55
56 /**
100fef9d 57 * Should we only count distinct contacts?
6a488035
TO
58 */
59 private $_is_distinct;
60
61 /**
100fef9d 62 * Which mailing are we browsing events from?
6a488035
TO
63 */
64 private $_mailing_id;
65
66 /**
100fef9d 67 * Do we want events tied to a specific job?
6a488035
TO
68 */
69 private $_job_id;
70
71 /**
100fef9d 72 * For click-through events, do we only want those from a specific url?
6a488035
TO
73 */
74 private $_url_id;
75
76 /**
100fef9d 77 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
78 *
79 * @var array
80 */
81 public $_columnHeaders;
82
83 /**
84 * Class constructor
85 *
90c8230e
TO
86 * @param string $event
87 * The event type (queue/delivered/open...).
88 * @param bool $distinct
89 * Count only distinct contact events?.
90 * @param int $mailing
91 * ID of the mailing to query.
92 * @param int $job
93 * ID of the job to query. If null, all jobs from $mailing are queried.
94 * @param int $url
95 * If the event type is a click-through, do we want only those from a specific url?.
6a488035 96 *
6c8f6e67 97 * @return \CRM_Mailing_Selector_Event
6a488035 98 */
00be9182 99 public function __construct($event, $distinct, $mailing, $job = NULL, $url = NULL) {
353ffa53 100 $this->_event_type = $event;
6a488035 101 $this->_is_distinct = $distinct;
353ffa53
TO
102 $this->_mailing_id = $mailing;
103 $this->_job_id = $job;
104 $this->_url_id = $url;
6a488035 105 }
6a488035
TO
106
107 /**
108 * This method returns the links that are given for each search row.
109 *
110 * @return array
6a488035 111 */
00be9182 112 public static function &links() {
6a488035
TO
113 return self::$_links;
114 }
6a488035
TO
115
116 /**
100fef9d 117 * Getter for array of the parameters required for creating pager.
6a488035 118 *
da6b46f4 119 * @param $action
c490a46a 120 * @param array $params
6a488035 121 */
00be9182 122 public function getPagerParams($action, &$params) {
6a488035 123 $params['csvString'] = NULL;
353ffa53
TO
124 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
125 $params['status'] = ts('%1 %%StatusMessage%%', array(1 => $this->eventToTitle()));
6a488035
TO
126 $params['buttonTop'] = 'PagerTopButton';
127 $params['buttonBottom'] = 'PagerBottomButton';
128 }
6a488035
TO
129
130 /**
100fef9d 131 * Returns the column headers as an array of tuples:
6a488035
TO
132 * (name, sortName (key to the sort array))
133 *
90c8230e
TO
134 * @param string $action
135 * The action being performed.
3f8d2862 136 * @param string $output
90c8230e 137 * What should the result set include (web/email/csv).
6a488035 138 *
a6c01b45
CW
139 * @return array
140 * the column headers that need to be displayed
6a488035 141 */
00be9182 142 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035
TO
143 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
144
145 $contact = CRM_Contact_BAO_Contact::getTableName();
146
147 $email = CRM_Core_BAO_Email::getTableName();
148
9da8dc8c 149 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
150 if (!isset($this->_columnHeaders)) {
151
152 $this->_columnHeaders = array(
153 array(
154 'name' => ts('Contact'),
155 'sort' => $contact . '.sort_name',
156 'direction' => CRM_Utils_Sort::ASCENDING,
157 ),
158 array(
159 'name' => ts('Email Address'),
160 'sort' => $email . '.email',
161 'direction' => CRM_Utils_Sort::DONTCARE,
162 ),
163 );
164
165 switch ($this->_event_type) {
166 case 'queue':
167 $dateSort = $job . '.start_date';
168 break;
169
170 case 'delivered':
171 $dateSort = CRM_Mailing_Event_BAO_Delivered::getTableName() . '.time_stamp';
172 break;
173
174 case 'opened':
175 $dateSort = CRM_Mailing_Event_BAO_Opened::getTableName() . '.time_stamp';
176 break;
177
178 case 'bounce':
179 $dateSort = CRM_Mailing_Event_BAO_Bounce::getTableName() . '.time_stamp';
180 $this->_columnHeaders = array_merge($this->_columnHeaders,
181 array(
182 array(
183 'name' => ts('Bounce Type'),
184 ),
185 array(
186 'name' => ts('Bounce Reason'),
187 ),
188 )
189 );
190 break;
191
192 case 'forward':
193 $dateSort = CRM_Mailing_Event_BAO_Forward::getTableName() . '.time_stamp';
194
195 $this->_columnHeaders = array_merge($this->_columnHeaders,
196 array(
197 array(
198 'name' => ts('Forwarded Email'),
199 ),
200 )
201 );
202 break;
203
204 case 'reply':
205 $dateSort = CRM_Mailing_Event_BAO_Reply::getTableName() . '.time_stamp';
206 break;
207
208 case 'unsubscribe':
209 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp';
210 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
211 array(
353ffa53
TO
212 'name' => ts('Unsubscribe'),
213 ),
214 ));
6a488035
TO
215 break;
216
217 case 'optout':
218 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp';
219 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
220 array(
353ffa53
TO
221 'name' => ts('Opt-Out'),
222 ),
223 ));
6a488035
TO
224 break;
225
226 case 'click':
227 $dateSort = CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName() . '.time_stamp';
228 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
229 array(
353ffa53
TO
230 'name' => ts('URL'),
231 ),
232 ));
6a488035
TO
233 break;
234
235 default:
236 return 0;
237 }
238
239 $this->_columnHeaders = array_merge($this->_columnHeaders,
240 array(
241 array(
242 'name' => ts('Date'),
243 'sort' => $dateSort,
244 'direction' => CRM_Utils_Sort::DESCENDING,
245 ),
246 )
247 );
248 }
249 return $this->_columnHeaders;
250 }
251
252 /**
253 * Returns total number of rows for the query.
254 *
255 * @param
256 *
a6c01b45
CW
257 * @return int
258 * Total number of rows
6a488035 259 */
00be9182 260 public function getTotalCount($action) {
6a488035
TO
261 switch ($this->_event_type) {
262 case 'queue':
263 $event = new CRM_Mailing_Event_BAO_Queue();
264 return $event->getTotalCount($this->_mailing_id,
265 $this->_job_id
266 );
6a488035
TO
267
268 case 'delivered':
269 $event = new CRM_Mailing_Event_BAO_Delivered();
270 return $event->getTotalCount($this->_mailing_id,
271 $this->_job_id,
272 $this->_is_distinct
273 );
6a488035
TO
274
275 case 'opened':
276 $event = new CRM_Mailing_Event_BAO_Opened();
277 return $event->getTotalCount($this->_mailing_id,
278 $this->_job_id,
279 $this->_is_distinct
280 );
6a488035
TO
281
282 case 'bounce':
283 $event = new CRM_Mailing_Event_BAO_Bounce();
284 return $event->getTotalCount($this->_mailing_id,
285 $this->_job_id,
286 $this->_is_distinct
287 );
6a488035
TO
288
289 case 'forward':
290 $event = new CRM_Mailing_Event_BAO_Forward();
291 return $event->getTotalCount($this->_mailing_id,
292 $this->_job_id,
293 $this->_is_distinct
294 );
6a488035
TO
295
296 case 'reply':
297 $event = new CRM_Mailing_Event_BAO_Reply();
298 return $event->getTotalCount($this->_mailing_id,
299 $this->_job_id,
300 $this->_is_distinct
301 );
6a488035
TO
302
303 case 'unsubscribe':
304 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
305 return $event->getTotalCount($this->_mailing_id,
306 $this->_job_id,
307 $this->_is_distinct
308 );
6a488035
TO
309
310 case 'optout':
311 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
312 return $event->getTotalCount($this->_mailing_id,
313 $this->_job_id,
314 $this->_is_distinct,
315 FALSE
316 );
6a488035
TO
317
318 case 'click':
319 $event = new CRM_Mailing_Event_BAO_TrackableURLOpen();
320 return $event->getTotalCount($this->_mailing_id,
321 $this->_job_id,
322 $this->_is_distinct,
323 $this->_url_id
324 );
6a488035
TO
325
326 default:
327 return 0;
328 }
329 }
330
331 /**
100fef9d 332 * Returns all the rows in the given offset and rowCount
6a488035 333 *
3f8d2862 334 * @param string $action
90c8230e
TO
335 * The action being performed.
336 * @param int $offset
337 * The row number to start from.
338 * @param int $rowCount
339 * The number of rows to return.
340 * @param string $sort
341 * The sql string that describes the sort order.
3f8d2862 342 * @param string $output
90c8230e 343 * What should the result set include (web/email/csv).
6a488035 344 *
a6c01b45
CW
345 * @return int
346 * the total number of rows for this action
6a488035 347 */
00be9182 348 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
349 switch ($this->_event_type) {
350 case 'queue':
351 return CRM_Mailing_Event_BAO_Queue::getRows($this->_mailing_id,
352 $this->_job_id, $offset, $rowCount, $sort
353 );
353ffa53 354 break;
6a488035
TO
355
356 case 'delivered':
357 return CRM_Mailing_Event_BAO_Delivered::getRows($this->_mailing_id,
358 $this->_job_id, $this->_is_distinct,
359 $offset, $rowCount, $sort
360 );
6a488035
TO
361
362 case 'opened':
363 return CRM_Mailing_Event_BAO_Opened::getRows($this->_mailing_id,
364 $this->_job_id, $this->_is_distinct,
365 $offset, $rowCount, $sort
366 );
6a488035
TO
367
368 case 'bounce':
369 return CRM_Mailing_Event_BAO_Bounce::getRows($this->_mailing_id,
370 $this->_job_id, $this->_is_distinct,
371 $offset, $rowCount, $sort
372 );
6a488035
TO
373
374 case 'forward':
375 return CRM_Mailing_Event_BAO_Forward::getRows($this->_mailing_id,
376 $this->_job_id, $this->_is_distinct,
377 $offset, $rowCount, $sort
378 );
379
380 case 'reply':
381 return CRM_Mailing_Event_BAO_Reply::getRows($this->_mailing_id,
382 $this->_job_id, $this->_is_distinct,
383 $offset, $rowCount, $sort
384 );
6a488035
TO
385
386 case 'unsubscribe':
387 return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id,
388 $this->_job_id, $this->_is_distinct,
389 $offset, $rowCount, $sort, TRUE
390 );
6a488035
TO
391
392 case 'optout':
393 return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id,
394 $this->_job_id, $this->_is_distinct,
395 $offset, $rowCount, $sort, FALSE
396 );
6a488035
TO
397
398 case 'click':
399 return CRM_Mailing_Event_BAO_TrackableURLOpen::getRows(
400 $this->_mailing_id, $this->_job_id,
401 $this->_is_distinct, $this->_url_id,
402 $offset, $rowCount, $sort
403 );
6a488035
TO
404
405 default:
406 return NULL;
407 }
408 }
409
410 /**
100fef9d 411 * Name of export file.
6a488035 412 *
90c8230e
TO
413 * @param string $output
414 * Type of output.
6a488035 415 *
a6c01b45
CW
416 * @return string
417 * name of the file
6a488035 418 */
6ea503d4
TO
419 public function getExportFileName($output = 'csv') {
420 }
6a488035 421
00be9182 422 public function eventToTitle() {
6a488035
TO
423 static $events = NULL;
424
425 if (empty($events)) {
426 $events = array(
427 'queue' => ts('Intended Recipients'),
428 'delivered' => ts('Successful Deliveries'),
429 'bounce' => ts('Bounces'),
430 'forward' => ts('Forwards'),
431 'reply' => $this->_is_distinct
353ffa53
TO
432 ? ts('Unique Replies')
433 : ts('Replies'),
6a488035
TO
434 'unsubscribe' => ts('Unsubscribe Requests'),
435 'optout' => ts('Opt-out Requests'),
436 'click' => $this->_is_distinct
353ffa53
TO
437 ? ts('Unique Click-throughs')
438 : ts('Click-throughs'),
6a488035 439 'opened' => $this->_is_distinct
353ffa53
TO
440 ? ts('Unique Tracked Opens')
441 : ts('Tracked Opens'),
6a488035
TO
442 );
443 }
444 return $events[$this->_event_type];
445 }
446
00be9182 447 public function getTitle() {
6a488035
TO
448 return $this->eventToTitle();
449 }
450}