3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Event_BAO_Query
{
40 public static function &getFields() {
42 $fields = array_merge($fields, CRM_Event_DAO_Event
::import());
43 $fields = array_merge($fields, self
::getParticipantFields());
44 $fields = array_merge($fields, CRM_Core_DAO_Discount
::export());
52 public static function &getParticipantFields() {
53 $fields = CRM_Event_BAO_Participant
::importableFields('Individual', TRUE, TRUE);
58 * Build select for CiviEvent.
64 public static function select(&$query) {
65 if (($query->_mode
& CRM_Contact_BAO_Query
::MODE_EVENT
) ||
66 CRM_Contact_BAO_Query
::componentPresent($query->_returnProperties
, 'participant_')
68 $query->_select
['participant_id'] = "civicrm_participant.id as participant_id";
69 $query->_element
['participant_id'] = 1;
70 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
73 if (!empty($query->_returnProperties
['participant_fee_level'])) {
74 $query->_select
['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
75 $query->_element
['participant_fee_level'] = 1;
78 //add participant contact ID
79 if (!empty($query->_returnProperties
['participant_contact_id'])) {
80 $query->_select
['participant_contact_id'] = "civicrm_participant.contact_id as participant_contact_id";
81 $query->_element
['participant_contact_id'] = 1;
85 if (!empty($query->_returnProperties
['participant_fee_amount'])) {
86 $query->_select
['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
87 $query->_element
['participant_fee_amount'] = 1;
91 if (!empty($query->_returnProperties
['participant_fee_currency'])) {
92 $query->_select
['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
93 $query->_element
['participant_fee_currency'] = 1;
96 //add event title also if event id is select
97 if (!empty($query->_returnProperties
['event_id']) ||
!empty($query->_returnProperties
['event_title'])) {
98 $query->_select
['event_id'] = "civicrm_event.id as event_id";
99 $query->_select
['event_title'] = "civicrm_event.title as event_title";
100 $query->_element
['event_id'] = 1;
101 $query->_element
['event_title'] = 1;
102 $query->_tables
['civicrm_event'] = 1;
103 $query->_whereTables
['civicrm_event'] = 1;
106 //add start date / end date
107 if (!empty($query->_returnProperties
['event_start_date'])) {
108 $query->_select
['event_start_date'] = "civicrm_event.start_date as event_start_date";
109 $query->_element
['event_start_date'] = 1;
112 if (!empty($query->_returnProperties
['event_end_date'])) {
113 $query->_select
['event_end_date'] = "civicrm_event.end_date as event_end_date";
114 $query->_element
['event_end_date'] = 1;
118 if (!empty($query->_returnProperties
['event_type'])) {
119 $query->_select
['event_type'] = "event_type.label as event_type";
120 $query->_element
['event_type'] = 1;
121 $query->_tables
['event_type'] = 1;
122 $query->_whereTables
['event_type'] = 1;
125 if (!empty($query->_returnProperties
['event_type_id'])) {
126 $query->_select
['event_type_id'] = "event_type.id as event_type_id";
127 $query->_element
['event_type_id'] = 1;
128 $query->_tables
['event_type'] = 1;
129 $query->_whereTables
['event_type'] = 1;
132 //add status and status_id
133 if (!empty($query->_returnProperties
['participant_status']) ||
!empty($query->_returnProperties
['participant_status_id'])) {
134 $query->_select
['participant_status'] = "participant_status.label as participant_status";
135 $query->_select
['participant_status_id'] = "participant_status.id as participant_status_id";
136 $query->_element
['participant_status_id'] = 1;
137 $query->_element
['participant_status'] = 1;
138 $query->_tables
['civicrm_participant'] = 1;
139 $query->_tables
['participant_status'] = 1;
140 $query->_whereTables
['civicrm_participant'] = 1;
141 $query->_whereTables
['participant_status'] = 1;
142 $query->_pseudoConstantsSelect
['participant_status'] = array(
143 'pseudoField' => 'participant_status',
144 'idCol' => 'participant_status_id',
148 //add participant_role and participant_role_id
149 if (!empty($query->_returnProperties
['participant_role']) ||
!empty($query->_returnProperties
['participant_role_id'])) {
150 $query->_select
['participant_role'] = "participant_role.label as participant_role";
151 $query->_select
['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
152 $query->_element
['participant_role'] = 1;
153 $query->_element
['participant_role_id'] = 1;
154 $query->_tables
['civicrm_participant'] = 1;
155 $query->_tables
['participant_role'] = 1;
156 $query->_whereTables
['civicrm_participant'] = 1;
157 $query->_whereTables
['participant_role'] = 1;
158 $query->_pseudoConstantsSelect
['participant_role'] = array(
159 'pseudoField' => 'participant_role',
160 'idCol' => 'participant_role_id',
165 if (!empty($query->_returnProperties
['participant_register_date'])) {
166 $query->_select
['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
167 $query->_element
['participant_register_date'] = 1;
171 if (!empty($query->_returnProperties
['participant_source'])) {
172 $query->_select
['participant_source'] = "civicrm_participant.source as participant_source";
173 $query->_element
['participant_source'] = 1;
174 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
178 if (!empty($query->_returnProperties
['participant_note'])) {
179 $query->_select
['participant_note'] = "civicrm_note.note as participant_note";
180 $query->_element
['participant_note'] = 1;
181 $query->_tables
['participant_note'] = 1;
182 $query->_whereTables
['civicrm_note'] = 1;
185 if (!empty($query->_returnProperties
['participant_is_pay_later'])) {
186 $query->_select
['participant_is_pay_later'] = "civicrm_participant.is_pay_later as participant_is_pay_later";
187 $query->_element
['participant_is_pay_later'] = 1;
190 if (!empty($query->_returnProperties
['participant_is_test'])) {
191 $query->_select
['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
192 $query->_element
['participant_is_test'] = 1;
195 if (!empty($query->_returnProperties
['participant_registered_by_id'])) {
196 $query->_select
['participant_registered_by_id'] = "civicrm_participant.registered_by_id as participant_registered_by_id";
197 $query->_element
['participant_registered_by_id'] = 1;
201 if (!empty($query->_returnProperties
['participant_discount_name'])) {
202 $query->_select
['participant_discount_name'] = "discount_name.title as participant_discount_name";
203 $query->_element
['participant_discount_name'] = 1;
204 $query->_tables
['civicrm_discount'] = 1;
205 $query->_tables
['participant_discount_name'] = 1;
206 $query->_whereTables
['civicrm_discount'] = 1;
207 $query->_whereTables
['participant_discount_name'] = 1;
210 //carry campaign id to selectors.
211 if (!empty($query->_returnProperties
['participant_campaign_id'])) {
212 $query->_select
['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
213 $query->_element
['participant_campaign_id'] = 1;
222 public static function where(&$query) {
224 foreach (array_keys($query->_params
) as $id) {
225 if (empty($query->_params
[$id][0])) {
228 if (substr($query->_params
[$id][0], 0, 6) == 'event_' ||
229 substr($query->_params
[$id][0], 0, 12) == 'participant_'
231 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
232 $query->_useDistinct
= TRUE;
234 $grouping = $query->_params
[$id][3];
235 self
::whereClauseSingle($query->_params
[$id], $query);
244 public static function whereClauseSingle(&$values, &$query) {
245 list($name, $op, $value, $grouping, $wildcard) = $values;
246 $fields = array_merge(CRM_Event_BAO_Event
::fields(), CRM_Event_BAO_Participant
::exportableFields());
249 case 'event_start_date_low':
250 case 'event_start_date_high':
251 $query->dateQueryBuilder($values,
252 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
256 case 'event_end_date_low':
257 case 'event_end_date_high':
258 $query->dateQueryBuilder($values,
259 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
263 case 'event_include_repeating_events':
265 * Include Repeating Events
267 //Get parent of this event
269 if ($query->_where
[$grouping]) {
270 foreach ($query->_where
[$grouping] as $key => $val) {
271 if (strstr($val, 'civicrm_event.id =')) {
273 $extractEventId = explode(" ", $val);
274 $value = $extractEventId[2];
275 unset($query->_where
[$grouping][$key]);
278 $extractEventId = explode(" ", $exEventId);
279 $value = $extractEventId[2];
280 unset($query->_where
[$grouping][$key]);
282 $thisEventHasParent = CRM_Core_BAO_RecurringEntity
::getParentFor($value, 'civicrm_event');
283 if ($thisEventHasParent) {
284 $getAllConnections = CRM_Core_BAO_RecurringEntity
::getEntitiesForParent($thisEventHasParent, 'civicrm_event');
285 $allEventIds = array();
286 foreach ($getAllConnections as $key => $val) {
287 $allEventIds[] = $val['id'];
289 if (!empty($allEventIds)) {
291 $value = "(" . implode(",", $allEventIds) . ")";
294 $query->_where
[$grouping][] = "civicrm_event.id $op {$value}";
295 $query->_qill
[$grouping][] = ts('Include Repeating Events');
296 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
299 case 'participant_is_test':
300 $key = array_search('civicrm_participant.is_test = 0', $query->_where
[$grouping]);
302 unset($query->_where
[$grouping][$key]);
304 case 'participant_test':
305 // We dont want to include all tests for sql OR CRM-7827
306 if (!$value ||
$query->getOperator() != 'OR') {
307 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_test",
313 $query->_qill
[$grouping][] = ts("Participant is a Test");
315 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
319 case 'participant_fee_id':
320 $feeLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $value, 'label');
321 $feeLabel = CRM_Core_DAO
::escapeString(trim($feeLabel));
323 $query->_where
[$grouping][] = "civicrm_participant.fee_level LIKE '%$feeLabel%'";
324 $query->_qill
[$grouping][] = ts("Fee level") . " contains $feeLabel";
326 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
329 case 'participant_fee_amount_high':
330 case 'participant_fee_amount_low':
331 $query->numberRangeBuilder($values,
332 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
336 case 'participant_status_id':
337 case 'participant_role_id':
338 if ($value && is_array($value) && strpos($op, 'IN') === FALSE) {
341 case 'participant_status':
342 case 'participant_role':
343 case 'participant_source':
344 case 'participant_id':
345 case 'participant_contact_id':
346 case 'participant_is_pay_later':
347 case 'participant_fee_amount':
348 case 'participant_fee_level':
350 if (in_array($name, array(
351 'participant_status_id',
352 'participant_role_id',
353 'participant_source',
355 'participant_contact_id',
356 'participant_fee_amount',
357 'participant_fee_level',
358 'participant_is_pay_later',
360 $name = str_replace('participant_', '', $name);
361 if ($name == 'is_pay_later') {
364 if ($name == 'role_id') {
365 $qillName = 'participant_role';
366 $query->_where
[$grouping][] = " civicrm_participant.$name REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', (array) $value) . "[[:>:]]' ";
370 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
372 if (in_array($name, array('participant_status', 'participant_role'))) {
373 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("$name.label", $op, $value, $dataType);
375 elseif ($name != 'role_id') {
376 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.$name", $op, $value, $dataType);
378 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
379 $query->_qill
[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
380 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
383 case 'participant_register_date':
384 $query->dateQueryBuilder($values,
385 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
390 case 'participant_event_id':
391 $name = str_replace('participant_', '', $name);
392 case 'event_is_public':
393 case 'event_type_id':
396 if (in_array($name, array(
402 $name = str_replace('event_', '', $name);
404 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
406 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.$name", $op, $value, $dataType);
407 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
408 if (!array_key_exists($qillName, $fields)) {
411 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op);
412 $query->_qill
[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
415 case 'participant_campaign_id':
416 if (CRM_Utils_Array
::value($op, $value)) {
417 $value = $value[$op];
421 'campaign' => $value,
422 'grouping' => $grouping,
423 'tableName' => 'civicrm_participant',
425 CRM_Campaign_BAO_Query
::componentSearchClause($campParams, $query);
431 * @param string $name
435 * @return null|string
437 public static function from($name, $mode, $side) {
440 case 'civicrm_participant':
441 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
444 case 'civicrm_event':
445 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
449 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
450 $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 ) ";
453 case 'participant_note':
454 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
455 civicrm_participant.id = civicrm_note.entity_id )";
458 case 'participant_status':
459 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
462 case 'participant_role':
463 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
464 $from .= " $side JOIN civicrm_option_value participant_role ON (civicrm_participant.role_id = participant_role.value
465 AND option_group_participant_role.id = participant_role.option_group_id ) ";
468 case 'participant_discount_name':
469 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
470 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
477 * Getter for the qill object.
481 public function qill() {
482 return (isset($this->_qill
)) ?
$this->_qill
: "";
487 * @param bool $includeCustomFields
491 public static function defaultReturnProperties(
493 $includeCustomFields = TRUE
496 if ($mode & CRM_Contact_BAO_Query
::MODE_EVENT
) {
499 'contact_sub_type' => 1,
504 'event_start_date' => 1,
505 'event_end_date' => 1,
507 'participant_id' => 1,
508 'participant_status' => 1,
509 'participant_role_id' => 1,
510 'participant_note' => 1,
511 'participant_register_date' => 1,
512 'participant_source' => 1,
513 'participant_fee_level' => 1,
514 'participant_is_test' => 1,
515 'participant_is_pay_later' => 1,
516 'participant_fee_amount' => 1,
517 'participant_discount_name' => 1,
518 'participant_fee_currency' => 1,
519 'participant_registered_by_id' => 1,
520 'participant_campaign_id' => 1,
523 if ($includeCustomFields) {
524 // also get all the custom participant properties
525 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Participant');
526 if (!empty($fields)) {
527 foreach ($fields as $name => $dontCare) {
528 $properties[$name] = 1;
538 * @param CRM_Core_Form $form
540 public static function buildSearchForm(&$form) {
541 $dataURLEventFee = CRM_Utils_System
::url('civicrm/ajax/eventFee',
546 $form->assign('dataURLEventFee', $dataURLEventFee);
548 $form->addEntityRef('event_id', ts('Event Name'), array(
550 'placeholder' => ts('- any -'),
551 'select' => array('minimumInputLength' => 0),
554 $form->addEntityRef('event_type_id', ts('Event Type'), array(
555 'entity' => 'option_value',
556 'placeholder' => ts('- any -'),
557 'select' => array('minimumInputLength' => 0),
559 'params' => array('option_group_id' => 'event_type'),
563 $form->add('text', 'participant_fee_id', ts('Fee Level'), array('class' => 'big crm-ajax-select'));
565 CRM_Core_Form_Date
::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
567 $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', array(1 => '<em>%1</em>')));
569 $form->addSelect('participant_status_id',
570 array('entity' => 'participant', 'label' => ts('Participant Status'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
573 $form->addSelect('participant_role_id',
574 array('entity' => 'participant', 'label' => ts('Participant Role'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
577 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
578 $form->addYesNo('participant_is_pay_later', ts('Participant is Pay Later?'), TRUE);
579 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
580 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
582 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
583 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
584 // add all the custom searchable fields
585 $extends = array('Participant', 'Event');
586 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
588 $form->assign('participantGroupTree', $groupDetails);
589 foreach ($groupDetails as $group) {
590 foreach ($group['fields'] as $field) {
591 $fieldId = $field['id'];
592 $elementName = 'custom_' . $fieldId;
593 CRM_Core_BAO_CustomField
::addQuickFormElement($form,
602 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'participant_campaign_id');
604 $form->assign('validCiviEvent', TRUE);
605 $form->setDefaults(array('participant_test' => 0));
612 public static function searchAction(&$row, $id) {
618 public static function tableNames(&$tables) {
619 //add participant table
620 if (!empty($tables['civicrm_event'])) {
621 $tables = array_merge(array('civicrm_participant' => 1), $tables);