QA fixes Round 2
[civicrm-core.git] / CRM / Event / BAO / Query.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
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 /**
39 * @return array
40 */
41 public static function &getFields() {
42 $fields = array();
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());
46
47 return $fields;
48 }
49
50 /**
51 * @return array
52 */
53 public static function &getParticipantFields() {
54 $fields = CRM_Event_BAO_Participant::importableFields('Individual', TRUE, TRUE);
55 return $fields;
56 }
57
58 /**
59 * Build select for CiviEvent
60 *
61 * @param $query
62 *
63 * @return void
64 */
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_')
68 ) {
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;
72
73 //add fee level
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;
77 }
78
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;
83 }
84
85 //add fee amount
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;
89 }
90
91 //add fee currency
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;
95 }
96
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;
105 }
106
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;
111 }
112
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;
116 }
117
118 //event type
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;
124 }
125
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;
131 }
132
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',
146 );
147 }
148
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',
162 );
163 }
164
165 //add register date
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;
169 }
170
171 //add source
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;
176 }
177
178 //participant note
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;
184 }
185
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;
189 }
190
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;
194 }
195
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;
199 }
200
201 // get discount name
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;
209 }
210
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;
215 }
216 }
217 }
218
219
220 /**
221 * @param $query
222 */
223 public static function where(&$query) {
224 $grouping = NULL;
225 foreach (array_keys($query->_params) as $id) {
226 if (empty($query->_params[$id][0])) {
227 continue;
228 }
229 if (substr($query->_params[$id][0], 0, 6) == 'event_' ||
230 substr($query->_params[$id][0], 0, 12) == 'participant_'
231 ) {
232 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
233 $query->_useDistinct = TRUE;
234 }
235 $grouping = $query->_params[$id][3];
236 self::whereClauseSingle($query->_params[$id], $query);
237 }
238 }
239 }
240
241 /**
242 * @param $values
243 * @param $query
244 */
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());
248
249 switch ($name) {
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'
254 );
255 return;
256
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'
261 );
262 return;
263
264 case 'event_include_repeating_events':
265 /**
266 * Include Repeating Events
267 */
268 //Get parent of this event
269 $exEventId = '';
270 if ($query->_where[$grouping]) {
271 foreach ($query->_where[$grouping] as $key => $val) {
272 if (strstr($val, 'civicrm_event.id =')) {
273 $exEventId = $val;
274 $extractEventId = explode(" ", $val);
275 $value = $extractEventId[2];
276 unset($query->_where[$grouping][$key]);
277 }
278 }
279 $extractEventId = explode(" ", $exEventId);
280 $value = $extractEventId[2];
281 unset($query->_where[$grouping][$key]);
282 }
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'];
289 }
290 if (!empty($allEventIds)) {
291 $op = "IN";
292 $value = "(" . implode(",", $allEventIds) . ")";
293 }
294 }
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;
298 return;
299
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",
304 $op,
305 $value,
306 "Boolean"
307 );
308 if ($value) {
309 $query->_qill[$grouping][] = ts("Participant is a Test");
310 }
311 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
312 }
313 return;
314
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));
318 if ($value) {
319 $query->_where[$grouping][] = "civicrm_participant.fee_level LIKE '%$feeLabel%'";
320 $query->_qill[$grouping][] = ts("Fee level") . " contains $feeLabel";
321 }
322 $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1;
323 return;
324
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'
329 );
330 return;
331
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':
342 $qillName = $name;
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);
345 }
346 if ($name == 'participant_pay_later') {
347 $name = str_replace('participant', 'is', $name);
348 $qillName = 'participant_' . $name;
349 }
350
351 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
352
353 if (in_array($name, array('participant_status', 'participant_role'))) {
354 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, $dataType);
355 }
356 else {
357 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_participant.$name", $op, $value, $dataType);
358 }
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;
362 return;
363
364 case 'participant_register_date':
365 $query->dateQueryBuilder($values,
366 'civicrm_participant', 'participant_register_date', 'register_date', 'Register Date'
367 );
368 return;
369
370 case 'event_id':
371 case 'event_is_public':
372 case 'event_type_id':
373
374 $qillName = $name;
375 if (in_array($name, array('event_id', 'event_is_public'))) {
376 $name = str_replace('event_', '', $name);
377 }
378 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
379
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)) {
383 break;
384 }
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));
387 return;
388
389 case 'participant_campaign_id':
390 $campParams = array(
391 'op' => $op,
392 'campaign' => $value,
393 'grouping' => $grouping,
394 'tableName' => 'civicrm_participant',
395 );
396 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
397 return;
398 }
399 }
400
401 /**
402 * @param string $name
403 * @param $mode
404 * @param $side
405 *
406 * @return null|string
407 */
408 public static function from($name, $mode, $side) {
409 $from = NULL;
410 switch ($name) {
411 case 'civicrm_participant':
412 $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
413 break;
414
415 case 'civicrm_event':
416 $from = " INNER JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";
417 break;
418
419 case 'event_type':
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 ) ";
422 break;
423
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 )";
427 break;
428
429 case 'participant_status':
430 $from .= " $side JOIN civicrm_participant_status_type participant_status ON (civicrm_participant.status_id = participant_status.id) ";
431 break;
432
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 ) ";
437 break;
438
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 ) ";
442 break;
443 }
444 return $from;
445 }
446
447 /**
448 * Getter for the qill object
449 *
450 * @return string
451 */
452 public function qill() {
453 return (isset($this->_qill)) ? $this->_qill : "";
454 }
455
456 /**
457 * @param $mode
458 * @param bool $includeCustomFields
459 *
460 * @return array|null
461 */
462 static function defaultReturnProperties(
463 $mode,
464 $includeCustomFields = TRUE
465 ) {
466 $properties = NULL;
467 if ($mode & CRM_Contact_BAO_Query::MODE_EVENT) {
468 $properties = array(
469 'contact_type' => 1,
470 'contact_sub_type' => 1,
471 'sort_name' => 1,
472 'display_name' => 1,
473 'event_id' => 1,
474 'event_title' => 1,
475 'event_start_date' => 1,
476 'event_end_date' => 1,
477 'event_type' => 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,
492 );
493
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;
500 }
501 }
502 }
503 }
504
505 return $properties;
506 }
507
508 /**
509 * @param CRM_Core_Form $form
510 */
511 public static function buildSearchForm(&$form) {
512 $dataURLEventFee = CRM_Utils_System::url('civicrm/ajax/eventFee',
513 "reset=1",
514 FALSE, NULL, FALSE
515 );
516
517 $form->assign('dataURLEventFee', $dataURLEventFee);
518
519 $eventId = $form->addEntityRef('event_id', ts('Event Name'), array(
520 'entity' => 'event',
521 'placeholder' => ts('- any -'),
522 'select' => array('minimumInputLength' => 0),
523 )
524 );
525 $eventType = $form->addEntityRef('event_type_id', ts('Event Type'), array(
526 'entity' => 'option_value',
527 'placeholder' => ts('- any -'),
528 'select' => array('minimumInputLength' => 0),
529 'api' => array(
530 'params' => array('option_group_id' => 'event_type'),
531 ),
532 )
533 );
534 $form->add('text', 'participant_fee_id', ts('Fee Level'), array('class' => 'big crm-ajax-select'));
535
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) ? '));
538
539 $form->addSelect('participant_status_id',
540 array('entity' => 'participant', 'label' => ts('Participant Status'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
541 );
542
543 $form->addSelect('participant_role_id',
544 array('entity' => 'participant', 'label' => ts('Participant Role'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
545 );
546
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));
551
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);
557 if ($groupDetails) {
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,
564 $elementName,
565 $fieldId,
566 FALSE, FALSE, TRUE
567 );
568 }
569 }
570 }
571
572 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'participant_campaign_id');
573
574 $form->assign('validCiviEvent', TRUE);
575 $form->setDefaults(array('participant_test' => 0));
576 }
577
578 /**
579 * @param $row
580 * @param int $id
581 */
582 public static function searchAction(&$row, $id) {
583 }
584
585 /**
586 * @param $tables
587 */
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);
592 }
593 }
594 }