Merge pull request #7978 from eileenmcnaughton/CRM-18240
[civicrm-core.git] / CRM / Event / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35 class CRM_Event_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 = array();
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
54 /**
55 * @return array
56 */
57 public static function &getParticipantFields() {
58 $fields = CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
59 return $fields;
60 }
61
62 /**
63 * Build select for CiviEvent.
64 *
65 * @param $query
66 */
67 public static function select(&$query) {
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
76 if (!empty($query->_returnProperties['participant_fee_level'])) {
77 $query->_select['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
78 $query->_element['participant_fee_level'] = 1;
79 }
80
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
87 //add fee amount
88 if (!empty($query->_returnProperties['participant_fee_amount'])) {
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
94 if (!empty($query->_returnProperties['participant_fee_currency'])) {
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
100 if (!empty($query->_returnProperties['event_id']) || !empty($query->_returnProperties['event_title'])) {
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
110 if (!empty($query->_returnProperties['event_start_date'])) {
111 $query->_select['event_start_date'] = "civicrm_event.start_date as event_start_date";
112 $query->_element['event_start_date'] = 1;
113 }
114
115 if (!empty($query->_returnProperties['event_end_date'])) {
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
121 if (!empty($query->_returnProperties['event_type'])) {
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
128 if (!empty($query->_returnProperties['event_type_id'])) {
129 $query->_select['event_type_id'] = "event_type.id as event_type_id";
130 $query->_element['event_type_id'] = 1;
131 $query->_tables['event_type'] = 1;
132 $query->_whereTables['event_type'] = 1;
133 }
134
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";
138 $query->_element['participant_status_id'] = 1;
139 $query->_tables['civicrm_participant'] = 1;
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;
147 $query->_tables['participant_status'] = 1;
148 $query->_whereTables['civicrm_participant'] = 1;
149 }
150
151 //add participant_role_id
152 if (!empty($query->_returnProperties['participant_role_id'])) {
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;
156 $query->_whereTables['civicrm_participant'] = 1;
157 $query->_pseudoConstantsSelect['participant_role_id'] = array(
158 'pseudoField' => 'participant_role_id',
159 'idCol' => 'participant_role_id',
160 );
161 }
162
163 //add participant_role
164 if (!empty($query->_returnProperties['participant_role'])) {
165 $query->_select['participant_role'] = "civicrm_participant.role_id as participant_role";
166 $query->_element['participant_role'] = 1;
167 $query->_tables['participant_role'] = 1;
168 $query->_whereTables['civicrm_participant'] = 1;
169 $query->_pseudoConstantsSelect['participant_role'] = array(
170 'pseudoField' => 'participant_role',
171 'idCol' => 'participant_role',
172 );
173 }
174
175 //add register date
176 if (!empty($query->_returnProperties['participant_register_date'])) {
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
182 if (!empty($query->_returnProperties['participant_source'])) {
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
189 if (!empty($query->_returnProperties['participant_note'])) {
190 $query->_select['participant_note'] = "civicrm_note.note as participant_note";
191 $query->_element['participant_note'] = 1;
192 $query->_tables['participant_note'] = 1;
193 $query->_whereTables['civicrm_note'] = 1;
194 }
195
196 if (!empty($query->_returnProperties['participant_is_pay_later'])) {
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
201 if (!empty($query->_returnProperties['participant_is_test'])) {
202 $query->_select['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
203 $query->_element['participant_is_test'] = 1;
204 }
205
206 if (!empty($query->_returnProperties['participant_registered_by_id'])) {
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
212 if (!empty($query->_returnProperties['participant_discount_name'])) {
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.
222 if (!empty($query->_returnProperties['participant_campaign_id'])) {
223 $query->_select['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
224 $query->_element['participant_campaign_id'] = 1;
225 }
226 }
227 }
228
229
230 /**
231 * @param $query
232 */
233 public static function where(&$query) {
234 $grouping = NULL;
235 foreach (array_keys($query->_params) as $id) {
236 if (empty($query->_params[$id][0])) {
237 continue;
238 }
239 if (substr($query->_params[$id][0], 0, 6) == 'event_' ||
240 substr($query->_params[$id][0], 0, 12) == 'participant_'
241 ) {
242 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
243 $query->_useDistinct = TRUE;
244 }
245 $grouping = $query->_params[$id][3];
246 self::whereClauseSingle($query->_params[$id], $query);
247 }
248 }
249 }
250
251 /**
252 * @param $values
253 * @param $query
254 */
255 public static function whereClauseSingle(&$values, &$query) {
256 list($name, $op, $value, $grouping, $wildcard) = $values;
257 $fields = array_merge(CRM_Event_BAO_Event::fields(), CRM_Event_BAO_Participant::exportableFields());
258
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;
273
274 case 'event_include_repeating_events':
275 /**
276 * Include Repeating Events
277 */
278 //Get parent of this event
279 $exEventId = '';
280 if ($query->_where[$grouping]) {
281 foreach ($query->_where[$grouping] as $key => $val) {
282 if (strstr($val, 'civicrm_event.id =')) {
283 $exEventId = $val;
284 $extractEventId = explode(" ", $val);
285 $value = $extractEventId[2];
286 $where = $query->_where[$grouping][$key];
287 }
288 elseif (strstr($val, 'civicrm_event.id IN')) {
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];
292 $where = $query->_where[$grouping][$key];
293 }
294 }
295 if ($exEventId) {
296 $extractEventId = explode(" ", $exEventId);
297 $value = $extractEventId[2];
298 }
299 elseif (!empty($matches[1])) {
300 $value = $matches[1];
301 }
302 $where = $query->_where[$grouping][$key];
303 }
304 $thisEventHasParent = CRM_Core_BAO_RecurringEntity::getParentFor($value, 'civicrm_event');
305 if ($thisEventHasParent) {
306 $getAllConnections = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($thisEventHasParent, 'civicrm_event');
307 $allEventIds = array();
308 foreach ($getAllConnections as $key => $val) {
309 $allEventIds[] = $val['id'];
310 }
311 if (!empty($allEventIds)) {
312 $op = "IN";
313 $value = "(" . implode(",", $allEventIds) . ")";
314 }
315 }
316 $query->_where[$grouping][] = "{$where} OR civicrm_event.id $op {$value}";
317 $query->_qill[$grouping][] = ts('Include Repeating Events');
318 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
319 return;
320
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 }
326 case 'participant_test':
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 );
334
335 $isTest = $value ? 'a Test' : 'not a Test';
336 $query->_qill[$grouping][] = ts("Participant is %1", array(1 => $isTest));
337 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
338 }
339 return;
340
341 case 'participant_fee_id':
342 foreach ($value as $k => &$val) {
343 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
344 $val = CRM_Core_DAO::escapeString(trim($val));
345 }
346 $feeLabel = implode('|', $value);
347 $query->_where[$grouping][] = "civicrm_participant.fee_level REGEXP '{$feeLabel}'";
348 $query->_qill[$grouping][] = ts("Fee level") . " IN " . implode(', ', $value);
349 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
350 return;
351
352 case 'participant_fee_amount_high':
353 case 'participant_fee_amount_low':
354 $query->numberRangeBuilder($values,
355 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
356 );
357 return;
358
359 case 'participant_status_id':
360 if ($value && is_array($value) && strpos($op, 'IN') === FALSE) {
361 $op = 'IN';
362 }
363 case 'participant_status':
364 case 'participant_source':
365 case 'participant_id':
366 case 'participant_contact_id':
367 case 'participant_is_pay_later':
368 case 'participant_fee_amount':
369 case 'participant_fee_level':
370 case 'participant_campaign_id':
371
372 $qillName = $name;
373 if (in_array($name, array(
374 'participant_status_id',
375 'participant_source',
376 'participant_id',
377 'participant_contact_id',
378 'participant_fee_amount',
379 'participant_fee_level',
380 'participant_is_pay_later',
381 'participant_campaign_id',
382 ))
383 ) {
384 $name = str_replace('participant_', '', $name);
385 if ($name == 'is_pay_later') {
386 $qillName = $name;
387 }
388 }
389 elseif ($name == 'participant_status') {
390 $name = 'status_id';
391 }
392
393 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
394 $tableName = empty($tableName) ? 'civicrm_participant' : $tableName;
395 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
396 $op = key($value);
397 $value = $value[$op];
398 }
399 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name", $op, $value, $dataType);
400
401 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
402 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
403 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
404 return;
405
406 case 'participant_role':
407 case 'participant_role_id':
408 $qillName = $name;
409 $name = 'role_id';
410
411 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
412 $op = key($value);
413 $value = $value[$op];
414 }
415 if (!strstr($op, 'NULL') && !strstr($op, 'EMPTY') && !strstr($op, 'LIKE')) {
416 $regexOp = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT REGEXP' : 'REGEXP';
417 $regexp = "[[:cntrl:]]*" . implode('[[:>:]]*|[[:<:]]*', (array) $value) . "[[:cntrl:]]*";
418 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.$name", $regexOp, $regexp, 'String');
419 }
420 else {
421 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name", $op, $value, $dataType);
422 }
423
424 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
425 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
426 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
427 return;
428
429 case 'participant_register_date':
430 case 'participant_register_date_high':
431 case 'participant_register_date_low':
432 $query->dateQueryBuilder($values,
433 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
434 );
435 return;
436
437 case 'event_id':
438 case 'participant_event_id':
439 $name = str_replace('participant_', '', $name);
440 case 'event_is_public':
441 case 'event_type_id':
442 case 'event_title':
443 $qillName = $name;
444 if (in_array($name, array(
445 'event_id',
446 'event_title',
447 'event_is_public',
448 )
449 )
450 ) {
451 $name = str_replace('event_', '', $name);
452 }
453 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
454
455 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.$name", $op, $value, $dataType);
456 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
457 if (!array_key_exists($qillName, $fields)) {
458 break;
459 }
460 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op);
461 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
462 return;
463 }
464 }
465
466 /**
467 * @param string $name
468 * @param $mode
469 * @param $side
470 *
471 * @return null|string
472 */
473 public static function from($name, $mode, $side) {
474 $from = NULL;
475 switch ($name) {
476 case 'civicrm_participant':
477 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
478 break;
479
480 case 'civicrm_event':
481 //CRM-17121
482 $from = " LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
483 break;
484
485 case 'event_type':
486 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
487 $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 ) ";
488 break;
489
490 case 'participant_note':
491 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
492 civicrm_participant.id = civicrm_note.entity_id )";
493 break;
494
495 case 'participant_status':
496 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
497 break;
498
499 case 'participant_role':
500 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
501 $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)
502 AND option_group_participant_role.id = participant_role.option_group_id ) ";
503 break;
504
505 case 'participant_discount_name':
506 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
507 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
508 break;
509 }
510 return $from;
511 }
512
513 /**
514 * Getter for the qill object.
515 *
516 * @return string
517 */
518 public function qill() {
519 return (isset($this->_qill)) ? $this->_qill : "";
520 }
521
522 /**
523 * @param $mode
524 * @param bool $includeCustomFields
525 *
526 * @return array|null
527 */
528 public static function defaultReturnProperties(
529 $mode,
530 $includeCustomFields = TRUE
531 ) {
532 $properties = NULL;
533 if ($mode & CRM_Contact_BAO_Query::MODE_EVENT) {
534 $properties = array(
535 'contact_type' => 1,
536 'contact_sub_type' => 1,
537 'sort_name' => 1,
538 'display_name' => 1,
539 'event_id' => 1,
540 'event_title' => 1,
541 'event_start_date' => 1,
542 'event_end_date' => 1,
543 'event_type' => 1,
544 'participant_id' => 1,
545 'participant_status' => 1,
546 'participant_status_id' => 1,
547 'participant_role' => 1,
548 'participant_role_id' => 1,
549 'participant_note' => 1,
550 'participant_register_date' => 1,
551 'participant_source' => 1,
552 'participant_fee_level' => 1,
553 'participant_is_test' => 1,
554 'participant_is_pay_later' => 1,
555 'participant_fee_amount' => 1,
556 'participant_discount_name' => 1,
557 'participant_fee_currency' => 1,
558 'participant_registered_by_id' => 1,
559 'participant_campaign_id' => 1,
560 );
561
562 if ($includeCustomFields) {
563 // also get all the custom participant properties
564 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Participant');
565 if (!empty($fields)) {
566 foreach ($fields as $name => $dontCare) {
567 $properties[$name] = 1;
568 }
569 }
570 }
571 }
572
573 return $properties;
574 }
575
576 /**
577 * @param CRM_Core_Form $form
578 */
579 public static function buildSearchForm(&$form) {
580 $dataURLEventFee = CRM_Utils_System::url('civicrm/ajax/eventFee',
581 "reset=1",
582 FALSE, NULL, FALSE
583 );
584
585 $form->assign('dataURLEventFee', $dataURLEventFee);
586
587 $form->addEntityRef('event_id', ts('Event Name'), array(
588 'entity' => 'event',
589 'placeholder' => ts('- any -'),
590 'multiple' => 1,
591 'select' => array('minimumInputLength' => 0),
592 )
593 );
594 $form->addEntityRef('event_type_id', ts('Event Type'), array(
595 'entity' => 'option_value',
596 'placeholder' => ts('- any -'),
597 'select' => array('minimumInputLength' => 0),
598 'api' => array(
599 'params' => array('option_group_id' => 'event_type'),
600 ),
601 )
602 );
603 $obj = new CRM_Report_Form_Event_ParticipantListing();
604 $form->add('select', 'participant_fee_id',
605 ts('Fee Level'),
606 $obj->getPriceLevels(),
607 FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -'))
608 );
609
610 CRM_Core_Form_Date::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
611
612 CRM_Core_Form_Date::buildDateRange($form, 'participant', 1, '_register_date_low', '_register_date_high', ts('From'), FALSE);
613
614 $form->addElement('hidden', 'event_date_range_error');
615 $form->addElement('hidden', 'participant_date_range_error');
616 $form->addFormRule(array('CRM_Event_BAO_Query', 'formRule'), $form);
617
618 $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', array(1 => '<em>%1</em>')));
619
620 $form->addSelect('participant_status_id',
621 array(
622 'entity' => 'participant',
623 'label' => ts('Participant Status'),
624 'multiple' => 'multiple',
625 'option_url' => NULL,
626 'placeholder' => ts('- any -'),
627 )
628 );
629
630 $form->addSelect('participant_role_id',
631 array(
632 'entity' => 'participant',
633 'label' => ts('Participant Role'),
634 'multiple' => 'multiple',
635 'option_url' => NULL,
636 'placeholder' => ts('- any -'),
637 )
638 );
639
640 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
641 $form->addYesNo('participant_is_pay_later', ts('Participant is Pay Later?'), TRUE);
642 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
643 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
644
645 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
646 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
647 // add all the custom searchable fields
648 $extends = array('Participant', 'Event');
649 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
650 if ($groupDetails) {
651 $form->assign('participantGroupTree', $groupDetails);
652 foreach ($groupDetails as $group) {
653 foreach ($group['fields'] as $field) {
654 $fieldId = $field['id'];
655 $elementName = 'custom_' . $fieldId;
656 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, TRUE);
657 }
658 }
659 }
660
661 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'participant_campaign_id');
662
663 $form->assign('validCiviEvent', TRUE);
664 $form->setDefaults(array('participant_test' => 0));
665 }
666
667 /**
668 * @param $row
669 * @param int $id
670 */
671 public static function searchAction(&$row, $id) {
672 }
673
674 /**
675 * @param $tables
676 */
677 public static function tableNames(&$tables) {
678 //add participant table
679 if (!empty($tables['civicrm_event'])) {
680 $tables = array_merge(array('civicrm_participant' => 1), $tables);
681 }
682 }
683
684 /**
685 * Check if the values in the date range are in correct chronological order.
686 *
687 * @todo Get this to work with CRM_Utils_Rule::validDateRange
688 *
689 * @param array $fields
690 * @param array $files
691 * @param CRM_Core_Form $form
692 *
693 * @return bool|array
694 */
695 public static function formRule($fields, $files, $form) {
696 $errors = array();
697
698 if ((empty($fields['event_start_date_low']) || empty($fields['event_end_date_high'])) && (empty($fields['participant_register_date_low']) || empty($fields['participant_register_date_high']))) {
699 return TRUE;
700 }
701 $lowDate = strtotime($fields['event_start_date_low']);
702 $highDate = strtotime($fields['event_end_date_high']);
703
704 if ($lowDate > $highDate) {
705 $errors['event_date_range_error'] = ts('Please check that your Event Date Range is in correct chronological order.');
706 }
707
708 $lowDate1 = strtotime($fields['participant_register_date_low']);
709 $highDate1 = strtotime($fields['participant_register_date_high']);
710
711 if ($lowDate1 > $highDate1) {
712 $errors['participant_date_range_error'] = ts('Please check that your Registration Date Range is in correct chronological order.');
713 }
714 return empty($errors) ? TRUE : $errors;
715 }
716
717 }