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_Event_BAO_Query
extends CRM_Core_BAO_Query
{
20 * Function get the import/export fields for contribution.
22 * @param bool $checkPermission
25 * Associative array of contribution fields
27 public static function &getFields($checkPermission = TRUE) {
29 $fields = array_merge($fields, CRM_Event_DAO_Event
::import());
30 $fields = array_merge($fields, self
::getParticipantFields());
31 $fields = array_merge($fields, CRM_Core_DAO_Discount
::export());
32 $fields['event'] = self
::getPseudoEventDateFieldMetadata();
39 public static function getParticipantFields() {
40 return CRM_Event_BAO_Participant
::importableFields('Individual', TRUE, TRUE);
44 * Build select for CiviEvent.
48 public static function select(&$query) {
49 if (($query->_mode
& CRM_Contact_BAO_Query
::MODE_EVENT
) ||
50 CRM_Contact_BAO_Query
::componentPresent($query->_returnProperties
, 'participant_')
52 $query->_select
['participant_id'] = 'civicrm_participant.id as participant_id';
53 $query->_element
['participant_id'] = 1;
54 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
57 if (!empty($query->_returnProperties
['participant_fee_level'])) {
58 $query->_select
['participant_fee_level'] = 'civicrm_participant.fee_level as participant_fee_level';
59 $query->_element
['participant_fee_level'] = 1;
62 //add participant contact ID
63 if (!empty($query->_returnProperties
['participant_contact_id'])) {
64 $query->_select
['participant_contact_id'] = 'civicrm_participant.contact_id as participant_contact_id';
65 $query->_element
['participant_contact_id'] = 1;
69 if (!empty($query->_returnProperties
['participant_fee_amount'])) {
70 $query->_select
['participant_fee_amount'] = 'civicrm_participant.fee_amount as participant_fee_amount';
71 $query->_element
['participant_fee_amount'] = 1;
75 if (!empty($query->_returnProperties
['participant_fee_currency'])) {
76 $query->_select
['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
77 $query->_element
['participant_fee_currency'] = 1;
80 //add event title also if event id is select
81 if (!empty($query->_returnProperties
['event_id']) ||
!empty($query->_returnProperties
['event_title'])) {
82 $query->_select
['event_id'] = "civicrm_event.id as event_id";
83 $query->_select
['event_title'] = "civicrm_event.title as event_title";
84 $query->_element
['event_id'] = 1;
85 $query->_element
['event_title'] = 1;
86 $query->_tables
['civicrm_event'] = 1;
87 $query->_whereTables
['civicrm_event'] = 1;
90 //add start date / end date
91 if (!empty($query->_returnProperties
['event_start_date'])) {
92 $query->_select
['event_start_date'] = "civicrm_event.start_date as event_start_date";
93 $query->_element
['event_start_date'] = 1;
96 if (!empty($query->_returnProperties
['event_end_date'])) {
97 $query->_select
['event_end_date'] = "civicrm_event.end_date as event_end_date";
98 $query->_element
['event_end_date'] = 1;
102 if (!empty($query->_returnProperties
['event_type'])) {
103 $query->_select
['event_type'] = "event_type.label as event_type";
104 $query->_element
['event_type'] = 1;
105 $query->_tables
['event_type'] = 1;
106 $query->_whereTables
['event_type'] = 1;
109 if (!empty($query->_returnProperties
['event_type_id'])) {
110 $query->_select
['event_type_id'] = "civicrm_event.event_type_id as event_type_id";
111 $query->_element
['event_type_id'] = 1;
112 $query->_tables
['civicrm_event'] = 1;
113 $query->_whereTables
['civicrm_event'] = 1;
117 if (!empty($query->_returnProperties
['participant_status_id'])) {
118 $query->_select
['participant_status_id'] = "civicrm_participant.status_id as participant_status_id";
119 $query->_element
['participant_status_id'] = 1;
120 $query->_tables
['civicrm_participant'] = 1;
121 $query->_whereTables
['civicrm_participant'] = 1;
124 // get particupant_status label
125 if (!empty($query->_returnProperties
['participant_status'])) {
126 $query->_select
['participant_status'] = "participant_status.label as participant_status";
127 $query->_element
['participant_status'] = 1;
128 $query->_tables
['participant_status'] = 1;
129 $query->_whereTables
['civicrm_participant'] = 1;
132 //add participant_role_id
133 if (!empty($query->_returnProperties
['participant_role_id'])) {
134 $query->_select
['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
135 $query->_element
['participant_role_id'] = 1;
136 $query->_tables
['civicrm_participant'] = 1;
137 $query->_whereTables
['civicrm_participant'] = 1;
138 $query->_pseudoConstantsSelect
['participant_role_id'] = [
139 'pseudoField' => 'participant_role_id',
140 'idCol' => 'participant_role_id',
144 //add participant_role
145 if (!empty($query->_returnProperties
['participant_role'])) {
146 $query->_select
['participant_role'] = "civicrm_participant.role_id as participant_role";
147 $query->_element
['participant_role'] = 1;
148 $query->_tables
['participant_role'] = 1;
149 $query->_whereTables
['civicrm_participant'] = 1;
150 $query->_pseudoConstantsSelect
['participant_role'] = [
151 'pseudoField' => 'participant_role',
152 'idCol' => 'participant_role',
157 if (!empty($query->_returnProperties
['participant_register_date'])) {
158 $query->_select
['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
159 $query->_element
['participant_register_date'] = 1;
163 if (!empty($query->_returnProperties
['participant_source'])) {
164 $query->_select
['participant_source'] = "civicrm_participant.source as participant_source";
165 $query->_element
['participant_source'] = 1;
166 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
170 if (!empty($query->_returnProperties
['participant_note'])) {
171 $query->_select
['participant_note'] = "participant_note.note as participant_note";
172 $query->_element
['participant_note'] = 1;
173 $query->_tables
['participant_note'] = 1;
174 $query->_whereTables
['participant_note'] = 1;
177 if (!empty($query->_returnProperties
['participant_is_pay_later'])) {
178 $query->_select
['participant_is_pay_later'] = "civicrm_participant.is_pay_later as participant_is_pay_later";
179 $query->_element
['participant_is_pay_later'] = 1;
182 if (!empty($query->_returnProperties
['participant_is_test'])) {
183 $query->_select
['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
184 $query->_element
['participant_is_test'] = 1;
187 if (!empty($query->_returnProperties
['participant_registered_by_id'])) {
188 $query->_select
['participant_registered_by_id'] = "civicrm_participant.registered_by_id as participant_registered_by_id";
189 $query->_element
['participant_registered_by_id'] = 1;
193 if (!empty($query->_returnProperties
['participant_discount_name'])) {
194 $query->_select
['participant_discount_name'] = "discount_name.title as participant_discount_name";
195 $query->_element
['participant_discount_name'] = 1;
196 $query->_tables
['civicrm_discount'] = 1;
197 $query->_tables
['participant_discount_name'] = 1;
198 $query->_whereTables
['civicrm_discount'] = 1;
199 $query->_whereTables
['participant_discount_name'] = 1;
202 //carry campaign id to selectors.
203 if (!empty($query->_returnProperties
['participant_campaign_id'])) {
204 $query->_select
['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
205 $query->_element
['participant_campaign_id'] = 1;
211 * Get event related where clauses.
213 * @param \CRM_Contact_BAO_Query $query
215 public static function where(&$query) {
216 foreach (array_keys($query->_params
) as $id) {
217 if (empty($query->_params
[$id][0])) {
220 if (substr($query->_params
[$id][0], 0, 6) == 'event_' ||
221 substr($query->_params
[$id][0], 0, 12) == 'participant_'
223 if ($query->_mode
== CRM_Contact_BAO_Query
::MODE_CONTACTS
) {
224 $query->_useDistinct
= TRUE;
226 self
::whereClauseSingle($query->_params
[$id], $query);
233 * @param \CRM_Contact_BAO_Query $query
235 * @throws \CRM_Core_Exception
237 public static function whereClauseSingle(&$values, &$query) {
238 $checkPermission = empty($query->_skipPermission
);
239 list($name, $op, $value, $grouping, $wildcard) = $values;
240 $fields = array_merge(CRM_Event_BAO_Event
::fields(), CRM_Event_BAO_Participant
::exportableFields());
241 $fieldSpec = $fields[$values[0]] ??
[];
246 $query->dateQueryBuilder($values,
247 'civicrm_event', 'event', 'start_date', ts('Event Active On'), TRUE, 'YmdHis', 'end_date'
251 case 'event_start_date_low':
252 case 'event_start_date_high':
253 $query->dateQueryBuilder($values,
254 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
258 case 'event_end_date_low':
259 case 'event_end_date_high':
260 $query->dateQueryBuilder($values,
261 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
265 case 'event_include_repeating_events':
267 * Include Repeating Events
269 //Get parent of this event
271 if ($query->_where
[$grouping]) {
272 foreach ($query->_where
[$grouping] as $key => $val) {
273 if (strstr($val, 'civicrm_event.id =')) {
275 $extractEventId = explode(" ", $val);
276 $value = $extractEventId[2];
277 $where = $query->_where
[$grouping][$key];
279 elseif (strstr($val, 'civicrm_event.id IN')) {
280 //extract the first event id if multiple events are selected
281 preg_match('/civicrm_event.id IN \(\"(\d+)/', $val, $matches);
282 $value = $matches[1];
283 $where = $query->_where
[$grouping][$key];
287 $extractEventId = explode(" ", $exEventId);
288 $value = $extractEventId[2];
290 elseif (!empty($matches[1])) {
291 $value = $matches[1];
293 $where = $query->_where
[$grouping][$key];
295 $thisEventHasParent = CRM_Core_BAO_RecurringEntity
::getParentFor($value, 'civicrm_event');
296 if ($thisEventHasParent) {
297 $getAllConnections = CRM_Core_BAO_RecurringEntity
::getEntitiesForParent($thisEventHasParent, 'civicrm_event');
299 foreach ($getAllConnections as $key => $val) {
300 $allEventIds[] = $val['id'];
302 if (!empty($allEventIds)) {
304 $value = "(" . implode(",", $allEventIds) . ")";
307 $query->_where
[$grouping][] = "{$where} OR civicrm_event.id $op {$value}";
308 $query->_qill
[$grouping][] = ts('Include Repeating Events');
309 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
312 case 'participant_is_test':
313 $key = array_search('civicrm_participant.is_test = 0', $query->_where
[$grouping]);
315 unset($query->_where
[$grouping][$key]);
317 case 'participant_test':
318 // We dont want to include all tests for sql OR CRM-7827
319 if (!$value ||
$query->getOperator() != 'OR') {
320 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_test",
326 $isTest = $value ?
'a Test' : 'not a Test';
327 $query->_qill
[$grouping][] = ts("Participant is %1", [1 => $isTest]);
328 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
332 case 'participant_fee_id':
334 foreach ($value as $val) {
335 $labels[] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
337 $query->_where
[$grouping][] = "civicrm_line_item.price_field_value_id IN (" . implode(', ', $value) . ")";
338 $query->_qill
[$grouping][] = ts("Fee level") . " IN " . implode(', ', $labels);
339 $query->_tables
['civicrm_participant'] = $query->_tables
['civicrm_line_item'] = $query->_whereTables
['civicrm_line_item'] = 1;
342 case 'participant_fee_amount_high':
343 case 'participant_fee_amount_low':
344 $query->numberRangeBuilder($values,
345 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
349 case 'participant_status_id':
350 $query->handleWhereFromMetadata($fieldSpec, $name, $value, $op);
353 case 'participant_status':
354 case 'participant_source':
355 case 'participant_id':
356 case 'participant_contact_id':
357 case 'participant_is_pay_later':
358 case 'participant_fee_amount':
359 case 'participant_fee_level':
360 case 'participant_campaign_id':
361 case 'participant_registered_by_id':
364 if (in_array($name, [
365 'participant_source',
367 'participant_contact_id',
368 'participant_fee_amount',
369 'participant_fee_level',
370 'participant_is_pay_later',
371 'participant_campaign_id',
372 'participant_registered_by_id',
374 $name = str_replace('participant_', '', $name);
375 if ($name == 'is_pay_later') {
379 elseif ($name == 'participant_status') {
383 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
384 $tableName = empty($tableName) ?
'civicrm_participant' : $tableName;
385 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
387 $value = $value[$op];
389 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("$tableName.$name", $op, $value, $dataType);
391 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
392 $query->_qill
[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
393 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
396 case 'participant_role':
397 case 'participant_role_id':
401 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
402 $tableName = empty($tableName) ?
'civicrm_participant' : $tableName;
403 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
405 $value = $value[$op];
407 if (!strstr($op, 'NULL') && !strstr($op, 'EMPTY') && !strstr($op, 'LIKE')) {
408 $regexOp = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT REGEXP' : 'REGEXP';
409 $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)";
410 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.$name", $regexOp, $regexp, 'String');
413 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("$tableName.$name", $op, $value, $dataType);
416 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
417 $query->_qill
[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
418 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
421 case 'participant_register_date':
422 case 'participant_register_date_high':
423 case 'participant_register_date_low':
424 $query->dateQueryBuilder($values,
425 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
430 case 'participant_event_id':
431 $name = str_replace('participant_', '', $name);
432 case 'event_is_public':
433 case 'event_type_id':
436 if (in_array($name, [
441 $name = str_replace('event_', '', $name);
443 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
445 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.$name", $op, $value, $dataType);
446 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
447 if (!array_key_exists($qillName, $fields)) {
450 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op, ['check_permission' => $checkPermission]);
451 $query->_qill
[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
454 case 'participant_note':
455 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
456 $query->_tables
['participant_note'] = $query->_whereTables
['participant_note'] = 1;
457 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause('participant_note.note', $op, $value, 'String');
458 $query->_qill
[$grouping][] = ts('%1 %2 %3', [1 => $fields[$name]['title'], 2 => $op, 3 => $value]);
464 * @param string $name
468 * @return null|string
470 public static function from($name, $mode, $side) {
473 case 'civicrm_participant':
474 $from = ' LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ';
477 case 'civicrm_event':
479 $from = ' LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ';
483 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
484 $from .= " $side JOIN civicrm_option_value event_type ON (civicrm_event.event_type_id = event_type.value AND option_group_event_type.id = event_type.option_group_id ) ";
487 case 'participant_note':
488 $from .= " $side JOIN civicrm_note participant_note ON ( participant_note.entity_table = 'civicrm_participant' AND
489 civicrm_participant.id = participant_note.entity_id )";
492 case 'participant_status':
493 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
496 case 'participant_role':
497 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
498 $from .= " $side JOIN civicrm_option_value participant_role ON ((civicrm_participant.role_id = participant_role.value OR SUBSTRING_INDEX(role_id,'\ 1', 1) = participant_role.value)
499 AND option_group_participant_role.id = participant_role.option_group_id ) ";
502 case 'participant_discount_name':
503 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
504 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
507 case 'civicrm_line_item':
508 $from .= " $side JOIN civicrm_line_item ON civicrm_line_item.entity_id = civicrm_participant.id AND civicrm_line_item.entity_table = 'civicrm_participant'";
516 * @param bool $includeCustomFields
520 public static function defaultReturnProperties(
522 $includeCustomFields = TRUE
525 if ($mode & CRM_Contact_BAO_Query
::MODE_EVENT
) {
528 'contact_sub_type' => 1,
533 'event_start_date' => 1,
534 'event_end_date' => 1,
536 'participant_id' => 1,
537 'participant_status' => 1,
538 'participant_status_id' => 1,
539 'participant_role' => 1,
540 'participant_role_id' => 1,
541 'participant_note' => 1,
542 'participant_register_date' => 1,
543 'participant_source' => 1,
544 'participant_fee_level' => 1,
545 'participant_is_test' => 1,
546 'participant_is_pay_later' => 1,
547 'participant_fee_amount' => 1,
548 'participant_discount_name' => 1,
549 'participant_fee_currency' => 1,
550 'participant_registered_by_id' => 1,
551 'participant_campaign_id' => 1,
554 if ($includeCustomFields) {
555 // also get all the custom participant properties
556 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Participant');
557 if (!empty($fields)) {
558 foreach ($fields as $name => $dontCare) {
559 $properties[$name] = 1;
569 * Get the metadata for fields to be included on the grant search form.
571 * @throws \CiviCRM_API3_Exception
573 public static function getSearchFieldMetadata() {
575 'participant_status_id',
576 'participant_register_date',
577 // Super-weird but we have to make it work.....
580 $metadata = civicrm_api3('Participant', 'getfields', [])['values'];
581 $metadata['event'] = self
::getPseudoEventDateFieldMetadata();
582 return array_intersect_key($metadata, array_flip($fields));
586 * Build the event search form.
588 * @param \CRM_Event_Form_Search $form
590 * @throws \CiviCRM_API3_Exception
591 * @throws \CRM_Core_Exception
593 public static function buildSearchForm(&$form) {
594 $form->addSearchFieldMetadata(['Participant' => self
::getSearchFieldMetadata()]);
595 $form->addFormFieldsFromMetadata();
596 $dataURLEventFee = CRM_Utils_System
::url('civicrm/ajax/eventFee',
601 $form->assign('dataURLEventFee', $dataURLEventFee);
603 $form->addEntityRef('event_id', ts('Event Name'), [
605 'placeholder' => ts('- any -'),
607 'select' => ['minimumInputLength' => 0],
609 $form->addEntityRef('event_type_id', ts('Event Type'), [
610 'entity' => 'OptionValue',
611 'placeholder' => ts('- any -'),
612 'select' => ['minimumInputLength' => 0],
614 'params' => ['option_group_id' => 'event_type'],
617 $obj = new CRM_Report_Form_Event_ParticipantListing();
618 $form->add('select', 'participant_fee_id',
620 $obj->getPriceLevels(),
621 FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')]
624 $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', [1 => '<em>%1</em>']));
626 $form->addSelect('participant_role_id',
628 'entity' => 'participant',
629 'label' => ts('Participant Role'),
630 'multiple' => 'multiple',
631 'option_url' => NULL,
632 'placeholder' => ts('- any -'),
636 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
637 $form->addYesNo('participant_is_pay_later', ts('Participant is Pay Later?'), TRUE);
638 $form->addElement('text', 'participant_fee_amount_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
639 $form->addElement('text', 'participant_fee_amount_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
641 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
642 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
644 self
::addCustomFormFields($form, ['Participant', 'Event']);
646 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'participant_campaign_id');
648 $form->assign('validCiviEvent', TRUE);
649 $form->setDefaults(['participant_test' => 0]);
655 public static function tableNames(&$tables) {
656 //add participant table
657 if (!empty($tables['civicrm_event'])) {
658 $tables = array_merge(['civicrm_participant' => 1], $tables);
663 * Get metadata from pseudo search field 'event'.
667 protected static function getPseudoEventDateFieldMetadata(): array {
670 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
671 'title' => ts('Event Active On'),
672 'table_name' => 'civicrm_event',
673 'where' => 'civicrm_event.start_date',
674 'where_end' => 'civicrm_event.end_date',
675 'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'],