Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 | 16 | */ |
86a0d21e | 17 | class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { |
6a488035 | 18 | |
0cf587a7 | 19 | /** |
e9ff5391 | 20 | * Function get the import/export fields for contribution. |
21 | * | |
22 | * @param bool $checkPermission | |
23 | * | |
0cf587a7 | 24 | * @return array |
e9ff5391 | 25 | * Associative array of contribution fields |
0cf587a7 | 26 | */ |
e9ff5391 | 27 | public static function &getFields($checkPermission = TRUE) { |
be2fb01f | 28 | $fields = []; |
6a488035 TO |
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()); | |
936ededf | 32 | $fields['event'] = self::getPseudoEventDateFieldMetadata(); |
6a488035 TO |
33 | return $fields; |
34 | } | |
35 | ||
0cf587a7 EM |
36 | /** |
37 | * @return array | |
38 | */ | |
936ededf | 39 | public static function getParticipantFields() { |
40 | return CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE); | |
6a488035 TO |
41 | } |
42 | ||
43 | /** | |
66f9e52b | 44 | * Build select for CiviEvent. |
6a488035 | 45 | * |
77b97be7 | 46 | * @param $query |
6a488035 | 47 | */ |
00be9182 | 48 | public static function select(&$query) { |
6a488035 TO |
49 | if (($query->_mode & CRM_Contact_BAO_Query::MODE_EVENT) || |
50 | CRM_Contact_BAO_Query::componentPresent($query->_returnProperties, 'participant_') | |
51 | ) { | |
7473db7f | 52 | $query->_select['participant_id'] = 'civicrm_participant.id as participant_id'; |
6a488035 TO |
53 | $query->_element['participant_id'] = 1; |
54 | $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1; | |
55 | ||
56 | //add fee level | |
a7488080 | 57 | if (!empty($query->_returnProperties['participant_fee_level'])) { |
7473db7f | 58 | $query->_select['participant_fee_level'] = 'civicrm_participant.fee_level as participant_fee_level'; |
6a488035 TO |
59 | $query->_element['participant_fee_level'] = 1; |
60 | } | |
61 | ||
e51b7372 | 62 | //add participant contact ID |
63 | if (!empty($query->_returnProperties['participant_contact_id'])) { | |
7473db7f | 64 | $query->_select['participant_contact_id'] = 'civicrm_participant.contact_id as participant_contact_id'; |
e51b7372 | 65 | $query->_element['participant_contact_id'] = 1; |
66 | } | |
67 | ||
6a488035 | 68 | //add fee amount |
a7488080 | 69 | if (!empty($query->_returnProperties['participant_fee_amount'])) { |
7473db7f | 70 | $query->_select['participant_fee_amount'] = 'civicrm_participant.fee_amount as participant_fee_amount'; |
6a488035 TO |
71 | $query->_element['participant_fee_amount'] = 1; |
72 | } | |
73 | ||
74 | //add fee currency | |
a7488080 | 75 | if (!empty($query->_returnProperties['participant_fee_currency'])) { |
6a488035 TO |
76 | $query->_select['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency"; |
77 | $query->_element['participant_fee_currency'] = 1; | |
78 | } | |
79 | ||
80 | //add event title also if event id is select | |
8cc574cf | 81 | if (!empty($query->_returnProperties['event_id']) || !empty($query->_returnProperties['event_title'])) { |
6a488035 TO |
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; | |
88 | } | |
89 | ||
90 | //add start date / end date | |
a7488080 | 91 | if (!empty($query->_returnProperties['event_start_date'])) { |
6a488035 TO |
92 | $query->_select['event_start_date'] = "civicrm_event.start_date as event_start_date"; |
93 | $query->_element['event_start_date'] = 1; | |
94 | } | |
95 | ||
a7488080 | 96 | if (!empty($query->_returnProperties['event_end_date'])) { |
6a488035 TO |
97 | $query->_select['event_end_date'] = "civicrm_event.end_date as event_end_date"; |
98 | $query->_element['event_end_date'] = 1; | |
99 | } | |
100 | ||
101 | //event type | |
a7488080 | 102 | if (!empty($query->_returnProperties['event_type'])) { |
6a488035 TO |
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; | |
107 | } | |
108 | ||
a7488080 | 109 | if (!empty($query->_returnProperties['event_type_id'])) { |
e4bee5e7 | 110 | $query->_select['event_type_id'] = "civicrm_event.event_type_id as event_type_id"; |
6a488035 | 111 | $query->_element['event_type_id'] = 1; |
e4bee5e7 PN |
112 | $query->_tables['civicrm_event'] = 1; |
113 | $query->_whereTables['civicrm_event'] = 1; | |
6a488035 TO |
114 | } |
115 | ||
f5d67b2a | 116 | //add status_id |
117 | if (!empty($query->_returnProperties['participant_status_id'])) { | |
118 | $query->_select['participant_status_id'] = "civicrm_participant.status_id as participant_status_id"; | |
6a488035 | 119 | $query->_element['participant_status_id'] = 1; |
6a488035 | 120 | $query->_tables['civicrm_participant'] = 1; |
f5d67b2a | 121 | $query->_whereTables['civicrm_participant'] = 1; |
122 | } | |
123 | ||
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; | |
6a488035 TO |
128 | $query->_tables['participant_status'] = 1; |
129 | $query->_whereTables['civicrm_participant'] = 1; | |
6a488035 TO |
130 | } |
131 | ||
f5d67b2a | 132 | //add participant_role_id |
133 | if (!empty($query->_returnProperties['participant_role_id'])) { | |
6a488035 TO |
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; | |
f5d67b2a | 137 | $query->_whereTables['civicrm_participant'] = 1; |
be2fb01f | 138 | $query->_pseudoConstantsSelect['participant_role_id'] = [ |
348b1fda | 139 | 'pseudoField' => 'participant_role_id', |
140 | 'idCol' => 'participant_role_id', | |
be2fb01f | 141 | ]; |
f5d67b2a | 142 | } |
143 | ||
144 | //add participant_role | |
145 | if (!empty($query->_returnProperties['participant_role'])) { | |
c0bcfbce | 146 | $query->_select['participant_role'] = "civicrm_participant.role_id as participant_role"; |
f5d67b2a | 147 | $query->_element['participant_role'] = 1; |
6a488035 TO |
148 | $query->_tables['participant_role'] = 1; |
149 | $query->_whereTables['civicrm_participant'] = 1; | |
be2fb01f | 150 | $query->_pseudoConstantsSelect['participant_role'] = [ |
348b1fda | 151 | 'pseudoField' => 'participant_role', |
152 | 'idCol' => 'participant_role', | |
be2fb01f | 153 | ]; |
6a488035 TO |
154 | } |
155 | ||
156 | //add register date | |
a7488080 | 157 | if (!empty($query->_returnProperties['participant_register_date'])) { |
6a488035 TO |
158 | $query->_select['participant_register_date'] = "civicrm_participant.register_date as participant_register_date"; |
159 | $query->_element['participant_register_date'] = 1; | |
160 | } | |
161 | ||
162 | //add source | |
a7488080 | 163 | if (!empty($query->_returnProperties['participant_source'])) { |
6a488035 TO |
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; | |
167 | } | |
168 | ||
169 | //participant note | |
a7488080 | 170 | if (!empty($query->_returnProperties['participant_note'])) { |
98a9c88c | 171 | $query->_select['participant_note'] = "participant_note.note as participant_note"; |
6a488035 TO |
172 | $query->_element['participant_note'] = 1; |
173 | $query->_tables['participant_note'] = 1; | |
98a9c88c | 174 | $query->_whereTables['participant_note'] = 1; |
6a488035 TO |
175 | } |
176 | ||
a7488080 | 177 | if (!empty($query->_returnProperties['participant_is_pay_later'])) { |
6a488035 TO |
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; | |
180 | } | |
181 | ||
a7488080 | 182 | if (!empty($query->_returnProperties['participant_is_test'])) { |
6a488035 TO |
183 | $query->_select['participant_is_test'] = "civicrm_participant.is_test as participant_is_test"; |
184 | $query->_element['participant_is_test'] = 1; | |
185 | } | |
186 | ||
a7488080 | 187 | if (!empty($query->_returnProperties['participant_registered_by_id'])) { |
6a488035 TO |
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; | |
190 | } | |
191 | ||
192 | // get discount name | |
a7488080 | 193 | if (!empty($query->_returnProperties['participant_discount_name'])) { |
6a488035 TO |
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; | |
200 | } | |
201 | ||
202 | //carry campaign id to selectors. | |
a7488080 | 203 | if (!empty($query->_returnProperties['participant_campaign_id'])) { |
6a488035 TO |
204 | $query->_select['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id'; |
205 | $query->_element['participant_campaign_id'] = 1; | |
206 | } | |
207 | } | |
208 | } | |
d75f2f47 | 209 | |
0cf587a7 | 210 | /** |
a43deb74 | 211 | * Get event related where clauses. |
212 | * | |
213 | * @param \CRM_Contact_BAO_Query $query | |
0cf587a7 | 214 | */ |
00be9182 | 215 | public static function where(&$query) { |
6a488035 | 216 | foreach (array_keys($query->_params) as $id) { |
a7488080 | 217 | if (empty($query->_params[$id][0])) { |
6a488035 TO |
218 | continue; |
219 | } | |
220 | if (substr($query->_params[$id][0], 0, 6) == 'event_' || | |
221 | substr($query->_params[$id][0], 0, 12) == 'participant_' | |
222 | ) { | |
2da8c6c4 | 223 | if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) { |
6a488035 TO |
224 | $query->_useDistinct = TRUE; |
225 | } | |
6a488035 TO |
226 | self::whereClauseSingle($query->_params[$id], $query); |
227 | } | |
228 | } | |
6a488035 TO |
229 | } |
230 | ||
0cf587a7 EM |
231 | /** |
232 | * @param $values | |
936ededf | 233 | * @param \CRM_Contact_BAO_Query $query |
a43deb74 | 234 | * |
235 | * @throws \CRM_Core_Exception | |
0cf587a7 | 236 | */ |
00be9182 | 237 | public static function whereClauseSingle(&$values, &$query) { |
f27fe817 | 238 | $checkPermission = empty($query->_skipPermission); |
6a488035 | 239 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3d6fe441 | 240 | $fields = array_merge(CRM_Event_BAO_Event::fields(), CRM_Event_BAO_Participant::exportableFields()); |
a43deb74 | 241 | $fieldSpec = $fields[$values[0]] ?? []; |
e51b7372 | 242 | |
6a488035 | 243 | switch ($name) { |
936ededf | 244 | case 'event_low': |
245 | case 'event_high': | |
246 | $query->dateQueryBuilder($values, | |
247 | 'civicrm_event', 'event', 'start_date', ts('Event Active On'), TRUE, 'YmdHis', 'end_date' | |
248 | ); | |
249 | return; | |
250 | ||
6a488035 TO |
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' | |
255 | ); | |
256 | return; | |
257 | ||
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' | |
262 | ); | |
263 | return; | |
d75f2f47 | 264 | |
62933949 | 265 | case 'event_include_repeating_events': |
266 | /** | |
267 | * Include Repeating Events | |
268 | */ | |
269 | //Get parent of this event | |
270 | $exEventId = ''; | |
a50a97b8 | 271 | if ($query->_where[$grouping]) { |
22e263ad | 272 | foreach ($query->_where[$grouping] as $key => $val) { |
62933949 | 273 | if (strstr($val, 'civicrm_event.id =')) { |
274 | $exEventId = $val; | |
275 | $extractEventId = explode(" ", $val); | |
276 | $value = $extractEventId[2]; | |
10b3cbde | 277 | $where = $query->_where[$grouping][$key]; |
62933949 | 278 | } |
948c107f | 279 | elseif (strstr($val, 'civicrm_event.id IN')) { |
f179424e | 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]; | |
10b3cbde | 283 | $where = $query->_where[$grouping][$key]; |
f179424e | 284 | } |
285 | } | |
286 | if ($exEventId) { | |
287 | $extractEventId = explode(" ", $exEventId); | |
288 | $value = $extractEventId[2]; | |
289 | } | |
948c107f | 290 | elseif (!empty($matches[1])) { |
f179424e | 291 | $value = $matches[1]; |
62933949 | 292 | } |
10b3cbde | 293 | $where = $query->_where[$grouping][$key]; |
62933949 | 294 | } |
295 | $thisEventHasParent = CRM_Core_BAO_RecurringEntity::getParentFor($value, 'civicrm_event'); | |
a50a97b8 | 296 | if ($thisEventHasParent) { |
a99e6f73 | 297 | $getAllConnections = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($thisEventHasParent, 'civicrm_event'); |
be2fb01f | 298 | $allEventIds = []; |
22e263ad | 299 | foreach ($getAllConnections as $key => $val) { |
a99e6f73 | 300 | $allEventIds[] = $val['id']; |
301 | } | |
a50a97b8 | 302 | if (!empty($allEventIds)) { |
62933949 | 303 | $op = "IN"; |
92fcb95f | 304 | $value = "(" . implode(",", $allEventIds) . ")"; |
62933949 | 305 | } |
306 | } | |
10b3cbde | 307 | $query->_where[$grouping][] = "{$where} OR civicrm_event.id $op {$value}"; |
d8f8b40f | 308 | $query->_qill[$grouping][] = ts('Include Repeating Events'); |
62933949 | 309 | $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1; |
310 | return; | |
d75f2f47 | 311 | |
3a6eb174 | 312 | case 'participant_is_test': |
313 | $key = array_search('civicrm_participant.is_test = 0', $query->_where[$grouping]); | |
314 | if (!empty($key)) { | |
315 | unset($query->_where[$grouping][$key]); | |
316 | } | |
6a488035 | 317 | case 'participant_test': |
28c666be CW |
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", | |
321 | $op, | |
322 | $value, | |
323 | "Boolean" | |
324 | ); | |
8e4742fc | 325 | |
326 | $isTest = $value ? 'a Test' : 'not a Test'; | |
be2fb01f | 327 | $query->_qill[$grouping][] = ts("Participant is %1", [1 => $isTest]); |
28c666be | 328 | $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1; |
6a488035 | 329 | } |
6a488035 TO |
330 | return; |
331 | ||
332 | case 'participant_fee_id': | |
72b84ded SL |
333 | $labels = []; |
334 | foreach ($value as $val) { | |
335 | $labels[] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label'); | |
6a488035 | 336 | } |
72b84ded SL |
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; | |
6a488035 TO |
340 | return; |
341 | ||
6a488035 TO |
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' | |
346 | ); | |
347 | return; | |
348 | ||
6a488035 | 349 | case 'participant_status_id': |
a43deb74 | 350 | $query->handleWhereFromMetadata($fieldSpec, $name, $value, $op); |
351 | return; | |
352 | ||
884fdc63 | 353 | case 'participant_status': |
e51b7372 | 354 | case 'participant_source': |
355 | case 'participant_id': | |
356 | case 'participant_contact_id': | |
3a6eb174 | 357 | case 'participant_is_pay_later': |
e51b7372 | 358 | case 'participant_fee_amount': |
359 | case 'participant_fee_level': | |
6f56dc34 | 360 | case 'participant_campaign_id': |
57a56c25 | 361 | case 'participant_registered_by_id': |
6f56dc34 | 362 | |
e51b7372 | 363 | $qillName = $name; |
be2fb01f | 364 | if (in_array($name, [ |
505f5439 | 365 | 'participant_source', |
366 | 'participant_id', | |
367 | 'participant_contact_id', | |
368 | 'participant_fee_amount', | |
369 | 'participant_fee_level', | |
370 | 'participant_is_pay_later', | |
371 | 'participant_campaign_id', | |
57a56c25 | 372 | 'participant_registered_by_id', |
90b461f1 | 373 | ])) { |
e51b7372 | 374 | $name = str_replace('participant_', '', $name); |
3a6eb174 | 375 | if ($name == 'is_pay_later') { |
376 | $qillName = $name; | |
377 | } | |
c0bcfbce | 378 | } |
379 | elseif ($name == 'participant_status') { | |
380 | $name = 'status_id'; | |
6a488035 TO |
381 | } |
382 | ||
e51b7372 | 383 | $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String'; |
f5d67b2a | 384 | $tableName = empty($tableName) ? 'civicrm_participant' : $tableName; |
505f5439 | 385 | if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
386 | $op = key($value); | |
e6d24997 | 387 | $value = $value[$op]; |
505f5439 | 388 | } |
c0bcfbce | 389 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name", $op, $value, $dataType); |
390 | ||
391 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op); | |
be2fb01f | 392 | $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]); |
c0bcfbce | 393 | $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1; |
394 | return; | |
395 | ||
396 | case 'participant_role': | |
397 | case 'participant_role_id': | |
8e153817 | 398 | $qillName = $name; |
399 | $name = 'role_id'; | |
c0bcfbce | 400 | |
52fa9e7e | 401 | $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String'; |
402 | $tableName = empty($tableName) ? 'civicrm_participant' : $tableName; | |
c0bcfbce | 403 | if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
404 | $op = key($value); | |
405 | $value = $value[$op]; | |
406 | } | |
407 | if (!strstr($op, 'NULL') && !strstr($op, 'EMPTY') && !strstr($op, 'LIKE')) { | |
408 | $regexOp = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT REGEXP' : 'REGEXP'; | |
4d795b6f | 409 | $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)"; |
c0bcfbce | 410 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.$name", $regexOp, $regexp, 'String'); |
411 | } | |
412 | else { | |
f5d67b2a | 413 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name", $op, $value, $dataType); |
6a488035 | 414 | } |
f5d67b2a | 415 | |
e51b7372 | 416 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op); |
be2fb01f | 417 | $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]); |
6a488035 TO |
418 | $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1; |
419 | return; | |
420 | ||
421 | case 'participant_register_date': | |
e520211a SL |
422 | case 'participant_register_date_high': |
423 | case 'participant_register_date_low': | |
6a488035 TO |
424 | $query->dateQueryBuilder($values, |
425 | 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date' | |
426 | ); | |
427 | return; | |
428 | ||
6a488035 | 429 | case 'event_id': |
3a6eb174 | 430 | case 'participant_event_id': |
431 | $name = str_replace('participant_', '', $name); | |
e51b7372 | 432 | case 'event_is_public': |
433 | case 'event_type_id': | |
22bb0e9d | 434 | case 'event_title': |
e51b7372 | 435 | $qillName = $name; |
be2fb01f | 436 | if (in_array($name, [ |
90b461f1 SL |
437 | 'event_id', |
438 | 'event_title', | |
439 | 'event_is_public', | |
440 | ])) { | |
e51b7372 | 441 | $name = str_replace('event_', '', $name); |
442 | } | |
443 | $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String'; | |
6a488035 | 444 | |
e51b7372 | 445 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.$name", $op, $value, $dataType); |
6a488035 | 446 | $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1; |
e51b7372 | 447 | if (!array_key_exists($qillName, $fields)) { |
448 | break; | |
449 | } | |
be2fb01f CW |
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]); | |
6a488035 | 452 | return; |
98a9c88c PF |
453 | |
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'); | |
be2fb01f | 458 | $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$name]['title'], 2 => $op, 3 => $value]); |
98a9c88c | 459 | break; |
6a488035 TO |
460 | } |
461 | } | |
462 | ||
0cf587a7 | 463 | /** |
100fef9d | 464 | * @param string $name |
0cf587a7 EM |
465 | * @param $mode |
466 | * @param $side | |
467 | * | |
468 | * @return null|string | |
469 | */ | |
00be9182 | 470 | public static function from($name, $mode, $side) { |
6a488035 TO |
471 | $from = NULL; |
472 | switch ($name) { | |
473 | case 'civicrm_participant': | |
7473db7f | 474 | $from = ' LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id '; |
6a488035 TO |
475 | break; |
476 | ||
477 | case 'civicrm_event': | |
d4152539 | 478 | //CRM-17121 |
7473db7f | 479 | $from = ' LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id '; |
6a488035 TO |
480 | break; |
481 | ||
482 | case 'event_type': | |
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 ) "; | |
485 | break; | |
486 | ||
487 | case 'participant_note': | |
98a9c88c PF |
488 | $from .= " $side JOIN civicrm_note participant_note ON ( participant_note.entity_table = 'civicrm_participant' AND |
489 | civicrm_participant.id = participant_note.entity_id )"; | |
6a488035 TO |
490 | break; |
491 | ||
492 | case 'participant_status': | |
493 | $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) "; | |
494 | break; | |
495 | ||
496 | case 'participant_role': | |
497 | $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')"; | |
348b1fda | 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) |
6a488035 TO |
499 | AND option_group_participant_role.id = participant_role.option_group_id ) "; |
500 | break; | |
501 | ||
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 ) "; | |
505 | break; | |
72b84ded SL |
506 | |
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'"; | |
509 | break; | |
6a488035 TO |
510 | } |
511 | return $from; | |
512 | } | |
513 | ||
0cf587a7 EM |
514 | /** |
515 | * @param $mode | |
516 | * @param bool $includeCustomFields | |
517 | * | |
518 | * @return array|null | |
519 | */ | |
505f5439 | 520 | public static function defaultReturnProperties( |
ddca8f33 | 521 | $mode, |
6a488035 TO |
522 | $includeCustomFields = TRUE |
523 | ) { | |
524 | $properties = NULL; | |
525 | if ($mode & CRM_Contact_BAO_Query::MODE_EVENT) { | |
be2fb01f | 526 | $properties = [ |
6a488035 TO |
527 | 'contact_type' => 1, |
528 | 'contact_sub_type' => 1, | |
529 | 'sort_name' => 1, | |
530 | 'display_name' => 1, | |
531 | 'event_id' => 1, | |
532 | 'event_title' => 1, | |
533 | 'event_start_date' => 1, | |
534 | 'event_end_date' => 1, | |
535 | 'event_type' => 1, | |
536 | 'participant_id' => 1, | |
537 | 'participant_status' => 1, | |
348b1fda | 538 | 'participant_status_id' => 1, |
539 | 'participant_role' => 1, | |
6a488035 TO |
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, | |
be2fb01f | 552 | ]; |
6a488035 TO |
553 | |
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; | |
560 | } | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | return $properties; | |
566 | } | |
567 | ||
6aac2db8 | 568 | /** |
61b4d091 | 569 | * Get the metadata for fields to be included on the grant search form. |
570 | * | |
571 | * @throws \CiviCRM_API3_Exception | |
572 | */ | |
573 | public static function getSearchFieldMetadata() { | |
574 | $fields = [ | |
575 | 'participant_status_id', | |
8a6fde27 | 576 | 'participant_register_date', |
936ededf | 577 | // Super-weird but we have to make it work..... |
578 | 'event', | |
61b4d091 | 579 | ]; |
580 | $metadata = civicrm_api3('Participant', 'getfields', [])['values']; | |
936ededf | 581 | $metadata['event'] = self::getPseudoEventDateFieldMetadata(); |
61b4d091 | 582 | return array_intersect_key($metadata, array_flip($fields)); |
583 | } | |
584 | ||
585 | /** | |
586 | * Build the event search form. | |
587 | * | |
588 | * @param \CRM_Event_Form_Search $form | |
589 | * | |
590 | * @throws \CiviCRM_API3_Exception | |
591 | * @throws \CRM_Core_Exception | |
6aac2db8 | 592 | */ |
00be9182 | 593 | public static function buildSearchForm(&$form) { |
61b4d091 | 594 | $form->addSearchFieldMetadata(['Participant' => self::getSearchFieldMetadata()]); |
595 | $form->addFormFieldsFromMetadata(); | |
6a488035 TO |
596 | $dataURLEventFee = CRM_Utils_System::url('civicrm/ajax/eventFee', |
597 | "reset=1", | |
598 | FALSE, NULL, FALSE | |
599 | ); | |
600 | ||
6a488035 TO |
601 | $form->assign('dataURLEventFee', $dataURLEventFee); |
602 | ||
be2fb01f | 603 | $form->addEntityRef('event_id', ts('Event Name'), [ |
90b461f1 SL |
604 | 'entity' => 'Event', |
605 | 'placeholder' => ts('- any -'), | |
606 | 'multiple' => 1, | |
607 | 'select' => ['minimumInputLength' => 0], | |
608 | ]); | |
be2fb01f | 609 | $form->addEntityRef('event_type_id', ts('Event Type'), [ |
90b461f1 SL |
610 | 'entity' => 'OptionValue', |
611 | 'placeholder' => ts('- any -'), | |
612 | 'select' => ['minimumInputLength' => 0], | |
613 | 'api' => [ | |
614 | 'params' => ['option_group_id' => 'event_type'], | |
615 | ], | |
616 | ]); | |
f179424e | 617 | $obj = new CRM_Report_Form_Event_ParticipantListing(); |
618 | $form->add('select', 'participant_fee_id', | |
619 | ts('Fee Level'), | |
620 | $obj->getPriceLevels(), | |
be2fb01f | 621 | FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')] |
f179424e | 622 | ); |
6a488035 | 623 | |
be2fb01f | 624 | $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', [1 => '<em>%1</em>'])); |
6a488035 | 625 | |
e51b7372 | 626 | $form->addSelect('participant_role_id', |
be2fb01f | 627 | [ |
505f5439 | 628 | 'entity' => 'participant', |
629 | 'label' => ts('Participant Role'), | |
630 | 'multiple' => 'multiple', | |
631 | 'option_url' => NULL, | |
9b81f881 | 632 | 'placeholder' => ts('- any -'), |
be2fb01f | 633 | ] |
e51b7372 | 634 | ); |
6a488035 | 635 | |
8a4f27dc | 636 | $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE); |
3a6eb174 | 637 | $form->addYesNo('participant_is_pay_later', ts('Participant is Pay Later?'), TRUE); |
be2fb01f CW |
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]); | |
6a488035 TO |
640 | |
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'); | |
86a0d21e | 643 | |
be2fb01f | 644 | self::addCustomFormFields($form, ['Participant', 'Event']); |
6a488035 TO |
645 | |
646 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'participant_campaign_id'); | |
d75f2f47 | 647 | |
6a488035 | 648 | $form->assign('validCiviEvent', TRUE); |
be2fb01f | 649 | $form->setDefaults(['participant_test' => 0]); |
6a488035 TO |
650 | } |
651 | ||
0cf587a7 EM |
652 | /** |
653 | * @param $tables | |
654 | */ | |
00be9182 | 655 | public static function tableNames(&$tables) { |
6a488035 | 656 | //add participant table |
a7488080 | 657 | if (!empty($tables['civicrm_event'])) { |
be2fb01f | 658 | $tables = array_merge(['civicrm_participant' => 1], $tables); |
6a488035 TO |
659 | } |
660 | } | |
96025800 | 661 | |
738d4d0b | 662 | /** |
936ededf | 663 | * Get metadata from pseudo search field 'event'. |
738d4d0b | 664 | * |
936ededf | 665 | * @return array |
738d4d0b | 666 | */ |
936ededf | 667 | protected static function getPseudoEventDateFieldMetadata(): array { |
668 | return [ | |
669 | 'name' => 'event', | |
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'], | |
676 | ]; | |
738d4d0b SB |
677 | } |
678 | ||
6a488035 | 679 | } |