Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b 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 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
244bbdd8 | 13 | * This api exposes CiviCRM Activity records. |
6a488035 TO |
14 | * |
15 | * @package CiviCRM_APIv3 | |
6a488035 TO |
16 | */ |
17 | ||
6a488035 | 18 | /** |
61fe4988 | 19 | * Creates or updates an Activity. |
6a488035 | 20 | * |
cf470720 | 21 | * @param array $params |
c28e1768 | 22 | * Array per getfields documentation. |
6a488035 | 23 | * |
77b97be7 | 24 | * @throws API_Exception |
a6c01b45 | 25 | * @return array |
00f8641b | 26 | * API result array |
6a488035 TO |
27 | */ |
28 | function civicrm_api3_activity_create($params) { | |
c16b4619 | 29 | $isNew = empty($params['id']); |
6a488035 | 30 | |
a7488080 | 31 | if (empty($params['id'])) { |
6a488035 TO |
32 | // an update does not require any mandatory parameters |
33 | civicrm_api3_verify_one_mandatory($params, | |
34 | NULL, | |
cf8f0fff | 35 | [ |
7cdbcb16 TO |
36 | 'activity_name', |
37 | 'activity_type_id', | |
38 | 'activity_label', | |
cf8f0fff | 39 | ] |
6a488035 TO |
40 | ); |
41 | } | |
42 | ||
6a488035 TO |
43 | // check for various error and required conditions |
44 | // note that almost all the processing in there should be managed by the wrapper layer | |
45 | // & should be removed - needs testing | |
46 | $errors = _civicrm_api3_activity_check_params($params); | |
47 | ||
48 | // this should not be required as should throw exception rather than return errors - | |
49 | //needs testing | |
50 | if (!empty($errors)) { | |
51 | return $errors; | |
52 | } | |
53 | ||
6a488035 | 54 | // processing for custom data |
cf8f0fff | 55 | $values = $activityArray = []; |
6a488035 TO |
56 | _civicrm_api3_custom_format_params($params, $values, 'Activity'); |
57 | ||
58 | if (!empty($values['custom'])) { | |
59 | $params['custom'] = $values['custom']; | |
60 | } | |
61 | ||
62 | // this should be set as a default rather than hard coded | |
63 | // needs testing | |
64 | $params['skipRecentView'] = TRUE; | |
65 | ||
66 | // If this is a case activity, see if there is an existing activity | |
67 | // and set it as an old revision. Also retrieve details we'll need. | |
68 | // this handling should all be moved to the BAO layer | |
7cdbcb16 TO |
69 | $case_id = ''; |
70 | $createRevision = FALSE; | |
cf8f0fff | 71 | $oldActivityValues = []; |
e96d5fa3 CW |
72 | // Lookup case id if not supplied |
73 | if (!isset($params['case_id']) && !empty($params['id'])) { | |
74 | $params['case_id'] = CRM_Core_DAO::singleValueQuery("SELECT case_id FROM civicrm_case_activity WHERE activity_id = " . (int) $params['id']); | |
75 | } | |
a7488080 | 76 | if (!empty($params['case_id'])) { |
6a488035 | 77 | $case_id = $params['case_id']; |
c16b4619 | 78 | if (!empty($params['id']) && Civi::settings()->get('civicaseActivityRevisions')) { |
cf8f0fff | 79 | $oldActivityParams = ['id' => $params['id']]; |
6a488035 TO |
80 | if (!$oldActivityValues) { |
81 | CRM_Activity_BAO_Activity::retrieve($oldActivityParams, $oldActivityValues); | |
82 | } | |
83 | if (empty($oldActivityValues)) { | |
10114f2d | 84 | throw new API_Exception(ts("Unable to locate existing activity.")); |
6a488035 TO |
85 | } |
86 | else { | |
87 | $activityDAO = new CRM_Activity_DAO_Activity(); | |
88 | $activityDAO->id = $params['id']; | |
89 | $activityDAO->is_current_revision = 0; | |
90 | if (!$activityDAO->save()) { | |
10114f2d | 91 | throw new API_Exception(ts("Unable to revision existing case activity.")); |
6a488035 TO |
92 | } |
93 | $createRevision = TRUE; | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | $deleteActivityAssignment = FALSE; | |
99 | if (isset($params['assignee_contact_id'])) { | |
100 | $deleteActivityAssignment = TRUE; | |
101 | } | |
102 | ||
103 | $deleteActivityTarget = FALSE; | |
104 | if (isset($params['target_contact_id'])) { | |
105 | $deleteActivityTarget = TRUE; | |
106 | } | |
107 | ||
108 | // this should all be handled at the BAO layer | |
109 | $params['deleteActivityAssignment'] = CRM_Utils_Array::value('deleteActivityAssignment', $params, $deleteActivityAssignment); | |
110 | $params['deleteActivityTarget'] = CRM_Utils_Array::value('deleteActivityTarget', $params, $deleteActivityTarget); | |
111 | ||
112 | if ($case_id && $createRevision) { | |
113 | // This is very similar to the copy-to-case action. | |
114 | if (!CRM_Utils_Array::crmIsEmptyArray($oldActivityValues['target_contact'])) { | |
115 | $oldActivityValues['targetContactIds'] = implode(',', array_unique($oldActivityValues['target_contact'])); | |
116 | } | |
117 | if (!CRM_Utils_Array::crmIsEmptyArray($oldActivityValues['assignee_contact'])) { | |
118 | $oldActivityValues['assigneeContactIds'] = implode(',', array_unique($oldActivityValues['assignee_contact'])); | |
119 | } | |
120 | $oldActivityValues['mode'] = 'copy'; | |
121 | $oldActivityValues['caseID'] = $case_id; | |
122 | $oldActivityValues['activityID'] = $oldActivityValues['id']; | |
123 | $oldActivityValues['contactID'] = $oldActivityValues['source_contact_id']; | |
124 | ||
125 | $copyToCase = CRM_Activity_Page_AJAX::_convertToCaseActivity($oldActivityValues); | |
126 | if (empty($copyToCase['error_msg'])) { | |
127 | // now fix some things that are different from copy-to-case | |
128 | // then fall through to the create below to update with the passed in params | |
129 | $params['id'] = $copyToCase['newId']; | |
130 | $params['is_auto'] = 0; | |
131 | $params['original_id'] = empty($oldActivityValues['original_id']) ? $oldActivityValues['id'] : $oldActivityValues['original_id']; | |
132 | } | |
133 | else { | |
10114f2d | 134 | throw new API_Exception(ts("Unable to create new revision of case activity.")); |
6a488035 TO |
135 | } |
136 | } | |
137 | ||
138 | // create activity | |
139 | $activityBAO = CRM_Activity_BAO_Activity::create($params); | |
140 | ||
141 | if (isset($activityBAO->id)) { | |
c16b4619 | 142 | if ($case_id && $isNew && !$createRevision) { |
18e0f096 CW |
143 | // If this is a brand new case activity, add to case(s) |
144 | foreach ((array) $case_id as $singleCaseId) { | |
cf8f0fff | 145 | $caseActivityParams = ['activity_id' => $activityBAO->id, 'case_id' => $singleCaseId]; |
18e0f096 CW |
146 | CRM_Case_BAO_Case::processCaseActivity($caseActivityParams); |
147 | } | |
6a488035 TO |
148 | } |
149 | ||
150 | _civicrm_api3_object_to_array($activityBAO, $activityArray[$activityBAO->id]); | |
244bbdd8 | 151 | return civicrm_api3_create_success($activityArray, $params, 'Activity', 'get', $activityBAO); |
6a488035 TO |
152 | } |
153 | } | |
11e09c59 TO |
154 | |
155 | /** | |
61fe4988 EM |
156 | * Specify Meta data for create. |
157 | * | |
158 | * Note that this data is retrievable via the getfields function and is used for pre-filling defaults and | |
159 | * ensuring mandatory requirements are met. | |
160 | * | |
cf470720 | 161 | * @param array $params |
c28e1768 | 162 | * Array of parameters determined by getfields. |
6a488035 TO |
163 | */ |
164 | function _civicrm_api3_activity_create_spec(&$params) { | |
165 | ||
cf8f0fff | 166 | $params['status_id']['api.aliases'] = ['activity_status']; |
67744c4e | 167 | |
cf8f0fff | 168 | $params['assignee_contact_id'] = [ |
6a488035 | 169 | 'name' => 'assignee_id', |
0a24dc9a CW |
170 | 'title' => 'Activity Assignee', |
171 | 'description' => 'Contact(s) assigned to this activity.', | |
6a488035 | 172 | 'type' => 1, |
7e61908b | 173 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
0a24dc9a | 174 | 'FKApiName' => 'Contact', |
cf8f0fff CW |
175 | ]; |
176 | $params['target_contact_id'] = [ | |
6a488035 TO |
177 | 'name' => 'target_id', |
178 | 'title' => 'Activity Target', | |
0a24dc9a | 179 | 'description' => 'Contact(s) participating in this activity.', |
6a488035 | 180 | 'type' => 1, |
7e61908b | 181 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
0a24dc9a | 182 | 'FKApiName' => 'Contact', |
cf8f0fff | 183 | ]; |
2f3d72cf | 184 | |
cf8f0fff | 185 | $params['source_contact_id'] = [ |
7cdbcb16 TO |
186 | 'name' => 'source_contact_id', |
187 | 'title' => 'Activity Source Contact', | |
0a24dc9a | 188 | 'description' => 'Person who created this activity. Defaults to current user.', |
7cdbcb16 | 189 | 'type' => 1, |
7e61908b | 190 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
7cdbcb16 | 191 | 'api.default' => 'user_contact_id', |
0a24dc9a | 192 | 'FKApiName' => 'Contact', |
c442f1b6 | 193 | 'api.required' => TRUE, |
cf8f0fff | 194 | ]; |
0a24dc9a | 195 | |
cf8f0fff | 196 | $params['case_id'] = [ |
0a24dc9a CW |
197 | 'name' => 'case_id', |
198 | 'title' => 'Case ID', | |
199 | 'description' => 'For creating an activity as part of a case.', | |
200 | 'type' => 1, | |
201 | 'FKClassName' => 'CRM_Case_DAO_Case', | |
202 | 'FKApiName' => 'Case', | |
cf8f0fff | 203 | ]; |
2f3d72cf | 204 | |
a06c30f5 | 205 | $params['activity_date_time']['api.default'] = 'now'; |
2c9e83fa | 206 | |
6a488035 TO |
207 | } |
208 | ||
1196e086 CW |
209 | /** |
210 | * Specify Metadata for get. | |
211 | * | |
212 | * @param array $params | |
213 | */ | |
214 | function _civicrm_api3_activity_get_spec(&$params) { | |
cf8f0fff | 215 | $params['tag_id'] = [ |
1196e086 CW |
216 | 'title' => 'Tags', |
217 | 'description' => 'Find activities with specified tags.', | |
760ac501 | 218 | 'type' => CRM_Utils_Type::T_INT, |
1196e086 CW |
219 | 'FKClassName' => 'CRM_Core_DAO_Tag', |
220 | 'FKApiName' => 'Tag', | |
40875a91 | 221 | 'supports_joins' => TRUE, |
cf8f0fff CW |
222 | ]; |
223 | $params['file_id'] = [ | |
40875a91 CW |
224 | 'title' => 'Attached Files', |
225 | 'description' => 'Find activities with attached files.', | |
760ac501 | 226 | 'type' => CRM_Utils_Type::T_INT, |
40875a91 CW |
227 | 'FKClassName' => 'CRM_Core_DAO_File', |
228 | 'FKApiName' => 'File', | |
cf8f0fff CW |
229 | ]; |
230 | $params['case_id'] = [ | |
1196e086 CW |
231 | 'title' => 'Cases', |
232 | 'description' => 'Find activities within specified cases.', | |
760ac501 | 233 | 'type' => CRM_Utils_Type::T_INT, |
1196e086 CW |
234 | 'FKClassName' => 'CRM_Case_DAO_Case', |
235 | 'FKApiName' => 'Case', | |
2c9e4446 | 236 | 'supports_joins' => TRUE, |
cf8f0fff CW |
237 | ]; |
238 | $params['contact_id'] = [ | |
526e0834 CW |
239 | 'title' => 'Activity Contact ID', |
240 | 'description' => 'Find activities involving this contact (as target, source, OR assignee).', | |
760ac501 | 241 | 'type' => CRM_Utils_Type::T_INT, |
526e0834 CW |
242 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
243 | 'FKApiName' => 'Contact', | |
cf8f0fff CW |
244 | ]; |
245 | $params['target_contact_id'] = [ | |
1196e086 CW |
246 | 'title' => 'Target Contact ID', |
247 | 'description' => 'Find activities with specified target contact.', | |
760ac501 | 248 | 'type' => CRM_Utils_Type::T_INT, |
1196e086 CW |
249 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
250 | 'FKApiName' => 'Contact', | |
cf8f0fff CW |
251 | ]; |
252 | $params['source_contact_id'] = [ | |
1196e086 CW |
253 | 'title' => 'Source Contact ID', |
254 | 'description' => 'Find activities with specified source contact.', | |
760ac501 | 255 | 'type' => CRM_Utils_Type::T_INT, |
1196e086 CW |
256 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
257 | 'FKApiName' => 'Contact', | |
cf8f0fff CW |
258 | ]; |
259 | $params['assignee_contact_id'] = [ | |
1196e086 CW |
260 | 'title' => 'Assignee Contact ID', |
261 | 'description' => 'Find activities with specified assignee contact.', | |
760ac501 | 262 | 'type' => CRM_Utils_Type::T_INT, |
1196e086 CW |
263 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
264 | 'FKApiName' => 'Contact', | |
cf8f0fff CW |
265 | ]; |
266 | $params['is_overdue'] = [ | |
760ac501 CW |
267 | 'title' => 'Is Activity Overdue', |
268 | 'description' => 'Incomplete activities with a past date.', | |
269 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
cf8f0fff | 270 | ]; |
1196e086 CW |
271 | } |
272 | ||
6a488035 | 273 | /** |
61fe4988 | 274 | * Gets a CiviCRM activity according to parameters. |
6a488035 | 275 | * |
cf470720 | 276 | * @param array $params |
61fe4988 | 277 | * Array per getfields documentation. |
6a488035 | 278 | * |
7c31ae57 | 279 | * @return array |
00f8641b | 280 | * API result array |
bbd2743b | 281 | * |
282 | * @throws \API_Exception | |
283 | * @throws \CiviCRM_API3_Exception | |
284 | * @throws \Civi\API\Exception\UnauthorizedException | |
6a488035 TO |
285 | */ |
286 | function civicrm_api3_activity_get($params) { | |
760ac501 | 287 | $options = _civicrm_api3_get_options_from_params($params, FALSE, 'Activity', 'get'); |
526e0834 | 288 | $sql = CRM_Utils_SQL_Select::fragment(); |
a6c2ebdc | 289 | _civicrm_activity_get_handleSourceContactNameOrderBy($params, $options, $sql); |
d544ffcd | 290 | |
d544ffcd CW |
291 | _civicrm_api3_activity_get_extraFilters($params, $sql); |
292 | ||
293 | // Handle is_overdue sort | |
294 | if (!empty($options['sort'])) { | |
295 | $sort = explode(', ', $options['sort']); | |
296 | ||
297 | foreach ($sort as $index => &$sortString) { | |
298 | // Get sort field and direction | |
299 | list($sortField, $dir) = array_pad(explode(' ', $sortString), 2, 'ASC'); | |
300 | if ($sortField == 'is_overdue') { | |
301 | $incomplete = implode(',', array_keys(CRM_Activity_BAO_Activity::getStatusesByType(CRM_Activity_BAO_Activity::INCOMPLETE))); | |
302 | $sql->orderBy("IF((a.activity_date_time >= NOW() OR a.status_id NOT IN ($incomplete)), 0, 1) $dir", NULL, $index); | |
303 | // Replace the sort with a placeholder which will be ignored by sql | |
304 | $sortString = '(1)'; | |
305 | } | |
306 | } | |
307 | $params['options']['sort'] = implode(', ', $sort); | |
308 | } | |
309 | ||
310 | // Ensure there's enough data for calculating is_overdue | |
311 | if (!empty($options['return']['is_overdue']) && (empty($options['return']['status_id']) || empty($options['return']['activity_date_time']))) { | |
312 | $options['return']['status_id'] = $options['return']['activity_date_time'] = 1; | |
313 | $params['return'] = array_keys($options['return']); | |
314 | } | |
315 | ||
316 | $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $sql); | |
84be264e | 317 | if ($options['is_count']) { |
318 | return civicrm_api3_create_success($activities, $params, 'Activity', 'get'); | |
319 | } | |
320 | ||
d544ffcd CW |
321 | $activities = _civicrm_api3_activity_get_formatResult($params, $activities, $options); |
322 | //legacy custom data get - so previous formatted response is still returned too | |
323 | return civicrm_api3_create_success($activities, $params, 'Activity', 'get'); | |
324 | } | |
325 | ||
a6c2ebdc | 326 | /** |
327 | * Handle source_contact_name as a sort parameter. | |
328 | * | |
329 | * This is passed from the activity selector - e.g search results or contact tab. | |
330 | * | |
331 | * It's a non-standard handling but this api already handles variations on handling source_contact | |
332 | * as a filter & as a field so it's in keeping with that. Source contact has a one-one relationship | |
333 | * with activity table. | |
334 | * | |
335 | * Test coverage in CRM_Activity_BAO_ActivtiyTest::testGetActivitiesforContactSummaryWithSortOptions | |
336 | * | |
337 | * @param array $params | |
338 | * @param array $options | |
339 | * @param CRM_Utils_SQL_Select $sql | |
340 | */ | |
341 | function _civicrm_activity_get_handleSourceContactNameOrderBy(&$params, &$options, $sql) { | |
342 | $sourceContactID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source'); | |
4a413eb6 AH |
343 | if (!empty($options['sort']) |
344 | && in_array($options['sort'], [ | |
a6c2ebdc | 345 | 'source_contact_name', |
346 | 'source_contact_name desc', | |
4a413eb6 | 347 | 'source_contact_name asc', |
a6c2ebdc | 348 | ])) { |
349 | $order = substr($options['sort'], -4) === 'desc' ? 'desc' : 'asc'; | |
350 | $sql->join( | |
351 | 'source_contact', | |
352 | "LEFT JOIN | |
c2068a3e TO |
353 | civicrm_activity_contact ac ON (ac.activity_id = a.id AND record_type_id = #sourceContactID) |
354 | LEFT JOIN civicrm_contact c ON c.id = ac.contact_id", | |
355 | ['sourceContactID' => $sourceContactID] | |
a6c2ebdc | 356 | ); |
357 | $sql->orderBy("c.display_name $order"); | |
358 | unset($options['sort'], $params['options']['sort']); | |
359 | } | |
360 | } | |
361 | ||
d544ffcd CW |
362 | /** |
363 | * Support filters beyond what basic_get can do. | |
364 | * | |
365 | * @param array $params | |
366 | * @param CRM_Utils_SQL_Select $sql | |
367 | * @throws \CiviCRM_API3_Exception | |
368 | * @throws \Exception | |
369 | */ | |
370 | function _civicrm_api3_activity_get_extraFilters(&$params, &$sql) { | |
371 | // Filter by activity contacts | |
cf8f0fff | 372 | $activityContactOptions = [ |
d544ffcd | 373 | 'contact_id' => NULL, |
550dcde8 | 374 | 'target_contact_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'), |
375 | 'source_contact_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source'), | |
376 | 'assignee_contact_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Assignees'), | |
cf8f0fff | 377 | ]; |
526e0834 CW |
378 | foreach ($activityContactOptions as $activityContactName => $activityContactValue) { |
379 | if (!empty($params[$activityContactName])) { | |
526e0834 | 380 | if (!is_array($params[$activityContactName])) { |
cf8f0fff | 381 | $params[$activityContactName] = ['=' => $params[$activityContactName]]; |
526e0834 CW |
382 | } |
383 | $clause = \CRM_Core_DAO::createSQLFilter('contact_id', $params[$activityContactName]); | |
384 | $typeClause = $activityContactValue ? 'record_type_id = #typeId AND ' : ''; | |
385 | $sql->where("a.id IN (SELECT activity_id FROM civicrm_activity_contact WHERE $typeClause !clause)", | |
cf8f0fff | 386 | ['#typeId' => $activityContactValue, '!clause' => $clause] |
526e0834 | 387 | ); |
6a488035 TO |
388 | } |
389 | } | |
749522a2 | 390 | |
760ac501 | 391 | // Handle is_overdue filter |
d544ffcd | 392 | // Boolean calculated field - does not support operators |
760ac501 | 393 | if (isset($params['is_overdue'])) { |
ce9d78e1 | 394 | $incomplete = implode(',', array_keys(CRM_Activity_BAO_Activity::getStatusesByType(CRM_Activity_BAO_Activity::INCOMPLETE))); |
760ac501 CW |
395 | if ($params['is_overdue']) { |
396 | $sql->where('a.activity_date_time < NOW()'); | |
ce9d78e1 | 397 | $sql->where("a.status_id IN ($incomplete)"); |
760ac501 CW |
398 | } |
399 | else { | |
ce9d78e1 | 400 | $sql->where("(a.activity_date_time >= NOW() OR a.status_id NOT IN ($incomplete))"); |
760ac501 CW |
401 | } |
402 | } | |
403 | ||
749522a2 TO |
404 | // Define how to handle filters on some related entities. |
405 | // Subqueries are nice in (a) avoiding duplicates and (b) when the result | |
406 | // list is expected to be bite-sized. Joins are nice (a) with larger | |
407 | // datasets and (b) checking for non-existent relations. | |
cf8f0fff CW |
408 | $rels = [ |
409 | 'tag_id' => [ | |
749522a2 TO |
410 | 'subquery' => 'a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_activity" AND !clause)', |
411 | 'join' => '!joinType civicrm_entity_tag !alias ON (!alias.entity_table = "civicrm_activity" AND !alias.entity_id = a.id)', | |
412 | 'column' => 'tag_id', | |
cf8f0fff CW |
413 | ], |
414 | 'file_id' => [ | |
749522a2 TO |
415 | 'subquery' => 'a.id IN (SELECT entity_id FROM civicrm_entity_file WHERE entity_table = "civicrm_activity" AND !clause)', |
416 | 'join' => '!joinType civicrm_entity_file !alias ON (!alias.entity_table = "civicrm_activity" AND !alias.entity_id = a.id)', | |
417 | 'column' => 'file_id', | |
cf8f0fff CW |
418 | ], |
419 | 'case_id' => [ | |
749522a2 TO |
420 | 'subquery' => 'a.id IN (SELECT activity_id FROM civicrm_case_activity WHERE !clause)', |
421 | 'join' => '!joinType civicrm_case_activity !alias ON (!alias.activity_id = a.id)', | |
422 | 'column' => 'case_id', | |
cf8f0fff CW |
423 | ], |
424 | ]; | |
749522a2 TO |
425 | foreach ($rels as $filter => $relSpec) { |
426 | if (!empty($params[$filter])) { | |
427 | if (!is_array($params[$filter])) { | |
cf8f0fff | 428 | $params[$filter] = ['=' => $params[$filter]]; |
749522a2 TO |
429 | } |
430 | // $mode is one of ('LEFT JOIN', 'INNER JOIN', 'SUBQUERY') | |
431 | $mode = isset($params[$filter]['IS NULL']) ? 'LEFT JOIN' : 'SUBQUERY'; | |
432 | if ($mode === 'SUBQUERY') { | |
433 | $clause = \CRM_Core_DAO::createSQLFilter($relSpec['column'], $params[$filter]); | |
434 | if ($clause) { | |
cf8f0fff | 435 | $sql->where($relSpec['subquery'], ['!clause' => $clause]); |
749522a2 TO |
436 | } |
437 | } | |
438 | else { | |
439 | $alias = 'actjoin_' . $filter; | |
440 | $clause = \CRM_Core_DAO::createSQLFilter($alias . "." . $relSpec['column'], $params[$filter]); | |
441 | if ($clause) { | |
cf8f0fff | 442 | $sql->join($alias, $relSpec['join'], ['!alias' => $alias, 'joinType' => $mode]); |
749522a2 TO |
443 | $sql->where($clause); |
444 | } | |
445 | } | |
0298287b | 446 | } |
6a488035 | 447 | } |
ab5fa8f2 TO |
448 | } |
449 | ||
450 | /** | |
61fe4988 | 451 | * Given a list of activities, append any extra data requested about the activities. |
ab5fa8f2 | 452 | * |
b081365f | 453 | * @note Called by civicrm-core and CiviHR |
ab5fa8f2 | 454 | * |
cf470720 TO |
455 | * @param array $params |
456 | * API request parameters. | |
ab5fa8f2 | 457 | * @param array $activities |
cdacd6ab | 458 | * @param array $options |
459 | * Options array (pre-processed to extract 'return' from params). | |
61fe4988 | 460 | * |
a6c01b45 | 461 | * @return array |
72b3a70c | 462 | * new activities list |
ab5fa8f2 | 463 | */ |
bc4b6f0f | 464 | function _civicrm_api3_activity_get_formatResult($params, $activities, $options) { |
30db5cbf CW |
465 | if (!$activities) { |
466 | return $activities; | |
467 | } | |
468 | ||
bc4b6f0f | 469 | $returns = $options['return']; |
6a488035 | 470 | foreach ($params as $n => $v) { |
cdacd6ab | 471 | // @todo - the per-parsing on options should have already done this. |
6a488035 TO |
472 | if (substr($n, 0, 7) == 'return.') { |
473 | $returnkey = substr($n, 7); | |
474 | $returns[$returnkey] = $v; | |
475 | } | |
476 | } | |
0298287b | 477 | |
cdacd6ab | 478 | _civicrm_api3_activity_fill_activity_contact_names($activities, $params, $returns); |
db6e8cb4 | 479 | |
cf8f0fff CW |
480 | $tagGet = ['tag_id', 'entity_id']; |
481 | $caseGet = $caseIds = []; | |
40875a91 CW |
482 | foreach (array_keys($returns) as $key) { |
483 | if (strpos($key, 'tag_id.') === 0) { | |
484 | $tagGet[] = $key; | |
485 | $returns['tag_id'] = 1; | |
486 | } | |
2c9e4446 CW |
487 | if (strpos($key, 'case_id.') === 0) { |
488 | $caseGet[] = str_replace('case_id.', '', $key); | |
489 | $returns['case_id'] = 1; | |
490 | } | |
40875a91 CW |
491 | } |
492 | ||
6a488035 TO |
493 | foreach ($returns as $n => $v) { |
494 | switch ($n) { | |
495 | case 'assignee_contact_id': | |
6a488035 | 496 | case 'target_contact_id': |
cdacd6ab | 497 | foreach ($activities as &$activity) { |
498 | if (!isset($activity[$n])) { | |
499 | $activity[$n] = []; | |
db6e8cb4 | 500 | } |
6a488035 | 501 | } |
35671d00 | 502 | |
42d30b83 | 503 | case 'source_contact_id': |
42d30b83 | 504 | break; |
35671d00 | 505 | |
30db5cbf | 506 | case 'tag_id': |
cf8f0fff | 507 | $tags = civicrm_api3('EntityTag', 'get', [ |
30db5cbf | 508 | 'entity_table' => 'civicrm_activity', |
cf8f0fff | 509 | 'entity_id' => ['IN' => array_keys($activities)], |
40875a91 | 510 | 'return' => $tagGet, |
cf8f0fff CW |
511 | 'options' => ['limit' => 0], |
512 | ]); | |
30db5cbf | 513 | foreach ($tags['values'] as $tag) { |
40875a91 CW |
514 | $key = (int) $tag['entity_id']; |
515 | unset($tag['entity_id'], $tag['id']); | |
516 | $activities[$key]['tag_id'][$tag['tag_id']] = $tag; | |
517 | } | |
518 | break; | |
519 | ||
520 | case 'file_id': | |
521 | $dao = CRM_Core_DAO::executeQuery("SELECT entity_id, file_id FROM civicrm_entity_file WHERE entity_table = 'civicrm_activity' AND entity_id IN (%1)", | |
cf8f0fff | 522 | [1 => [implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES]]); |
40875a91 CW |
523 | while ($dao->fetch()) { |
524 | $activities[$dao->entity_id]['file_id'][] = $dao->file_id; | |
30db5cbf CW |
525 | } |
526 | break; | |
527 | ||
bc4b6f0f CW |
528 | case 'case_id': |
529 | $dao = CRM_Core_DAO::executeQuery("SELECT activity_id, case_id FROM civicrm_case_activity WHERE activity_id IN (%1)", | |
cf8f0fff | 530 | [1 => [implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES]]); |
bc4b6f0f | 531 | while ($dao->fetch()) { |
18e0f096 | 532 | $activities[$dao->activity_id]['case_id'][] = $dao->case_id; |
2c9e4446 | 533 | $caseIds[$dao->case_id] = $dao->case_id; |
bc4b6f0f CW |
534 | } |
535 | break; | |
536 | ||
760ac501 CW |
537 | case 'is_overdue': |
538 | foreach ($activities as $key => $activityArray) { | |
539 | $activities[$key]['is_overdue'] = (int) CRM_Activity_BAO_Activity::isOverdue($activityArray); | |
540 | } | |
541 | break; | |
542 | ||
6a488035 TO |
543 | default: |
544 | if (substr($n, 0, 6) == 'custom') { | |
545 | $returnProperties[$n] = $v; | |
546 | } | |
2c9e4446 CW |
547 | } |
548 | } | |
549 | ||
550 | // Fetch case fields via the join syntax | |
551 | // Note this is limited to the first case if the activity belongs to more than one | |
552 | if ($caseGet && $caseIds) { | |
cf8f0fff CW |
553 | $cases = civicrm_api3('Case', 'get', [ |
554 | 'id' => ['IN' => $caseIds], | |
555 | 'options' => ['limit' => 0], | |
2c9e4446 CW |
556 | 'check_permissions' => !empty($params['check_permissions']), |
557 | 'return' => $caseGet, | |
cf8f0fff | 558 | ]); |
2c9e4446 CW |
559 | foreach ($activities as &$activity) { |
560 | if (!empty($activity['case_id'])) { | |
f748c073 | 561 | $case = $cases['values'][$activity['case_id'][0]] ?? NULL; |
2c9e4446 CW |
562 | if ($case) { |
563 | foreach ($case as $key => $value) { | |
564 | if ($key != 'id') { | |
565 | $activity['case_id.' . $key] = $value; | |
566 | } | |
567 | } | |
568 | } | |
569 | } | |
6a488035 TO |
570 | } |
571 | } | |
526e0834 | 572 | |
40875a91 | 573 | // Legacy extras |
526e0834 CW |
574 | if (!empty($params['contact_id'])) { |
575 | $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get'); | |
576 | $typeOptions = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate'); | |
577 | foreach ($activities as $key => &$activityArray) { | |
578 | if (!empty($activityArray['status_id'])) { | |
579 | $activityArray['status'] = $statusOptions[$activityArray['status_id']]; | |
580 | } | |
581 | if (!empty($activityArray['activity_type_id'])) { | |
582 | $activityArray['activity_name'] = $typeOptions[$activityArray['activity_type_id']]; | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
30db5cbf | 587 | if (!empty($returnProperties) || !empty($params['contact_id'])) { |
6a488035 | 588 | foreach ($activities as $activityId => $values) { |
10114f2d | 589 | //@todo - should possibly load activity type id if not loaded (update with id) |
e9ff5391 | 590 | _civicrm_api3_custom_data_get($activities[$activityId], CRM_Utils_Array::value('check_permissions', $params), 'Activity', $activityId, NULL, CRM_Utils_Array::value('activity_type_id', $values)); |
6a488035 TO |
591 | } |
592 | } | |
ab5fa8f2 | 593 | return $activities; |
6a488035 | 594 | } |
cdacd6ab | 595 | |
596 | /** | |
597 | * Append activity contact details to activity results. | |
598 | * | |
599 | * Adds id & name of activity contacts to results array if check_permissions | |
600 | * does not block access to them. | |
601 | * | |
602 | * For historical reasons source_contact_id is always added & is not an array. | |
603 | * The others are added depending on requested return params. | |
604 | * | |
605 | * @param array $activities | |
606 | * @param array $params | |
607 | * @param array $returns | |
608 | */ | |
609 | function _civicrm_api3_activity_fill_activity_contact_names(&$activities, $params, $returns) { | |
610 | $contactTypes = array_flip(CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate')); | |
611 | $assigneeType = $contactTypes['Activity Assignees']; | |
612 | $targetType = $contactTypes['Activity Targets']; | |
613 | $sourceType = $contactTypes['Activity Source']; | |
614 | $typeMap = [ | |
615 | $assigneeType => 'assignee', | |
616 | $sourceType => 'source', | |
7c31ae57 | 617 | $targetType => 'target', |
cdacd6ab | 618 | ]; |
619 | ||
620 | $activityContactTypes = [$sourceType]; | |
621 | ||
622 | if (!empty($returns['target_contact_name']) || !empty($returns['target_contact_id'])) { | |
623 | $activityContactTypes[] = $targetType; | |
624 | } | |
625 | if (!empty($returns['assignee_contact_name']) || (!empty($returns['assignee_contact_id']))) { | |
626 | $activityContactTypes[] = $assigneeType; | |
627 | } | |
628 | $activityContactParams = [ | |
629 | 'activity_id' => ['IN' => array_keys($activities)], | |
630 | 'return' => [ | |
631 | 'activity_id', | |
632 | 'record_type_id', | |
633 | 'contact_id.display_name', | |
c2ce41b6 | 634 | 'contact_id.sort_name', |
7c31ae57 | 635 | 'contact_id', |
cdacd6ab | 636 | ], |
c2ce41b6 | 637 | 'options' => ['limit' => 0], |
cdacd6ab | 638 | 'check_permissions' => !empty($params['check_permissions']), |
639 | ]; | |
640 | if (count($activityContactTypes) < 3) { | |
641 | $activityContactParams['record_type_id'] = ['IN' => $activityContactTypes]; | |
642 | } | |
643 | $activityContacts = civicrm_api3('ActivityContact', 'get', $activityContactParams)['values']; | |
644 | foreach ($activityContacts as $activityContact) { | |
645 | $contactID = $activityContact['contact_id']; | |
646 | $recordType = $typeMap[$activityContact['record_type_id']]; | |
647 | if (in_array($recordType, ['target', 'assignee'])) { | |
648 | $activities[$activityContact['activity_id']][$recordType . '_contact_id'][] = $contactID; | |
2e1f50d6 CW |
649 | $activities[$activityContact['activity_id']][$recordType . '_contact_name'][$contactID] = $activityContact['contact_id.display_name'] ?? ''; |
650 | $activities[$activityContact['activity_id']][$recordType . '_contact_sort_name'][$contactID] = $activityContact['contact_id.sort_name'] ?? ''; | |
cdacd6ab | 651 | } |
652 | else { | |
653 | $activities[$activityContact['activity_id']]['source_contact_id'] = $contactID; | |
2e1f50d6 CW |
654 | $activities[$activityContact['activity_id']]['source_contact_name'] = $activityContact['contact_id.display_name'] ?? ''; |
655 | $activities[$activityContact['activity_id']]['source_contact_sort_name'] = $activityContact['contact_id.sort_name'] ?? ''; | |
cdacd6ab | 656 | } |
657 | } | |
658 | } | |
6a488035 TO |
659 | |
660 | /** | |
661 | * Delete a specified Activity. | |
662 | * | |
cf470720 TO |
663 | * @param array $params |
664 | * Array holding 'id' of activity to be deleted. | |
6a488035 | 665 | * |
10114f2d | 666 | * @throws API_Exception |
6a488035 | 667 | * |
61fe4988 | 668 | * @return array |
00f8641b | 669 | * API result array |
6a488035 TO |
670 | */ |
671 | function civicrm_api3_activity_delete($params) { | |
672 | ||
673 | if (CRM_Activity_BAO_Activity::deleteActivity($params)) { | |
244bbdd8 | 674 | return civicrm_api3_create_success(1, $params, 'Activity', 'delete'); |
6a488035 TO |
675 | } |
676 | else { | |
df1dded9 | 677 | throw new API_Exception('Could not delete Activity: ' . (int) $params['id']); |
6a488035 TO |
678 | } |
679 | } | |
680 | ||
681 | /** | |
61fe4988 | 682 | * Check for required params. |
6a488035 | 683 | * |
cf470720 TO |
684 | * @param array $params |
685 | * Associated array of fields. | |
10114f2d EM |
686 | * |
687 | * @throws API_Exception | |
688 | * @throws Exception | |
a6c01b45 | 689 | * @return array |
72b3a70c | 690 | * array with errors |
6a488035 TO |
691 | */ |
692 | function _civicrm_api3_activity_check_params(&$params) { | |
cf8f0fff | 693 | $activityIds = [ |
6b409353 CW |
694 | 'activity' => $params['id'] ?? NULL, |
695 | 'parent' => $params['parent_id'] ?? NULL, | |
696 | 'original' => $params['original_id'] ?? NULL, | |
cf8f0fff | 697 | ]; |
6a488035 TO |
698 | |
699 | foreach ($activityIds as $id => $value) { | |
700 | if ($value && | |
701 | !CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id') | |
702 | ) { | |
10114f2d | 703 | throw new API_Exception('Invalid ' . ucfirst($id) . ' Id'); |
6a488035 TO |
704 | } |
705 | } | |
706 | // this should be handled by wrapper layer & probably the api would already manage it | |
707 | //correctly by doing pseudoconstant validation | |
708 | // needs testing | |
a60ed840 | 709 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate'); |
f748c073 | 710 | $activityName = $params['activity_name'] ?? NULL; |
7cdbcb16 | 711 | $activityName = ucfirst($activityName); |
f748c073 | 712 | $activityLabel = $params['activity_label'] ?? NULL; |
6a488035 | 713 | if ($activityLabel) { |
a60ed840 | 714 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'create'); |
6a488035 TO |
715 | } |
716 | ||
f748c073 | 717 | $activityTypeId = $params['activity_type_id'] ?? NULL; |
6a488035 TO |
718 | |
719 | if ($activityName || $activityLabel) { | |
720 | $activityTypeIdInList = array_search(($activityName ? $activityName : $activityLabel), $activityTypes); | |
721 | ||
722 | if (!$activityTypeIdInList) { | |
35671d00 | 723 | $errorString = $activityName ? "Invalid Activity Name : $activityName" : "Invalid Activity Type Label"; |
6a488035 TO |
724 | throw new Exception($errorString); |
725 | } | |
726 | elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) { | |
10114f2d | 727 | throw new API_Exception('Mismatch in Activity'); |
6a488035 TO |
728 | } |
729 | $params['activity_type_id'] = $activityTypeIdInList; | |
730 | } | |
731 | elseif ($activityTypeId && | |
732 | !array_key_exists($activityTypeId, $activityTypes) | |
733 | ) { | |
10114f2d | 734 | throw new API_Exception('Invalid Activity Type ID'); |
6a488035 TO |
735 | } |
736 | ||
6a488035 TO |
737 | // check for activity duration minutes |
738 | // this should be validated @ the wrapper layer not here | |
739 | // needs testing | |
740 | if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) { | |
10114f2d | 741 | throw new API_Exception('Invalid Activity Duration (in minutes)'); |
6a488035 TO |
742 | } |
743 | ||
6a488035 TO |
744 | return NULL; |
745 | } | |
746 | ||
dabf9814 | 747 | /** |
61fe4988 EM |
748 | * Get parameters for activity list. |
749 | * | |
7cdbcb16 | 750 | * @see _civicrm_api3_generic_getlist_params |
dabf9814 | 751 | * |
7cdbcb16 TO |
752 | * @param array $request |
753 | * API request. | |
dabf9814 CW |
754 | */ |
755 | function _civicrm_api3_activity_getlist_params(&$request) { | |
cf8f0fff | 756 | $fieldsToReturn = [ |
7cdbcb16 TO |
757 | 'activity_date_time', |
758 | 'activity_type_id', | |
759 | 'subject', | |
760 | 'source_contact_id', | |
cf8f0fff | 761 | ]; |
dabf9814 CW |
762 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
763 | $request['params']['options']['sort'] = 'activity_date_time DESC'; | |
cf8f0fff | 764 | $request['params'] += [ |
dabf9814 CW |
765 | 'is_current_revision' => 1, |
766 | 'is_deleted' => 0, | |
cf8f0fff | 767 | ]; |
dabf9814 CW |
768 | } |
769 | ||
770 | /** | |
61fe4988 EM |
771 | * Get output for activity list. |
772 | * | |
dabf9814 CW |
773 | * @see _civicrm_api3_generic_getlist_output |
774 | * | |
8c6b335b CW |
775 | * @param array $result |
776 | * @param array $request | |
dabf9814 CW |
777 | * |
778 | * @return array | |
779 | */ | |
780 | function _civicrm_api3_activity_getlist_output($result, $request) { | |
cf8f0fff | 781 | $output = []; |
dabf9814 CW |
782 | if (!empty($result['values'])) { |
783 | foreach ($result['values'] as $row) { | |
cf8f0fff | 784 | $data = [ |
dabf9814 CW |
785 | 'id' => $row[$request['id_field']], |
786 | 'label' => $row[$request['label_field']] ? $row[$request['label_field']] : ts('(no subject)'), | |
cf8f0fff | 787 | 'description' => [ |
4a413eb6 | 788 | CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id']), |
cf8f0fff CW |
789 | ], |
790 | ]; | |
dabf9814 CW |
791 | if (!empty($row['activity_date_time'])) { |
792 | $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['activity_date_time']); | |
793 | } | |
794 | if (!empty($row['source_contact_id'])) { | |
cf8f0fff | 795 | $data['description'][] = ts('By %1', [ |
7cdbcb16 | 796 | 1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name'), |
cf8f0fff | 797 | ]); |
dabf9814 | 798 | } |
8a938c69 CW |
799 | // Add repeating info |
800 | $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_activity'); | |
801 | $data['extra']['is_recur'] = FALSE; | |
802 | if ($repeat) { | |
cf8f0fff | 803 | $data['suffix'] = ts('(%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]); |
8a938c69 CW |
804 | $data['extra']['is_recur'] = TRUE; |
805 | } | |
dabf9814 CW |
806 | $output[] = $data; |
807 | } | |
808 | } | |
809 | return $output; | |
810 | } |