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