Clean up code for batch 19
[civicrm-core.git] / CRM / Activity / BAO / Query.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Activity_BAO_Query {
36
37 /**
100fef9d 38 * Build select for Case
6a488035 39 *
77b97be7
EM
40 * @param $query
41 *
6a488035 42 * @return void
6a488035 43 */
00be9182 44 public static function select(&$query) {
a7488080 45 if (!empty($query->_returnProperties['activity_id'])) {
6a488035
TO
46 $query->_select['activity_id'] = "civicrm_activity.id as activity_id";
47 $query->_element['activity_id'] = 1;
48 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
49 }
50
a7488080 51 if (!empty($query->_returnProperties['activity_type_id'])) {
5fa296f7 52 $query->_select['activity_type_id'] = "activity_type.value as activity_type_id";
6a488035
TO
53 $query->_element['activity_type_id'] = 1;
54 $query->_tables['civicrm_activity'] = 1;
55 $query->_tables['activity_type'] = 1;
56 $query->_whereTables['civicrm_activity'] = 1;
57 $query->_whereTables['activity_type'] = 1;
58 }
59
a7488080 60 if (!empty($query->_returnProperties['activity_type'])) {
5fa296f7 61 $query->_select['activity_type'] = "activity_type.label as activity_type";
6a488035
TO
62 $query->_element['activity_type'] = 1;
63 $query->_tables['civicrm_activity'] = 1;
64 $query->_tables['activity_type'] = 1;
65 $query->_whereTables['civicrm_activity'] = 1;
66 $query->_whereTables['activity_type'] = 1;
67 }
68
a7488080 69 if (!empty($query->_returnProperties['activity_subject'])) {
6a488035
TO
70 $query->_select['activity_subject'] = "civicrm_activity.subject as activity_subject";
71 $query->_element['activity_subject'] = 1;
72 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
73 }
74
a7488080 75 if (!empty($query->_returnProperties['activity_date_time'])) {
6a488035
TO
76 $query->_select['activity_date_time'] = "civicrm_activity.activity_date_time as activity_date_time";
77 $query->_element['activity_date_time'] = 1;
78 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
79 }
80
a7488080 81 if (!empty($query->_returnProperties['activity_status_id'])) {
6a488035
TO
82 $query->_select['activity_status_id'] = "activity_status.value as activity_status_id";
83 $query->_element['activity_status_id'] = 1;
84 $query->_tables['civicrm_activity'] = 1;
85 $query->_tables['activity_status'] = 1;
86 $query->_whereTables['civicrm_activity'] = 1;
87 $query->_whereTables['activity_status'] = 1;
88 }
89
a7488080 90 if (!empty($query->_returnProperties['activity_status'])) {
5fa296f7 91 $query->_select['activity_status'] = "activity_status.label as activity_status,
92 civicrm_activity.status_id as status_id";
6a488035
TO
93 $query->_element['activity_status'] = 1;
94 $query->_tables['civicrm_activity'] = 1;
95 $query->_tables['activity_status'] = 1;
96 $query->_whereTables['civicrm_activity'] = 1;
97 $query->_whereTables['activity_status'] = 1;
98 }
99
a7488080 100 if (!empty($query->_returnProperties['activity_duration'])) {
6a488035
TO
101 $query->_select['activity_duration'] = "civicrm_activity.duration as activity_duration";
102 $query->_element['activity_duration'] = 1;
103 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
104 }
105
a7488080 106 if (!empty($query->_returnProperties['activity_location'])) {
6a488035
TO
107 $query->_select['activity_location'] = "civicrm_activity.location as activity_location";
108 $query->_element['activity_location'] = 1;
109 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
110 }
111
a7488080 112 if (!empty($query->_returnProperties['activity_details'])) {
6a488035
TO
113 $query->_select['activity_details'] = "civicrm_activity.details as activity_details";
114 $query->_element['activity_details'] = 1;
115 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
116 }
117
a7488080 118 if (!empty($query->_returnProperties['source_record_id'])) {
6a488035
TO
119 $query->_select['source_record_id'] = "civicrm_activity.source_record_id as source_record_id";
120 $query->_element['source_record_id'] = 1;
121 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
122 }
123
a7488080 124 if (!empty($query->_returnProperties['activity_is_test'])) {
6a488035
TO
125 $query->_select['activity_is_test'] = "civicrm_activity.is_test as activity_is_test";
126 $query->_element['activity_is_test'] = 1;
127 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
128 }
129
a7488080 130 if (!empty($query->_returnProperties['activity_campaign_id'])) {
6a488035
TO
131 $query->_select['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
132 $query->_element['activity_campaign_id'] = 1;
133 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
134 }
135
a7488080 136 if (!empty($query->_returnProperties['activity_engagement_level'])) {
6a488035
TO
137 $query->_select['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
138 $query->_element['activity_engagement_level'] = 1;
139 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
140 }
141
a7488080 142 if (!empty($query->_returnProperties['source_contact'])) {
37eb6ff9 143 $query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
6a488035
TO
144 $query->_element['source_contact'] = 1;
145 $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
146 }
4041c9b6 147
a7488080 148 if (!empty($query->_returnProperties['activity_result'])) {
4041c9b6
DG
149 $query->_select['activity_result'] = 'civicrm_activity.result as activity_result';
150 $query->_element['result'] = 1;
151 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
152 }
ffd93213 153
4c24c842
PN
154 if (CRM_Utils_Array::value('parent_id', $query->_returnProperties)) {
155 $query->_tables['parent_id'] = 1;
156 $query->_whereTables['parent_id'] = 1;
157 $query->_element['parent_id'] = 1;
158 }
6a488035
TO
159 }
160
161 /**
162 * Given a list of conditions in query generate the required
163 * where clause
164 *
da6b46f4
EM
165 * @param $query
166 *
6a488035 167 * @return void
6a488035 168 */
00be9182 169 public static function where(&$query) {
6a488035
TO
170 foreach (array_keys($query->_params) as $id) {
171 if (substr($query->_params[$id][0], 0, 9) == 'activity_') {
172 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
173 $query->_useDistinct = TRUE;
174 }
56bf8668 175 $query->_params[$id][3];
6a488035
TO
176 self::whereClauseSingle($query->_params[$id], $query);
177 }
178 }
6a488035
TO
179 }
180
181 /**
100fef9d 182 * Where clause for a single field
6a488035 183 *
da6b46f4
EM
184 * @param $values
185 * @param $query
6a488035 186 * @return void
6a488035 187 */
00be9182 188 public static function whereClauseSingle(&$values, &$query) {
56bf8668 189 list($name, $op, $value, $grouping) = $values;
6a488035 190
9ab34172 191 $fields = CRM_Activity_BAO_Activity::exportableFields();
6a488035
TO
192 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
193 if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
194 $query->_skipDeleteClause = TRUE;
195 }
196
197 switch ($name) {
198 case 'activity_type_id':
9ab34172 199 case 'activity_status_id':
9ab34172 200 case 'activity_engagement_level':
201 case 'activity_subject':
202 case 'activity_id':
9ab34172 203 $qillName = $name;
9ab34172 204 if (in_array($name, array('activity_engagement_level', 'activity_id'))) {
205 $name = $qillName = str_replace('activity_', '', $name);
6a488035 206 }
9ab34172 207 if (in_array($name, array('activity_status_id', 'activity_subject'))) {
208 $name = str_replace('activity_', '', $name);
209 $qillName = str_replace('_id', '', $qillName);
210 }
211 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
212
afa0b07c 213 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.$name", $op, $value, $dataType);
9ab34172 214 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
215 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
6a488035
TO
216 break;
217
afa0b07c 218 case 'activity_type':
219 case 'activity_status':
220 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, 'String');
221 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
222 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value));
223 $query->_tables[$name] = $query->_whereTables[$name] = 1;
224 break;
225
6a488035
TO
226 case 'activity_survey_id':
227 if (!$value) {
228 break;
229 }
230 $value = CRM_Utils_Type::escape($value, 'Integer');
56a5616a 231 $query->_where[$grouping][] = " civicrm_activity.source_record_id = $value";
6a488035
TO
232 $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
233 break;
234
6a488035
TO
235 case 'activity_role':
236 CRM_Contact_BAO_Query::$_activityRole = $values[2];
e7e657f0 237 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 238 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
239 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
240 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
f813f78e 241
91da6cd5 242 if ($values[2]) {
6a488035 243 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
91da6cd5 244 if ($values[2] == 1) {
a24b3694 245 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
91da6cd5
DL
246 $query->_qill[$grouping][] = ts('Activity created by');
247 }
4c9b6178 248 elseif ($values[2] == 2) {
a24b3694 249 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
91da6cd5
DL
250 $query->_qill[$grouping][] = ts('Activity assigned to');
251 }
4c9b6178 252 elseif ($values[2] == 3) {
a24b3694 253 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
91da6cd5
DL
254 $query->_qill[$grouping][] = ts('Activity targeted to');
255 }
6a488035
TO
256 }
257 break;
258
6a488035 259 case 'activity_test':
56bf8668 260 // We don't want to include all tests for sql OR CRM-7827
28c666be 261 if (!$value || $query->getOperator() != 'OR') {
353ffa53 262 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
28c666be
CW
263 if ($value) {
264 $query->_qill[$grouping][] = ts('Activity is a Test');
265 }
6a488035
TO
266 }
267 break;
268
269 case 'activity_date':
270 case 'activity_date_low':
271 case 'activity_date_high':
272 $query->dateQueryBuilder($values,
273 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
274 );
275 break;
276
6a488035
TO
277 case 'activity_taglist':
278 $taglist = $value;
279 $value = array();
280 foreach ($taglist as $val) {
281 if ($val) {
282 $val = explode(',', $val);
283 foreach ($val as $tId) {
284 if (is_numeric($tId)) {
285 $value[$tId] = 1;
286 }
287 }
288 }
289 }
91da6cd5 290
6a488035
TO
291 case 'activity_tags':
292 $value = array_keys($value);
cd43c5e3 293 $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
6a488035
TO
294
295 $names = array();
6a488035
TO
296 if (is_array($value)) {
297 foreach ($value as $k => $v) {
298 $names[] = $activityTags[$v];
299 }
300 }
301 $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
302 $query->_qill[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
303 $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
304 break;
305
306 case 'activity_campaign_id':
307 $campParams = array(
308 'op' => $op,
309 'campaign' => $value,
310 'grouping' => $grouping,
311 'tableName' => 'civicrm_activity',
312 );
313 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
4041c9b6 314 break;
ea100cb5 315
4041c9b6 316 case 'activity_result':
22e263ad 317 if (is_array($value)) {
4041c9b6 318 $safe = NULL;
353ffa53 319 while (list(, $k) = each($value)) {
4041c9b6
DG
320 $safe[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
321 }
322 $query->_where[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")";
323 $query->_qill[$grouping][] = ts("Activity Result - %1", array(1 => implode(' or ', $safe)));
324 }
325 break;
4c24c842
PN
326
327 case 'parent_id':
328 if ($value == 1) {
329 $query->_where[$grouping][] = "parent_id.parent_id IS NOT NULL";
330 $query->_qill[$grouping][] = ts('Activities which have Followup Activities');
331 }
332 elseif ($value == 2) {
333 $query->_where[$grouping][] = "parent_id.parent_id IS NULL";
334 $query->_qill[$grouping][] = ts('Activities without Followup Activities');
335 }
336 break;
ffd93213 337
4c24c842
PN
338 case 'followup_parent_id':
339 if ($value == 1) {
340 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL";
341 $query->_qill[$grouping][] = ts('Activities which are Followup Activities');
342 }
343 elseif ($value == 2) {
344 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL";
353ffa53 345 $query->_qill[$grouping][] = ts('Activities which are not Followup Activities');
4c24c842
PN
346 }
347 break;
6a488035
TO
348 }
349 }
350
ffd93213 351 /**
100fef9d 352 * @param string $name
ffd93213
EM
353 * @param $mode
354 * @param $side
355 *
356 * @return null|string
357 */
00be9182 358 public static function from($name, $mode, $side) {
6a488035
TO
359 $from = NULL;
360 switch ($name) {
361 case 'civicrm_activity':
9393897f 362 //CRM-7480 we are going to civicrm_activity table either
6a488035
TO
363 //from civicrm_activity_target or civicrm_activity_assignment.
364 //as component specific activities does not have entry in
365 //activity target table so lets consider civicrm_activity_assignment.
91da6cd5
DL
366 $from .= " INNER JOIN civicrm_activity_contact
367 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
368 $from .= " INNER JOIN civicrm_activity
369 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
370 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
6a488035 371
6a488035
TO
372 break;
373
374 case 'activity_status':
1d85d241 375 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
6a488035
TO
376 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
377 AND option_group_activity_status.id = activity_status.option_group_id ) ";
378 break;
379
380 case 'activity_type':
1d85d241 381 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
6a488035
TO
382 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
383 AND option_group_activity_type.id = activity_type.option_group_id ) ";
384 break;
385
386 case 'civicrm_activity_tag':
387 $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 ) ";
388 break;
389
390 case 'source_contact':
37eb6ff9 391 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
392 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
393 $from = "
394 LEFT JOIN civicrm_activity_contact ac
395 ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
396 INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
6a488035 397 break;
ffd93213 398
4c24c842
PN
399 case 'parent_id':
400 $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id";
401 break;
6a488035
TO
402 }
403
404 return $from;
405 }
406
407 /**
100fef9d 408 * Getter for the qill object
6a488035
TO
409 *
410 * @return string
6a488035 411 */
00be9182 412 public function qill() {
6a488035
TO
413 return (isset($this->_qill)) ? $this->_qill : "";
414 }
415
416 /**
100fef9d 417 * Add all the elements shared between case activity search and advanced search
6a488035 418 *
6a488035 419 *
100fef9d 420 * @param CRM_Core_Form $form
6a488035 421 * @return void
6a488035 422 */
00be9182 423 public static function buildSearchForm(&$form) {
6a488035 424 $activityOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
9ab34172 425 $form->addSelect('activity_type_id',
426 array('entity' => 'activity', 'label' => 'Activity Type(s)', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
427 );
6a488035
TO
428
429 CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
4c24c842
PN
430 $followUpActivity = array(
431 1 => ts('Yes'),
432 2 => ts('No'),
433 );
434 $form->addRadio('parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
435 $form->addRadio('followup_parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
91da6cd5 436 $activityRoles = array(
18d2d5e2 437 3 => ts('With'),
91da6cd5 438 2 => ts('Assigned to'),
18d2d5e2 439 1 => ts('Added by'),
91da6cd5 440 );
b847e6e7 441 $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
18d2d5e2 442 $form->setDefaults(array('activity_role' => 3));
9ab34172 443 $activityStatus = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'status_id', array('flip' => 1, 'labelColumn' => 'name'));
444 $form->addSelect('status_id',
445 array('entity' => 'activity', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
446 );
447 $form->setDefaults(array('status_id' => array($activityStatus['Completed'], $activityStatus['Scheduled'])));
6a488035
TO
448 $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
449 $form->addYesNo('activity_test', ts('Activity is a Test?'));
450 $activity_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
451 if ($activity_tags) {
452 foreach ($activity_tags as $tagID => $tagName) {
453 $form->_tagElement = &$form->addElement('checkbox', "activity_tags[$tagID]",
454 NULL, $tagName
455 );
456 }
457 }
458
459 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
95ef220a 460 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
6a488035 461
9231464f 462 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
28a04ea9 463 if ($surveys) {
464 $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
465 array('' => ts('- none -')) + $surveys, FALSE,
466 array('class' => 'crm-select2')
467 );
468 }
6a488035
TO
469 $extends = array('Activity');
470 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
471 if ($groupDetails) {
472 $form->assign('activityGroupTree', $groupDetails);
473 foreach ($groupDetails as $group) {
474 foreach ($group['fields'] as $field) {
475 $fieldId = $field['id'];
476 $elementName = 'custom_' . $fieldId;
477 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, FALSE, TRUE);
478 }
479 }
480 }
481
482 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
483
484 //add engagement level CRM-7775
485 $buildEngagementLevel = FALSE;
4041c9b6 486 $buildSurveyResult = FALSE;
6a488035
TO
487 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
488 CRM_Campaign_BAO_Campaign::accessCampaign()
489 ) {
490 $buildEngagementLevel = TRUE;
76773c5a 491 $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'context' => 'search'));
6a488035 492
4041c9b6 493 // Add survey result field.
353ffa53 494 $optionGroups = CRM_Campaign_BAO_Survey::getResultSets('name');
4041c9b6 495 $resultOptions = array();
481a74f4
TO
496 foreach ($optionGroups as $gid => $name) {
497 if ($name) {
4041c9b6
DG
498 $value = array();
499 $value = CRM_Core_OptionGroup::values($name);
500 if (!empty($value)) {
353ffa53 501 while (list($k, $v) = each($value)) {
4041c9b6
DG
502 $resultOptions[$v] = $v;
503 }
504 }
505 }
506 }
507 // If no survey result options have been created, don't build
508 // the field to avoid clutter.
22e263ad 509 if (count($resultOptions) > 0) {
4041c9b6
DG
510 $buildSurveyResult = TRUE;
511 asort($resultOptions);
512 $form->add('select', 'activity_result', ts("Survey Result"),
513 $resultOptions, FALSE,
02ddf039 514 array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
4041c9b6
DG
515 );
516 }
517 }
77b97be7 518
6a488035 519 $form->assign('buildEngagementLevel', $buildEngagementLevel);
4041c9b6 520 $form->assign('buildSurveyResult', $buildSurveyResult);
6a488035
TO
521 $form->setDefaults(array('activity_test' => 0));
522 }
523
ffd93213
EM
524 /**
525 * @param $mode
526 * @param bool $includeCustomFields
527 *
528 * @return array|null
529 */
00be9182 530 public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
6a488035
TO
531 $properties = NULL;
532 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
533 $properties = array(
534 'activity_id' => 1,
535 'contact_type' => 1,
536 'contact_sub_type' => 1,
537 'sort_name' => 1,
538 'display_name' => 1,
539 'activity_type' => 1,
5511233c 540 'activity_type_id' => 1,
6a488035
TO
541 'activity_subject' => 1,
542 'activity_date_time' => 1,
543 'activity_duration' => 1,
544 'activity_location' => 1,
545 'activity_details' => 1,
546 'activity_status' => 1,
37eb6ff9 547 'source_contact' => 1,
6a488035
TO
548 'source_record_id' => 1,
549 'activity_is_test' => 1,
550 'activity_campaign_id' => 1,
4041c9b6 551 'result' => 1,
6a488035 552 'activity_engagement_level' => 1,
4c24c842 553 'parent_id' => 1,
6a488035
TO
554 );
555
556 if ($includeCustomFields) {
557 // also get all the custom activity properties
558 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
559 if (!empty($fields)) {
560 foreach ($fields as $name => $dontCare) {
561 $properties[$name] = 1;
562 }
563 }
564 }
565 }
566
567 return $properties;
568 }
569}