comment fixes for Report/Page
[civicrm-core.git] / CRM / Mailing / Selector / Event.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 /**
b44e3f84 45 * Array of supported links, currently 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 /**
fe482240 84 * Class constructor.
6a488035 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(
b812c745 153 'sort_name' => array(
6a488035
TO
154 'name' => ts('Contact'),
155 'sort' => $contact . '.sort_name',
156 'direction' => CRM_Utils_Sort::ASCENDING,
157 ),
b812c745 158 'email' => array(
6a488035
TO
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':
388ffbf6 171 $this->_columnHeaders = array(
172 'contact_id' => array(
b812c745 173 'name' => ts('Internal Contact ID'),
174 'sort' => $contact . '.id',
175 'direction' => CRM_Utils_Sort::ASCENDING,
176 ),
177 ) + $this->_columnHeaders;
6a488035
TO
178 $dateSort = CRM_Mailing_Event_BAO_Delivered::getTableName() . '.time_stamp';
179 break;
180
181 case 'opened':
182 $dateSort = CRM_Mailing_Event_BAO_Opened::getTableName() . '.time_stamp';
183 break;
184
185 case 'bounce':
186 $dateSort = CRM_Mailing_Event_BAO_Bounce::getTableName() . '.time_stamp';
187 $this->_columnHeaders = array_merge($this->_columnHeaders,
188 array(
189 array(
190 'name' => ts('Bounce Type'),
191 ),
192 array(
193 'name' => ts('Bounce Reason'),
194 ),
195 )
196 );
197 break;
198
199 case 'forward':
200 $dateSort = CRM_Mailing_Event_BAO_Forward::getTableName() . '.time_stamp';
201
202 $this->_columnHeaders = array_merge($this->_columnHeaders,
203 array(
204 array(
205 'name' => ts('Forwarded Email'),
206 ),
207 )
208 );
209 break;
210
211 case 'reply':
212 $dateSort = CRM_Mailing_Event_BAO_Reply::getTableName() . '.time_stamp';
213 break;
214
215 case 'unsubscribe':
216 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp';
217 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
218 array(
353ffa53
TO
219 'name' => ts('Unsubscribe'),
220 ),
221 ));
6a488035
TO
222 break;
223
224 case 'optout':
225 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp';
226 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
227 array(
353ffa53
TO
228 'name' => ts('Opt-Out'),
229 ),
230 ));
6a488035
TO
231 break;
232
233 case 'click':
234 $dateSort = CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName() . '.time_stamp';
235 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
236 array(
353ffa53
TO
237 'name' => ts('URL'),
238 ),
239 ));
6a488035
TO
240 break;
241
242 default:
243 return 0;
244 }
245
388ffbf6 246 $this->_columnHeaders = array_merge($this->_columnHeaders, array(
247 'date' => array(
248 'name' => ts('Date'),
249 'sort' => $dateSort,
250 'direction' => CRM_Utils_Sort::DESCENDING,
30e4662b 251 ),
252 ));
6a488035
TO
253 }
254 return $this->_columnHeaders;
255 }
256
257 /**
258 * Returns total number of rows for the query.
259 *
260 * @param
261 *
a6c01b45
CW
262 * @return int
263 * Total number of rows
6a488035 264 */
00be9182 265 public function getTotalCount($action) {
6a488035
TO
266 switch ($this->_event_type) {
267 case 'queue':
268 $event = new CRM_Mailing_Event_BAO_Queue();
795492f3 269 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
270 $this->_job_id
271 );
795492f3 272 return $result;
6a488035
TO
273
274 case 'delivered':
275 $event = new CRM_Mailing_Event_BAO_Delivered();
795492f3 276 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
277 $this->_job_id,
278 $this->_is_distinct
279 );
795492f3 280 return $result;
6a488035
TO
281
282 case 'opened':
283 $event = new CRM_Mailing_Event_BAO_Opened();
795492f3 284 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
285 $this->_job_id,
286 $this->_is_distinct
287 );
795492f3 288 return $result;
6a488035
TO
289
290 case 'bounce':
291 $event = new CRM_Mailing_Event_BAO_Bounce();
795492f3 292 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
293 $this->_job_id,
294 $this->_is_distinct
295 );
795492f3 296 return $result;
6a488035
TO
297
298 case 'forward':
299 $event = new CRM_Mailing_Event_BAO_Forward();
795492f3 300 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
301 $this->_job_id,
302 $this->_is_distinct
303 );
795492f3 304 return $result;
6a488035
TO
305
306 case 'reply':
307 $event = new CRM_Mailing_Event_BAO_Reply();
795492f3 308 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
309 $this->_job_id,
310 $this->_is_distinct
311 );
795492f3 312 return $result;
6a488035
TO
313
314 case 'unsubscribe':
315 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
795492f3 316 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
317 $this->_job_id,
318 $this->_is_distinct
319 );
795492f3 320 return $result;
6a488035
TO
321
322 case 'optout':
323 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
795492f3 324 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
325 $this->_job_id,
326 $this->_is_distinct,
327 FALSE
328 );
795492f3 329 return $result;
6a488035
TO
330
331 case 'click':
332 $event = new CRM_Mailing_Event_BAO_TrackableURLOpen();
795492f3 333 $result = $event->getTotalCount($this->_mailing_id,
6a488035
TO
334 $this->_job_id,
335 $this->_is_distinct,
336 $this->_url_id
337 );
795492f3 338 return $result;
6a488035
TO
339
340 default:
341 return 0;
342 }
343 }
344
345 /**
fe482240 346 * Returns all the rows in the given offset and rowCount.
6a488035 347 *
3f8d2862 348 * @param string $action
90c8230e
TO
349 * The action being performed.
350 * @param int $offset
351 * The row number to start from.
352 * @param int $rowCount
353 * The number of rows to return.
354 * @param string $sort
355 * The sql string that describes the sort order.
3f8d2862 356 * @param string $output
90c8230e 357 * What should the result set include (web/email/csv).
6a488035 358 *
a6c01b45
CW
359 * @return int
360 * the total number of rows for this action
6a488035 361 */
00be9182 362 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
363 switch ($this->_event_type) {
364 case 'queue':
795492f3 365 $rows = CRM_Mailing_Event_BAO_Queue::getRows($this->_mailing_id,
6a488035
TO
366 $this->_job_id, $offset, $rowCount, $sort
367 );
795492f3 368 return $rows;
6a488035
TO
369
370 case 'delivered':
795492f3 371 $rows = CRM_Mailing_Event_BAO_Delivered::getRows($this->_mailing_id,
6a488035
TO
372 $this->_job_id, $this->_is_distinct,
373 $offset, $rowCount, $sort
374 );
795492f3 375 return $rows;
6a488035
TO
376
377 case 'opened':
795492f3 378 $rows = CRM_Mailing_Event_BAO_Opened::getRows($this->_mailing_id,
6a488035
TO
379 $this->_job_id, $this->_is_distinct,
380 $offset, $rowCount, $sort
381 );
795492f3 382 return $rows;
6a488035
TO
383
384 case 'bounce':
795492f3 385 $rows = CRM_Mailing_Event_BAO_Bounce::getRows($this->_mailing_id,
6a488035
TO
386 $this->_job_id, $this->_is_distinct,
387 $offset, $rowCount, $sort
388 );
795492f3 389 return $rows;
6a488035
TO
390
391 case 'forward':
795492f3 392 $rows = CRM_Mailing_Event_BAO_Forward::getRows($this->_mailing_id,
6a488035
TO
393 $this->_job_id, $this->_is_distinct,
394 $offset, $rowCount, $sort
395 );
795492f3 396 return $rows;
6a488035
TO
397
398 case 'reply':
795492f3 399 $rows = CRM_Mailing_Event_BAO_Reply::getRows($this->_mailing_id,
6a488035
TO
400 $this->_job_id, $this->_is_distinct,
401 $offset, $rowCount, $sort
402 );
795492f3 403 return $rows;
6a488035
TO
404
405 case 'unsubscribe':
795492f3 406 $rows = CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id,
6a488035
TO
407 $this->_job_id, $this->_is_distinct,
408 $offset, $rowCount, $sort, TRUE
409 );
795492f3 410 return $rows;
6a488035
TO
411
412 case 'optout':
795492f3 413 $rows = CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id,
6a488035
TO
414 $this->_job_id, $this->_is_distinct,
415 $offset, $rowCount, $sort, FALSE
416 );
795492f3 417 return $rows;
6a488035
TO
418
419 case 'click':
795492f3 420 $rows = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows(
6a488035
TO
421 $this->_mailing_id, $this->_job_id,
422 $this->_is_distinct, $this->_url_id,
423 $offset, $rowCount, $sort
424 );
795492f3 425 return $rows;
6a488035
TO
426
427 default:
428 return NULL;
429 }
430 }
431
432 /**
100fef9d 433 * Name of export file.
6a488035 434 *
90c8230e
TO
435 * @param string $output
436 * Type of output.
6a488035 437 *
795492f3 438 * @return string|NULL
a6c01b45 439 * name of the file
6a488035 440 */
6ea503d4 441 public function getExportFileName($output = 'csv') {
795492f3 442 return NULL;
6ea503d4 443 }
6a488035 444
00be9182 445 public function eventToTitle() {
6a488035
TO
446 static $events = NULL;
447
448 if (empty($events)) {
449 $events = array(
450 'queue' => ts('Intended Recipients'),
451 'delivered' => ts('Successful Deliveries'),
452 'bounce' => ts('Bounces'),
453 'forward' => ts('Forwards'),
795492f3 454 'reply' => $this->_is_distinct ? ts('Unique Replies') : ts('Replies'),
6a488035
TO
455 'unsubscribe' => ts('Unsubscribe Requests'),
456 'optout' => ts('Opt-out Requests'),
795492f3 457 'click' => $this->_is_distinct ? ts('Unique Click-throughs') : ts('Click-throughs'),
aa6b3363 458 'opened' => $this->_is_distinct ? ts('Unique Tracked Opens') : ts('Total Tracked Opens'),
6a488035
TO
459 );
460 }
461 return $events[$this->_event_type];
462 }
463
00be9182 464 public function getTitle() {
6a488035
TO
465 return $this->eventToTitle();
466 }
96025800 467
6a488035 468}