3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is used to retrieve and display a range of
20 * contacts that match the given criteria (specifically for
21 * results of advanced search options.
23 class CRM_Mailing_Selector_Event
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
26 * Array of supported links, currently null
30 public static $_links = NULL;
33 * What event type are we browsing?
39 * Should we only count distinct contacts?
42 private $_is_distinct;
45 * Which mailing are we browsing events from?
51 * Do we want events tied to a specific job?
57 * For click-through events, do we only want those from a specific url?
63 * We use desc to remind us what that column is, name is used in the tpl
67 public $_columnHeaders;
72 * @param string $event
73 * The event type (queue/delivered/open...).
74 * @param bool $distinct
75 * Count only distinct contact events?.
77 * ID of the mailing to query.
79 * ID of the job to query. If null, all jobs from $mailing are queried.
81 * If the event type is a click-through, do we want only those from a specific url?.
83 * @return \CRM_Mailing_Selector_Event
85 public function __construct($event, $distinct, $mailing, $job = NULL, $url = NULL) {
86 $this->_event_type
= $event;
87 $this->_is_distinct
= $distinct;
88 $this->_mailing_id
= $mailing;
89 $this->_job_id
= $job;
90 $this->_url_id
= $url;
94 * This method returns the links that are given for each search row.
98 public static function &links() {
103 * Getter for array of the parameters required for creating pager.
106 * @param array $params
108 public function getPagerParams($action, &$params) {
109 $params['csvString'] = NULL;
110 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
111 $params['status'] = ts('%1 %%StatusMessage%%', [1 => $this->eventToTitle()]);
112 $params['buttonTop'] = 'PagerTopButton';
113 $params['buttonBottom'] = 'PagerBottomButton';
117 * Returns the column headers as an array of tuples:
118 * (name, sortName (key to the sort array))
120 * @param string $action
121 * The action being performed.
122 * @param string $output
123 * What should the result set include (web/email/csv).
126 * the column headers that need to be displayed
128 public function &getColumnHeaders($action = NULL, $output = NULL) {
129 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
131 $contact = CRM_Contact_BAO_Contact
::getTableName();
133 $email = CRM_Core_BAO_Email
::getTableName();
135 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
136 if (!isset($this->_columnHeaders
)) {
138 $this->_columnHeaders
= [
140 'name' => ts('Contact'),
141 'sort' => $contact . '.sort_name',
142 'direction' => CRM_Utils_Sort
::ASCENDING
,
145 'name' => ts('Email Address'),
146 'sort' => $email . '.email',
147 'direction' => CRM_Utils_Sort
::DONTCARE
,
151 switch ($this->_event_type
) {
153 $dateSort = $job . '.start_date';
157 $this->_columnHeaders
= [
159 'name' => ts('Internal Contact ID'),
160 'sort' => $contact . '.id',
161 'direction' => CRM_Utils_Sort
::ASCENDING
,
163 ] +
$this->_columnHeaders
;
164 $dateSort = CRM_Mailing_Event_BAO_Delivered
::getTableName() . '.time_stamp';
168 $dateSort = CRM_Mailing_Event_BAO_Opened
::getTableName() . '.time_stamp';
172 $dateSort = CRM_Mailing_Event_BAO_Bounce
::getTableName() . '.time_stamp';
173 $this->_columnHeaders
= array_merge($this->_columnHeaders
,
176 'name' => ts('Bounce Type'),
179 'name' => ts('Bounce Reason'),
186 $dateSort = CRM_Mailing_Event_BAO_Forward
::getTableName() . '.time_stamp';
188 $this->_columnHeaders
= array_merge($this->_columnHeaders
,
191 'name' => ts('Forwarded Email'),
198 $dateSort = CRM_Mailing_Event_BAO_Reply
::getTableName() . '.time_stamp';
202 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe
::getTableName() . '.time_stamp';
203 $this->_columnHeaders
= array_merge($this->_columnHeaders
, [
205 'name' => ts('Unsubscribe'),
211 $dateSort = CRM_Mailing_Event_BAO_Unsubscribe
::getTableName() . '.time_stamp';
212 $this->_columnHeaders
= array_merge($this->_columnHeaders
, [
214 'name' => ts('Opt-Out'),
220 $dateSort = CRM_Mailing_Event_BAO_TrackableURLOpen
::getTableName() . '.time_stamp';
221 $this->_columnHeaders
= array_merge($this->_columnHeaders
, [
232 $this->_columnHeaders
= array_merge($this->_columnHeaders
, [
234 'name' => ts('Date'),
236 'direction' => CRM_Utils_Sort
::DESCENDING
,
240 return $this->_columnHeaders
;
244 * Returns total number of rows for the query.
246 * @param string $action
249 * Total number of rows
251 public function getTotalCount($action) {
252 switch ($this->_event_type
) {
254 $event = new CRM_Mailing_Event_BAO_Queue();
255 $result = $event->getTotalCount($this->_mailing_id
,
261 $event = new CRM_Mailing_Event_BAO_Delivered();
262 $result = $event->getTotalCount($this->_mailing_id
,
269 $event = new CRM_Mailing_Event_BAO_Opened();
270 $result = $event->getTotalCount($this->_mailing_id
,
277 $event = new CRM_Mailing_Event_BAO_Bounce();
278 $result = $event->getTotalCount($this->_mailing_id
,
285 $event = new CRM_Mailing_Event_BAO_Forward();
286 $result = $event->getTotalCount($this->_mailing_id
,
293 $event = new CRM_Mailing_Event_BAO_Reply();
294 $result = $event->getTotalCount($this->_mailing_id
,
301 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
302 $result = $event->getTotalCount($this->_mailing_id
,
309 $event = new CRM_Mailing_Event_BAO_Unsubscribe();
310 $result = $event->getTotalCount($this->_mailing_id
,
318 $event = new CRM_Mailing_Event_BAO_TrackableURLOpen();
319 $result = $event->getTotalCount($this->_mailing_id
,
332 * Returns all the rows in the given offset and rowCount.
334 * @param string $action
335 * The action being performed.
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.
342 * @param string $output
343 * What should the result set include (web/email/csv).
346 * the total number of rows for this action
348 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
349 switch ($this->_event_type
) {
351 $rows = CRM_Mailing_Event_BAO_Queue
::getRows($this->_mailing_id
,
352 $this->_job_id
, $offset, $rowCount, $sort
357 $rows = CRM_Mailing_Event_BAO_Delivered
::getRows($this->_mailing_id
,
358 $this->_job_id
, $this->_is_distinct
,
359 $offset, $rowCount, $sort
364 $rows = CRM_Mailing_Event_BAO_Opened
::getRows($this->_mailing_id
,
365 $this->_job_id
, $this->_is_distinct
,
366 $offset, $rowCount, $sort
371 $rows = CRM_Mailing_Event_BAO_Bounce
::getRows($this->_mailing_id
,
372 $this->_job_id
, $this->_is_distinct
,
373 $offset, $rowCount, $sort
378 $rows = CRM_Mailing_Event_BAO_Forward
::getRows($this->_mailing_id
,
379 $this->_job_id
, $this->_is_distinct
,
380 $offset, $rowCount, $sort
385 $rows = CRM_Mailing_Event_BAO_Reply
::getRows($this->_mailing_id
,
386 $this->_job_id
, $this->_is_distinct
,
387 $offset, $rowCount, $sort
392 $rows = CRM_Mailing_Event_BAO_Unsubscribe
::getRows($this->_mailing_id
,
393 $this->_job_id
, $this->_is_distinct
,
394 $offset, $rowCount, $sort, TRUE
399 $rows = CRM_Mailing_Event_BAO_Unsubscribe
::getRows($this->_mailing_id
,
400 $this->_job_id
, $this->_is_distinct
,
401 $offset, $rowCount, $sort, FALSE
406 $rows = CRM_Mailing_Event_BAO_TrackableURLOpen
::getRows(
407 $this->_mailing_id
, $this->_job_id
,
408 $this->_is_distinct
, $this->_url_id
,
409 $offset, $rowCount, $sort
419 * Name of export file.
421 * @param string $output
424 * @return string|NULL
427 public function getExportFileName($output = 'csv') {
432 * Get the title for the mailing event type.
436 public function eventToTitle() {
437 static $events = NULL;
439 if (empty($events)) {
441 'queue' => ts('Intended Recipients'),
442 'delivered' => ts('Successful Deliveries'),
443 'bounce' => ts('Bounces'),
444 'forward' => ts('Forwards'),
445 'reply' => $this->_is_distinct ?
ts('Unique Replies') : ts('Replies'),
446 'unsubscribe' => ts('Unsubscribe Requests'),
447 'optout' => ts('Opt-out Requests'),
448 'click' => $this->_is_distinct ?
ts('Unique Click-throughs') : ts('Click-throughs'),
449 'opened' => $this->_is_distinct ?
ts('Unique Tracked Opens') : ts('Total Tracked Opens'),
452 return $events[$this->_event_type
];
456 * Get the title of the event.
460 public function getTitle() {
461 return $this->eventToTitle();