Merge pull request #3127 from GinkgoFJG/CRM-14354
[civicrm-core.git] / CRM / Event / BAO / Query.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Event_BAO_Query {
37
38 static function &getFields() {
39 $fields = array();
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());
43
44 return $fields;
45 }
46
47 static function &getParticipantFields() {
48 $fields = CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
49 return $fields;
50 }
51
52 /**
53 * build select for CiviEvent
54 *
55 * @return void
56 * @access public
57 */
58 static function select(&$query) {
59 if (($query->_mode & CRM_Contact_BAO_Query::MODE_EVENT) ||
60 CRM_Contact_BAO_Query::componentPresent($query->_returnProperties, 'participant_')
61 ) {
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;
65
66 //add fee level
67 if (!empty($query->_returnProperties['participant_fee_level'])) {
68 $query->_select['participant_fee_level'] = "civicrm_participant.fee_level as participant_fee_level";
69 $query->_element['participant_fee_level'] = 1;
70 }
71
72 //add fee amount
73 if (!empty($query->_returnProperties['participant_fee_amount'])) {
74 $query->_select['participant_fee_amount'] = "civicrm_participant.fee_amount as participant_fee_amount";
75 $query->_element['participant_fee_amount'] = 1;
76 }
77
78 //add fee currency
79 if (!empty($query->_returnProperties['participant_fee_currency'])) {
80 $query->_select['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
81 $query->_element['participant_fee_currency'] = 1;
82 }
83
84 //add event title also if event id is select
85 if (!empty($query->_returnProperties['event_id']) || !empty($query->_returnProperties['event_title'])) {
86 $query->_select['event_id'] = "civicrm_event.id as event_id";
87 $query->_select['event_title'] = "civicrm_event.title as event_title";
88 $query->_element['event_id'] = 1;
89 $query->_element['event_title'] = 1;
90 $query->_tables['civicrm_event'] = 1;
91 $query->_whereTables['civicrm_event'] = 1;
92 }
93
94 //add start date / end date
95 if (!empty($query->_returnProperties['event_start_date'])) {
96 $query->_select['event_start_date'] = "civicrm_event.start_date as event_start_date";
97 $query->_element['event_start_date'] = 1;
98 }
99
100 if (!empty($query->_returnProperties['event_end_date'])) {
101 $query->_select['event_end_date'] = "civicrm_event.end_date as event_end_date";
102 $query->_element['event_end_date'] = 1;
103 }
104
105 //event type
106 if (!empty($query->_returnProperties['event_type'])) {
107 $query->_select['event_type'] = "event_type.label as event_type";
108 $query->_element['event_type'] = 1;
109 $query->_tables['event_type'] = 1;
110 $query->_whereTables['event_type'] = 1;
111 }
112
113 if (!empty($query->_returnProperties['event_type_id'])) {
114 $query->_select['event_type_id'] = "event_type.id as event_type_id";
115 $query->_element['event_type_id'] = 1;
116 $query->_tables['event_type'] = 1;
117 $query->_whereTables['event_type'] = 1;
118 }
119
120 //add status and status_id
121 if (!empty($query->_returnProperties['participant_status']) || !empty($query->_returnProperties['participant_status_id'])) {
122 $query->_select['participant_status'] = "participant_status.label as participant_status";
123 $query->_select['participant_status_id'] = "participant_status.id as participant_status_id";
124 $query->_element['participant_status_id'] = 1;
125 $query->_element['participant_status'] = 1;
126 $query->_tables['civicrm_participant'] = 1;
127 $query->_tables['participant_status'] = 1;
128 $query->_whereTables['civicrm_participant'] = 1;
129 $query->_whereTables['participant_status'] = 1;
130 }
131
132 //add participant_role and participant_role_id
133 if (!empty($query->_returnProperties['participant_role']) || !empty($query->_returnProperties['participant_role_id'])) {
134 $query->_select['participant_role'] = "participant_role.label as participant_role";
135 $query->_select['participant_role_id'] = "civicrm_participant.role_id as participant_role_id";
136 $query->_element['participant_role'] = 1;
137 $query->_element['participant_role_id'] = 1;
138 $query->_tables['civicrm_participant'] = 1;
139 $query->_tables['participant_role'] = 1;
140 $query->_whereTables['civicrm_participant'] = 1;
141 $query->_whereTables['participant_role'] = 1;
142 $query->_pseudoConstantsSelect['participant_role'] = array(
143 'pseudoField' => 'participant_role',
144 'idCol' => 'participant_role_id',
145 );
146 }
147
148 //add register date
149 if (!empty($query->_returnProperties['participant_register_date'])) {
150 $query->_select['participant_register_date'] = "civicrm_participant.register_date as participant_register_date";
151 $query->_element['participant_register_date'] = 1;
152 }
153
154 //add source
155 if (!empty($query->_returnProperties['participant_source'])) {
156 $query->_select['participant_source'] = "civicrm_participant.source as participant_source";
157 $query->_element['participant_source'] = 1;
158 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
159 }
160
161 //participant note
162 if (!empty($query->_returnProperties['participant_note'])) {
163 $query->_select['participant_note'] = "civicrm_note.note as participant_note";
164 $query->_element['participant_note'] = 1;
165 $query->_tables['participant_note'] = 1;
166 $query->_whereTables['civicrm_note'] = 1;
167 }
168
169 if (!empty($query->_returnProperties['participant_is_pay_later'])) {
170 $query->_select['participant_is_pay_later'] = "civicrm_participant.is_pay_later as participant_is_pay_later";
171 $query->_element['participant_is_pay_later'] = 1;
172 }
173
174 if (!empty($query->_returnProperties['participant_is_test'])) {
175 $query->_select['participant_is_test'] = "civicrm_participant.is_test as participant_is_test";
176 $query->_element['participant_is_test'] = 1;
177 }
178
179 if (!empty($query->_returnProperties['participant_registered_by_id'])) {
180 $query->_select['participant_registered_by_id'] = "civicrm_participant.registered_by_id as participant_registered_by_id";
181 $query->_element['participant_registered_by_id'] = 1;
182 }
183
184 // get discount name
185 if (!empty($query->_returnProperties['participant_discount_name'])) {
186 $query->_select['participant_discount_name'] = "discount_name.title as participant_discount_name";
187 $query->_element['participant_discount_name'] = 1;
188 $query->_tables['civicrm_discount'] = 1;
189 $query->_tables['participant_discount_name'] = 1;
190 $query->_whereTables['civicrm_discount'] = 1;
191 $query->_whereTables['participant_discount_name'] = 1;
192 }
193
194 //carry campaign id to selectors.
195 if (!empty($query->_returnProperties['participant_campaign_id'])) {
196 $query->_select['participant_campaign_id'] = 'civicrm_participant.campaign_id as participant_campaign_id';
197 $query->_element['participant_campaign_id'] = 1;
198 }
199 }
200 }
201
202 static function where(&$query) {
203 $grouping = NULL;
204 foreach (array_keys($query->_params) as $id) {
205 if (empty($query->_params[$id][0])) {
206 continue;
207 }
208 if (substr($query->_params[$id][0], 0, 6) == 'event_' ||
209 substr($query->_params[$id][0], 0, 12) == 'participant_'
210 ) {
211 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
212 $query->_useDistinct = TRUE;
213 }
214 $grouping = $query->_params[$id][3];
215 self::whereClauseSingle($query->_params[$id], $query);
216 }
217 }
218 }
219
220 static function whereClauseSingle(&$values, &$query) {
221 list($name, $op, $value, $grouping, $wildcard) = $values;
222 switch ($name) {
223 case 'event_start_date_low':
224 case 'event_start_date_high':
225 $query->dateQueryBuilder($values,
226 'civicrm_event', 'event_start_date', 'start_date', 'Start Date'
227 );
228 return;
229
230 case 'event_end_date_low':
231 case 'event_end_date_high':
232 $query->dateQueryBuilder($values,
233 'civicrm_event', 'event_end_date', 'end_date', 'End Date'
234 );
235 return;
236
237 case 'event_id':
238 $query->_where[$grouping][] = "civicrm_event.id $op {$value}";
239 $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $value, 'title');
240 $query->_qill[$grouping][] = ts('Event') . " $op {$eventTitle}";
241 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
242 return;
243
244 case 'event_type_id':
245
246 $eventTypes = CRM_Core_OptionGroup::values("event_type");
247 $query->_where[$grouping][] = "civicrm_participant.event_id = civicrm_event.id and civicrm_event.event_type_id = '{$value}'";
248 $query->_qill[$grouping][] = ts('Event Type - %1', array(1 => $eventTypes[$value]));
249 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
250 return;
251
252 case 'participant_test':
253 // We dont want to include all tests for sql OR CRM-7827
254 if (!$value || $query->getOperator() != 'OR') {
255 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.is_test",
256 $op,
257 $value,
258 "Boolean"
259 );
260 if ($value) {
261 $query->_qill[$grouping][] = ts("Participant is a Test");
262 }
263 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
264 }
265 return;
266
267 case 'participant_fee_id':
268 $feeLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $value, 'label');
269 $feeLabel = CRM_Core_DAO::escapeString(trim($feeLabel));
270 if ($value) {
271 $query->_where[$grouping][] = "civicrm_participant.fee_level LIKE '%$feeLabel%'";
272 $query->_qill[$grouping][] = ts("Fee level") . " contains $feeLabel";
273 }
274 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
275 return;
276
277 case 'participant_fee_amount':
278 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.fee_amount",
279 $op,
280 $value,
281 "Money"
282 );
283 if ($value) {
284 $query->_qill[$grouping][] = ts("Fee Amount") . " $op $value";
285 }
286 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
287 return;
288
289 case 'participant_fee_amount_high':
290 case 'participant_fee_amount_low':
291 $query->numberRangeBuilder($values,
292 'civicrm_participant', 'participant_fee_amount', 'fee_amount', 'Fee Amount'
293 );
294 return;
295
296 case 'participant_pay_later':
297 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.is_pay_later",
298 $op,
299 $value,
300 "Boolean"
301 );
302 $query->_qill[$grouping][] = $value ? ts("Participant is Pay Later") : ts("Participant is not Pay Later");
303 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
304 return;
305
306 case 'participant_status':
307 case 'participant_status_id':
308 $val = array();
309 if (is_array($value)) {
310 foreach ($value as $k => $v) {
311 if ($v) {
312 $val[$k] = $k;
313 }
314 }
315 $status = implode(',', $val);
316 }
317 else {
318 $status = $value;
319 }
320
321 if (count($val) > 1) {
322 $op = 'IN';
323 $status = "({$status})";
324 }
325
326 $statusTypes = CRM_Event_PseudoConstant::participantStatus();
327 $names = array();
328
329 if (!empty($val)) {
330 foreach ($val as $id => $dontCare) {
331 $names[] = $statusTypes[$id];
332 }
333 }
334 else {
335 $names[] = $statusTypes[$value];
336 }
337
338 $query->_qill[$grouping][] = ts('Participant Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
339
340 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.status_id",
341 $op,
342 $status,
343 "Integer"
344 );
345 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
346 return;
347
348 case 'participant_role':
349 case 'participant_role_id':
350 $val = array();
351 if (is_array($value)) {
352 foreach ($value as $k => $v) {
353 if ($v) {
354 $val[$k] = $k;
355 }
356 }
357 }
358 else {
359 $val = array($value => 1);
360 }
361
362 $roleTypes = CRM_Event_PseudoConstant::participantRole();
363
364 $names = array();
365 foreach ($val as $id => $dontCare) {
366 $names[] = $roleTypes[$id];
367 }
368
369 if (!empty($names)) {
370 $query->_qill[$grouping][] =
371 ts('Participant Role %1', array(1 => $op)) .
372 ' ' .
373 implode(' ' . ts('or') . ' ', $names);
374 $query->_where[$grouping][] =
375 " civicrm_participant.role_id REGEXP '[[:<:]]" .
376 implode('[[:>:]]|[[:<:]]', array_keys($val)) .
377 "[[:>:]]' ";
378
379 $query->_tables['civicrm_participant'] =
380 $query->_whereTables['civicrm_participant'] = 1;
381 }
382 return;
383
384 case 'participant_source':
385 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.source",
386 $op,
387 $value,
388 "String"
389 );
390 $query->_qill[$grouping][] = ts("Participant Source") . " $op $value";
391 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
392 return;
393
394 case 'participant_register_date':
395 $query->dateQueryBuilder($values,
396 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
397 );
398 return;
399
400 case 'participant_id':
401 $query->_where[$grouping][] = "civicrm_participant.id $op $value";
402 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
403 return;
404
405 case 'event_id':
406 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.id",
407 $op,
408 $value,
409 "Integer"
410 );
411 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
412 $title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $value, "title");
413 $query->_qill[$grouping][] = ts('Event') . " $op $value";
414 return;
415
416 case 'participant_contact_id':
417 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.contact_id",
418 $op,
419 $value,
420 "Integer"
421 );
422 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
423 return;
424
425 case 'event_is_public':
426 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_event.is_public",
427 $op,
428 $value,
429 "Integer"
430 );
431 $query->_tables['civicrm_event'] = $query->_whereTables['civicrm_event'] = 1;
432 return;
433
434 case 'participant_campaign_id':
435 $campParams = array(
436 'op' => $op,
437 'campaign' => $value,
438 'grouping' => $grouping,
439 'tableName' => 'civicrm_participant',
440 );
441 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
442 return;
443 }
444 }
445
446 static function from($name, $mode, $side) {
447 $from = NULL;
448 switch ($name) {
449 case 'civicrm_participant':
450 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
451 break;
452
453 case 'civicrm_event':
454 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
455 break;
456
457 case 'event_type':
458 $from = " $side JOIN civicrm_option_group option_group_event_type ON (option_group_event_type.name = 'event_type')";
459 $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 ) ";
460 break;
461
462 case 'participant_note':
463 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_participant' AND
464 civicrm_participant.id = civicrm_note.entity_id )";
465 break;
466
467 case 'participant_status':
468 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
469 break;
470
471 case 'participant_role':
472 $from = " $side JOIN civicrm_option_group option_group_participant_role ON (option_group_participant_role.name = 'participant_role')";
473 $from .= " $side JOIN civicrm_option_value participant_role ON (civicrm_participant.role_id = participant_role.value
474 AND option_group_participant_role.id = participant_role.option_group_id ) ";
475 break;
476
477 case 'participant_discount_name':
478 $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )";
479 $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) ";
480 break;
481 }
482 return $from;
483 }
484
485 /**
486 * getter for the qill object
487 *
488 * @return string
489 * @access public
490 */
491 function qill() {
492 return (isset($this->_qill)) ? $this->_qill : "";
493 }
494
495 static function defaultReturnProperties($mode,
496 $includeCustomFields = TRUE
497 ) {
498 $properties = NULL;
499 if ($mode & CRM_Contact_BAO_Query::MODE_EVENT) {
500 $properties = array(
501 'contact_type' => 1,
502 'contact_sub_type' => 1,
503 'sort_name' => 1,
504 'display_name' => 1,
505 'event_id' => 1,
506 'event_title' => 1,
507 'event_start_date' => 1,
508 'event_end_date' => 1,
509 'event_type' => 1,
510 'participant_id' => 1,
511 'participant_status' => 1,
512 'participant_role_id' => 1,
513 'participant_note' => 1,
514 'participant_register_date' => 1,
515 'participant_source' => 1,
516 'participant_fee_level' => 1,
517 'participant_is_test' => 1,
518 'participant_is_pay_later' => 1,
519 'participant_fee_amount' => 1,
520 'participant_discount_name' => 1,
521 'participant_fee_currency' => 1,
522 'participant_registered_by_id' => 1,
523 'participant_campaign_id' => 1,
524 );
525
526 if ($includeCustomFields) {
527 // also get all the custom participant properties
528 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Participant');
529 if (!empty($fields)) {
530 foreach ($fields as $name => $dontCare) {
531 $properties[$name] = 1;
532 }
533 }
534 }
535 }
536
537 return $properties;
538 }
539
540 /**
541 * @param CRM_Core_Form $form
542 */
543 static function buildSearchForm(&$form) {
544 $dataURLEventFee = CRM_Utils_System::url('civicrm/ajax/eventFee',
545 "reset=1",
546 FALSE, NULL, FALSE
547 );
548
549 $form->assign('dataURLEventFee', $dataURLEventFee);
550
551 $eventId = $form->addEntityRef('event_id', ts('Event Name'), array(
552 'entity' => 'event',
553 'placeholder' => ts('- any -'),
554 'select' => array('minimumInputLength' => 0),
555 )
556 );
557 $eventType = $form->addEntityRef('event_type_id', ts('Event Type'), array(
558 'entity' => 'option_value',
559 'placeholder' => ts('- any -'),
560 'select' => array('minimumInputLength' => 0),
561 'api' => array(
562 'params' => array('option_group_id' => 'event_type'),
563 ),
564 )
565 );
566 $form->add('text', 'participant_fee_id', ts('Fee Level'), array('class' => 'big crm-ajax-select'));
567
568 CRM_Core_Form_Date::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
569
570 $status = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
571 asort($status);
572 foreach ($status as $id => $Name) {
573 $form->_participantStatus = &$form->addElement('checkbox', "participant_status_id[$id]", NULL, $Name);
574 }
575
576 foreach (CRM_Event_PseudoConstant::participantRole() as $rId => $rName) {
577 $form->_participantRole = &$form->addElement('checkbox', "participant_role_id[$rId]", NULL, $rName);
578 }
579
580 $form->addYesNo('participant_test', ts('Participant is a Test?'), TRUE);
581 $form->addYesNo('participant_pay_later', ts('Participant is Pay Later?'), TRUE);
582 $form->addElement('text', 'participant_fee_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
583 $form->addElement('text', 'participant_fee_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
584
585 $form->addRule('participant_fee_amount_low', ts('Please enter a valid money value.'), 'money');
586 $form->addRule('participant_fee_amount_high', ts('Please enter a valid money value.'), 'money');
587 // add all the custom searchable fields
588 $extends = array('Participant', 'Event');
589 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
590 if ($groupDetails) {
591 $form->assign('participantGroupTree', $groupDetails);
592 foreach ($groupDetails as $group) {
593 foreach ($group['fields'] as $field) {
594 $fieldId = $field['id'];
595 $elementName = 'custom_' . $fieldId;
596 CRM_Core_BAO_CustomField::addQuickFormElement($form,
597 $elementName,
598 $fieldId,
599 FALSE, FALSE, TRUE
600 );
601 }
602 }
603 }
604
605 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'participant_campaign_id');
606
607 $form->assign('validCiviEvent', TRUE);
608 $form->setDefaults(array('participant_test' => 0));
609 }
610
611 static function searchAction(&$row, $id) {}
612
613 static function tableNames(&$tables) {
614 //add participant table
615 if (!empty($tables['civicrm_event'])) {
616 $tables = array_merge(array('civicrm_participant' => 1), $tables);
617 }
618 }
619 }
620