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