Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Activity_BAO_Query { | |
18 | ||
19 | /** | |
2e2605fe | 20 | * Build select for Case. |
6a488035 | 21 | * |
b6c94f42 | 22 | * @param CRM_Contact_BAO_Query $query |
6a488035 | 23 | */ |
00be9182 | 24 | public static function select(&$query) { |
a7488080 | 25 | if (!empty($query->_returnProperties['activity_id'])) { |
da236f9a | 26 | $query->_select['activity_id'] = 'civicrm_activity.id as activity_id'; |
6a488035 TO |
27 | $query->_element['activity_id'] = 1; |
28 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
29 | } | |
30 | ||
a7488080 | 31 | if (!empty($query->_returnProperties['activity_type_id'])) { |
da236f9a | 32 | $query->_select['activity_type_id'] = 'activity_type.value as activity_type_id'; |
6a488035 TO |
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 | ||
a7488080 | 40 | if (!empty($query->_returnProperties['activity_type'])) { |
da236f9a | 41 | $query->_select['activity_type'] = 'activity_type.label as activity_type'; |
6a488035 TO |
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 | ||
a7488080 | 49 | if (!empty($query->_returnProperties['activity_subject'])) { |
da236f9a | 50 | $query->_select['activity_subject'] = 'civicrm_activity.subject as activity_subject'; |
6a488035 TO |
51 | $query->_element['activity_subject'] = 1; |
52 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
53 | } | |
54 | ||
a7488080 | 55 | if (!empty($query->_returnProperties['activity_date_time'])) { |
da236f9a | 56 | $query->_select['activity_date_time'] = 'civicrm_activity.activity_date_time as activity_date_time'; |
6a488035 TO |
57 | $query->_element['activity_date_time'] = 1; |
58 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
59 | } | |
60 | ||
a7488080 | 61 | if (!empty($query->_returnProperties['activity_status_id'])) { |
7bd16b05 | 62 | $query->_select['activity_status_id'] = 'civicrm_activity.status_id as activity_status_id'; |
6a488035 TO |
63 | $query->_element['activity_status_id'] = 1; |
64 | $query->_tables['civicrm_activity'] = 1; | |
6a488035 | 65 | $query->_whereTables['civicrm_activity'] = 1; |
6a488035 TO |
66 | } |
67 | ||
a7488080 | 68 | if (!empty($query->_returnProperties['activity_status'])) { |
6b051312 | 69 | $query->_select['activity_status_id'] = 1; |
6a488035 TO |
70 | $query->_element['activity_status'] = 1; |
71 | $query->_tables['civicrm_activity'] = 1; | |
6a488035 | 72 | $query->_whereTables['civicrm_activity'] = 1; |
6a488035 TO |
73 | } |
74 | ||
a7488080 | 75 | if (!empty($query->_returnProperties['activity_duration'])) { |
da236f9a | 76 | $query->_select['activity_duration'] = 'civicrm_activity.duration as activity_duration'; |
6a488035 TO |
77 | $query->_element['activity_duration'] = 1; |
78 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
79 | } | |
80 | ||
a7488080 | 81 | if (!empty($query->_returnProperties['activity_location'])) { |
da236f9a | 82 | $query->_select['activity_location'] = 'civicrm_activity.location as activity_location'; |
6a488035 TO |
83 | $query->_element['activity_location'] = 1; |
84 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
85 | } | |
86 | ||
a7488080 | 87 | if (!empty($query->_returnProperties['activity_details'])) { |
da236f9a | 88 | $query->_select['activity_details'] = 'civicrm_activity.details as activity_details'; |
6a488035 TO |
89 | $query->_element['activity_details'] = 1; |
90 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
91 | } | |
92 | ||
a7488080 | 93 | if (!empty($query->_returnProperties['source_record_id'])) { |
da236f9a | 94 | $query->_select['source_record_id'] = 'civicrm_activity.source_record_id as source_record_id'; |
6a488035 TO |
95 | $query->_element['source_record_id'] = 1; |
96 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
97 | } | |
98 | ||
a7488080 | 99 | if (!empty($query->_returnProperties['activity_is_test'])) { |
da236f9a | 100 | $query->_select['activity_is_test'] = 'civicrm_activity.is_test as activity_is_test'; |
6a488035 TO |
101 | $query->_element['activity_is_test'] = 1; |
102 | $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1; | |
103 | } | |
104 | ||
a7488080 | 105 | if (!empty($query->_returnProperties['activity_campaign_id'])) { |
6a488035 TO |
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 | ||
a7488080 | 111 | if (!empty($query->_returnProperties['activity_engagement_level'])) { |
6a488035 TO |
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 | ||
a7488080 | 117 | if (!empty($query->_returnProperties['source_contact'])) { |
37eb6ff9 | 118 | $query->_select['source_contact'] = 'source_contact.sort_name as source_contact'; |
6a488035 | 119 | $query->_element['source_contact'] = 1; |
fd95406d | 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; | |
6a488035 | 127 | } |
4041c9b6 | 128 | |
a7488080 | 129 | if (!empty($query->_returnProperties['activity_result'])) { |
4041c9b6 DG |
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 | } | |
ffd93213 | 134 | |
f3acfdd9 | 135 | if (!empty($query->_returnProperties['parent_id'])) { |
4c24c842 PN |
136 | $query->_tables['parent_id'] = 1; |
137 | $query->_whereTables['parent_id'] = 1; | |
138 | $query->_element['parent_id'] = 1; | |
139 | } | |
da236f9a | 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 | } | |
6a488035 TO |
149 | } |
150 | ||
151 | /** | |
b6c94f42 | 152 | * Given a list of conditions in query generate the required where clause. |
6a488035 | 153 | * |
1c1e779e | 154 | * @param \CRM_Contact_BAO_Query $query |
155 | * | |
156 | * @throws \CRM_Core_Exception | |
6a488035 | 157 | */ |
00be9182 | 158 | public static function where(&$query) { |
6a488035 | 159 | foreach (array_keys($query->_params) as $id) { |
1c1e779e | 160 | if (substr($query->_params[$id][0], 0, 9) === 'activity_') { |
2da8c6c4 | 161 | if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) { |
6a488035 TO |
162 | $query->_useDistinct = TRUE; |
163 | } | |
56bf8668 | 164 | $query->_params[$id][3]; |
6a488035 TO |
165 | self::whereClauseSingle($query->_params[$id], $query); |
166 | } | |
167 | } | |
6a488035 TO |
168 | } |
169 | ||
170 | /** | |
fe482240 | 171 | * Where clause for a single field. |
6a488035 | 172 | * |
b6c94f42 | 173 | * @param array $values |
174 | * @param CRM_Contact_BAO_Query $query | |
6b051312 | 175 | * |
176 | * @throws \CRM_Core_Exception | |
6a488035 | 177 | */ |
00be9182 | 178 | public static function whereClauseSingle(&$values, &$query) { |
56bf8668 | 179 | list($name, $op, $value, $grouping) = $values; |
6a488035 | 180 | |
9ab34172 | 181 | $fields = CRM_Activity_BAO_Activity::exportableFields(); |
6b051312 | 182 | $fieldSpec = $query->getFieldSpec($name); |
6a488035 TO |
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 | } | |
6b051312 | 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; | |
6a488035 TO |
193 | |
194 | switch ($name) { | |
195 | case 'activity_type_id': | |
9ab34172 | 196 | case 'activity_status_id': |
9ab34172 | 197 | case 'activity_engagement_level': |
9ab34172 | 198 | case 'activity_id': |
6f56dc34 | 199 | case 'activity_campaign_id': |
da236f9a | 200 | case 'activity_priority_id': |
288674ef | 201 | // We no longer expect "subject" as a specific criteria (as of CRM-19447), |
1cd7479e | 202 | // but we still use activity_subject in Activity.Get API |
288674ef | 203 | case 'activity_subject': |
6f56dc34 | 204 | |
9ab34172 | 205 | $qillName = $name; |
96f94695 | 206 | if (in_array($name, ['activity_engagement_level', 'activity_id'])) { |
9ab34172 | 207 | $name = $qillName = str_replace('activity_', '', $name); |
6a488035 | 208 | } |
96f94695 | 209 | if (in_array($name, [ |
96f94695 | 210 | 'activity_subject', |
211 | 'activity_priority_id', | |
212 | ])) { | |
9ab34172 | 213 | $name = str_replace('activity_', '', $name); |
214 | $qillName = str_replace('_id', '', $qillName); | |
215 | } | |
6f56dc34 | 216 | if ($name == 'activity_campaign_id') { |
96f94695 | 217 | $name = 'campaign_id'; |
6f56dc34 | 218 | } |
219 | ||
9ab34172 | 220 | $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String'; |
221 | ||
6b051312 | 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); | |
9ab34172 | 227 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op); |
96f94695 | 228 | $query->_qill[$grouping][] = ts('%1 %2 %3', [ |
229 | 1 => $fields[$qillName]['title'], | |
230 | 2 => $op, | |
231 | 3 => $value, | |
232 | ]); | |
6a488035 TO |
233 | break; |
234 | ||
92e9c781 AS |
235 | case 'activity_text': |
236 | self::whereClauseSingleActivityText($values, $query); | |
c9c5c58d AS |
237 | break; |
238 | ||
afa0b07c | 239 | case 'activity_type': |
da236f9a | 240 | case 'activity_priority': |
afa0b07c | 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); | |
96f94695 | 243 | $query->_qill[$grouping][] = ts('%1 %2 %3', [ |
244 | 1 => $fields[$name]['title'], | |
245 | 2 => $op, | |
246 | 3 => $value, | |
247 | ]); | |
afa0b07c | 248 | $query->_tables[$name] = $query->_whereTables[$name] = 1; |
249 | break; | |
250 | ||
6a488035 TO |
251 | case 'activity_survey_id': |
252 | if (!$value) { | |
253 | break; | |
254 | } | |
255 | $value = CRM_Utils_Type::escape($value, 'Integer'); | |
56a5616a | 256 | $query->_where[$grouping][] = " civicrm_activity.source_record_id = $value"; |
6a488035 TO |
257 | $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title'); |
258 | break; | |
259 | ||
6a488035 TO |
260 | case 'activity_role': |
261 | CRM_Contact_BAO_Query::$_activityRole = $values[2]; | |
44f817d4 | 262 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
a24b3694 | 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); | |
f813f78e | 266 | |
91da6cd5 | 267 | if ($values[2]) { |
6a488035 | 268 | $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1; |
91da6cd5 | 269 | if ($values[2] == 1) { |
a24b3694 | 270 | $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID"; |
91da6cd5 DL |
271 | $query->_qill[$grouping][] = ts('Activity created by'); |
272 | } | |
4c9b6178 | 273 | elseif ($values[2] == 2) { |
a24b3694 | 274 | $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID"; |
91da6cd5 DL |
275 | $query->_qill[$grouping][] = ts('Activity assigned to'); |
276 | } | |
4c9b6178 | 277 | elseif ($values[2] == 3) { |
a24b3694 | 278 | $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID"; |
91da6cd5 DL |
279 | $query->_qill[$grouping][] = ts('Activity targeted to'); |
280 | } | |
6a488035 TO |
281 | } |
282 | break; | |
283 | ||
6a488035 | 284 | case 'activity_test': |
56bf8668 | 285 | // We don't want to include all tests for sql OR CRM-7827 |
28c666be | 286 | if (!$value || $query->getOperator() != 'OR') { |
353ffa53 | 287 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean"); |
28c666be CW |
288 | if ($value) { |
289 | $query->_qill[$grouping][] = ts('Activity is a Test'); | |
290 | } | |
6a488035 TO |
291 | } |
292 | break; | |
293 | ||
294 | case 'activity_date': | |
295 | case 'activity_date_low': | |
296 | case 'activity_date_high': | |
27cedb98 | 297 | case 'activity_date_time_low': |
298 | case 'activity_date_time_high': | |
6a488035 | 299 | $query->dateQueryBuilder($values, |
96f94695 | 300 | 'civicrm_activity', str_replace([ |
301 | '_high', | |
302 | '_low', | |
303 | ], '', $name), 'activity_date_time', ts('Activity Date') | |
6a488035 TO |
304 | ); |
305 | break; | |
306 | ||
6a488035 TO |
307 | case 'activity_taglist': |
308 | $taglist = $value; | |
96f94695 | 309 | $value = []; |
6a488035 TO |
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 | } | |
91da6cd5 | 320 | |
6a488035 | 321 | case 'activity_tags': |
96f94695 | 322 | $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]); |
6a488035 | 323 | |
74aa8a2e PN |
324 | if (!is_array($value)) { |
325 | $value = explode(',', $value); | |
326 | } | |
327 | ||
96f94695 | 328 | $names = []; |
74aa8a2e PN |
329 | foreach ($value as $k => $v) { |
330 | $names[] = $activityTags[$v]; | |
6a488035 | 331 | } |
74aa8a2e | 332 | |
6a488035 | 333 | $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")"; |
96f94695 | 334 | $query->_qill[$grouping][] = ts('Activity Tag %1', [1 => $op]) . ' ' . implode(' ' . ts('OR') . ' ', $names); |
6a488035 TO |
335 | $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1; |
336 | break; | |
337 | ||
4041c9b6 | 338 | case 'activity_result': |
22e263ad | 339 | if (is_array($value)) { |
61d5e68b | 340 | $safe = []; |
7de0b24d | 341 | foreach ($value as $id => $k) { |
4041c9b6 DG |
342 | $safe[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'"; |
343 | } | |
344 | $query->_where[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")"; | |
96f94695 | 345 | $query->_qill[$grouping][] = ts("Activity Result - %1", [1 => implode(' or ', $safe)]); |
4041c9b6 DG |
346 | } |
347 | break; | |
4c24c842 PN |
348 | |
349 | case 'parent_id': | |
350 | if ($value == 1) { | |
fb192794 | 351 | $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL"; |
4c24c842 PN |
352 | $query->_qill[$grouping][] = ts('Activities which have Followup Activities'); |
353 | } | |
354 | elseif ($value == 2) { | |
fb192794 | 355 | $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL"; |
4c24c842 PN |
356 | $query->_qill[$grouping][] = ts('Activities without Followup Activities'); |
357 | } | |
358 | break; | |
ffd93213 | 359 | |
4c24c842 PN |
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"; | |
353ffa53 | 367 | $query->_qill[$grouping][] = ts('Activities which are not Followup Activities'); |
4c24c842 PN |
368 | } |
369 | break; | |
fd95406d | 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); | |
96f94695 | 376 | $query->_qill[$grouping][] = ts('%1 %2 %3', [ |
377 | 1 => $fields[$name]['title'], | |
378 | 2 => $op, | |
379 | 3 => $value, | |
380 | ]); | |
fd95406d | 381 | break; |
6a488035 TO |
382 | } |
383 | } | |
384 | ||
ffd93213 | 385 | /** |
100fef9d | 386 | * @param string $name |
ffd93213 EM |
387 | * @param $mode |
388 | * @param $side | |
389 | * | |
390 | * @return null|string | |
391 | */ | |
00be9182 | 392 | public static function from($name, $mode, $side) { |
6a488035 TO |
393 | $from = NULL; |
394 | switch ($name) { | |
395 | case 'civicrm_activity': | |
9393897f | 396 | //CRM-7480 we are going to civicrm_activity table either |
6a488035 TO |
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. | |
8a06db38 | 400 | $from .= " $side JOIN civicrm_activity_contact |
91da6cd5 | 401 | ON ( civicrm_activity_contact.contact_id = contact_a.id ) "; |
8a06db38 | 402 | $from .= " $side JOIN civicrm_activity |
91da6cd5 DL |
403 | ON ( civicrm_activity.id = civicrm_activity_contact.activity_id |
404 | AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )"; | |
db54182d | 405 | // Do not show deleted contact's activity |
7c9cdbb7 DG |
406 | $from .= " INNER JOIN civicrm_contact |
407 | ON ( civicrm_activity_contact.contact_id = civicrm_contact.id and civicrm_contact.is_deleted != 1 )"; | |
6a488035 TO |
408 | break; |
409 | ||
6a488035 | 410 | case 'activity_type': |
1d85d241 | 411 | $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')"; |
6a488035 TO |
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 | ||
da236f9a | 416 | case 'activity_priority': |
c6ac46ad | 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; | |
da236f9a | 421 | |
6a488035 TO |
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': | |
0bc2b887 PN |
427 | $sourceID = CRM_Core_PseudoConstant::getKey( |
428 | 'CRM_Activity_BAO_ActivityContact', | |
429 | 'record_type_id', | |
430 | 'Activity Source' | |
431 | ); | |
37eb6ff9 | 432 | $from = " |
0bc2b887 PN |
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)"; | |
6a488035 | 437 | break; |
ffd93213 | 438 | |
4c24c842 PN |
439 | case 'parent_id': |
440 | $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id"; | |
441 | break; | |
6a488035 TO |
442 | } |
443 | ||
444 | return $from; | |
445 | } | |
446 | ||
e6dda67a | 447 | /** |
448 | * Get the metadata for fields to be included on the activity search form. | |
449 | * | |
a13c931b | 450 | * @throws \CiviCRM_API3_Exception |
e6dda67a | 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() { | |
a13c931b | 455 | $fields = ['activity_type_id', 'activity_date_time', 'priority_id', 'activity_location', 'activity_status_id']; |
e6dda67a | 456 | $metadata = civicrm_api3('Activity', 'getfields', [])['values']; |
359fdb6f | 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, | |
1c1e779e | 462 | 'html' => [ |
463 | 'type' => 'Text', | |
464 | ], | |
359fdb6f | 465 | ]; |
466 | return $metadata; | |
e6dda67a | 467 | } |
468 | ||
6a488035 | 469 | /** |
fe482240 | 470 | * Add all the elements shared between case activity search and advanced search. |
6a488035 | 471 | * |
e6dda67a | 472 | * @param CRM_Core_Form_Search $form |
a13c931b | 473 | * |
474 | * @throws \CiviCRM_API3_Exception | |
475 | * @throws \CRM_Core_Exception | |
6a488035 | 476 | */ |
00be9182 | 477 | public static function buildSearchForm(&$form) { |
e6dda67a | 478 | $form->addSearchFieldMetadata(['Activity' => self::getSearchFieldMetadata()]); |
479 | $form->addFormFieldsFromMetadata(); | |
6a488035 | 480 | |
96f94695 | 481 | $followUpActivity = [ |
4c24c842 PN |
482 | 1 => ts('Yes'), |
483 | 2 => ts('No'), | |
96f94695 | 484 | ]; |
485 | $form->addRadio('parent_id', NULL, $followUpActivity, ['allowClear' => TRUE]); | |
486 | $form->addRadio('followup_parent_id', NULL, $followUpActivity, ['allowClear' => TRUE]); | |
487 | $activityRoles = [ | |
18d2d5e2 | 488 | 3 => ts('With'), |
91da6cd5 | 489 | 2 => ts('Assigned to'), |
18d2d5e2 | 490 | 1 => ts('Added by'), |
96f94695 | 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 | ]); | |
d57052f7 | 498 | $ssID = $form->get('ssID'); |
96f94695 | 499 | $status = [$activityStatus['Completed'], $activityStatus['Scheduled']]; |
d57052f7 | 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 | } | |
a13c931b | 504 | $form->setDefaults(['activity_status_id' => $status]); |
d57052f7 | 505 | |
92e9c781 | 506 | $form->addElement('text', 'activity_text', ts('Activity Text'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
1cd7479e | 507 | |
508 | $form->addRadio('activity_option', '', CRM_Core_SelectValues::activityTextOptions()); | |
96f94695 | 509 | $form->setDefaults(['activity_option' => 'both']); |
c9c5c58d | 510 | |
6a488035 | 511 | $form->addYesNo('activity_test', ts('Activity is a Test?')); |
74aa8a2e PN |
512 | $activity_tags = CRM_Core_BAO_Tag::getColorTags('civicrm_activity'); |
513 | ||
6a488035 | 514 | if ($activity_tags) { |
74aa8a2e PN |
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 | ] | |
15a788af | 523 | ); |
6a488035 TO |
524 | } |
525 | ||
526 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity'); | |
95ef220a | 527 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE); |
6a488035 | 528 | |
9231464f | 529 | $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE); |
28a04ea9 | 530 | if ($surveys) { |
531 | $form->add('select', 'activity_survey_id', ts('Survey / Petition'), | |
96f94695 | 532 | ['' => ts('- none -')] + $surveys, FALSE, |
533 | ['class' => 'crm-select2'] | |
534 | ); | |
28a04ea9 | 535 | } |
86a0d21e | 536 | |
96f94695 | 537 | CRM_Core_BAO_Query::addCustomFormFields($form, ['Activity']); |
6a488035 TO |
538 | |
539 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id'); | |
540 | ||
7808aae6 | 541 | // Add engagement level CRM-7775. |
6a488035 | 542 | $buildEngagementLevel = FALSE; |
4041c9b6 | 543 | $buildSurveyResult = FALSE; |
6a488035 TO |
544 | if (CRM_Campaign_BAO_Campaign::isCampaignEnable() && |
545 | CRM_Campaign_BAO_Campaign::accessCampaign() | |
546 | ) { | |
547 | $buildEngagementLevel = TRUE; | |
96f94695 | 548 | $form->addSelect('activity_engagement_level', [ |
549 | 'entity' => 'activity', | |
550 | 'context' => 'search', | |
551 | ]); | |
6a488035 | 552 | |
4041c9b6 | 553 | // Add survey result field. |
353ffa53 | 554 | $optionGroups = CRM_Campaign_BAO_Survey::getResultSets('name'); |
96f94695 | 555 | $resultOptions = []; |
481a74f4 TO |
556 | foreach ($optionGroups as $gid => $name) { |
557 | if ($name) { | |
4041c9b6 DG |
558 | $value = CRM_Core_OptionGroup::values($name); |
559 | if (!empty($value)) { | |
61d5e68b | 560 | foreach ($value as $k => $v) { |
4041c9b6 DG |
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. | |
22e263ad | 568 | if (count($resultOptions) > 0) { |
4041c9b6 DG |
569 | $buildSurveyResult = TRUE; |
570 | asort($resultOptions); | |
571 | $form->add('select', 'activity_result', ts("Survey Result"), | |
572 | $resultOptions, FALSE, | |
96f94695 | 573 | [ |
574 | 'id' => 'activity_result', | |
575 | 'multiple' => 'multiple', | |
576 | 'class' => 'crm-select2', | |
577 | ] | |
4041c9b6 DG |
578 | ); |
579 | } | |
580 | } | |
77b97be7 | 581 | |
6a488035 | 582 | $form->assign('buildEngagementLevel', $buildEngagementLevel); |
4041c9b6 | 583 | $form->assign('buildSurveyResult', $buildSurveyResult); |
96f94695 | 584 | $form->setDefaults(['activity_test' => 0]); |
6a488035 TO |
585 | } |
586 | ||
ffd93213 EM |
587 | /** |
588 | * @param $mode | |
589 | * @param bool $includeCustomFields | |
590 | * | |
591 | * @return array|null | |
592 | */ | |
00be9182 | 593 | public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) { |
6a488035 TO |
594 | $properties = NULL; |
595 | if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) { | |
96f94695 | 596 | $properties = [ |
6a488035 TO |
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, | |
5511233c | 603 | 'activity_type_id' => 1, |
6a488035 TO |
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, | |
da236f9a | 610 | 'activity_priority' => 1, |
37eb6ff9 | 611 | 'source_contact' => 1, |
6a488035 TO |
612 | 'source_record_id' => 1, |
613 | 'activity_is_test' => 1, | |
614 | 'activity_campaign_id' => 1, | |
4041c9b6 | 615 | 'result' => 1, |
6a488035 | 616 | 'activity_engagement_level' => 1, |
4c24c842 | 617 | 'parent_id' => 1, |
96f94695 | 618 | ]; |
6a488035 TO |
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 | } | |
96025800 | 633 | |
7bd16b05 | 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() { | |
96f94695 | 641 | $properties = [ |
7bd16b05 | 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, | |
96f94695 | 656 | ]; |
7bd16b05 | 657 | |
658 | return $properties; | |
659 | } | |
660 | ||
c9c5c58d AS |
661 | /** |
662 | * Where/qill clause for notes | |
663 | * | |
664 | * @param array $values | |
d934f895 | 665 | * @param CRM_Contact_BAO_Query $query |
666 | * | |
667 | * @throws \CRM_Core_Exception | |
c9c5c58d | 668 | */ |
92e9c781 | 669 | public static function whereClauseSingleActivityText(&$values, &$query) { |
c9c5c58d | 670 | list($name, $op, $value, $grouping, $wildcard) = $values; |
c9c5c58d | 671 | $activityOptionValues = $query->getWhereValues('activity_option', $grouping); |
1cd7479e | 672 | $activityOption = CRM_Utils_Array::value(2, $activityOptionValues, 6); |
c9c5c58d AS |
673 | |
674 | $query->_useDistinct = TRUE; | |
675 | ||
96f94695 | 676 | $label = ts('Activity Text (%1)', [1 => CRM_Utils_Array::value($activityOption, CRM_Core_SelectValues::activityTextOptions())]); |
677 | $clauses = []; | |
c9c5c58d AS |
678 | if ($activityOption % 2 == 0) { |
679 | $clauses[] = $query->buildClause('civicrm_activity.details', $op, $value, 'String'); | |
c9c5c58d AS |
680 | } |
681 | if ($activityOption % 3 == 0) { | |
682 | $clauses[] = $query->buildClause('civicrm_activity.subject', $op, $value, 'String'); | |
c9c5c58d | 683 | } |
1cd7479e | 684 | |
c9c5c58d | 685 | $query->_where[$grouping][] = "( " . implode(' OR ', $clauses) . " )"; |
1cd7479e | 686 | list($qillOp, $qillVal) = $query->buildQillForFieldValue(NULL, $name, $value, $op); |
96f94695 | 687 | $query->_qill[$grouping][] = ts("%1 %2 '%3'", [ |
688 | 1 => $label, | |
689 | 2 => $qillOp, | |
690 | 3 => $qillVal, | |
691 | ]); | |
c9c5c58d AS |
692 | } |
693 | ||
6a488035 | 694 | } |