4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 class CRM_Event_BAO_Query
{
38 static function &getFields() {
40 $fields = array_merge($fields, CRM_Event_DAO_Event
::import());
41 $fields = array_merge($fields, self
::getParticipantFields());
42 $fields = array_merge($fields, CRM_Core_DAO_Discount
::export());
47 static function &getParticipantFields() {
48 $fields = CRM_Event_BAO_Participant
::importableFields('Individual', TRUE, TRUE);
53 * build select for CiviEvent
58 static function select(&$query) {
59 if (($query->_mode
& CRM_Contact_BAO_Query
::MODE_EVENT
) ||
60 CRM_Contact_BAO_Query
::componentPresent($query->_returnProperties
, 'participant_')
62 $query->_select
['participant_id'] = "civicrm_participant.id as participant_id";
63 $query->_element
['participant_id'] = 1;
64 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
67 if (CRM_Utils_Array
::value('participant_fee_level', $query->_returnProperties
)) {
68 $query->_select
['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
69 $query->_element
['participant_fee_level'] = 1;
73 if (CRM_Utils_Array
::value('participant_fee_amount', $query->_returnProperties
)) {
74 $query->_select
['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
75 $query->_element
['participant_fee_amount'] = 1;
79 if (CRM_Utils_Array
::value('participant_fee_currency', $query->_returnProperties
)) {
80 $query->_select
['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
81 $query->_element
['participant_fee_currency'] = 1;
84 //add event title also if event id is select
85 if (CRM_Utils_Array
::value('event_id', $query->_returnProperties
) ||
86 CRM_Utils_Array
::value('event_title', $query->_returnProperties
)
88 $query->_select
['event_id'] = "civicrm_event.id as event_id";
89 $query->_select
['event_title'] = "civicrm_event.title as event_title";
90 $query->_element
['event_id'] = 1;
91 $query->_element
['event_title'] = 1;
92 $query->_tables
['civicrm_event'] = 1;
93 $query->_whereTables
['civicrm_event'] = 1;
96 //add start date / end date
97 if (CRM_Utils_Array
::value('event_start_date', $query->_returnProperties
)) {
98 $query->_select
['event_start_date'] = "civicrm_event.start_date as event_start_date";
99 $query->_element
['event_start_date'] = 1;
102 if (CRM_Utils_Array
::value('event_end_date', $query->_returnProperties
)) {
103 $query->_select
['event_end_date'] = "civicrm_event.end_date as event_end_date";
104 $query->_element
['event_end_date'] = 1;
108 if (CRM_Utils_Array
::value('event_type', $query->_returnProperties
)) {
109 $query->_select
['event_type'] = "event_type.label as event_type";
110 $query->_element
['event_type'] = 1;
111 $query->_tables
['event_type'] = 1;
112 $query->_whereTables
['event_type'] = 1;
115 if (CRM_Utils_Array
::value('event_type_id', $query->_returnProperties
)) {
116 $query->_select
['event_type_id'] = "event_type.id as event_type_id";
117 $query->_element
['event_type_id'] = 1;
118 $query->_tables
['event_type'] = 1;
119 $query->_whereTables
['event_type'] = 1;
122 //add status and status_id
123 if (CRM_Utils_Array
::value('participant_status', $query->_returnProperties
) ||
124 CRM_Utils_Array
::value('participant_status_id', $query->_returnProperties
)
126 $query->_select
['participant_status'] = "participant_status.label as participant_status";
127 $query->_select
['participant_status_id'] = "participant_status.id as participant_status_id";
128 $query->_element
['participant_status_id'] = 1;
129 $query->_element
['participant_status'] = 1;
130 $query->_tables
['civicrm_participant'] = 1;
131 $query->_tables
['participant_status'] = 1;
132 $query->_whereTables
['civicrm_participant'] = 1;
133 $query->_whereTables
['participant_status'] = 1;
137 if (CRM_Utils_Array
::value('participant_role', $query->_returnProperties
)) {
138 $query->_select
['participant_role'] = "participant_role.label as participant_role";
139 $query->_element
['participant_role'] = 1;
140 $query->_tables
['civicrm_participant'] = 1;
141 $query->_tables
['participant_role'] = 1;
142 $query->_whereTables
['civicrm_participant'] = 1;
143 $query->_whereTables
['participant_role'] = 1;
146 if (CRM_Utils_Array
::value('participant_role_id', $query->_returnProperties
)) {
147 $query->_select
['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
148 $query->_element
['participant_role_id'] = 1;
149 $query->_tables
['civicrm_participant'] = 1;
150 $query->_tables
['participant_role'] = 1;
151 $query->_whereTables
['civicrm_participant'] = 1;
152 $query->_whereTables
['participant_role'] = 1;
156 if (CRM_Utils_Array
::value('participant_register_date', $query->_returnProperties
)) {
157 $query->_select
['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
158 $query->_element
['participant_register_date'] = 1;
162 if (CRM_Utils_Array
::value('participant_source', $query->_returnProperties
)) {
163 $query->_select
['participant_source'] = "civicrm_participant.source as participant_source";
164 $query->_element
['participant_source'] = 1;
165 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
169 if (CRM_Utils_Array
::value('participant_note', $query->_returnProperties
)) {
170 $query->_select
['participant_note'] = "civicrm_note.note as participant_note";
171 $query->_element
['participant_note'] = 1;
172 $query->_tables
['participant_note'] = 1;
173 $query->_whereTables
['civicrm_note'] = 1;
176 if (CRM_Utils_Array
::value('participant_is_pay_later', $query->_returnProperties
)) {
177 $query->_select
['participant_is_pay_later'] = "civicrm_participant.is_pay_later as participant_is_pay_later";
178 $query->_element
['participant_is_pay_later'] = 1;
181 if (CRM_Utils_Array
::value('participant_is_test', $query->_returnProperties
)) {
182 $query->_select
['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
183 $query->_element
['participant_is_test'] = 1;
186 if (CRM_Utils_Array
::value('participant_registered_by_id', $query->_returnProperties
)) {
187 $query->_select
['participant_registered_by_id'] = "civicrm_participant.registered_by_id as participant_registered_by_id";
188 $query->_element
['participant_registered_by_id'] = 1;
192 if (CRM_Utils_Array
::value('participant_discount_name', $query->_returnProperties
)) {
193 $query->_select
['participant_discount_name'] = "discount_name.title as participant_discount_name";
194 $query->_element
['participant_discount_name'] = 1;
195 $query->_tables
['civicrm_discount'] = 1;
196 $query->_tables
['participant_discount_name'] = 1;
197 $query->_whereTables
['civicrm_discount'] = 1;
198 $query->_whereTables
['participant_discount_name'] = 1;
201 //carry campaign id to selectors.
202 if (CRM_Utils_Array
::value('participant_campaign_id', $query->_returnProperties
)) {
203 $query->_select
['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
204 $query->_element
['participant_campaign_id'] = 1;
209 static function where(&$query) {
211 foreach (array_keys($query->_params
) as $id) {
212 if (!CRM_Utils_Array
::value(0, $query->_params
[$id])) {
215 if (substr($query->_params
[$id][0], 0, 6) == 'event_' ||
216 substr($query->_params
[$id][0], 0, 12) == 'participant_'
218 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
219 $query->_useDistinct
= TRUE;
221 $grouping = $query->_params
[$id][3];
222 self
::whereClauseSingle($query->_params
[$id], $query);
227 static function whereClauseSingle(&$values, &$query) {
228 list($name, $op, $value, $grouping, $wildcard) = $values;
230 case 'event_start_date_low':
231 case 'event_start_date_high':
232 $query->dateQueryBuilder($values,
233 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
237 case 'event_end_date_low':
238 case 'event_end_date_high':
239 $query->dateQueryBuilder($values,
240 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
245 $query->_where
[$grouping][] = "civicrm_event.id $op {$value}";
246 $eventTitle = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $value, 'title');
247 $query->_qill
[$grouping][] = ts('Event') . " $op {$eventTitle}";
248 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
251 case 'event_type_id':
253 $eventTypes = CRM_Core_OptionGroup
::values("event_type");
254 $query->_where
[$grouping][] = "civicrm_participant.event_id = civicrm_event.id and civicrm_event.event_type_id = '{$value}'";
255 $query->_qill
[$grouping][] = ts('Event Type - %1', array(1 => $eventTypes[$value]));
256 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
259 case 'participant_test':
260 // We dont want to include all tests for sql OR CRM-7827
261 if (!$value ||
$query->getOperator() != 'OR') {
262 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_test",
268 $query->_qill
[$grouping][] = ts("Participant is a Test");
270 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
274 case 'participant_fee_id':
275 $feeLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_FieldValue', $value, 'label');
276 $feeLabel = CRM_Core_DAO
::escapeString(trim($feeLabel));
278 $query->_where
[$grouping][] = "civicrm_participant.fee_level $op '$feeLabel'";
279 $query->_qill
[$grouping][] = ts("Fee level") . " $op $feeLabel";
281 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
284 case 'participant_fee_amount':
285 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.fee_amount",
291 $query->_qill
[$grouping][] = ts("Fee Amount") . " $op $value";
293 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
296 case 'participant_fee_amount_high':
297 case 'participant_fee_amount_low':
298 $query->numberRangeBuilder($values,
299 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
303 case 'participant_pay_later':
304 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_pay_later",
309 $query->_qill
[$grouping][] = $value ?
ts("Participant is Pay Later") : ts("Participant is not Pay Later");
310 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
313 case 'participant_status':
314 case 'participant_status_id':
316 if (is_array($value)) {
317 foreach ($value as $k => $v) {
322 $status = implode(',', $val);
328 if (count($val) > 1) {
330 $status = "({$status})";
333 $statusTypes = CRM_Event_PseudoConstant
::participantStatus();
337 foreach ($val as $id => $dontCare) {
338 $names[] = $statusTypes[$id];
342 $names[] = $statusTypes[$value];
345 $query->_qill
[$grouping][] = ts('Participant Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
347 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.status_id",
352 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
355 case 'participant_role_id':
357 if (is_array($value)) {
358 foreach ($value as $k => $v) {
365 $val = array($value => 1);
368 $roleTypes = CRM_Event_PseudoConstant
::participantRole();
371 foreach ($val as $id => $dontCare) {
372 $names[] = $roleTypes[$id];
375 if (!empty($names)) {
376 $query->_qill
[$grouping][] =
377 ts('Participant Role %1', array(1 => $op)) .
379 implode(' ' . ts('or') . ' ', $names);
380 $query->_where
[$grouping][] =
381 " civicrm_participant.role_id REGEXP '[[:<:]]" .
382 implode('[[:>:]]|[[:<:]]', array_keys($val)) .
385 $query->_tables
['civicrm_participant'] =
386 $query->_whereTables
['civicrm_participant'] = 1;
390 case 'participant_source':
391 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.source",
396 $query->_qill
[$grouping][] = ts("Participant Source") . " $op $value";
397 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
400 case 'participant_register_date':
401 $query->dateQueryBuilder($values,
402 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
406 case 'participant_id':
407 $query->_where
[$grouping][] = "civicrm_participant.id $op $value";
408 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
412 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.id",
417 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
418 $title = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $value, "title");
419 $query->_qill
[$grouping][] = ts('Event') . " $op $value";
422 case 'participant_contact_id':
423 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.contact_id",
428 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
431 case 'event_is_public':
432 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.is_public",
437 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
440 case 'participant_campaign_id':
443 'campaign' => $value,
444 'grouping' => $grouping,
445 'tableName' => 'civicrm_participant',
447 CRM_Campaign_BAO_Query
::componentSearchClause($campParams, $query);
452 static function from($name, $mode, $side) {
455 case 'civicrm_participant':
456 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
459 case 'civicrm_event':
460 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
464 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
465 $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 ) ";
468 case 'participant_note':
469 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
470 civicrm_participant.id = civicrm_note.entity_id )";
473 case 'participant_status':
474 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
477 case 'participant_role':
478 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
479 $from .= " $side JOIN civicrm_option_value participant_role ON (civicrm_participant.role_id = participant_role.value
480 AND option_group_participant_role.id = participant_role.option_group_id ) ";
483 case 'participant_discount_name':
484 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
485 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
492 * getter for the qill object
498 return (isset($this->_qill
)) ?
$this->_qill
: "";
501 static function defaultReturnProperties($mode,
502 $includeCustomFields = TRUE
505 if ($mode & CRM_Contact_BAO_Query
::MODE_EVENT
) {
508 'contact_sub_type' => 1,
513 'event_start_date' => 1,
514 'event_end_date' => 1,
516 'participant_id' => 1,
517 'participant_status' => 1,
518 'participant_role_id' => 1,
519 'participant_note' => 1,
520 'participant_register_date' => 1,
521 'participant_source' => 1,
522 'participant_fee_level' => 1,
523 'participant_is_test' => 1,
524 'participant_is_pay_later' => 1,
525 'participant_fee_amount' => 1,
526 'participant_discount_name' => 1,
527 'participant_fee_currency' => 1,
528 'participant_registered_by_id' => 1,
529 'participant_campaign_id' => 1,
532 if ($includeCustomFields) {
533 // also get all the custom participant properties
534 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Participant');
535 if (!empty($fields)) {
536 foreach ($fields as $name => $dontCare) {
537 $properties[$name] = 1;
546 static function buildSearchForm(&$form) {
547 $dataURLEvent = CRM_Utils_System
::url('civicrm/ajax/event',
551 $dataURLEventType = CRM_Utils_System
::url('civicrm/ajax/eventType',
555 $dataURLEventFee = CRM_Utils_System
::url('civicrm/ajax/eventFee',
560 $form->assign('dataURLEvent', $dataURLEvent);
561 $form->assign('dataURLEventType', $dataURLEventType);
562 $form->assign('dataURLEventFee', $dataURLEventFee);
564 $eventId = &$form->add('text', 'event_name', ts('Event Name'));
565 $eventType = &$form->add('text', 'event_type', ts('Event Type'));
566 $participantFee = &$form->add('text', 'participant_fee_level', ts('Fee Level'));
568 //elements for assigning value operation
569 $eventNameId = &$form->add('hidden', 'event_id', '', array('id' => 'event_id'));
570 $eventTypeId = &$form->add('hidden', 'event_type_id', '', array('id' => 'event_type_id'));
571 $participantFeeId = &$form->add('hidden', 'participant_fee_id', '', array('id' => 'participant_fee_id'));
573 CRM_Core_Form_Date
::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
575 $status = CRM_Event_PseudoConstant
::participantStatus(NULL, NULL, 'label');
577 foreach ($status as $id => $Name) {
578 $form->_participantStatus
= &$form->addElement('checkbox', "participant_status_id[$id]", NULL, $Name);
581 foreach (CRM_Event_PseudoConstant
::participantRole() as $rId => $rName) {
582 $form->_participantRole
= &$form->addElement('checkbox', "participant_role_id[$rId]", NULL, $rName);
585 $form->addYesNo('participant_test', ts('Participant is a Test?'));
586 $form->addYesNo('participant_pay_later', ts('Participant is Pay Later?'));
587 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
588 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
590 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
591 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
592 // add all the custom searchable fields
593 $extends = array('Participant');
594 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
596 $form->assign('participantGroupTree', $groupDetails);
597 foreach ($groupDetails as $group) {
598 foreach ($group['fields'] as $field) {
599 $fieldId = $field['id'];
600 $elementName = 'custom_' . $fieldId;
601 CRM_Core_BAO_CustomField
::addQuickFormElement($form,
610 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'participant_campaign_id');
612 $form->assign('validCiviEvent', TRUE);
613 $form->setDefaults(array('participant_test' => 0));
616 static function searchAction(&$row, $id) {}
618 static function tableNames(&$tables) {
619 //add participant table
620 if (CRM_Utils_Array
::value('civicrm_event', $tables)) {
621 $tables = array_merge(array('civicrm_participant' => 1), $tables);