4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
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 public 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 public static function &getParticipantFields() {
54 $fields = CRM_Event_BAO_Participant
::importableFields('Individual', TRUE, TRUE);
59 * Build select for CiviEvent
65 public static function select(&$query) {
66 if (($query->_mode
& CRM_Contact_BAO_Query
::MODE_EVENT
) ||
67 CRM_Contact_BAO_Query
::componentPresent($query->_returnProperties
, 'participant_')
69 $query->_select
['participant_id'] = "civicrm_participant.id as participant_id";
70 $query->_element
['participant_id'] = 1;
71 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
74 if (!empty($query->_returnProperties
['participant_fee_level'])) {
75 $query->_select
['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
76 $query->_element
['participant_fee_level'] = 1;
79 //add participant contact ID
80 if (!empty($query->_returnProperties
['participant_contact_id'])) {
81 $query->_select
['participant_contact_id'] = "civicrm_participant.contact_id as participant_contact_id";
82 $query->_element
['participant_contact_id'] = 1;
86 if (!empty($query->_returnProperties
['participant_fee_amount'])) {
87 $query->_select
['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
88 $query->_element
['participant_fee_amount'] = 1;
92 if (!empty($query->_returnProperties
['participant_fee_currency'])) {
93 $query->_select
['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
94 $query->_element
['participant_fee_currency'] = 1;
97 //add event title also if event id is select
98 if (!empty($query->_returnProperties
['event_id']) ||
!empty($query->_returnProperties
['event_title'])) {
99 $query->_select
['event_id'] = "civicrm_event.id as event_id";
100 $query->_select
['event_title'] = "civicrm_event.title as event_title";
101 $query->_element
['event_id'] = 1;
102 $query->_element
['event_title'] = 1;
103 $query->_tables
['civicrm_event'] = 1;
104 $query->_whereTables
['civicrm_event'] = 1;
107 //add start date / end date
108 if (!empty($query->_returnProperties
['event_start_date'])) {
109 $query->_select
['event_start_date'] = "civicrm_event.start_date as event_start_date";
110 $query->_element
['event_start_date'] = 1;
113 if (!empty($query->_returnProperties
['event_end_date'])) {
114 $query->_select
['event_end_date'] = "civicrm_event.end_date as event_end_date";
115 $query->_element
['event_end_date'] = 1;
119 if (!empty($query->_returnProperties
['event_type'])) {
120 $query->_select
['event_type'] = "event_type.label as event_type";
121 $query->_element
['event_type'] = 1;
122 $query->_tables
['event_type'] = 1;
123 $query->_whereTables
['event_type'] = 1;
126 if (!empty($query->_returnProperties
['event_type_id'])) {
127 $query->_select
['event_type_id'] = "event_type.id as event_type_id";
128 $query->_element
['event_type_id'] = 1;
129 $query->_tables
['event_type'] = 1;
130 $query->_whereTables
['event_type'] = 1;
133 //add status and status_id
134 if (!empty($query->_returnProperties
['participant_status']) ||
!empty($query->_returnProperties
['participant_status_id'])) {
135 $query->_select
['participant_status'] = "participant_status.label as participant_status";
136 $query->_select
['participant_status_id'] = "participant_status.id as participant_status_id";
137 $query->_element
['participant_status_id'] = 1;
138 $query->_element
['participant_status'] = 1;
139 $query->_tables
['civicrm_participant'] = 1;
140 $query->_tables
['participant_status'] = 1;
141 $query->_whereTables
['civicrm_participant'] = 1;
142 $query->_whereTables
['participant_status'] = 1;
143 $query->_pseudoConstantsSelect
['participant_status'] = array(
144 'pseudoField' => 'participant_status',
145 'idCol' => 'participant_status_id',
149 //add participant_role and participant_role_id
150 if (!empty($query->_returnProperties
['participant_role']) ||
!empty($query->_returnProperties
['participant_role_id'])) {
151 $query->_select
['participant_role'] = "participant_role.label as participant_role";
152 $query->_select
['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
153 $query->_element
['participant_role'] = 1;
154 $query->_element
['participant_role_id'] = 1;
155 $query->_tables
['civicrm_participant'] = 1;
156 $query->_tables
['participant_role'] = 1;
157 $query->_whereTables
['civicrm_participant'] = 1;
158 $query->_whereTables
['participant_role'] = 1;
159 $query->_pseudoConstantsSelect
['participant_role'] = array(
160 'pseudoField' => 'participant_role',
161 'idCol' => 'participant_role_id',
166 if (!empty($query->_returnProperties
['participant_register_date'])) {
167 $query->_select
['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
168 $query->_element
['participant_register_date'] = 1;
172 if (!empty($query->_returnProperties
['participant_source'])) {
173 $query->_select
['participant_source'] = "civicrm_participant.source as participant_source";
174 $query->_element
['participant_source'] = 1;
175 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
179 if (!empty($query->_returnProperties
['participant_note'])) {
180 $query->_select
['participant_note'] = "civicrm_note.note as participant_note";
181 $query->_element
['participant_note'] = 1;
182 $query->_tables
['participant_note'] = 1;
183 $query->_whereTables
['civicrm_note'] = 1;
186 if (!empty($query->_returnProperties
['participant_is_pay_later'])) {
187 $query->_select
['participant_is_pay_later'] = "civicrm_participant.is_pay_later as participant_is_pay_later";
188 $query->_element
['participant_is_pay_later'] = 1;
191 if (!empty($query->_returnProperties
['participant_is_test'])) {
192 $query->_select
['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
193 $query->_element
['participant_is_test'] = 1;
196 if (!empty($query->_returnProperties
['participant_registered_by_id'])) {
197 $query->_select
['participant_registered_by_id'] = "civicrm_participant.registered_by_id as participant_registered_by_id";
198 $query->_element
['participant_registered_by_id'] = 1;
202 if (!empty($query->_returnProperties
['participant_discount_name'])) {
203 $query->_select
['participant_discount_name'] = "discount_name.title as participant_discount_name";
204 $query->_element
['participant_discount_name'] = 1;
205 $query->_tables
['civicrm_discount'] = 1;
206 $query->_tables
['participant_discount_name'] = 1;
207 $query->_whereTables
['civicrm_discount'] = 1;
208 $query->_whereTables
['participant_discount_name'] = 1;
211 //carry campaign id to selectors.
212 if (!empty($query->_returnProperties
['participant_campaign_id'])) {
213 $query->_select
['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
214 $query->_element
['participant_campaign_id'] = 1;
223 public static function where(&$query) {
225 foreach (array_keys($query->_params
) as $id) {
226 if (empty($query->_params
[$id][0])) {
229 if (substr($query->_params
[$id][0], 0, 6) == 'event_' ||
230 substr($query->_params
[$id][0], 0, 12) == 'participant_'
232 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
233 $query->_useDistinct
= TRUE;
235 $grouping = $query->_params
[$id][3];
236 self
::whereClauseSingle($query->_params
[$id], $query);
245 public static function whereClauseSingle(&$values, &$query) {
246 list($name, $op, $value, $grouping, $wildcard) = $values;
247 $fields = array_merge(self
::fields(), CRM_Event_BAO_Event
::fields(), CRM_Event_BAO_Participant
::exportableFields());
250 case 'event_start_date_low':
251 case 'event_start_date_high':
252 $query->dateQueryBuilder($values,
253 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
257 case 'event_end_date_low':
258 case 'event_end_date_high':
259 $query->dateQueryBuilder($values,
260 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
264 case 'event_include_repeating_events':
266 * Include Repeating Events
268 //Get parent of this event
270 if ($query->_where
[$grouping]) {
271 foreach ($query->_where
[$grouping] as $key => $val) {
272 if (strstr($val, 'civicrm_event.id =')) {
274 $extractEventId = explode(" ", $val);
275 $value = $extractEventId[2];
276 unset($query->_where
[$grouping][$key]);
279 $extractEventId = explode(" ", $exEventId);
280 $value = $extractEventId[2];
281 unset($query->_where
[$grouping][$key]);
283 $thisEventHasParent = CRM_Core_BAO_RecurringEntity
::getParentFor($value, 'civicrm_event');
284 if ($thisEventHasParent) {
285 $getAllConnections = CRM_Core_BAO_RecurringEntity
::getEntitiesForParent($thisEventHasParent, 'civicrm_event');
286 $allEventIds = array();
287 foreach ($getAllConnections as $key => $val) {
288 $allEventIds[] = $val['id'];
290 if (!empty($allEventIds)) {
292 $value = "(" . implode(",", $allEventIds) . ")";
295 $query->_where
[$grouping][] = "civicrm_event.id $op {$value}";
296 $query->_qill
[$grouping][] = ts('Include Repeating Events (If Any) ') . " = TRUE";
297 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
300 case 'participant_test':
301 // We dont want to include all tests for sql OR CRM-7827
302 if (!$value ||
$query->getOperator() != 'OR') {
303 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_test",
309 $query->_qill
[$grouping][] = ts("Participant is a Test");
311 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
315 case 'participant_fee_id':
316 $feeLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $value, 'label');
317 $feeLabel = CRM_Core_DAO
::escapeString(trim($feeLabel));
319 $query->_where
[$grouping][] = "civicrm_participant.fee_level LIKE '%$feeLabel%'";
320 $query->_qill
[$grouping][] = ts("Fee level") . " contains $feeLabel";
322 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
325 case 'participant_fee_amount_high':
326 case 'participant_fee_amount_low':
327 $query->numberRangeBuilder($values,
328 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
332 case 'participant_status':
333 case 'participant_status_id':
334 case 'participant_role':
335 case 'participant_role_id':
336 case 'participant_source':
337 case 'participant_id':
338 case 'participant_contact_id':
339 case 'participant_pay_later':
340 case 'participant_fee_amount':
341 case 'participant_fee_level':
343 if (in_array($name, array('participant_status_id', 'participant_role_id', 'participant_source', 'participant_id', 'participant_contact_id', 'participant_fee_amount', 'participant_fee_level'))) {
344 $name = str_replace('participant_', '', $name);
346 if ($name == 'participant_pay_later') {
347 $name = str_replace('participant', 'is', $name);
348 $qillName = 'participant_' . $name;
351 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
353 if (in_array($name, array('participant_status', 'participant_role'))) {
354 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("$name.label", $op, $value, $dataType);
357 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.$name", $op, $value, $dataType);
359 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Participant', $name, $value, $op);
360 $query->_qill
[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
361 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
364 case 'participant_register_date':
365 $query->dateQueryBuilder($values,
366 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
371 case 'event_is_public':
372 case 'event_type_id':
375 if (in_array($name, array('event_id', 'event_is_public'))) {
376 $name = str_replace('event_', '', $name);
378 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type
::typeToString($fields[$qillName]['type']) : 'String';
380 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.$name", $op, $value, $dataType);
381 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
382 if (!array_key_exists($qillName, $fields)) {
385 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op);
386 $query->_qill
[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
389 case 'participant_campaign_id':
392 'campaign' => $value,
393 'grouping' => $grouping,
394 'tableName' => 'civicrm_participant',
396 CRM_Campaign_BAO_Query
::componentSearchClause($campParams, $query);
402 * @param string $name
406 * @return null|string
408 public static function from($name, $mode, $side) {
411 case 'civicrm_participant':
412 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
415 case 'civicrm_event':
416 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
420 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
421 $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 ) ";
424 case 'participant_note':
425 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
426 civicrm_participant.id = civicrm_note.entity_id )";
429 case 'participant_status':
430 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
433 case 'participant_role':
434 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
435 $from .= " $side JOIN civicrm_option_value participant_role ON (civicrm_participant.role_id = participant_role.value
436 AND option_group_participant_role.id = participant_role.option_group_id ) ";
439 case 'participant_discount_name':
440 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
441 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
448 * Getter for the qill object
452 public function qill() {
453 return (isset($this->_qill
)) ?
$this->_qill
: "";
458 * @param bool $includeCustomFields
462 static function defaultReturnProperties(
464 $includeCustomFields = TRUE
467 if ($mode & CRM_Contact_BAO_Query
::MODE_EVENT
) {
470 'contact_sub_type' => 1,
475 'event_start_date' => 1,
476 'event_end_date' => 1,
478 'participant_id' => 1,
479 'participant_status' => 1,
480 'participant_role_id' => 1,
481 'participant_note' => 1,
482 'participant_register_date' => 1,
483 'participant_source' => 1,
484 'participant_fee_level' => 1,
485 'participant_is_test' => 1,
486 'participant_is_pay_later' => 1,
487 'participant_fee_amount' => 1,
488 'participant_discount_name' => 1,
489 'participant_fee_currency' => 1,
490 'participant_registered_by_id' => 1,
491 'participant_campaign_id' => 1,
494 if ($includeCustomFields) {
495 // also get all the custom participant properties
496 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Participant');
497 if (!empty($fields)) {
498 foreach ($fields as $name => $dontCare) {
499 $properties[$name] = 1;
509 * @param CRM_Core_Form $form
511 public static function buildSearchForm(&$form) {
512 $dataURLEventFee = CRM_Utils_System
::url('civicrm/ajax/eventFee',
517 $form->assign('dataURLEventFee', $dataURLEventFee);
519 $eventId = $form->addEntityRef('event_id', ts('Event Name'), array(
521 'placeholder' => ts('- any -'),
522 'select' => array('minimumInputLength' => 0),
525 $eventType = $form->addEntityRef('event_type_id', ts('Event Type'), array(
526 'entity' => 'option_value',
527 'placeholder' => ts('- any -'),
528 'select' => array('minimumInputLength' => 0),
530 'params' => array('option_group_id' => 'event_type'),
534 $form->add('text', 'participant_fee_id', ts('Fee Level'), array('class' => 'big crm-ajax-select'));
536 CRM_Core_Form_Date
::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
537 $eventIncludeRepeatingEvents = &$form->addElement('checkbox', "event_include_repeating_events", NULL, ts(' Include Repeating Events (If Any) ? '));
539 $form->addSelect('participant_status_id',
540 array('entity' => 'participant', 'label' => ts('Participant Status'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
543 $form->addSelect('participant_role_id',
544 array('entity' => 'participant', 'label' => ts('Participant Role'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
547 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
548 $form->addYesNo('participant_pay_later', ts('Participant is Pay Later?'), TRUE);
549 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
550 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
552 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
553 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
554 // add all the custom searchable fields
555 $extends = array('Participant', 'Event');
556 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
558 $form->assign('participantGroupTree', $groupDetails);
559 foreach ($groupDetails as $group) {
560 foreach ($group['fields'] as $field) {
561 $fieldId = $field['id'];
562 $elementName = 'custom_' . $fieldId;
563 CRM_Core_BAO_CustomField
::addQuickFormElement($form,
572 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'participant_campaign_id');
574 $form->assign('validCiviEvent', TRUE);
575 $form->setDefaults(array('participant_test' => 0));
582 public static function searchAction(&$row, $id) {
588 public static function tableNames(&$tables) {
589 //add participant table
590 if (!empty($tables['civicrm_event'])) {
591 $tables = array_merge(array('civicrm_participant' => 1), $tables);