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