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