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) {
210 $testCondition = $grouping = NULL;
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 if ($query->_params
[$id][0] == 'participant_test') {
222 $testCondition = $id;
225 $grouping = $query->_params
[$id][3];
226 self
::whereClauseSingle($query->_params
[$id], $query);
229 // Only add test condition if other fields are selected
230 if ($grouping !== NULL && $testCondition &&
231 // we dont want to include all tests for sql OR CRM-7827
232 $query->getOperator() != 'OR'
234 self
::whereClauseSingle($query->_params
[$testCondition], $query);
238 static function whereClauseSingle(&$values, &$query) {
239 list($name, $op, $value, $grouping, $wildcard) = $values;
241 case 'event_start_date_low':
242 case 'event_start_date_high':
243 $query->dateQueryBuilder($values,
244 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
248 case 'event_end_date_low':
249 case 'event_end_date_high':
250 $query->dateQueryBuilder($values,
251 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
256 $query->_where
[$grouping][] = "civicrm_event.id $op {$value}";
257 $eventTitle = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $value, 'title');
258 $query->_qill
[$grouping][] = ts('Event') . " $op {$eventTitle}";
259 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
262 case 'event_type_id':
264 $eventTypes = CRM_Core_OptionGroup
::values("event_type");
265 $query->_where
[$grouping][] = "civicrm_participant.event_id = civicrm_event.id and civicrm_event.event_type_id = '{$value}'";
266 $query->_qill
[$grouping][] = ts('Event Type - %1', array(1 => $eventTypes[$value]));
267 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
270 case 'participant_test':
271 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.is_test",
277 $query->_qill
[$grouping][] = ts("Participant is a Test");
279 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
282 case 'participant_fee_id':
283 $feeLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_FieldValue', $value, 'label');
284 $feeLabel = CRM_Core_DAO
::escapeString(trim($feeLabel));
286 $query->_where
[$grouping][] = "civicrm_participant.fee_level $op '$feeLabel'";
287 $query->_qill
[$grouping][] = ts("Fee level") . " $op $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) > 1) {
338 $status = "({$status})";
341 $statusTypes = CRM_Event_PseudoConstant
::participantStatus();
345 foreach ($val as $id => $dontCare) {
346 $names[] = $statusTypes[$id];
350 $names[] = $statusTypes[$value];
353 $query->_qill
[$grouping][] = ts('Participant Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
355 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.status_id",
360 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
363 case 'participant_role_id':
365 if (is_array($value)) {
366 foreach ($value as $k => $v) {
373 $val = array($value => 1);
376 $roleTypes = CRM_Event_PseudoConstant
::participantRole();
379 foreach ($val as $id => $dontCare) {
380 $names[] = $roleTypes[$id];
383 if (!empty($names)) {
384 $query->_qill
[$grouping][] =
385 ts('Participant Role %1', array(1 => $op)) .
387 implode(' ' . ts('or') . ' ', $names);
388 $query->_where
[$grouping][] =
389 " civicrm_participant.role_id REGEXP '[[:<:]]" .
390 implode('[[:>:]]|[[:<:]]', array_keys($val)) .
393 $query->_tables
['civicrm_participant'] =
394 $query->_whereTables
['civicrm_participant'] = 1;
398 case 'participant_source':
399 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.source",
404 $query->_qill
[$grouping][] = ts("Participant Source") . " $op $value";
405 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
408 case 'participant_register_date':
409 $query->dateQueryBuilder($values,
410 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
414 case 'participant_id':
415 $query->_where
[$grouping][] = "civicrm_participant.id $op $value";
416 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
420 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.id",
425 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
426 $title = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $value, "title");
427 $query->_qill
[$grouping][] = ts('Event') . " $op $value";
430 case 'participant_contact_id':
431 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_participant.contact_id",
436 $query->_tables
['civicrm_participant'] = $query->_whereTables
['civicrm_participant'] = 1;
439 case 'event_is_public':
440 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_event.is_public",
445 $query->_tables
['civicrm_event'] = $query->_whereTables
['civicrm_event'] = 1;
448 case 'participant_campaign_id':
451 'campaign' => $value,
452 'grouping' => $grouping,
453 'tableName' => 'civicrm_participant',
455 CRM_Campaign_BAO_Query
::componentSearchClause($campParams, $query);
460 static function from($name, $mode, $side) {
463 case 'civicrm_participant':
464 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
467 case 'civicrm_event':
468 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
472 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
473 $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 ) ";
476 case 'participant_note':
477 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
478 civicrm_participant.id = civicrm_note.entity_id )";
481 case 'participant_status':
482 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
485 case 'participant_role':
486 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
487 $from .= " $side JOIN civicrm_option_value participant_role ON (civicrm_participant.role_id = participant_role.value
488 AND option_group_participant_role.id = participant_role.option_group_id ) ";
491 case 'participant_discount_name':
492 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
493 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
500 * getter for the qill object
506 return (isset($this->_qill
)) ?
$this->_qill
: "";
509 static function defaultReturnProperties($mode,
510 $includeCustomFields = TRUE
513 if ($mode & CRM_Contact_BAO_Query
::MODE_EVENT
) {
516 'contact_sub_type' => 1,
521 'event_start_date' => 1,
522 'event_end_date' => 1,
524 'participant_id' => 1,
525 'participant_status' => 1,
526 'participant_role_id' => 1,
527 'participant_note' => 1,
528 'participant_register_date' => 1,
529 'participant_source' => 1,
530 'participant_fee_level' => 1,
531 'participant_is_test' => 1,
532 'participant_is_pay_later' => 1,
533 'participant_fee_amount' => 1,
534 'participant_discount_name' => 1,
535 'participant_fee_currency' => 1,
536 'participant_registered_by_id' => 1,
537 'participant_campaign_id' => 1,
540 if ($includeCustomFields) {
541 // also get all the custom participant properties
542 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Participant');
543 if (!empty($fields)) {
544 foreach ($fields as $name => $dontCare) {
545 $properties[$name] = 1;
554 static function buildSearchForm(&$form) {
555 $dataURLEvent = CRM_Utils_System
::url('civicrm/ajax/event',
559 $dataURLEventType = CRM_Utils_System
::url('civicrm/ajax/eventType',
563 $dataURLEventFee = CRM_Utils_System
::url('civicrm/ajax/eventFee',
568 $form->assign('dataURLEvent', $dataURLEvent);
569 $form->assign('dataURLEventType', $dataURLEventType);
570 $form->assign('dataURLEventFee', $dataURLEventFee);
572 $eventId = &$form->add('text', 'event_name', ts('Event Name'));
573 $eventType = &$form->add('text', 'event_type', ts('Event Type'));
574 $participantFee = &$form->add('text', 'participant_fee_level', ts('Fee Level'));
576 //elements for assigning value operation
577 $eventNameId = &$form->add('hidden', 'event_id', '', array('id' => 'event_id'));
578 $eventTypeId = &$form->add('hidden', 'event_type_id', '', array('id' => 'event_type_id'));
579 $participantFeeId = &$form->add('hidden', 'participant_fee_id', '', array('id' => 'participant_fee_id'));
581 CRM_Core_Form_Date
::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
583 $status = CRM_Event_PseudoConstant
::participantStatus(NULL, NULL, 'label');
585 foreach ($status as $id => $Name) {
586 $form->_participantStatus
= &$form->addElement('checkbox', "participant_status_id[$id]", NULL, $Name);
589 foreach (CRM_Event_PseudoConstant
::participantRole() as $rId => $rName) {
590 $form->_participantRole
= &$form->addElement('checkbox', "participant_role_id[$rId]", NULL, $rName);
593 $form->addYesNo('participant_test', ts('Participant is a Test?'));
594 $form->addYesNo('participant_pay_later', ts('Participant is Pay Later?'));
595 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
596 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
598 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
599 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
600 // add all the custom searchable fields
601 $extends = array('Participant');
602 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
604 $form->assign('participantGroupTree', $groupDetails);
605 foreach ($groupDetails as $group) {
606 foreach ($group['fields'] as $field) {
607 $fieldId = $field['id'];
608 $elementName = 'custom_' . $fieldId;
609 CRM_Core_BAO_CustomField
::addQuickFormElement($form,
618 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'participant_campaign_id');
620 $form->assign('validCiviEvent', TRUE);
621 $form->setDefaults(array('participant_test' => 0));
624 static function searchAction(&$row, $id) {}
626 static function tableNames(&$tables) {
627 //add participant table
628 if (CRM_Utils_Array
::value('civicrm_event', $tables)) {
629 $tables = array_merge(array('civicrm_participant' => 1), $tables);