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