4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Event_BAO_Query
{
41 static function &getFields() {
43 $fields = array_merge($fields, CRM_Event_DAO_Event
::import());
44 $fields = array_merge($fields, self
::getParticipantFields());
45 $fields = array_merge($fields, CRM_Core_DAO_Discount
::export());
53 static function &getParticipantFields() {
54 $fields = CRM_Event_BAO_Participant
::importableFields('Individual', TRUE, TRUE);
59 * build select for CiviEvent
66 static function select(&$query) {
67 if (($query->_mode
& CRM_Contact_BAO_Query
::MODE_EVENT
) ||
68 CRM_Contact_BAO_Query
::componentPresent($query->_returnProperties
, 'participant_')
70 $query->_select
['participant_id'] = "civicrm_participant.id as participant_id";
71 $query->_element
['participant_id'] = 1;
72 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
75 if (!empty($query->_returnProperties
['participant_fee_level'])) {
76 $query->_select
['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
77 $query->_element
['participant_fee_level'] = 1;
81 if (!empty($query->_returnProperties
['participant_fee_amount'])) {
82 $query->_select
['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
83 $query->_element
['participant_fee_amount'] = 1;
87 if (!empty($query->_returnProperties
['participant_fee_currency'])) {
88 $query->_select
['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
89 $query->_element
['participant_fee_currency'] = 1;
92 //add event title also if event id is select
93 if (!empty($query->_returnProperties
['event_id']) ||
!empty($query->_returnProperties
['event_title'])) {
94 $query->_select
['event_id'] = "civicrm_event.id as event_id";
95 $query->_select
['event_title'] = "civicrm_event.title as event_title";
96 $query->_element
['event_id'] = 1;
97 $query->_element
['event_title'] = 1;
98 $query->_tables
['civicrm_event'] = 1;
99 $query->_whereTables
['civicrm_event'] = 1;
102 //add start date / end date
103 if (!empty($query->_returnProperties
['event_start_date'])) {
104 $query->_select
['event_start_date'] = "civicrm_event.start_date as event_start_date";
105 $query->_element
['event_start_date'] = 1;
108 if (!empty($query->_returnProperties
['event_end_date'])) {
109 $query->_select
['event_end_date'] = "civicrm_event.end_date as event_end_date";
110 $query->_element
['event_end_date'] = 1;
114 if (!empty($query->_returnProperties
['event_type'])) {
115 $query->_select
['event_type'] = "event_type.label as event_type";
116 $query->_element
['event_type'] = 1;
117 $query->_tables
['event_type'] = 1;
118 $query->_whereTables
['event_type'] = 1;
121 if (!empty($query->_returnProperties
['event_type_id'])) {
122 $query->_select
['event_type_id'] = "event_type.id as event_type_id";
123 $query->_element
['event_type_id'] = 1;
124 $query->_tables
['event_type'] = 1;
125 $query->_whereTables
['event_type'] = 1;
128 //add status and status_id
129 if (!empty($query->_returnProperties
['participant_status']) ||
!empty($query->_returnProperties
['participant_status_id'])) {
130 $query->_select
['participant_status'] = "participant_status.label as participant_status";
131 $query->_select
['participant_status_id'] = "participant_status.id as participant_status_id";
132 $query->_element
['participant_status_id'] = 1;
133 $query->_element
['participant_status'] = 1;
134 $query->_tables
['civicrm_participant'] = 1;
135 $query->_tables
['participant_status'] = 1;
136 $query->_whereTables
['civicrm_participant'] = 1;
137 $query->_whereTables
['participant_status'] = 1;
140 //add participant_role and participant_role_id
141 if (!empty($query->_returnProperties
['participant_role']) ||
!empty($query->_returnProperties
['participant_role_id'])) {
142 $query->_select
['participant_role'] = "participant_role.label as participant_role";
143 $query->_select
['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
144 $query->_element
['participant_role'] = 1;
145 $query->_element
['participant_role_id'] = 1;
146 $query->_tables
['civicrm_participant'] = 1;
147 $query->_tables
['participant_role'] = 1;
148 $query->_whereTables
['civicrm_participant'] = 1;
149 $query->_whereTables
['participant_role'] = 1;
150 $query->_pseudoConstantsSelect
['participant_role'] = array(
151 'pseudoField' => 'participant_role',
152 'idCol' => 'participant_role_id',
157 if (!empty($query->_returnProperties
['participant_register_date'])) {
158 $query->_select
['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
159 $query->_element
['participant_register_date'] = 1;
163 if (!empty($query->_returnProperties
['participant_source'])) {
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;
170 if (!empty($query->_returnProperties
['participant_note'])) {
171 $query->_select
['participant_note'] = "civicrm_note.note as participant_note";
172 $query->_element
['participant_note'] = 1;
173 $query->_tables
['participant_note'] = 1;
174 $query->_whereTables
['civicrm_note'] = 1;
177 if (!empty($query->_returnProperties
['participant_is_pay_later'])) {
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;
182 if (!empty($query->_returnProperties
['participant_is_test'])) {
183 $query->_select
['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
184 $query->_element
['participant_is_test'] = 1;
187 if (!empty($query->_returnProperties
['participant_registered_by_id'])) {
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;
193 if (!empty($query->_returnProperties
['participant_discount_name'])) {
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;
202 //carry campaign id to selectors.
203 if (!empty($query->_returnProperties
['participant_campaign_id'])) {
204 $query->_select
['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
205 $query->_element
['participant_campaign_id'] = 1;
213 static function where(&$query) {
215 foreach (array_keys($query->_params
) as $id) {
216 if (empty($query->_params
[$id][0])) {
219 if (substr($query->_params
[$id][0], 0, 6) == 'event_' ||
220 substr($query->_params
[$id][0], 0, 12) == 'participant_'
222 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
223 $query->_useDistinct
= TRUE;
225 $grouping = $query->_params
[$id][3];
226 self
::whereClauseSingle($query->_params
[$id], $query);
235 static function whereClauseSingle(&$values, &$query) {
236 list($name, $op, $value, $grouping, $wildcard) = $values;
238 case 'event_start_date_low':
239 case 'event_start_date_high':
240 $query->dateQueryBuilder($values,
241 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
245 case 'event_end_date_low':
246 case 'event_end_date_high':
247 $query->dateQueryBuilder($values,
248 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
253 $query->_where
[$grouping][] = "civicrm_event.id $op {$value}";
254 $eventTitle = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $value, 'title');
255 $query->_qill
[$grouping][] = ts('Event') . " $op {$eventTitle}";
256 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
259 case 'event_type_id':
261 $eventTypes = CRM_Core_OptionGroup
::values("event_type");
262 $query->_where
[$grouping][] = "civicrm_participant.event_id = civicrm_event.id and civicrm_event.event_type_id = '{$value}'";
263 $query->_qill
[$grouping][] = ts('Event Type - %1', array(1 => $eventTypes[$value]));
264 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
267 case 'participant_test':
268 // We dont want to include all tests for sql OR CRM-7827
269 if (!$value ||
$query->getOperator() != 'OR') {
270 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_test",
276 $query->_qill
[$grouping][] = ts("Participant is a Test");
278 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
282 case 'participant_fee_id':
283 $feeLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $value, 'label');
284 $feeLabel = CRM_Core_DAO
::escapeString(trim($feeLabel));
286 $query->_where
[$grouping][] = "civicrm_participant.fee_level LIKE '%$feeLabel%'";
287 $query->_qill
[$grouping][] = ts("Fee level") . " contains $feeLabel";
289 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
292 case 'participant_fee_amount':
293 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.fee_amount",
299 $query->_qill
[$grouping][] = ts("Fee Amount") . " $op $value";
301 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
304 case 'participant_fee_amount_high':
305 case 'participant_fee_amount_low':
306 $query->numberRangeBuilder($values,
307 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
311 case 'participant_pay_later':
312 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_pay_later",
317 $query->_qill
[$grouping][] = $value ?
ts("Participant is Pay Later") : ts("Participant is not Pay Later");
318 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
321 case 'participant_status':
322 case 'participant_status_id':
324 if (is_array($value)) {
325 foreach ($value as $k => $v) {
330 $status = implode(',', $val);
336 if (count($val) > 0) {
338 $status = "({$status})";
341 $statusTypes = CRM_Event_PseudoConstant
::participantStatus();
345 foreach ($val as $id => $dontCare) {
346 $names[] = $statusTypes[$id];
350 if (!empty($value)) {
351 $names[] = $statusTypes[$value];
355 $query->_qill
[$grouping][] = ts('Participant Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
357 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.status_id",
362 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
365 case 'participant_role':
366 case 'participant_role_id':
368 if (is_array($value)) {
369 foreach ($value as $k => $v) {
376 $val = array($value => 1);
379 $roleTypes = CRM_Event_PseudoConstant
::participantRole();
382 foreach ($val as $id => $dontCare) {
383 if (!empty($roleTypes[$id]) ) {
384 $names[] = $roleTypes[$id];
388 if (!empty($names)) {
389 $query->_qill
[$grouping][] =
390 ts('Participant Role %1', array(1 => $op)) .
392 implode(' ' . ts('or') . ' ', $names);
393 $query->_where
[$grouping][] =
394 " civicrm_participant.role_id REGEXP '[[:<:]]" .
395 implode('[[:>:]]|[[:<:]]', array_keys($val)) .
398 $query->_tables
['civicrm_participant'] =
399 $query->_whereTables
['civicrm_participant'] = 1;
403 case 'participant_source':
404 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.source",
409 $query->_qill
[$grouping][] = ts("Participant Source") . " $op $value";
410 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
413 case 'participant_register_date':
414 $query->dateQueryBuilder($values,
415 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
419 case 'participant_id':
420 $query->_where
[$grouping][] = "civicrm_participant.id $op $value";
421 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
425 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.id",
430 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
431 $title = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $value, "title");
432 $query->_qill
[$grouping][] = ts('Event') . " $op $value";
435 case 'participant_contact_id':
436 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.contact_id",
441 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
444 case 'event_is_public':
445 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.is_public",
450 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
453 case 'participant_campaign_id':
456 'campaign' => $value,
457 'grouping' => $grouping,
458 'tableName' => 'civicrm_participant',
460 CRM_Campaign_BAO_Query
::componentSearchClause($campParams, $query);
470 * @return null|string
472 static function from($name, $mode, $side) {
475 case 'civicrm_participant':
476 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
479 case 'civicrm_event':
480 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
484 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
485 $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 case 'participant_note':
489 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
490 civicrm_participant.id = civicrm_note.entity_id )";
493 case 'participant_status':
494 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
497 case 'participant_role':
498 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
499 $from .= " $side JOIN civicrm_option_value participant_role ON (civicrm_participant.role_id = participant_role.value
500 AND option_group_participant_role.id = participant_role.option_group_id ) ";
503 case 'participant_discount_name':
504 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
505 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
512 * getter for the qill object
518 return (isset($this->_qill
)) ?
$this->_qill
: "";
523 * @param bool $includeCustomFields
527 static function defaultReturnProperties($mode,
528 $includeCustomFields = TRUE
531 if ($mode & CRM_Contact_BAO_Query
::MODE_EVENT
) {
534 'contact_sub_type' => 1,
539 'event_start_date' => 1,
540 'event_end_date' => 1,
542 'participant_id' => 1,
543 'participant_status' => 1,
544 'participant_role_id' => 1,
545 'participant_note' => 1,
546 'participant_register_date' => 1,
547 'participant_source' => 1,
548 'participant_fee_level' => 1,
549 'participant_is_test' => 1,
550 'participant_is_pay_later' => 1,
551 'participant_fee_amount' => 1,
552 'participant_discount_name' => 1,
553 'participant_fee_currency' => 1,
554 'participant_registered_by_id' => 1,
555 'participant_campaign_id' => 1,
558 if ($includeCustomFields) {
559 // also get all the custom participant properties
560 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Participant');
561 if (!empty($fields)) {
562 foreach ($fields as $name => $dontCare) {
563 $properties[$name] = 1;
573 * @param CRM_Core_Form $form
575 static function buildSearchForm(&$form) {
576 $dataURLEventFee = CRM_Utils_System
::url('civicrm/ajax/eventFee',
581 $form->assign('dataURLEventFee', $dataURLEventFee);
583 $eventId = $form->addEntityRef('event_id', ts('Event Name'), array(
585 'placeholder' => ts('- any -'),
586 'select' => array('minimumInputLength' => 0),
589 $eventType = $form->addEntityRef('event_type_id', ts('Event Type'), array(
590 'entity' => 'option_value',
591 'placeholder' => ts('- any -'),
592 'select' => array('minimumInputLength' => 0),
594 'params' => array('option_group_id' => 'event_type'),
598 $form->add('text', 'participant_fee_id', ts('Fee Level'), array('class' => 'big crm-ajax-select'));
600 CRM_Core_Form_Date
::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
602 $status = CRM_Event_PseudoConstant
::participantStatus(NULL, NULL, 'label');
603 foreach ($status as $id => $Name) {
604 $form->_participantStatus
= &$form->addElement('checkbox', "participant_status_id[$id]", NULL, $Name);
607 foreach (CRM_Event_PseudoConstant
::participantRole() as $rId => $rName) {
608 $form->_participantRole
= &$form->addElement('checkbox', "participant_role_id[$rId]", NULL, $rName);
611 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
612 $form->addYesNo('participant_pay_later', ts('Participant is Pay Later?'), TRUE);
613 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
614 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
616 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
617 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
618 // add all the custom searchable fields
619 $extends = array('Participant', 'Event');
620 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
622 $form->assign('participantGroupTree', $groupDetails);
623 foreach ($groupDetails as $group) {
624 foreach ($group['fields'] as $field) {
625 $fieldId = $field['id'];
626 $elementName = 'custom_' . $fieldId;
627 CRM_Core_BAO_CustomField
::addQuickFormElement($form,
636 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'participant_campaign_id');
638 $form->assign('validCiviEvent', TRUE);
639 $form->setDefaults(array('participant_test' => 0));
646 static function searchAction(&$row, $id) {}
651 static function tableNames(&$tables) {
652 //add participant table
653 if (!empty($tables['civicrm_event'])) {
654 $tables = array_merge(array('civicrm_participant' => 1), $tables);