Merge pull request #13078 from agh1/contactdetail-no-or2
[civicrm-core.git] / CRM / Activity / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Activity_BAO_Query {
18
19 /**
20 * Build select for Case.
21 *
22 * @param CRM_Contact_BAO_Query $query
23 */
24 public static function select(&$query) {
25 if (!empty($query->_returnProperties['activity_id'])) {
26 $query->_select['activity_id'] = 'civicrm_activity.id as activity_id';
27 $query->_element['activity_id'] = 1;
28 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
29 }
30
31 if (!empty($query->_returnProperties['activity_type_id'])) {
32 $query->_select['activity_type_id'] = 'activity_type.value as activity_type_id';
33 $query->_element['activity_type_id'] = 1;
34 $query->_tables['civicrm_activity'] = 1;
35 $query->_tables['activity_type'] = 1;
36 $query->_whereTables['civicrm_activity'] = 1;
37 $query->_whereTables['activity_type'] = 1;
38 }
39
40 if (!empty($query->_returnProperties['activity_type'])) {
41 $query->_select['activity_type'] = 'activity_type.label as activity_type';
42 $query->_element['activity_type'] = 1;
43 $query->_tables['civicrm_activity'] = 1;
44 $query->_tables['activity_type'] = 1;
45 $query->_whereTables['civicrm_activity'] = 1;
46 $query->_whereTables['activity_type'] = 1;
47 }
48
49 if (!empty($query->_returnProperties['activity_subject'])) {
50 $query->_select['activity_subject'] = 'civicrm_activity.subject as activity_subject';
51 $query->_element['activity_subject'] = 1;
52 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
53 }
54
55 if (!empty($query->_returnProperties['activity_date_time'])) {
56 $query->_select['activity_date_time'] = 'civicrm_activity.activity_date_time as activity_date_time';
57 $query->_element['activity_date_time'] = 1;
58 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
59 }
60
61 if (!empty($query->_returnProperties['activity_status_id'])) {
62 $query->_select['activity_status_id'] = 'civicrm_activity.status_id as activity_status_id';
63 $query->_element['activity_status_id'] = 1;
64 $query->_tables['civicrm_activity'] = 1;
65 $query->_whereTables['civicrm_activity'] = 1;
66 }
67
68 if (!empty($query->_returnProperties['activity_status'])) {
69 $query->_select['activity_status_id'] = 1;
70 $query->_element['activity_status'] = 1;
71 $query->_tables['civicrm_activity'] = 1;
72 $query->_whereTables['civicrm_activity'] = 1;
73 }
74
75 if (!empty($query->_returnProperties['activity_duration'])) {
76 $query->_select['activity_duration'] = 'civicrm_activity.duration as activity_duration';
77 $query->_element['activity_duration'] = 1;
78 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
79 }
80
81 if (!empty($query->_returnProperties['activity_location'])) {
82 $query->_select['activity_location'] = 'civicrm_activity.location as activity_location';
83 $query->_element['activity_location'] = 1;
84 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
85 }
86
87 if (!empty($query->_returnProperties['activity_details'])) {
88 $query->_select['activity_details'] = 'civicrm_activity.details as activity_details';
89 $query->_element['activity_details'] = 1;
90 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
91 }
92
93 if (!empty($query->_returnProperties['source_record_id'])) {
94 $query->_select['source_record_id'] = 'civicrm_activity.source_record_id as source_record_id';
95 $query->_element['source_record_id'] = 1;
96 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
97 }
98
99 if (!empty($query->_returnProperties['activity_is_test'])) {
100 $query->_select['activity_is_test'] = 'civicrm_activity.is_test as activity_is_test';
101 $query->_element['activity_is_test'] = 1;
102 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
103 }
104
105 if (!empty($query->_returnProperties['activity_campaign_id'])) {
106 $query->_select['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
107 $query->_element['activity_campaign_id'] = 1;
108 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
109 }
110
111 if (!empty($query->_returnProperties['activity_engagement_level'])) {
112 $query->_select['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
113 $query->_element['activity_engagement_level'] = 1;
114 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
115 }
116
117 if (!empty($query->_returnProperties['source_contact'])) {
118 $query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
119 $query->_element['source_contact'] = 1;
120 $query->_tables['civicrm_activity'] = $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
121 }
122
123 if (!empty($query->_returnProperties['source_contact_id'])) {
124 $query->_select['source_contact_id'] = 'source_contact.id as source_contact_id';
125 $query->_element['source_contact_id'] = 1;
126 $query->_tables['civicrm_activity'] = $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
127 }
128
129 if (!empty($query->_returnProperties['activity_result'])) {
130 $query->_select['activity_result'] = 'civicrm_activity.result as activity_result';
131 $query->_element['result'] = 1;
132 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
133 }
134
135 if (CRM_Utils_Array::value('parent_id', $query->_returnProperties)) {
136 $query->_tables['parent_id'] = 1;
137 $query->_whereTables['parent_id'] = 1;
138 $query->_element['parent_id'] = 1;
139 }
140
141 if (!empty($query->_returnProperties['activity_priority'])) {
142 $query->_select['activity_priority'] = 'activity_priority.label as activity_priority,
143 civicrm_activity.priority_id as priority_id';
144 $query->_element['activity_priority'] = 1;
145 $query->_tables['activity_priority'] = 1;
146 $query->_whereTables['activity_priority'] = 1;
147 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
148 }
149 }
150
151 /**
152 * Given a list of conditions in query generate the required where clause.
153 *
154 * @param \CRM_Contact_BAO_Query $query
155 *
156 * @throws \CRM_Core_Exception
157 */
158 public static function where(&$query) {
159 foreach (array_keys($query->_params) as $id) {
160 if (substr($query->_params[$id][0], 0, 9) === 'activity_') {
161 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
162 $query->_useDistinct = TRUE;
163 }
164 $query->_params[$id][3];
165 self::whereClauseSingle($query->_params[$id], $query);
166 }
167 }
168 }
169
170 /**
171 * Where clause for a single field.
172 *
173 * @param array $values
174 * @param CRM_Contact_BAO_Query $query
175 *
176 * @throws \CRM_Core_Exception
177 */
178 public static function whereClauseSingle(&$values, &$query) {
179 list($name, $op, $value, $grouping) = $values;
180
181 $fields = CRM_Activity_BAO_Activity::exportableFields();
182 $fieldSpec = $query->getFieldSpec($name);
183 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
184 if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
185 $query->_skipDeleteClause = TRUE;
186 }
187 // @todo we want to do this in a more metadata driven way, and also in contribute.
188 // But for the rc...
189 $namesToConvert = [
190 'activity_status' => 'activity_status_id',
191 ];
192 $name = $namesToConvert[$name] ?? $name;
193
194 switch ($name) {
195 case 'activity_type_id':
196 case 'activity_status_id':
197 case 'activity_engagement_level':
198 case 'activity_id':
199 case 'activity_campaign_id':
200 case 'activity_priority_id':
201 // We no longer expect "subject" as a specific criteria (as of CRM-19447),
202 // but we still use activity_subject in Activity.Get API
203 case 'activity_subject':
204
205 $qillName = $name;
206 if (in_array($name, ['activity_engagement_level', 'activity_id'])) {
207 $name = $qillName = str_replace('activity_', '', $name);
208 }
209 if (in_array($name, [
210 'activity_subject',
211 'activity_priority_id',
212 ])) {
213 $name = str_replace('activity_', '', $name);
214 $qillName = str_replace('_id', '', $qillName);
215 }
216 if ($name == 'activity_campaign_id') {
217 $name = 'campaign_id';
218 }
219
220 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
221
222 $where = $fieldSpec['where'];
223 if (!$where) {
224 $where = 'civicrm_activity.' . $name;
225 }
226 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($where, $op, $value, $dataType);
227 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
228 $query->_qill[$grouping][] = ts('%1 %2 %3', [
229 1 => $fields[$qillName]['title'],
230 2 => $op,
231 3 => $value,
232 ]);
233 break;
234
235 case 'activity_text':
236 self::whereClauseSingleActivityText($values, $query);
237 break;
238
239 case 'activity_type':
240 case 'activity_priority':
241 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, 'String');
242 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
243 $query->_qill[$grouping][] = ts('%1 %2 %3', [
244 1 => $fields[$name]['title'],
245 2 => $op,
246 3 => $value,
247 ]);
248 $query->_tables[$name] = $query->_whereTables[$name] = 1;
249 break;
250
251 case 'activity_survey_id':
252 if (!$value) {
253 break;
254 }
255 $value = CRM_Utils_Type::escape($value, 'Integer');
256 $query->_where[$grouping][] = " civicrm_activity.source_record_id = $value";
257 $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
258 break;
259
260 case 'activity_role':
261 CRM_Contact_BAO_Query::$_activityRole = $values[2];
262 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
263 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
264 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
265 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
266
267 if ($values[2]) {
268 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
269 if ($values[2] == 1) {
270 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
271 $query->_qill[$grouping][] = ts('Activity created by');
272 }
273 elseif ($values[2] == 2) {
274 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
275 $query->_qill[$grouping][] = ts('Activity assigned to');
276 }
277 elseif ($values[2] == 3) {
278 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
279 $query->_qill[$grouping][] = ts('Activity targeted to');
280 }
281 }
282 break;
283
284 case 'activity_test':
285 // We don't want to include all tests for sql OR CRM-7827
286 if (!$value || $query->getOperator() != 'OR') {
287 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
288 if ($value) {
289 $query->_qill[$grouping][] = ts('Activity is a Test');
290 }
291 }
292 break;
293
294 case 'activity_date':
295 case 'activity_date_low':
296 case 'activity_date_high':
297 case 'activity_date_time_low':
298 case 'activity_date_time_high':
299 $query->dateQueryBuilder($values,
300 'civicrm_activity', str_replace([
301 '_high',
302 '_low',
303 ], '', $name), 'activity_date_time', ts('Activity Date')
304 );
305 break;
306
307 case 'activity_taglist':
308 $taglist = $value;
309 $value = [];
310 foreach ($taglist as $val) {
311 if ($val) {
312 $val = explode(',', $val);
313 foreach ($val as $tId) {
314 if (is_numeric($tId)) {
315 $value[$tId] = 1;
316 }
317 }
318 }
319 }
320
321 case 'activity_tags':
322 $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
323
324 if (!is_array($value)) {
325 $value = explode(',', $value);
326 }
327
328 $names = [];
329 foreach ($value as $k => $v) {
330 $names[] = $activityTags[$v];
331 }
332
333 $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
334 $query->_qill[$grouping][] = ts('Activity Tag %1', [1 => $op]) . ' ' . implode(' ' . ts('OR') . ' ', $names);
335 $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
336 break;
337
338 case 'activity_result':
339 if (is_array($value)) {
340 $safe = [];
341 foreach ($value as $id => $k) {
342 $safe[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
343 }
344 $query->_where[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")";
345 $query->_qill[$grouping][] = ts("Activity Result - %1", [1 => implode(' or ', $safe)]);
346 }
347 break;
348
349 case 'parent_id':
350 if ($value == 1) {
351 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL";
352 $query->_qill[$grouping][] = ts('Activities which have Followup Activities');
353 }
354 elseif ($value == 2) {
355 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL";
356 $query->_qill[$grouping][] = ts('Activities without Followup Activities');
357 }
358 break;
359
360 case 'followup_parent_id':
361 if ($value == 1) {
362 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL";
363 $query->_qill[$grouping][] = ts('Activities which are Followup Activities');
364 }
365 elseif ($value == 2) {
366 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL";
367 $query->_qill[$grouping][] = ts('Activities which are not Followup Activities');
368 }
369 break;
370
371 case 'source_contact':
372 case 'source_contact_id':
373 $columnName = strstr($name, '_id') ? 'id' : 'sort_name';
374 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("source_contact.{$columnName}", $op, $value, CRM_Utils_Type::typeToString($fields[$name]['type']));
375 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Contact', $columnName, $value, $op);
376 $query->_qill[$grouping][] = ts('%1 %2 %3', [
377 1 => $fields[$name]['title'],
378 2 => $op,
379 3 => $value,
380 ]);
381 break;
382 }
383 }
384
385 /**
386 * @param string $name
387 * @param $mode
388 * @param $side
389 *
390 * @return null|string
391 */
392 public static function from($name, $mode, $side) {
393 $from = NULL;
394 switch ($name) {
395 case 'civicrm_activity':
396 //CRM-7480 we are going to civicrm_activity table either
397 //from civicrm_activity_target or civicrm_activity_assignment.
398 //as component specific activities does not have entry in
399 //activity target table so lets consider civicrm_activity_assignment.
400 $from .= " $side JOIN civicrm_activity_contact
401 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
402 $from .= " $side JOIN civicrm_activity
403 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
404 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
405 // Do not show deleted contact's activity
406 $from .= " INNER JOIN civicrm_contact
407 ON ( civicrm_activity_contact.contact_id = civicrm_contact.id and civicrm_contact.is_deleted != 1 )";
408 break;
409
410 case 'activity_type':
411 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
412 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
413 AND option_group_activity_type.id = activity_type.option_group_id ) ";
414 break;
415
416 case 'activity_priority':
417 $from .= " $side JOIN civicrm_option_group option_group_activity_priority ON (option_group_activity_priority.name = 'priority')";
418 $from .= " $side JOIN civicrm_option_value activity_priority ON (civicrm_activity.priority_id = activity_priority.value
419 AND option_group_activity_priority.id = activity_priority.option_group_id ) ";
420 break;
421
422 case 'civicrm_activity_tag':
423 $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 ) ";
424 break;
425
426 case 'source_contact':
427 $sourceID = CRM_Core_PseudoConstant::getKey(
428 'CRM_Activity_BAO_ActivityContact',
429 'record_type_id',
430 'Activity Source'
431 );
432 $from = "
433 LEFT JOIN civicrm_activity_contact source_activity
434 ON (source_activity.activity_id = civicrm_activity_contact.activity_id
435 AND source_activity.record_type_id = {$sourceID})
436 LEFT JOIN civicrm_contact source_contact ON (source_activity.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 * Get the metadata for fields to be included on the activity search form.
449 *
450 * @throws \CiviCRM_API3_Exception
451 * @todo ideally this would be a trait included on the activity search & advanced search
452 * rather than a static function.
453 */
454 public static function getSearchFieldMetadata() {
455 $fields = ['activity_type_id', 'activity_date_time', 'priority_id', 'activity_location', 'activity_status_id'];
456 $metadata = civicrm_api3('Activity', 'getfields', [])['values'];
457 $metadata = array_intersect_key($metadata, array_flip($fields));
458 $metadata['activity_text'] = [
459 'title' => ts('Activity Text'),
460 'type' => CRM_Utils_Type::T_STRING,
461 'is_pseudofield' => TRUE,
462 'html' => [
463 'type' => 'Text',
464 ],
465 ];
466 return $metadata;
467 }
468
469 /**
470 * Add all the elements shared between case activity search and advanced search.
471 *
472 * @param CRM_Core_Form_Search $form
473 *
474 * @throws \CiviCRM_API3_Exception
475 * @throws \CRM_Core_Exception
476 */
477 public static function buildSearchForm(&$form) {
478 $form->addSearchFieldMetadata(['Activity' => self::getSearchFieldMetadata()]);
479 $form->addFormFieldsFromMetadata();
480
481 $followUpActivity = [
482 1 => ts('Yes'),
483 2 => ts('No'),
484 ];
485 $form->addRadio('parent_id', NULL, $followUpActivity, ['allowClear' => TRUE]);
486 $form->addRadio('followup_parent_id', NULL, $followUpActivity, ['allowClear' => TRUE]);
487 $activityRoles = [
488 3 => ts('With'),
489 2 => ts('Assigned to'),
490 1 => ts('Added by'),
491 ];
492 $form->addRadio('activity_role', NULL, $activityRoles, ['allowClear' => TRUE]);
493 $form->setDefaults(['activity_role' => 3]);
494 $activityStatus = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'status_id', [
495 'flip' => 1,
496 'labelColumn' => 'name',
497 ]);
498 $ssID = $form->get('ssID');
499 $status = [$activityStatus['Completed'], $activityStatus['Scheduled']];
500 //If status is saved in smart group.
501 if (!empty($ssID) && !empty($form->_formValues['activity_status_id'])) {
502 $status = $form->_formValues['activity_status_id'];
503 }
504 $form->setDefaults(['activity_status_id' => $status]);
505
506 $form->addElement('text', 'activity_text', ts('Activity Text'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
507
508 $form->addRadio('activity_option', '', CRM_Core_SelectValues::activityTextOptions());
509 $form->setDefaults(['activity_option' => 'both']);
510
511 $form->addYesNo('activity_test', ts('Activity is a Test?'));
512 $activity_tags = CRM_Core_BAO_Tag::getColorTags('civicrm_activity');
513
514 if ($activity_tags) {
515 $form->add('select2', 'activity_tags', ts('Activity Tag(s)'),
516 $activity_tags, FALSE, [
517 'id' => 'activity_tags',
518 'multiple' =>
519 'multiple',
520 'class' => 'crm-select2',
521 'placeholder' => ts('- select -'),
522 ]
523 );
524 }
525
526 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
527 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
528
529 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
530 if ($surveys) {
531 $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
532 ['' => ts('- none -')] + $surveys, FALSE,
533 ['class' => 'crm-select2']
534 );
535 }
536
537 CRM_Core_BAO_Query::addCustomFormFields($form, ['Activity']);
538
539 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
540
541 // Add engagement level CRM-7775.
542 $buildEngagementLevel = FALSE;
543 $buildSurveyResult = FALSE;
544 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
545 CRM_Campaign_BAO_Campaign::accessCampaign()
546 ) {
547 $buildEngagementLevel = TRUE;
548 $form->addSelect('activity_engagement_level', [
549 'entity' => 'activity',
550 'context' => 'search',
551 ]);
552
553 // Add survey result field.
554 $optionGroups = CRM_Campaign_BAO_Survey::getResultSets('name');
555 $resultOptions = [];
556 foreach ($optionGroups as $gid => $name) {
557 if ($name) {
558 $value = CRM_Core_OptionGroup::values($name);
559 if (!empty($value)) {
560 foreach ($value as $k => $v) {
561 $resultOptions[$v] = $v;
562 }
563 }
564 }
565 }
566 // If no survey result options have been created, don't build
567 // the field to avoid clutter.
568 if (count($resultOptions) > 0) {
569 $buildSurveyResult = TRUE;
570 asort($resultOptions);
571 $form->add('select', 'activity_result', ts("Survey Result"),
572 $resultOptions, FALSE,
573 [
574 'id' => 'activity_result',
575 'multiple' => 'multiple',
576 'class' => 'crm-select2',
577 ]
578 );
579 }
580 }
581
582 $form->assign('buildEngagementLevel', $buildEngagementLevel);
583 $form->assign('buildSurveyResult', $buildSurveyResult);
584 $form->setDefaults(['activity_test' => 0]);
585 }
586
587 /**
588 * @param $mode
589 * @param bool $includeCustomFields
590 *
591 * @return array|null
592 */
593 public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
594 $properties = NULL;
595 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
596 $properties = [
597 'activity_id' => 1,
598 'contact_type' => 1,
599 'contact_sub_type' => 1,
600 'sort_name' => 1,
601 'display_name' => 1,
602 'activity_type' => 1,
603 'activity_type_id' => 1,
604 'activity_subject' => 1,
605 'activity_date_time' => 1,
606 'activity_duration' => 1,
607 'activity_location' => 1,
608 'activity_details' => 1,
609 'activity_status' => 1,
610 'activity_priority' => 1,
611 'source_contact' => 1,
612 'source_record_id' => 1,
613 'activity_is_test' => 1,
614 'activity_campaign_id' => 1,
615 'result' => 1,
616 'activity_engagement_level' => 1,
617 'parent_id' => 1,
618 ];
619
620 if ($includeCustomFields) {
621 // also get all the custom activity properties
622 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
623 if (!empty($fields)) {
624 foreach ($fields as $name => $dontCare) {
625 $properties[$name] = 1;
626 }
627 }
628 }
629 }
630
631 return $properties;
632 }
633
634 /**
635 * Get the list of fields required to populate the selector.
636 *
637 * The default return properties array returns far too many fields for 'everyday use. Every field you add to this array
638 * kills a small kitten so add carefully.
639 */
640 public static function selectorReturnProperties() {
641 $properties = [
642 'activity_id' => 1,
643 'contact_type' => 1,
644 'contact_sub_type' => 1,
645 'sort_name' => 1,
646 'display_name' => 1,
647 'activity_type_id' => 1,
648 'activity_subject' => 1,
649 'activity_date_time' => 1,
650 'activity_status_id' => 1,
651 'source_contact' => 1,
652 'source_record_id' => 1,
653 'activity_is_test' => 1,
654 'activity_campaign_id' => 1,
655 'activity_engagement_level' => 1,
656 ];
657
658 return $properties;
659 }
660
661 /**
662 * Where/qill clause for notes
663 *
664 * @param array $values
665 * @param CRM_Contact_BAO_Query $query
666 *
667 * @throws \CRM_Core_Exception
668 */
669 public static function whereClauseSingleActivityText(&$values, &$query) {
670 list($name, $op, $value, $grouping, $wildcard) = $values;
671 $activityOptionValues = $query->getWhereValues('activity_option', $grouping);
672 $activityOption = CRM_Utils_Array::value(2, $activityOptionValues, 6);
673
674 $query->_useDistinct = TRUE;
675
676 $label = ts('Activity Text (%1)', [1 => CRM_Utils_Array::value($activityOption, CRM_Core_SelectValues::activityTextOptions())]);
677 $clauses = [];
678 if ($activityOption % 2 == 0) {
679 $clauses[] = $query->buildClause('civicrm_activity.details', $op, $value, 'String');
680 }
681 if ($activityOption % 3 == 0) {
682 $clauses[] = $query->buildClause('civicrm_activity.subject', $op, $value, 'String');
683 }
684
685 $query->_where[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
686 list($qillOp, $qillVal) = $query->buildQillForFieldValue(NULL, $name, $value, $op);
687 $query->_qill[$grouping][] = ts("%1 %2 '%3'", [
688 1 => $label,
689 2 => $qillOp,
690 3 => $qillVal,
691 ]);
692 }
693
694 }