Merge pull request #15415 from ixiam/dev_issue#1297
[civicrm-core.git] / CRM / Event / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35 class CRM_Event_BAO_Query extends CRM_Core_BAO_Query {
36
37 /**
38 * Function get the import/export fields for contribution.
39 *
40 * @param bool $checkPermission
41 *
42 * @return array
43 * Associative array of contribution fields
44 */
45 public static function &getFields($checkPermission = TRUE) {
46 $fields = [];
47 $fields = array_merge($fields, CRM_Event_DAO_Event::import());
48 $fields = array_merge($fields, self::getParticipantFields());
49 $fields = array_merge($fields, CRM_Core_DAO_Discount::export());
50 $fields['event'] = self::getPseudoEventDateFieldMetadata();
51 return $fields;
52 }
53
54 /**
55 * @return array
56 */
57 public static function getParticipantFields() {
58 return CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
59 }
60
61 /**
62 * Build select for CiviEvent.
63 *
64 * @param $query
65 */
66 public static function select(&$query) {
67 if (($query->_mode & CRM_Contact_BAO_Query::MODE_EVENT) ||
68 CRM_Contact_BAO_Query::componentPresent($query->_returnProperties, 'participant_')
69 ) {
70 $query->_select['participant_id'] = "civicrm_participant.id as participant_id";
71 $query->_element['participant_id'] = 1;
72 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
73
74 //add fee level
75 if (!empty($query->_returnProperties['participant_fee_level'])) {
76 $query->_select['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
77 $query->_element['participant_fee_level'] = 1;
78 }
79
80 //add participant contact ID
81 if (!empty($query->_returnProperties['participant_contact_id'])) {
82 $query->_select['participant_contact_id'] = "civicrm_participant.contact_id as participant_contact_id";
83 $query->_element['participant_contact_id'] = 1;
84 }
85
86 //add fee amount
87 if (!empty($query->_returnProperties['participant_fee_amount'])) {
88 $query->_select['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
89 $query->_element['participant_fee_amount'] = 1;
90 }
91
92 //add fee currency
93 if (!empty($query->_returnProperties['participant_fee_currency'])) {
94 $query->_select['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
95 $query->_element['participant_fee_currency'] = 1;
96 }
97
98 //add event title also if event id is select
99 if (!empty($query->_returnProperties['event_id']) || !empty($query->_returnProperties['event_title'])) {
100 $query->_select['event_id'] = "civicrm_event.id as event_id";
101 $query->_select['event_title'] = "civicrm_event.title as event_title";
102 $query->_element['event_id'] = 1;
103 $query->_element['event_title'] = 1;
104 $query->_tables['civicrm_event'] = 1;
105 $query->_whereTables['civicrm_event'] = 1;
106 }
107
108 //add start date / end date
109 if (!empty($query->_returnProperties['event_start_date'])) {
110 $query->_select['event_start_date'] = "civicrm_event.start_date as event_start_date";
111 $query->_element['event_start_date'] = 1;
112 }
113
114 if (!empty($query->_returnProperties['event_end_date'])) {
115 $query->_select['event_end_date'] = "civicrm_event.end_date as event_end_date";
116 $query->_element['event_end_date'] = 1;
117 }
118
119 //event type
120 if (!empty($query->_returnProperties['event_type'])) {
121 $query->_select['event_type'] = "event_type.label as event_type";
122 $query->_element['event_type'] = 1;
123 $query->_tables['event_type'] = 1;
124 $query->_whereTables['event_type'] = 1;
125 }
126
127 if (!empty($query->_returnProperties['event_type_id'])) {
128 $query->_select['event_type_id'] = "civicrm_event.event_type_id as event_type_id";
129 $query->_element['event_type_id'] = 1;
130 $query->_tables['civicrm_event'] = 1;
131 $query->_whereTables['civicrm_event'] = 1;
132 }
133
134 //add status_id
135 if (!empty($query->_returnProperties['participant_status_id'])) {
136 $query->_select['participant_status_id'] = "civicrm_participant.status_id as participant_status_id";
137 $query->_element['participant_status_id'] = 1;
138 $query->_tables['civicrm_participant'] = 1;
139 $query->_whereTables['civicrm_participant'] = 1;
140 }
141
142 // get particupant_status label
143 if (!empty($query->_returnProperties['participant_status'])) {
144 $query->_select['participant_status'] = "participant_status.label as participant_status";
145 $query->_element['participant_status'] = 1;
146 $query->_tables['participant_status'] = 1;
147 $query->_whereTables['civicrm_participant'] = 1;
148 }
149
150 //add participant_role_id
151 if (!empty($query->_returnProperties['participant_role_id'])) {
152 $query->_select['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
153 $query->_element['participant_role_id'] = 1;
154 $query->_tables['civicrm_participant'] = 1;
155 $query->_whereTables['civicrm_participant'] = 1;
156 $query->_pseudoConstantsSelect['participant_role_id'] = [
157 'pseudoField' => 'participant_role_id',
158 'idCol' => 'participant_role_id',
159 ];
160 }
161
162 //add participant_role
163 if (!empty($query->_returnProperties['participant_role'])) {
164 $query->_select['participant_role'] = "civicrm_participant.role_id as participant_role";
165 $query->_element['participant_role'] = 1;
166 $query->_tables['participant_role'] = 1;
167 $query->_whereTables['civicrm_participant'] = 1;
168 $query->_pseudoConstantsSelect['participant_role'] = [
169 'pseudoField' => 'participant_role',
170 'idCol' => 'participant_role',
171 ];
172 }
173
174 //add register date
175 if (!empty($query->_returnProperties['participant_register_date'])) {
176 $query->_select['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
177 $query->_element['participant_register_date'] = 1;
178 }
179
180 //add source
181 if (!empty($query->_returnProperties['participant_source'])) {
182 $query->_select['participant_source'] = "civicrm_participant.source as participant_source";
183 $query->_element['participant_source'] = 1;
184 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
185 }
186
187 //participant note
188 if (!empty($query->_returnProperties['participant_note'])) {
189 $query->_select['participant_note'] = "participant_note.note as participant_note";
190 $query->_element['participant_note'] = 1;
191 $query->_tables['participant_note'] = 1;
192 $query->_whereTables['participant_note'] = 1;
193 }
194
195 if (!empty($query->_returnProperties['participant_is_pay_later'])) {
196 $query->_select['participant_is_pay_later'] = "civicrm_participant.is_pay_later as participant_is_pay_later";
197 $query->_element['participant_is_pay_later'] = 1;
198 }
199
200 if (!empty($query->_returnProperties['participant_is_test'])) {
201 $query->_select['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
202 $query->_element['participant_is_test'] = 1;
203 }
204
205 if (!empty($query->_returnProperties['participant_registered_by_id'])) {
206 $query->_select['participant_registered_by_id'] = "civicrm_participant.registered_by_id as participant_registered_by_id";
207 $query->_element['participant_registered_by_id'] = 1;
208 }
209
210 // get discount name
211 if (!empty($query->_returnProperties['participant_discount_name'])) {
212 $query->_select['participant_discount_name'] = "discount_name.title as participant_discount_name";
213 $query->_element['participant_discount_name'] = 1;
214 $query->_tables['civicrm_discount'] = 1;
215 $query->_tables['participant_discount_name'] = 1;
216 $query->_whereTables['civicrm_discount'] = 1;
217 $query->_whereTables['participant_discount_name'] = 1;
218 }
219
220 //carry campaign id to selectors.
221 if (!empty($query->_returnProperties['participant_campaign_id'])) {
222 $query->_select['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
223 $query->_element['participant_campaign_id'] = 1;
224 }
225 }
226 }
227
228 /**
229 * @param $query
230 */
231 public static function where(&$query) {
232 $grouping = NULL;
233 foreach (array_keys($query->_params) as $id) {
234 if (empty($query->_params[$id][0])) {
235 continue;
236 }
237 if (substr($query->_params[$id][0], 0, 6) == 'event_' ||
238 substr($query->_params[$id][0], 0, 12) == 'participant_'
239 ) {
240 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
241 $query->_useDistinct = TRUE;
242 }
243 $grouping = $query->_params[$id][3];
244 self::whereClauseSingle($query->_params[$id], $query);
245 }
246 }
247 }
248
249 /**
250 * @param $values
251 * @param \CRM_Contact_BAO_Query $query
252 */
253 public static function whereClauseSingle(&$values, &$query) {
254 $checkPermission = empty($query->_skipPermission);
255 list($name, $op, $value, $grouping, $wildcard) = $values;
256 $fields = array_merge(CRM_Event_BAO_Event::fields(), CRM_Event_BAO_Participant::exportableFields());
257
258 switch ($name) {
259 case 'event_low':
260 case 'event_high':
261 $query->dateQueryBuilder($values,
262 'civicrm_event', 'event', 'start_date', ts('Event Active On'), TRUE, 'YmdHis', 'end_date'
263 );
264 return;
265
266 case 'event_start_date_low':
267 case 'event_start_date_high':
268 $query->dateQueryBuilder($values,
269 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
270 );
271 return;
272
273 case 'event_end_date_low':
274 case 'event_end_date_high':
275 $query->dateQueryBuilder($values,
276 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
277 );
278 return;
279
280 case 'event_include_repeating_events':
281 /**
282 * Include Repeating Events
283 */
284 //Get parent of this event
285 $exEventId = '';
286 if ($query->_where[$grouping]) {
287 foreach ($query->_where[$grouping] as $key => $val) {
288 if (strstr($val, 'civicrm_event.id =')) {
289 $exEventId = $val;
290 $extractEventId = explode(" ", $val);
291 $value = $extractEventId[2];
292 $where = $query->_where[$grouping][$key];
293 }
294 elseif (strstr($val, 'civicrm_event.id IN')) {
295 //extract the first event id if multiple events are selected
296 preg_match('/civicrm_event.id IN \(\"(\d+)/', $val, $matches);
297 $value = $matches[1];
298 $where = $query->_where[$grouping][$key];
299 }
300 }
301 if ($exEventId) {
302 $extractEventId = explode(" ", $exEventId);
303 $value = $extractEventId[2];
304 }
305 elseif (!empty($matches[1])) {
306 $value = $matches[1];
307 }
308 $where = $query->_where[$grouping][$key];
309 }
310 $thisEventHasParent = CRM_Core_BAO_RecurringEntity::getParentFor($value, 'civicrm_event');
311 if ($thisEventHasParent) {
312 $getAllConnections = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($thisEventHasParent, 'civicrm_event');
313 $allEventIds = [];
314 foreach ($getAllConnections as $key => $val) {
315 $allEventIds[] = $val['id'];
316 }
317 if (!empty($allEventIds)) {
318 $op = "IN";
319 $value = "(" . implode(",", $allEventIds) . ")";
320 }
321 }
322 $query->_where[$grouping][] = "{$where} OR civicrm_event.id $op {$value}";
323 $query->_qill[$grouping][] = ts('Include Repeating Events');
324 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
325 return;
326
327 case 'participant_is_test':
328 $key = array_search('civicrm_participant.is_test = 0', $query->_where[$grouping]);
329 if (!empty($key)) {
330 unset($query->_where[$grouping][$key]);
331 }
332 case 'participant_test':
333 // We dont want to include all tests for sql OR CRM-7827
334 if (!$value || $query->getOperator() != 'OR') {
335 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.is_test",
336 $op,
337 $value,
338 "Boolean"
339 );
340
341 $isTest = $value ? 'a Test' : 'not a Test';
342 $query->_qill[$grouping][] = ts("Participant is %1", [1 => $isTest]);
343 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
344 }
345 return;
346
347 case 'participant_fee_id':
348 $labels = [];
349 foreach ($value as $val) {
350 $labels[] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
351 }
352 $query->_where[$grouping][] = "civicrm_line_item.price_field_value_id IN (" . implode(', ', $value) . ")";
353 $query->_qill[$grouping][] = ts("Fee level") . " IN " . implode(', ', $labels);
354 $query->_tables['civicrm_participant'] = $query->_tables['civicrm_line_item'] = $query->_whereTables['civicrm_line_item'] = 1;
355 return;
356
357 case 'participant_fee_amount_high':
358 case 'participant_fee_amount_low':
359 $query->numberRangeBuilder($values,
360 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
361 );
362 return;
363
364 case 'participant_status_id':
365 if ($value && is_array($value) && strpos($op, 'IN') === FALSE) {
366 $op = 'IN';
367 }
368 case 'participant_status':
369 case 'participant_source':
370 case 'participant_id':
371 case 'participant_contact_id':
372 case 'participant_is_pay_later':
373 case 'participant_fee_amount':
374 case 'participant_fee_level':
375 case 'participant_campaign_id':
376 case 'participant_registered_by_id':
377
378 $qillName = $name;
379 if (in_array($name, [
380 'participant_status_id',
381 'participant_source',
382 'participant_id',
383 'participant_contact_id',
384 'participant_fee_amount',
385 'participant_fee_level',
386 'participant_is_pay_later',
387 'participant_campaign_id',
388 'participant_registered_by_id',
389 ])) {
390 $name = str_replace('participant_', '', $name);
391 if ($name == 'is_pay_later') {
392 $qillName = $name;
393 }
394 }
395 elseif ($name == 'participant_status') {
396 $name = 'status_id';
397 }
398
399 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
400 $tableName = empty($tableName) ? 'civicrm_participant' : $tableName;
401 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
402 $op = key($value);
403 $value = $value[$op];
404 }
405 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name", $op, $value, $dataType);
406
407 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
408 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
409 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
410 return;
411
412 case 'participant_role':
413 case 'participant_role_id':
414 $qillName = $name;
415 $name = 'role_id';
416
417 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
418 $tableName = empty($tableName) ? 'civicrm_participant' : $tableName;
419 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
420 $op = key($value);
421 $value = $value[$op];
422 }
423 if (!strstr($op, 'NULL') && !strstr($op, 'EMPTY') && !strstr($op, 'LIKE')) {
424 $regexOp = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT REGEXP' : 'REGEXP';
425 $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)";
426 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.$name", $regexOp, $regexp, 'String');
427 }
428 else {
429 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name", $op, $value, $dataType);
430 }
431
432 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
433 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
434 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
435 return;
436
437 case 'participant_register_date':
438 case 'participant_register_date_high':
439 case 'participant_register_date_low':
440 $query->dateQueryBuilder($values,
441 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
442 );
443 return;
444
445 case 'event_id':
446 case 'participant_event_id':
447 $name = str_replace('participant_', '', $name);
448 case 'event_is_public':
449 case 'event_type_id':
450 case 'event_title':
451 $qillName = $name;
452 if (in_array($name, [
453 'event_id',
454 'event_title',
455 'event_is_public',
456 ])) {
457 $name = str_replace('event_', '', $name);
458 }
459 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
460
461 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.$name", $op, $value, $dataType);
462 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
463 if (!array_key_exists($qillName, $fields)) {
464 break;
465 }
466 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op, ['check_permission' => $checkPermission]);
467 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
468 return;
469
470 case 'participant_note':
471 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
472 $query->_tables['participant_note'] = $query->_whereTables['participant_note'] = 1;
473 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('participant_note.note', $op, $value, 'String');
474 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$name]['title'], 2 => $op, 3 => $value]);
475 break;
476 }
477 }
478
479 /**
480 * @param string $name
481 * @param $mode
482 * @param $side
483 *
484 * @return null|string
485 */
486 public static function from($name, $mode, $side) {
487 $from = NULL;
488 switch ($name) {
489 case 'civicrm_participant':
490 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
491 break;
492
493 case 'civicrm_event':
494 //CRM-17121
495 $from = " LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
496 break;
497
498 case 'event_type':
499 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
500 $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 ) ";
501 break;
502
503 case 'participant_note':
504 $from .= " $side JOIN civicrm_note participant_note ON ( participant_note.entity_table = 'civicrm_participant' AND
505 civicrm_participant.id = participant_note.entity_id )";
506 break;
507
508 case 'participant_status':
509 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
510 break;
511
512 case 'participant_role':
513 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
514 $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)
515 AND option_group_participant_role.id = participant_role.option_group_id ) ";
516 break;
517
518 case 'participant_discount_name':
519 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
520 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
521 break;
522
523 case 'civicrm_line_item':
524 $from .= " $side JOIN civicrm_line_item ON civicrm_line_item.entity_id = civicrm_participant.id AND civicrm_line_item.entity_table = 'civicrm_participant'";
525 break;
526 }
527 return $from;
528 }
529
530 /**
531 * @param $mode
532 * @param bool $includeCustomFields
533 *
534 * @return array|null
535 */
536 public static function defaultReturnProperties(
537 $mode,
538 $includeCustomFields = TRUE
539 ) {
540 $properties = NULL;
541 if ($mode & CRM_Contact_BAO_Query::MODE_EVENT) {
542 $properties = [
543 'contact_type' => 1,
544 'contact_sub_type' => 1,
545 'sort_name' => 1,
546 'display_name' => 1,
547 'event_id' => 1,
548 'event_title' => 1,
549 'event_start_date' => 1,
550 'event_end_date' => 1,
551 'event_type' => 1,
552 'participant_id' => 1,
553 'participant_status' => 1,
554 'participant_status_id' => 1,
555 'participant_role' => 1,
556 'participant_role_id' => 1,
557 'participant_note' => 1,
558 'participant_register_date' => 1,
559 'participant_source' => 1,
560 'participant_fee_level' => 1,
561 'participant_is_test' => 1,
562 'participant_is_pay_later' => 1,
563 'participant_fee_amount' => 1,
564 'participant_discount_name' => 1,
565 'participant_fee_currency' => 1,
566 'participant_registered_by_id' => 1,
567 'participant_campaign_id' => 1,
568 ];
569
570 if ($includeCustomFields) {
571 // also get all the custom participant properties
572 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Participant');
573 if (!empty($fields)) {
574 foreach ($fields as $name => $dontCare) {
575 $properties[$name] = 1;
576 }
577 }
578 }
579 }
580
581 return $properties;
582 }
583
584 /**
585 * Get the metadata for fields to be included on the grant search form.
586 *
587 * @throws \CiviCRM_API3_Exception
588 */
589 public static function getSearchFieldMetadata() {
590 $fields = [
591 'participant_status_id',
592 'participant_register_date',
593 // Super-weird but we have to make it work.....
594 'event',
595 ];
596 $metadata = civicrm_api3('Participant', 'getfields', [])['values'];
597 $metadata['event'] = self::getPseudoEventDateFieldMetadata();
598 return array_intersect_key($metadata, array_flip($fields));
599 }
600
601 /**
602 * Build the event search form.
603 *
604 * @param \CRM_Event_Form_Search $form
605 *
606 * @throws \CiviCRM_API3_Exception
607 * @throws \CRM_Core_Exception
608 */
609 public static function buildSearchForm(&$form) {
610 $form->addSearchFieldMetadata(['Participant' => self::getSearchFieldMetadata()]);
611 $form->addFormFieldsFromMetadata();
612 $dataURLEventFee = CRM_Utils_System::url('civicrm/ajax/eventFee',
613 "reset=1",
614 FALSE, NULL, FALSE
615 );
616
617 $form->assign('dataURLEventFee', $dataURLEventFee);
618
619 $form->addEntityRef('event_id', ts('Event Name'), [
620 'entity' => 'Event',
621 'placeholder' => ts('- any -'),
622 'multiple' => 1,
623 'select' => ['minimumInputLength' => 0],
624 ]);
625 $form->addEntityRef('event_type_id', ts('Event Type'), [
626 'entity' => 'OptionValue',
627 'placeholder' => ts('- any -'),
628 'select' => ['minimumInputLength' => 0],
629 'api' => [
630 'params' => ['option_group_id' => 'event_type'],
631 ],
632 ]);
633 $obj = new CRM_Report_Form_Event_ParticipantListing();
634 $form->add('select', 'participant_fee_id',
635 ts('Fee Level'),
636 $obj->getPriceLevels(),
637 FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')]
638 );
639
640 $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', [1 => '<em>%1</em>']));
641
642 $form->addSelect('participant_role_id',
643 [
644 'entity' => 'participant',
645 'label' => ts('Participant Role'),
646 'multiple' => 'multiple',
647 'option_url' => NULL,
648 'placeholder' => ts('- any -'),
649 ]
650 );
651
652 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
653 $form->addYesNo('participant_is_pay_later', ts('Participant is Pay Later?'), TRUE);
654 $form->addElement('text', 'participant_fee_amount_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
655 $form->addElement('text', 'participant_fee_amount_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
656
657 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
658 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
659
660 self::addCustomFormFields($form, ['Participant', 'Event']);
661
662 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'participant_campaign_id');
663
664 $form->assign('validCiviEvent', TRUE);
665 $form->setDefaults(['participant_test' => 0]);
666 }
667
668 /**
669 * @param $tables
670 */
671 public static function tableNames(&$tables) {
672 //add participant table
673 if (!empty($tables['civicrm_event'])) {
674 $tables = array_merge(['civicrm_participant' => 1], $tables);
675 }
676 }
677
678 /**
679 * Get metadata from pseudo search field 'event'.
680 *
681 * @return array
682 */
683 protected static function getPseudoEventDateFieldMetadata(): array {
684 return [
685 'name' => 'event',
686 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
687 'title' => ts('Event Active On'),
688 'table_name' => 'civicrm_event',
689 'where' => 'civicrm_event.start_date',
690 'where_end' => 'civicrm_event.end_date',
691 'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'],
692 ];
693 }
694
695 }