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