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