Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-16-01-08-03
[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.value 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 $query->_element['activity_type'] = 1;
65 $query->_tables['civicrm_activity'] = 1;
66 $query->_tables['activity_type'] = 1;
67 $query->_whereTables['civicrm_activity'] = 1;
68 $query->_whereTables['activity_type'] = 1;
69 }
70
71 if (!empty($query->_returnProperties['activity_subject'])) {
72 $query->_select['activity_subject'] = "civicrm_activity.subject as activity_subject";
73 $query->_element['activity_subject'] = 1;
74 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
75 }
76
77 if (!empty($query->_returnProperties['activity_date_time'])) {
78 $query->_select['activity_date_time'] = "civicrm_activity.activity_date_time as activity_date_time";
79 $query->_element['activity_date_time'] = 1;
80 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
81 }
82
83 if (!empty($query->_returnProperties['activity_status_id'])) {
84 $query->_select['activity_status_id'] = "activity_status.value as activity_status_id";
85 $query->_element['activity_status_id'] = 1;
86 $query->_tables['civicrm_activity'] = 1;
87 $query->_tables['activity_status'] = 1;
88 $query->_whereTables['civicrm_activity'] = 1;
89 $query->_whereTables['activity_status'] = 1;
90 }
91
92 if (!empty($query->_returnProperties['activity_status'])) {
93 $query->_select['activity_status'] = "activity_status.label as activity_status,
94 civicrm_activity.status_id as status_id";
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][] = " civicrm_activity.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 /**
398 * @param string $name
399 * @param $mode
400 * @param $side
401 *
402 * @return null|string
403 */
404 static function from($name, $mode, $side) {
405 $from = NULL;
406 switch ($name) {
407 case 'civicrm_activity':
408 //CRM-7480 we are going to civicrm_activity table either
409 //from civicrm_activity_target or civicrm_activity_assignment.
410 //as component specific activities does not have entry in
411 //activity target table so lets consider civicrm_activity_assignment.
412 $from .= " INNER JOIN civicrm_activity_contact
413 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
414 $from .= " INNER JOIN civicrm_activity
415 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
416 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
417
418 $activityRole = CRM_Contact_BAO_Query::$_activityRole;
419 break;
420
421 case 'activity_status':
422 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
423 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
424 AND option_group_activity_status.id = activity_status.option_group_id ) ";
425 break;
426
427 case 'activity_type':
428 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
429 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
430 AND option_group_activity_type.id = activity_type.option_group_id ) ";
431 break;
432
433 case 'civicrm_activity_tag':
434 $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 ) ";
435 break;
436
437 case 'source_contact':
438 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
439 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
440 $from = "
441 LEFT JOIN civicrm_activity_contact ac
442 ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
443 INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
444 break;
445
446 case 'parent_id':
447 $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id";
448 break;
449 }
450
451 return $from;
452 }
453
454 /**
455 * Getter for the qill object
456 *
457 * @return string
458 * @access public
459 */
460 function qill() {
461 return (isset($this->_qill)) ? $this->_qill : "";
462 }
463
464 /**
465 * Add all the elements shared between case activity search and advanced search
466 *
467 * @access public
468 *
469 * @param CRM_Core_Form $form
470 * @return void
471 * @static
472 */
473 static function buildSearchForm(&$form) {
474 $activityOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
475 asort($activityOptions);
476 foreach ($activityOptions as $activityID => $activity) {
477 $form->_activityElement =&
478 $form->addElement(
479 'checkbox',
480 "activity_type_id[$activityID]",
481 NULL,
482 $activity,
483 array('onClick' => 'showCustomData( this.id );')
484 );
485 }
486
487 CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
488 $followUpActivity = array(
489 1 => ts('Yes'),
490 2 => ts('No'),
491 );
492 $form->addRadio('parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
493 $form->addRadio('followup_parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
494 $activityRoles = array(
495 3 => ts('With'),
496 2 => ts('Assigned to'),
497 1 => ts('Added by'),
498 );
499 $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
500 $form->setDefaults(array('activity_role' => 3));
501 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
502 foreach ($activityStatus as $activityStatusID => $activityStatusName) {
503 $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
504 }
505 $form->addGroup($activity_status, 'activity_status', ts('Activity Status'));
506 $form->setDefaults(array('activity_status[1]' => 1, 'activity_status[2]' => 1));
507 $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
508 $form->addYesNo('activity_test', ts('Activity is a Test?'));
509 $activity_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
510 if ($activity_tags) {
511 foreach ($activity_tags as $tagID => $tagName) {
512 $form->_tagElement = &$form->addElement('checkbox', "activity_tags[$tagID]",
513 NULL, $tagName
514 );
515 }
516 }
517
518 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
519 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
520
521 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
522 if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
523 array('' => ts('- none -')) + $surveys, FALSE,
524 array('class' => 'crm-select2')
525 );
526
527 $extends = array('Activity');
528 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
529 if ($groupDetails) {
530 $form->assign('activityGroupTree', $groupDetails);
531 foreach ($groupDetails as $group) {
532 foreach ($group['fields'] as $field) {
533 $fieldId = $field['id'];
534 $elementName = 'custom_' . $fieldId;
535 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, FALSE, TRUE);
536 }
537 }
538 }
539
540 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
541
542 //add engagement level CRM-7775
543 $buildEngagementLevel = FALSE;
544 $buildSurveyResult = FALSE;
545 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
546 CRM_Campaign_BAO_Campaign::accessCampaign()
547 ) {
548 $buildEngagementLevel = TRUE;
549 $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'option_url' => NULL));
550
551 // Add survey result field.
552 $optionGroups = CRM_Campaign_BAO_Survey::getResultSets( 'name' );
553 $resultOptions = array();
554 foreach ( $optionGroups as $gid => $name ) {
555 if ( $name ) {
556 $value = array();
557 $value = CRM_Core_OptionGroup::values($name);
558 if (!empty($value)) {
559 while(list($k,$v) = each($value)) {
560 $resultOptions[$v] = $v;
561 }
562 }
563 }
564 }
565 // If no survey result options have been created, don't build
566 // the field to avoid clutter.
567 if(count($resultOptions) > 0) {
568 $buildSurveyResult = TRUE;
569 asort($resultOptions);
570 $form->add('select', 'activity_result', ts("Survey Result"),
571 $resultOptions, FALSE,
572 array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
573 );
574 }
575 }
576
577 $form->assign('buildEngagementLevel', $buildEngagementLevel);
578 $form->assign('buildSurveyResult', $buildSurveyResult);
579 $form->setDefaults(array('activity_test' => 0));
580 }
581
582 /**
583 * @param $mode
584 * @param bool $includeCustomFields
585 *
586 * @return array|null
587 */
588 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
589 $properties = NULL;
590 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
591 $properties = array(
592 'activity_id' => 1,
593 'contact_type' => 1,
594 'contact_sub_type' => 1,
595 'sort_name' => 1,
596 'display_name' => 1,
597 'activity_type' => 1,
598 'activity_type_id' => 1,
599 'activity_subject' => 1,
600 'activity_date_time' => 1,
601 'activity_duration' => 1,
602 'activity_location' => 1,
603 'activity_details' => 1,
604 'activity_status' => 1,
605 'source_contact' => 1,
606 'source_record_id' => 1,
607 'activity_is_test' => 1,
608 'activity_campaign_id' => 1,
609 'result' => 1,
610 'activity_engagement_level' => 1,
611 'parent_id' => 1,
612 );
613
614 if ($includeCustomFields) {
615 // also get all the custom activity properties
616 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
617 if (!empty($fields)) {
618 foreach ($fields as $name => $dontCare) {
619 $properties[$name] = 1;
620 }
621 }
622 }
623 }
624
625 return $properties;
626 }
627
628 /**
629 * @param $query
630 * @param $value
631 * @param $pseudoconstantType
632 * @param $op
633 * @param $grouping
634 * @param array $params
635 *
636 * @return array
637 */
638 static function buildWhereAndQill(&$query, $value, $pseudoconstantType, $op, $grouping, $params) {
639 $matches = $val = $clause = array();
640
641 if (is_array($value)) {
642 foreach ($value as $k => $v) {
643 if ($k) {
644 $val[] = $k;
645 }
646 }
647
648 if (count($val) > 0) {
649 // Overwrite $op so it works with an IN where statement.
650 $op = 'IN';
651 }
652 else {
653 // If we somehow have an empty array, just return
654 return;
655 }
656 $value = $matches[0] = $val;
657 }
658 else {
659 preg_match_all('/\d+/', $value, $matches);
660 }
661 foreach ($matches[0] as $qill) {
662 $clause[] = CRM_Utils_Array::value($qill, $pseudoconstantType);
663 }
664
665 $query->_qill[$grouping][] = ts($params[0] . ' %1 ', array(1 => $op)) . implode(' ' . ts('or') . ' ', $clause);
666 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($params[1],
667 $op, $value, "Integer"
668 );
669 return $matches[0];
670 }
671 }
672