Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-16-12-52-48
[civicrm-core.git] / CRM / Activity / BAO / Query.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Activity_BAO_Query {
37
38 /**
39 * build select for Case
40 *
77b97be7
EM
41 * @param $query
42 *
6a488035
TO
43 * @return void
44 * @access public
45 */
46 static function select(&$query) {
a7488080 47 if (!empty($query->_returnProperties['activity_id'])) {
6a488035
TO
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
a7488080 53 if (!empty($query->_returnProperties['activity_type_id'])) {
6a488035
TO
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
a7488080 62 if (!empty($query->_returnProperties['activity_type'])) {
5511233c
TC
63 $query->_select['activity_type'] = "activity_type.label as activity_type,
64 civicrm_activity.activity_type_id as activity_type_id";
6a488035
TO
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
a7488080 72 if (!empty($query->_returnProperties['activity_subject'])) {
6a488035
TO
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
a7488080 78 if (!empty($query->_returnProperties['activity_date_time'])) {
6a488035
TO
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
a7488080 84 if (!empty($query->_returnProperties['activity_status_id'])) {
6a488035
TO
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
a7488080 93 if (!empty($query->_returnProperties['activity_status'])) {
6a488035
TO
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
a7488080 102 if (!empty($query->_returnProperties['activity_duration'])) {
6a488035
TO
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
a7488080 108 if (!empty($query->_returnProperties['activity_location'])) {
6a488035
TO
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
a7488080 114 if (!empty($query->_returnProperties['activity_details'])) {
6a488035
TO
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
a7488080 120 if (!empty($query->_returnProperties['source_record_id'])) {
6a488035
TO
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
a7488080 126 if (!empty($query->_returnProperties['activity_is_test'])) {
6a488035
TO
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
a7488080 132 if (!empty($query->_returnProperties['activity_campaign_id'])) {
6a488035
TO
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
a7488080 138 if (!empty($query->_returnProperties['activity_engagement_level'])) {
6a488035
TO
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
a7488080 144 if (!empty($query->_returnProperties['source_contact'])) {
37eb6ff9 145 $query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
6a488035
TO
146 $query->_element['source_contact'] = 1;
147 $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
148 }
4041c9b6 149
a7488080 150 if (!empty($query->_returnProperties['activity_result'])) {
4041c9b6
DG
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 }
ffd93213 155
4c24c842
PN
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 }
6a488035
TO
161 }
162
163 /**
164 * Given a list of conditions in query generate the required
165 * where clause
166 *
da6b46f4
EM
167 * @param $query
168 *
6a488035
TO
169 * @return void
170 * @access public
171 */
172 static function where(&$query) {
28c666be 173 $grouping = NULL;
6a488035
TO
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 }
6a488035
TO
179 $grouping = $query->_params[$id][3];
180 self::whereClauseSingle($query->_params[$id], $query);
181 }
182 }
6a488035
TO
183 }
184
185 /**
186 * where clause for a single field
187 *
da6b46f4
EM
188 * @param $values
189 * @param $query
6a488035
TO
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':
3e4a4597 204 case 'activity_type':
6a488035 205 $types = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
6a488035
TO
206 //get the component activity types.
207 $compActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, TRUE);
3e4a4597 208 $activityTypeIds = self::buildWhereAndQill($query, $value, $types, $op, $grouping, array('Activity Type', 'civicrm_activity.activity_type_id'));
77b97be7 209
3e4a4597
PN
210 foreach ($activityTypeIds as $activityTypeId) {
211 if (array_key_exists($activityTypeId, $compActTypes)) {
6a488035 212 CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
3e4a4597 213 break;
6a488035
TO
214 }
215 }
6a488035
TO
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];
e7e657f0 239 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 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);
f813f78e 243
91da6cd5 244 if ($values[2]) {
6a488035 245 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
91da6cd5 246 if ($values[2] == 1) {
a24b3694 247 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
91da6cd5
DL
248 $query->_qill[$grouping][] = ts('Activity created by');
249 }
250 else if ($values[2] == 2) {
a24b3694 251 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
91da6cd5
DL
252 $query->_qill[$grouping][] = ts('Activity assigned to');
253 }
254 else if ($values[2] == 3) {
a24b3694 255 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
91da6cd5
DL
256 $query->_qill[$grouping][] = ts('Activity targeted to');
257 }
6a488035
TO
258 }
259 break;
260
261 case 'activity_status':
262 $status = CRM_Core_PseudoConstant::activityStatus();
77b97be7 263
3e4a4597 264 $activityTypeIds = self::buildWhereAndQill($query, $value, $status, $op, $grouping, array('Activity Status', 'civicrm_activity.status_id'));
6a488035
TO
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':
28c666be
CW
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 }
6a488035
TO
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 }
91da6cd5 336
6a488035
TO
337 case 'activity_tags':
338 $value = array_keys($value);
cd43c5e3 339 $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
6a488035
TO
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);
4041c9b6
DG
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;
4c24c842
PN
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;
ffd93213 383
4c24c842
PN
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;
6a488035
TO
394 }
395 }
396
ffd93213
EM
397 /**
398 * @param $name
399 * @param $mode
400 * @param $side
401 *
402 * @return null|string
403 */
6a488035
TO
404 static function from($name, $mode, $side) {
405 $from = NULL;
406 switch ($name) {
407 case 'civicrm_activity':
9393897f 408 //CRM-7480 we are going to civicrm_activity table either
6a488035
TO
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.
91da6cd5
DL
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 )";
6a488035 417
6a488035 418 $activityRole = CRM_Contact_BAO_Query::$_activityRole;
6a488035
TO
419 break;
420
421 case 'activity_status':
1d85d241 422 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
6a488035
TO
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':
1d85d241 428 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
6a488035
TO
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':
37eb6ff9 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)";
6a488035 444 break;
ffd93213 445
4c24c842
PN
446 case 'parent_id':
447 $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id";
448 break;
6a488035
TO
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 advanaced search
466 *
467 * @access public
468 *
8a4f27dc 469 * @param $form CRM_Core_Form
6a488035
TO
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);
4c24c842
PN
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));
91da6cd5 494 $activityRoles = array(
18d2d5e2 495 3 => ts('With'),
91da6cd5 496 2 => ts('Assigned to'),
18d2d5e2 497 1 => ts('Added by'),
91da6cd5 498 );
b847e6e7 499 $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
18d2d5e2 500 $form->setDefaults(array('activity_role' => 3));
6a488035
TO
501 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
502 foreach ($activityStatus as $activityStatusID => $activityStatusName) {
503 $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
504 }
4cda11c9 505 $form->addGroup($activity_status, 'activity_status', ts('Activity Status'));
6a488035
TO
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');
95ef220a 519 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
6a488035 520
9231464f
KJ
521 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
522 if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
02ddf039
CW
523 array('' => ts('- none -')) + $surveys, FALSE,
524 array('class' => 'crm-select2')
6a488035
TO
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;
4041c9b6 544 $buildSurveyResult = FALSE;
6a488035
TO
545 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
546 CRM_Campaign_BAO_Campaign::accessCampaign()
547 ) {
548 $buildEngagementLevel = TRUE;
15cf9198 549 $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'option_url' => NULL));
6a488035 550
4041c9b6
DG
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,
02ddf039 572 array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
4041c9b6
DG
573 );
574 }
575 }
77b97be7 576
6a488035 577 $form->assign('buildEngagementLevel', $buildEngagementLevel);
4041c9b6 578 $form->assign('buildSurveyResult', $buildSurveyResult);
6a488035
TO
579 $form->setDefaults(array('activity_test' => 0));
580 }
581
ffd93213
EM
582 /**
583 * @param $mode
584 * @param bool $includeCustomFields
585 *
586 * @return array|null
587 */
6a488035
TO
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,
5511233c 598 'activity_type_id' => 1,
6a488035
TO
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,
37eb6ff9 605 'source_contact' => 1,
6a488035
TO
606 'source_record_id' => 1,
607 'activity_is_test' => 1,
608 'activity_campaign_id' => 1,
4041c9b6 609 'result' => 1,
6a488035 610 'activity_engagement_level' => 1,
4c24c842 611 'parent_id' => 1,
6a488035
TO
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 }
77b97be7 627
ffd93213
EM
628 /**
629 * @param $query
630 * @param $value
631 * @param $pseudoconstantType
632 * @param $op
633 * @param $grouping
634 * @param $params
635 *
636 * @return array
637 */
3e4a4597
PN
638 static function buildWhereAndQill(&$query, $value, $pseudoconstantType, $op, $grouping, $params) {
639 $matches = $val = $clause = array();
77b97be7 640
3e4a4597
PN
641 if (is_array($value)) {
642 foreach ($value as $k => $v) {
643 if ($k) {
644 $val[] = $k;
645 }
646 }
77b97be7 647
3e4a4597
PN
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 }
77b97be7 664
3e4a4597
PN
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 }
6a488035
TO
671}
672