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
17 class CRM_Report_Form_Event_ParticipantListing
extends CRM_Report_Form
{
19 protected $_summary = NULL;
21 protected $_contribField = FALSE;
22 protected $_groupFilter = TRUE;
23 protected $_tagFilter = TRUE;
24 protected $_balance = FALSE;
27 protected $_customGroupExtends = array(
34 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
39 public function __construct() {
40 $this->_autoIncludeIndexedFieldsAsOrderBys
= 1;
42 $this->_columns
= array(
43 'civicrm_contact' => array(
44 'dao' => 'CRM_Contact_DAO_Contact',
45 'fields' => array_merge(array(
46 // CRM-17115 - to avoid changing report output at this stage re-instate
47 // old field name for sort name
48 'sort_name_linked' => array(
49 'title' => ts('Participant Name'),
52 'dbAlias' => 'contact_civireport.sort_name',
55 $this->getBasicContactFields(),
57 'age_at_event' => array(
58 'title' => ts('Age at Event'),
59 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, event_civireport.start_date)',
63 'grouping' => 'contact-fields',
66 'title' => ts('Last Name, First Name'),
68 'default_weight' => '0',
69 'default_order' => 'ASC',
71 'first_name' => array(
72 'name' => 'first_name',
73 'title' => ts('First Name'),
76 'name' => 'gender_id',
77 'title' => ts('Gender'),
79 'birth_date' => array(
80 'name' => 'birth_date',
81 'title' => ts('Birth Date'),
83 'age_at_event' => array(
84 'name' => 'age_at_event',
85 'title' => ts('Age at Event'),
87 'contact_type' => array(
88 'title' => ts('Contact Type'),
90 'contact_sub_type' => array(
91 'title' => ts('Contact Subtype'),
94 'filters' => CRM_Report_Form
::getBasicContactFilters(),
96 'civicrm_email' => array(
97 'dao' => 'CRM_Core_DAO_Email',
100 'title' => ts('Email'),
104 'grouping' => 'contact-fields',
107 'title' => ts('Participant E-mail'),
108 'operator' => 'like',
113 $this->_columns +
= $this->getAddressColumns();
114 $this->_columns +
= array(
115 'civicrm_participant' => array(
116 'dao' => 'CRM_Event_DAO_Participant',
118 'participant_id' => array('title' => ts('Participant ID')),
119 'participant_record' => array(
121 'no_display' => TRUE,
126 'type' => CRM_Utils_Type
::T_STRING
,
128 'status_id' => array(
129 'title' => ts('Status'),
133 'title' => ts('Role'),
136 'fee_currency' => array(
138 'no_display' => TRUE,
140 'registered_by_id' => array(
141 'title' => ts('Registered by Participant ID'),
143 'registered_by_name' => array(
144 'title' => ts('Registered by Participant Name'),
145 'name' => 'registered_by_id',
148 'title' => ts('Source'),
150 'participant_fee_level' => NULL,
151 'participant_fee_amount' => array('title' => ts('Participant Fee')),
152 'participant_register_date' => array('title' => ts('Registration Date')),
153 'total_paid' => array(
154 'title' => ts('Total Paid'),
155 'dbAlias' => 'IFNULL(SUM(ft.total_amount), 0)',
159 'title' => ts('Balance'),
160 'dbAlias' => 'participant_civireport.fee_amount - IFNULL(SUM(ft.total_amount), 0)',
164 'grouping' => 'event-fields',
167 'name' => 'event_id',
168 'title' => ts('Event'),
169 'operatorType' => CRM_Report_Form
::OP_ENTITYREF
,
170 'type' => CRM_Utils_Type
::T_INT
,
171 'attributes' => array(
173 'select' => array('minimumInputLength' => 0),
177 'name' => 'status_id',
178 'title' => ts('Participant Status'),
179 'type' => CRM_Utils_Type
::T_INT
,
180 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
181 'options' => CRM_Event_PseudoConstant
::participantStatus(NULL, NULL, 'label'),
185 'title' => ts('Participant Role'),
186 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
187 'options' => CRM_Event_PseudoConstant
::participantRole(),
189 'participant_register_date' => array(
190 'title' => ts('Registration Date'),
191 'operatorType' => CRM_Report_Form
::OP_DATE
,
193 'fee_currency' => array(
194 'title' => ts('Fee Currency'),
195 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
196 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
198 'type' => CRM_Utils_Type
::T_STRING
,
200 'registered_by_id' => array(
201 'title' => ts('Registered by Participant ID'),
202 'type' => CRM_Utils_Type
::T_STRING
,
203 'operator' => 'like',
206 'title' => ts('Source'),
207 'type' => CRM_Utils_Type
::T_STRING
,
208 'operator' => 'like',
211 'order_bys' => array(
212 'participant_register_date' => array(
213 'title' => ts('Registration Date'),
214 'default_weight' => '1',
215 'default_order' => 'ASC',
218 'title' => ts('Event'),
219 'default_weight' => '1',
220 'default_order' => 'ASC',
224 'civicrm_phone' => array(
225 'dao' => 'CRM_Core_DAO_Phone',
228 'title' => ts('Phone'),
233 'grouping' => 'contact-fields',
235 'civicrm_event' => array(
236 'dao' => 'CRM_Event_DAO_Event',
238 'event_type_id' => array(
239 'title' => ts('Event Type'),
241 'event_start_date' => array(
242 'title' => ts('Event Start Date'),
244 'event_end_date' => array(
245 'title' => ts('Event End Date'),
248 'grouping' => 'event-fields',
251 'name' => 'event_type_id',
252 'title' => ts('Event Type'),
253 'type' => CRM_Utils_Type
::T_INT
,
254 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
255 'options' => CRM_Core_OptionGroup
::values('event_type'),
257 'event_start_date' => array(
258 'title' => ts('Event Start Date'),
259 'operatorType' => CRM_Report_Form
::OP_DATE
,
261 'event_end_date' => array(
262 'title' => ts('Event End Date'),
263 'operatorType' => CRM_Report_Form
::OP_DATE
,
266 'order_bys' => array(
267 'event_type_id' => array(
268 'title' => ts('Event Type'),
269 'default_weight' => '2',
270 'default_order' => 'ASC',
272 'event_start_date' => array(
273 'title' => ts('Event Start Date'),
277 'civicrm_note' => array(
278 'dao' => 'CRM_Core_DAO_Note',
280 'participant_note' => array(
282 'title' => ts('Participant Note'),
286 'civicrm_contribution' => array(
287 'dao' => 'CRM_Contribute_DAO_Contribution',
289 'contribution_id' => array(
291 'no_display' => TRUE,
293 'csv_display' => TRUE,
294 'title' => ts('Contribution ID'),
296 'financial_type_id' => array('title' => ts('Financial Type')),
297 'receive_date' => array('title' => ts('Payment Date')),
298 'contribution_status_id' => array('title' => ts('Contribution Status')),
299 'payment_instrument_id' => array('title' => ts('Payment Type')),
300 'contribution_source' => array(
302 'title' => ts('Contribution Source'),
306 'no_display' => TRUE,
309 'fee_amount' => array('title' => ts('Transaction Fee')),
310 'net_amount' => NULL,
312 'grouping' => 'contrib-fields',
314 'receive_date' => array(
315 'title' => ts('Payment Date'),
316 'operatorType' => CRM_Report_Form
::OP_DATE
,
318 'financial_type_id' => array(
319 'title' => ts('Financial Type'),
320 'type' => CRM_Utils_Type
::T_INT
,
321 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
322 'options' => CRM_Contribute_PseudoConstant
::financialType(),
325 'title' => ts('Contribution Currency'),
326 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
327 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
329 'type' => CRM_Utils_Type
::T_STRING
,
331 'payment_instrument_id' => array(
332 'title' => ts('Payment Type'),
333 'type' => CRM_Utils_Type
::T_INT
,
334 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
335 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
337 'contribution_status_id' => array(
338 'title' => ts('Contribution Status'),
339 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
340 'options' => CRM_Contribute_BAO_Contribution
::buildOptions('contribution_status_id', 'search'),
345 'civicrm_line_item' => array(
346 'dao' => 'CRM_Price_DAO_LineItem',
347 'grouping' => 'priceset-fields',
349 'price_field_value_id' => array(
350 'name' => 'price_field_value_id',
351 'title' => ts('Fee Level'),
352 'type' => CRM_Utils_Type
::T_INT
,
353 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
354 'options' => $this->getPriceLevels(),
360 $this->_options
= array(
361 'blank_column_begin' => array(
362 'title' => ts('Blank column at the Begining'),
363 'type' => 'checkbox',
365 'blank_column_end' => array(
366 'title' => ts('Blank column at the End'),
369 '' => ts('-select-'),
377 // CRM-17115 avoid duplication of sort_name - would be better to standardise name
378 // & behaviour across reports but trying for no change at this point.
379 $this->_columns
['civicrm_contact']['fields']['sort_name']['no_display'] = TRUE;
381 // If we have campaigns enabled, add those elements to both the fields, filters and sorting
382 $this->addCampaignFields('civicrm_participant', FALSE, TRUE);
384 $this->_currencyColumn
= 'civicrm_participant_fee_currency';
385 parent
::__construct();
389 * Searches database for priceset values.
393 public function getPriceLevels() {
395 SELECT CONCAT(cv.label, ' (', ps.title, ' - ', cf.label , ')') label, cv.id
396 FROM civicrm_price_field_value cv
397 LEFT JOIN civicrm_price_field cf
398 ON cv.price_field_id = cf.id
399 LEFT JOIN civicrm_price_set_entity ce
400 ON ce.price_set_id = cf.price_set_id
401 LEFT JOIN civicrm_price_set ps
402 ON ce.price_set_id = ps.id
403 WHERE ce.entity_table = 'civicrm_event'
406 $dao = CRM_Core_DAO
::executeQuery($query);
408 while ($dao->fetch()) {
409 $elements[$dao->id
] = "$dao->label\n";
415 public function preProcess() {
416 parent
::preProcess();
419 public function select() {
421 $this->_columnHeaders
= array();
423 //add blank column at the Start
424 if (array_key_exists('options', $this->_params
) &&
425 !empty($this->_params
['options']['blank_column_begin'])
427 $select[] = " '' as blankColumnBegin";
428 $this->_columnHeaders
['blankColumnBegin']['title'] = '_ _ _ _';
430 foreach ($this->_columns
as $tableName => $table) {
431 if (array_key_exists('fields', $table)) {
432 foreach ($table['fields'] as $fieldName => $field) {
433 if (!empty($field['required']) ||
434 !empty($this->_params
['fields'][$fieldName])
436 if ($tableName == 'civicrm_contribution') {
437 $this->_contribField
= TRUE;
439 if ($fieldName == 'total_paid' ||
$fieldName == 'balance') {
440 $this->_balance
= TRUE;
441 // modify the select if filtered by fee_level as the from clause
442 // already selects the total_amount from civicrm_contribution table
443 if (!empty($this->_params
['price_field_value_id_value'])) {
444 $field['dbAlias'] = str_replace('SUM(ft.total_amount)', 'ft.total_amount', $field['dbAlias']);
447 $alias = "{$tableName}_{$fieldName}";
448 $select[] = "{$field['dbAlias']} as $alias";
449 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
450 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = $field['no_display'] ??
NULL;
451 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
452 $this->_selectAliases
[] = $alias;
457 //add blank column at the end
458 $blankcols = $this->_params
['blank_column_end'] ??
NULL;
460 for ($i = 1; $i <= $blankcols; $i++
) {
461 $select[] = " '' as blankColumnEnd_{$i}";
462 $this->_columnHeaders
["blank_{$i}"]['title'] = "_ _ _ _";
466 $this->_selectClauses
= $select;
467 $this->_select
= "SELECT " . implode(', ', $select) . " ";
477 public static function formRule($fields, $files, $self) {
478 $errors = $grouping = array();
482 public function from() {
484 FROM civicrm_participant {$this->_aliases['civicrm_participant']}
485 LEFT JOIN civicrm_event {$this->_aliases['civicrm_event']}
486 ON ({$this->_aliases['civicrm_event']}.id = {$this->_aliases['civicrm_participant']}.event_id ) AND
487 {$this->_aliases['civicrm_event']}.is_template = 0
488 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
489 ON ({$this->_aliases['civicrm_participant']}.contact_id = {$this->_aliases['civicrm_contact']}.id )
493 $this->joinAddressFromContact();
494 $this->joinPhoneFromContact();
495 $this->joinEmailFromContact();
497 // Include participant note.
498 if ($this->isTableSelected('civicrm_note')) {
500 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
501 ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_participant' AND
502 {$this->_aliases['civicrm_participant']}.id = {$this->_aliases['civicrm_note']}.entity_id )";
505 if ($this->_contribField
) {
507 LEFT JOIN civicrm_participant_payment pp
508 ON ({$this->_aliases['civicrm_participant']}.id = pp.participant_id)
509 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
510 ON (pp.contribution_id = {$this->_aliases['civicrm_contribution']}.id)
513 if (!empty($this->_params
['price_field_value_id_value'])) {
515 LEFT JOIN civicrm_line_item line_item_civireport
516 ON line_item_civireport.entity_table = 'civicrm_participant' AND
517 line_item_civireport.entity_id = {$this->_aliases['civicrm_participant']}.id AND
518 line_item_civireport.qty > 0
521 if ($this->_balance
) {
523 LEFT JOIN civicrm_entity_financial_trxn eft
524 ON (eft.entity_id = {$this->_aliases['civicrm_contribution']}.id)
525 LEFT JOIN civicrm_financial_trxn ft
526 ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') AND
532 public function where() {
534 foreach ($this->_columns
as $tableName => $table) {
535 if (array_key_exists('filters', $table)) {
536 foreach ($table['filters'] as $fieldName => $field) {
539 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
540 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
541 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
542 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
544 if ($relative ||
$from ||
$to) {
545 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
549 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
551 if ($fieldName == 'rid') {
552 $value = $this->_params
["{$fieldName}_value"] ??
NULL;
553 if (!empty($value)) {
555 if ($op == 'notin') {
559 $regexp = "[[:cntrl:]]*" . implode('[[:>:]]*|[[:<:]]*', $value) . "[[:cntrl:]]*";
560 $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'";
566 $clause = $this->whereClause($field,
568 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
569 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
570 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
575 if (!empty($clause)) {
576 $clauses[] = $clause;
581 if (empty($clauses)) {
582 $this->_where
= "WHERE {$this->_aliases['civicrm_participant']}.is_test = 0 ";
585 $this->_where
= "WHERE {$this->_aliases['civicrm_participant']}.is_test = 0 AND " .
586 implode(' AND ', $clauses);
588 if ($this->_aclWhere
) {
589 $this->_where
.= " AND {$this->_aclWhere} ";
593 public function groupBy() {
594 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, "{$this->_aliases['civicrm_participant']}.id");
597 public function postProcess() {
598 // get the acl clauses built before we assemble the query
599 $this->buildACLClause($this->_aliases
['civicrm_contact']);
600 parent
::postProcess();
613 private function _initBasicRow(&$rows, &$entryFound, $row, $rowId, $rowNum, $types) {
614 if (!array_key_exists($rowId, $row)) {
618 $value = $row[$rowId];
620 $rows[$rowNum][$rowId] = $types[$value];
626 * Alter display of rows.
628 * Iterate through the rows retrieved via SQL and make changes for display purposes,
629 * such as rendering contacts as links.
632 * Rows generated by SQL, with an array for each row.
634 public function alterDisplay(&$rows) {
636 $eventType = CRM_Core_OptionGroup
::values('event_type');
637 $financialTypes = CRM_Contribute_PseudoConstant
::financialType();
638 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'label');
639 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
641 foreach ($rows as $rowNum => $row) {
642 // make count columns point to detail report
643 // convert display name to links
644 if (array_key_exists('civicrm_participant_event_id', $row)) {
645 $eventId = $row['civicrm_participant_event_id'];
647 $rows[$rowNum]['civicrm_participant_event_id'] = CRM_Event_PseudoConstant
::event($eventId, FALSE);
649 $url = CRM_Report_Utils_Report
::getNextUrl('event/income',
650 'reset=1&force=1&id_op=in&id_value=' . $eventId,
651 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
653 $rows[$rowNum]['civicrm_participant_event_id_link'] = $url;
654 $rows[$rowNum]['civicrm_participant_event_id_hover'] = ts("View Event Income Details for this Event");
659 // handle event type id
660 $this->_initBasicRow($rows, $entryFound, $row, 'civicrm_event_event_type_id', $rowNum, $eventType);
662 // handle participant status id
663 if (array_key_exists('civicrm_participant_status_id', $row)) {
664 $statusId = $row['civicrm_participant_status_id'];
666 $rows[$rowNum]['civicrm_participant_status_id'] = CRM_Event_PseudoConstant
::participantStatus($statusId, FALSE, 'label');
671 // handle participant role id
672 if (array_key_exists('civicrm_participant_role_id', $row)) {
673 $roleId = $row['civicrm_participant_role_id'];
675 $roles = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roleId);
677 foreach ($roles as $role) {
678 $roleId[$role] = CRM_Event_PseudoConstant
::participantRole($role, FALSE);
680 $rows[$rowNum]['civicrm_participant_role_id'] = implode(', ', $roleId);
685 // Handle registered by name
686 if (array_key_exists('civicrm_participant_registered_by_name', $row)) {
687 $registeredById = $row['civicrm_participant_registered_by_name'];
688 if ($registeredById) {
689 $registeredByContactId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $registeredById, 'contact_id', 'id');
690 $rows[$rowNum]['civicrm_participant_registered_by_name'] = CRM_Contact_BAO_Contact
::displayName($registeredByContactId);
691 $rows[$rowNum]['civicrm_participant_registered_by_name_link'] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $registeredByContactId, $this->_absoluteUrl
);
692 $rows[$rowNum]['civicrm_participant_registered_by_name_hover'] = ts('View Contact Summary for Contact that registered the participant.');
696 // Handle value seperator in Fee Level
697 if (array_key_exists('civicrm_participant_participant_fee_level', $row)) {
698 $feeLevel = $row['civicrm_participant_participant_fee_level'];
700 CRM_Event_BAO_Participant
::fixEventLevel($feeLevel);
701 $rows[$rowNum]['civicrm_participant_participant_fee_level'] = $feeLevel;
706 // Convert display name to link
707 $displayName = $row['civicrm_contact_sort_name_linked'] ??
NULL;
708 $cid = $row['civicrm_contact_id'] ??
NULL;
709 $id = $row['civicrm_participant_participant_record'] ??
NULL;
711 if ($displayName && $cid && $id) {
712 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
713 "reset=1&force=1&id_op=eq&id_value=$cid",
714 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
717 $viewUrl = CRM_Utils_System
::url("civicrm/contact/view/participant",
718 "reset=1&id=$id&cid=$cid&action=view&context=participant"
721 $contactTitle = ts('View Contact Details');
722 $participantTitle = ts('View Participant Record');
724 $rows[$rowNum]['civicrm_contact_sort_name_linked'] = "<a title='$contactTitle' href=$url>$displayName</a>";
725 // Add a "View" link to the participant record if this isn't a CSV/PDF/printed document.
726 if ($this->_outputMode
!== 'csv' && $this->_outputMode
!== 'pdf' && $this->_outputMode
!== 'print') {
727 $rows[$rowNum]['civicrm_contact_sort_name_linked'] .=
728 "<span style='float: right;'><a title='$participantTitle' href=$viewUrl>" .
729 ts('View') . "</a></span>";
734 // Convert campaign_id to campaign title
735 $this->_initBasicRow($rows, $entryFound, $row, 'civicrm_participant_campaign_id', $rowNum, $this->campaigns
);
737 // handle contribution status
738 $this->_initBasicRow($rows, $entryFound, $row, 'civicrm_contribution_contribution_status_id', $rowNum, $contributionStatus);
740 // handle payment instrument
741 $this->_initBasicRow($rows, $entryFound, $row, 'civicrm_contribution_payment_instrument_id', $rowNum, $paymentInstruments);
743 // handle financial type
744 $this->_initBasicRow($rows, $entryFound, $row, 'civicrm_contribution_financial_type_id', $rowNum, $financialTypes);
746 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'event/participantListing', 'View Event Income Details') ?
TRUE : $entryFound;
748 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'event/ParticipantListing', 'List all participant(s) for this ') ?
TRUE : $entryFound;
750 // skip looking further in rows, if first row itself doesn't
751 // have the column we need