Merge pull request #3216 from xurizaemon/CRM-14596
[civicrm-core.git] / CRM / Activity / 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_Activity_BAO_Query {
37
38 /**
39 * build select for Case
40 *
41 * @param $query
42 *
43 * @return void
44 * @access public
45 */
46 static function select(&$query) {
47 if (!empty($query->_returnProperties['activity_id'])) {
48 $query->_select['activity_id'] = "civicrm_activity.id as activity_id";
49 $query->_element['activity_id'] = 1;
50 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
51 }
52
53 if (!empty($query->_returnProperties['activity_type_id'])) {
54 $query->_select['activity_type_id'] = "activity_type.id as activity_type_id";
55 $query->_element['activity_type_id'] = 1;
56 $query->_tables['civicrm_activity'] = 1;
57 $query->_tables['activity_type'] = 1;
58 $query->_whereTables['civicrm_activity'] = 1;
59 $query->_whereTables['activity_type'] = 1;
60 }
61
62 if (!empty($query->_returnProperties['activity_type'])) {
63 $query->_select['activity_type'] = "activity_type.label as activity_type,
64 civicrm_activity.activity_type_id as activity_type_id";
65 $query->_element['activity_type'] = 1;
66 $query->_tables['civicrm_activity'] = 1;
67 $query->_tables['activity_type'] = 1;
68 $query->_whereTables['civicrm_activity'] = 1;
69 $query->_whereTables['activity_type'] = 1;
70 }
71
72 if (!empty($query->_returnProperties['activity_subject'])) {
73 $query->_select['activity_subject'] = "civicrm_activity.subject as activity_subject";
74 $query->_element['activity_subject'] = 1;
75 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
76 }
77
78 if (!empty($query->_returnProperties['activity_date_time'])) {
79 $query->_select['activity_date_time'] = "civicrm_activity.activity_date_time as activity_date_time";
80 $query->_element['activity_date_time'] = 1;
81 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
82 }
83
84 if (!empty($query->_returnProperties['activity_status_id'])) {
85 $query->_select['activity_status_id'] = "activity_status.value as activity_status_id";
86 $query->_element['activity_status_id'] = 1;
87 $query->_tables['civicrm_activity'] = 1;
88 $query->_tables['activity_status'] = 1;
89 $query->_whereTables['civicrm_activity'] = 1;
90 $query->_whereTables['activity_status'] = 1;
91 }
92
93 if (!empty($query->_returnProperties['activity_status'])) {
94 $query->_select['activity_status'] = "activity_status.label as activity_status";
95 $query->_element['activity_status'] = 1;
96 $query->_tables['civicrm_activity'] = 1;
97 $query->_tables['activity_status'] = 1;
98 $query->_whereTables['civicrm_activity'] = 1;
99 $query->_whereTables['activity_status'] = 1;
100 }
101
102 if (!empty($query->_returnProperties['activity_duration'])) {
103 $query->_select['activity_duration'] = "civicrm_activity.duration as activity_duration";
104 $query->_element['activity_duration'] = 1;
105 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
106 }
107
108 if (!empty($query->_returnProperties['activity_location'])) {
109 $query->_select['activity_location'] = "civicrm_activity.location as activity_location";
110 $query->_element['activity_location'] = 1;
111 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
112 }
113
114 if (!empty($query->_returnProperties['activity_details'])) {
115 $query->_select['activity_details'] = "civicrm_activity.details as activity_details";
116 $query->_element['activity_details'] = 1;
117 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
118 }
119
120 if (!empty($query->_returnProperties['source_record_id'])) {
121 $query->_select['source_record_id'] = "civicrm_activity.source_record_id as source_record_id";
122 $query->_element['source_record_id'] = 1;
123 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
124 }
125
126 if (!empty($query->_returnProperties['activity_is_test'])) {
127 $query->_select['activity_is_test'] = "civicrm_activity.is_test as activity_is_test";
128 $query->_element['activity_is_test'] = 1;
129 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
130 }
131
132 if (!empty($query->_returnProperties['activity_campaign_id'])) {
133 $query->_select['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
134 $query->_element['activity_campaign_id'] = 1;
135 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
136 }
137
138 if (!empty($query->_returnProperties['activity_engagement_level'])) {
139 $query->_select['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
140 $query->_element['activity_engagement_level'] = 1;
141 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
142 }
143
144 if (!empty($query->_returnProperties['source_contact'])) {
145 $query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
146 $query->_element['source_contact'] = 1;
147 $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
148 }
149
150 if (!empty($query->_returnProperties['activity_result'])) {
151 $query->_select['activity_result'] = 'civicrm_activity.result as activity_result';
152 $query->_element['result'] = 1;
153 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
154 }
155
156 if (CRM_Utils_Array::value('parent_id', $query->_returnProperties)) {
157 $query->_tables['parent_id'] = 1;
158 $query->_whereTables['parent_id'] = 1;
159 $query->_element['parent_id'] = 1;
160 }
161 }
162
163 /**
164 * Given a list of conditions in query generate the required
165 * where clause
166 *
167 * @param $query
168 *
169 * @return void
170 * @access public
171 */
172 static function where(&$query) {
173 $grouping = NULL;
174 foreach (array_keys($query->_params) as $id) {
175 if (substr($query->_params[$id][0], 0, 9) == 'activity_') {
176 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
177 $query->_useDistinct = TRUE;
178 }
179 $grouping = $query->_params[$id][3];
180 self::whereClauseSingle($query->_params[$id], $query);
181 }
182 }
183 }
184
185 /**
186 * where clause for a single field
187 *
188 * @param $values
189 * @param $query
190 * @return void
191 * @access public
192 */
193 static function whereClauseSingle(&$values, &$query) {
194 list($name, $op, $value, $grouping, $wildcard) = $values;
195
196 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
197 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
198 if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
199 $query->_skipDeleteClause = TRUE;
200 }
201
202 switch ($name) {
203 case 'activity_type_id':
204 case 'activity_type':
205 $types = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
206 //get the component activity types.
207 $compActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, TRUE);
208 $activityTypeIds = self::buildWhereAndQill($query, $value, $types, $op, $grouping, array('Activity Type', 'civicrm_activity.activity_type_id'));
209
210 foreach ($activityTypeIds as $activityTypeId) {
211 if (array_key_exists($activityTypeId, $compActTypes)) {
212 CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
213 break;
214 }
215 }
216 break;
217
218 case 'activity_survey_id':
219 if (!$value) {
220 break;
221 }
222 $value = CRM_Utils_Type::escape($value, 'Integer');
223 $query->_where[$grouping][] = " source_record_id = $value";
224 $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
225 break;
226
227 case 'activity_engagement_level':
228 if (!$value) {
229 break;
230 }
231
232 $value = CRM_Utils_Type::escape($value, 'Integer');
233 $query->_where[$grouping][] = " engagement_level = $value";
234 $query->_qill[$grouping][] = ts('Engagement Index') . ' - ' . CRM_Core_OptionGroup::getLabel('engagement_index', $value);
235 break;
236
237 case 'activity_role':
238 CRM_Contact_BAO_Query::$_activityRole = $values[2];
239 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
240 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
241 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
242 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
243
244 if ($values[2]) {
245 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
246 if ($values[2] == 1) {
247 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
248 $query->_qill[$grouping][] = ts('Activity created by');
249 }
250 else if ($values[2] == 2) {
251 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
252 $query->_qill[$grouping][] = ts('Activity assigned to');
253 }
254 else if ($values[2] == 3) {
255 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
256 $query->_qill[$grouping][] = ts('Activity targeted to');
257 }
258 }
259 break;
260
261 case 'activity_status':
262 $status = CRM_Core_PseudoConstant::activityStatus();
263
264 $activityTypeIds = self::buildWhereAndQill($query, $value, $status, $op, $grouping, array('Activity Status', 'civicrm_activity.status_id'));
265 break;
266
267 case 'activity_subject':
268 $n = trim($value);
269 $value = strtolower(CRM_Core_DAO::escapeString($n));
270 if ($wildcard) {
271 if (strpos($value, '%') !== FALSE) {
272 // only add wild card if not there
273 $value = "'$value'";
274 }
275 else {
276 $value = "'%$value%'";
277 }
278 $op = 'LIKE';
279 }
280 else {
281 $value = "'$value'";
282 }
283 $wc = ($op != 'LIKE') ? "LOWER(civicrm_activity.subject)" : "civicrm_activity.subject";
284 $query->_where[$grouping][] = " $wc $op $value";
285 $query->_qill[$grouping][] = ts('Subject') . " $op - '$n'";
286 break;
287
288 case 'activity_test':
289 // We dont want to include all tests for sql OR CRM-7827
290 if (!$value || $query->getOperator() != 'OR') {
291 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
292 if ($value) {
293 $query->_qill[$grouping][] = ts('Activity is a Test');
294 }
295 }
296 break;
297
298 case 'activity_date':
299 case 'activity_date_low':
300 case 'activity_date_high':
301 $query->dateQueryBuilder($values,
302 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
303 );
304 break;
305
306 case 'activity_id':
307 if (empty($value)) {
308 break;
309 }
310
311 if (is_array($value)) {
312 foreach ($value as $k => $v) {
313 $value[$k] = CRM_Utils_Type::escape($v, 'Integer');
314 }
315 }
316 else {
317 $value = array(CRM_Utils_Type::escape($value, 'Integer'));
318 }
319 $query->_where[$grouping][] = "civicrm_activity.id IN (" . implode(",", $value) . ")";
320 $query->_qill[$grouping][] = ts('Activity Id(s) %1', array(1 => implode($value)));
321 break;
322
323 case 'activity_taglist':
324 $taglist = $value;
325 $value = array();
326 foreach ($taglist as $val) {
327 if ($val) {
328 $val = explode(',', $val);
329 foreach ($val as $tId) {
330 if (is_numeric($tId)) {
331 $value[$tId] = 1;
332 }
333 }
334 }
335 }
336
337 case 'activity_tags':
338 $value = array_keys($value);
339 $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
340
341 $names = array();
342 $val = array();
343 if (is_array($value)) {
344 foreach ($value as $k => $v) {
345 $names[] = $activityTags[$v];
346 }
347 }
348 $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
349 $query->_qill[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
350 $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
351 break;
352
353 case 'activity_campaign_id':
354 $campParams = array(
355 'op' => $op,
356 'campaign' => $value,
357 'grouping' => $grouping,
358 'tableName' => 'civicrm_activity',
359 );
360 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
361 break;
362 case 'activity_result':
363 if(is_array($value)) {
364 $safe = NULL;
365 while(list(,$k) = each($value)) {
366 $safe[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
367 }
368 $query->_where[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")";
369 $query->_qill[$grouping][] = ts("Activity Result - %1", array(1 => implode(' or ', $safe)));
370 }
371 break;
372
373 case 'parent_id':
374 if ($value == 1) {
375 $query->_where[$grouping][] = "parent_id.parent_id IS NOT NULL";
376 $query->_qill[$grouping][] = ts('Activities which have Followup Activities');
377 }
378 elseif ($value == 2) {
379 $query->_where[$grouping][] = "parent_id.parent_id IS NULL";
380 $query->_qill[$grouping][] = ts('Activities without Followup Activities');
381 }
382 break;
383
384 case 'followup_parent_id':
385 if ($value == 1) {
386 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL";
387 $query->_qill[$grouping][] = ts('Activities which are Followup Activities');
388 }
389 elseif ($value == 2) {
390 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL";
391 $query->_qill[$grouping][] = ts('Activities which are not Followup Activities');
392 }
393 break;
394 }
395 }
396
397 static function from($name, $mode, $side) {
398 $from = NULL;
399 switch ($name) {
400 case 'civicrm_activity':
401 //CRM-7480 we are going to civicrm_activity table either
402 //from civicrm_activity_target or civicrm_activity_assignment.
403 //as component specific activities does not have entry in
404 //activity target table so lets consider civicrm_activity_assignment.
405 $from .= " INNER JOIN civicrm_activity_contact
406 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
407 $from .= " INNER JOIN civicrm_activity
408 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
409 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
410
411 $activityRole = CRM_Contact_BAO_Query::$_activityRole;
412 break;
413
414 case 'activity_status':
415 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
416 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
417 AND option_group_activity_status.id = activity_status.option_group_id ) ";
418 break;
419
420 case 'activity_type':
421 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
422 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
423 AND option_group_activity_type.id = activity_type.option_group_id ) ";
424 break;
425
426 case 'civicrm_activity_tag':
427 $from .= " $side JOIN civicrm_entity_tag as civicrm_activity_tag ON ( civicrm_activity_tag.entity_table = 'civicrm_activity' AND civicrm_activity_tag.entity_id = civicrm_activity.id ) ";
428 break;
429
430 case 'source_contact':
431 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
432 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
433 $from = "
434 LEFT JOIN civicrm_activity_contact ac
435 ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
436 INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
437 break;
438
439 case 'parent_id':
440 $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id";
441 break;
442 }
443
444 return $from;
445 }
446
447 /**
448 * getter for the qill object
449 *
450 * @return string
451 * @access public
452 */
453 function qill() {
454 return (isset($this->_qill)) ? $this->_qill : "";
455 }
456
457 /**
458 * add all the elements shared between case activity search and advanaced search
459 *
460 * @access public
461 *
462 * @param $form CRM_Core_Form
463 * @return void
464 * @static
465 */
466 static function buildSearchForm(&$form) {
467 $activityOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
468 asort($activityOptions);
469 foreach ($activityOptions as $activityID => $activity) {
470 $form->_activityElement =&
471 $form->addElement(
472 'checkbox',
473 "activity_type_id[$activityID]",
474 NULL,
475 $activity,
476 array('onClick' => 'showCustomData( this.id );')
477 );
478 }
479
480 CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
481 $followUpActivity = array(
482 1 => ts('Yes'),
483 2 => ts('No'),
484 );
485 $form->addRadio('parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
486 $form->addRadio('followup_parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
487 $activityRoles = array(
488 3 => ts('With'),
489 2 => ts('Assigned to'),
490 1 => ts('Added by'),
491 );
492 $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
493 $form->setDefaults(array('activity_role' => 3));
494 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
495 foreach ($activityStatus as $activityStatusID => $activityStatusName) {
496 $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
497 }
498 $form->addGroup($activity_status, 'activity_status', ts('Activity Status'));
499 $form->setDefaults(array('activity_status[1]' => 1, 'activity_status[2]' => 1));
500 $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
501 $form->addYesNo('activity_test', ts('Activity is a Test?'));
502 $activity_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
503 if ($activity_tags) {
504 foreach ($activity_tags as $tagID => $tagName) {
505 $form->_tagElement = &$form->addElement('checkbox', "activity_tags[$tagID]",
506 NULL, $tagName
507 );
508 }
509 }
510
511 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
512 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
513
514 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
515 if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
516 array('' => ts('- none -')) + $surveys, FALSE,
517 array('class' => 'crm-select2')
518 );
519
520 $extends = array('Activity');
521 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
522 if ($groupDetails) {
523 $form->assign('activityGroupTree', $groupDetails);
524 foreach ($groupDetails as $group) {
525 foreach ($group['fields'] as $field) {
526 $fieldId = $field['id'];
527 $elementName = 'custom_' . $fieldId;
528 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, FALSE, TRUE);
529 }
530 }
531 }
532
533 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
534
535 //add engagement level CRM-7775
536 $buildEngagementLevel = FALSE;
537 $buildSurveyResult = FALSE;
538 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
539 CRM_Campaign_BAO_Campaign::accessCampaign()
540 ) {
541 $buildEngagementLevel = TRUE;
542 $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'option_url' => NULL));
543
544 // Add survey result field.
545 $optionGroups = CRM_Campaign_BAO_Survey::getResultSets( 'name' );
546 $resultOptions = array();
547 foreach ( $optionGroups as $gid => $name ) {
548 if ( $name ) {
549 $value = array();
550 $value = CRM_Core_OptionGroup::values($name);
551 if (!empty($value)) {
552 while(list($k,$v) = each($value)) {
553 $resultOptions[$v] = $v;
554 }
555 }
556 }
557 }
558 // If no survey result options have been created, don't build
559 // the field to avoid clutter.
560 if(count($resultOptions) > 0) {
561 $buildSurveyResult = TRUE;
562 asort($resultOptions);
563 $form->add('select', 'activity_result', ts("Survey Result"),
564 $resultOptions, FALSE,
565 array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
566 );
567 }
568 }
569
570 $form->assign('buildEngagementLevel', $buildEngagementLevel);
571 $form->assign('buildSurveyResult', $buildSurveyResult);
572 $form->setDefaults(array('activity_test' => 0));
573 }
574
575 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
576 $properties = NULL;
577 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
578 $properties = array(
579 'activity_id' => 1,
580 'contact_type' => 1,
581 'contact_sub_type' => 1,
582 'sort_name' => 1,
583 'display_name' => 1,
584 'activity_type' => 1,
585 'activity_type_id' => 1,
586 'activity_subject' => 1,
587 'activity_date_time' => 1,
588 'activity_duration' => 1,
589 'activity_location' => 1,
590 'activity_details' => 1,
591 'activity_status' => 1,
592 'source_contact' => 1,
593 'source_record_id' => 1,
594 'activity_is_test' => 1,
595 'activity_campaign_id' => 1,
596 'result' => 1,
597 'activity_engagement_level' => 1,
598 'parent_id' => 1,
599 );
600
601 if ($includeCustomFields) {
602 // also get all the custom activity properties
603 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
604 if (!empty($fields)) {
605 foreach ($fields as $name => $dontCare) {
606 $properties[$name] = 1;
607 }
608 }
609 }
610 }
611
612 return $properties;
613 }
614
615 static function buildWhereAndQill(&$query, $value, $pseudoconstantType, $op, $grouping, $params) {
616 $matches = $val = $clause = array();
617
618 if (is_array($value)) {
619 foreach ($value as $k => $v) {
620 if ($k) {
621 $val[] = $k;
622 }
623 }
624
625 if (count($val) > 0) {
626 // Overwrite $op so it works with an IN where statement.
627 $op = 'IN';
628 }
629 else {
630 // If we somehow have an empty array, just return
631 return;
632 }
633 $value = $matches[0] = $val;
634 }
635 else {
636 preg_match_all('/\d+/', $value, $matches);
637 }
638 foreach ($matches[0] as $qill) {
639 $clause[] = CRM_Utils_Array::value($qill, $pseudoconstantType);
640 }
641
642 $query->_qill[$grouping][] = ts($params[0] . ' %1 ', array(1 => $op)) . implode(' ' . ts('or') . ' ', $clause);
643 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($params[1],
644 $op, $value, "Integer"
645 );
646 return $matches[0];
647 }
648 }
649