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) { | |
162 | // If this is a brand new case activity we need to add this | |
163 | $caseActivityParams = array('activity_id' => $activityBAO->id, 'case_id' => $case_id); | |
164 | CRM_Case_BAO_Case::processCaseActivity($caseActivityParams); | |
165 | } | |
166 | ||
167 | _civicrm_api3_object_to_array($activityBAO, $activityArray[$activityBAO->id]); | |
244bbdd8 | 168 | return civicrm_api3_create_success($activityArray, $params, 'Activity', 'get', $activityBAO); |
6a488035 TO |
169 | } |
170 | } | |
11e09c59 TO |
171 | |
172 | /** | |
61fe4988 EM |
173 | * Specify Meta data for create. |
174 | * | |
175 | * Note that this data is retrievable via the getfields function and is used for pre-filling defaults and | |
176 | * ensuring mandatory requirements are met. | |
177 | * | |
cf470720 | 178 | * @param array $params |
c28e1768 | 179 | * Array of parameters determined by getfields. |
6a488035 TO |
180 | */ |
181 | function _civicrm_api3_activity_create_spec(&$params) { | |
182 | ||
61fe4988 | 183 | // Default for source_contact_id = currently logged in user. |
6a488035 TO |
184 | $params['source_contact_id']['api.default'] = 'user_contact_id'; |
185 | ||
67744c4e CW |
186 | $params['status_id']['api.aliases'] = array('activity_status'); |
187 | ||
6a488035 TO |
188 | $params['assignee_contact_id'] = array( |
189 | 'name' => 'assignee_id', | |
0a24dc9a CW |
190 | 'title' => 'Activity Assignee', |
191 | 'description' => 'Contact(s) assigned to this activity.', | |
6a488035 | 192 | 'type' => 1, |
7e61908b | 193 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
0a24dc9a | 194 | 'FKApiName' => 'Contact', |
6a488035 TO |
195 | ); |
196 | $params['target_contact_id'] = array( | |
197 | 'name' => 'target_id', | |
198 | 'title' => 'Activity Target', | |
0a24dc9a | 199 | 'description' => 'Contact(s) participating in this activity.', |
6a488035 | 200 | 'type' => 1, |
7e61908b | 201 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
0a24dc9a | 202 | 'FKApiName' => 'Contact', |
6a488035 | 203 | ); |
2f3d72cf | 204 | |
205 | $params['source_contact_id'] = array( | |
7cdbcb16 TO |
206 | 'name' => 'source_contact_id', |
207 | 'title' => 'Activity Source Contact', | |
0a24dc9a | 208 | 'description' => 'Person who created this activity. Defaults to current user.', |
7cdbcb16 | 209 | 'type' => 1, |
7e61908b | 210 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
7cdbcb16 | 211 | 'api.default' => 'user_contact_id', |
0a24dc9a CW |
212 | 'FKApiName' => 'Contact', |
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.', | |
235 | 'type' => 1, | |
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.', | |
243 | 'type' => 1, | |
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.', | |
250 | 'type' => 1, | |
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).', | |
257 | 'type' => 1, | |
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.', | |
264 | 'type' => 1, | |
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.', | |
271 | 'type' => 1, | |
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.', | |
278 | 'type' => 1, | |
279 | 'FKClassName' => 'CRM_Contact_DAO_Contact', | |
280 | 'FKApiName' => 'Contact', | |
281 | ); | |
282 | } | |
283 | ||
6a488035 | 284 | /** |
61fe4988 | 285 | * Gets a CiviCRM activity according to parameters. |
6a488035 | 286 | * |
cf470720 | 287 | * @param array $params |
61fe4988 | 288 | * Array per getfields documentation. |
6a488035 | 289 | * |
bbd2743b | 290 | * @return array API result array |
00f8641b | 291 | * API result array |
bbd2743b | 292 | * |
293 | * @throws \API_Exception | |
294 | * @throws \CiviCRM_API3_Exception | |
295 | * @throws \Civi\API\Exception\UnauthorizedException | |
6a488035 TO |
296 | */ |
297 | function civicrm_api3_activity_get($params) { | |
bbd2743b | 298 | if (!empty($params['check_permissions']) && !CRM_Core_Permission::check('view all activities')) { |
299 | // In absence of view all activities permission it's possible to see a specific activity by ACL. | |
300 | // Note still allowing view all activities to override ACLs is based on the 'don't change too much | |
301 | // if you are not sure principle' and it could be argued that the ACLs should always be applied. | |
302 | if (empty($params['id']) || !empty($params['contact_id'])) { | |
303 | // We fall back to the original blunt permissions if we don't have an id to check or we are about | |
304 | // to go to the weird place that the legacy 'contact_id' parameter takes us to. | |
305 | throw new \Civi\API\Exception\UnauthorizedException( | |
306 | "Cannot access activities. Required permission: 'view all activities''" | |
307 | ); | |
308 | } | |
309 | ||
310 | if (!CRM_Activity_BAO_Activity::checkPermission($params['id'], CRM_Core_Action::VIEW)) { | |
311 | throw new \Civi\API\Exception\UnauthorizedException( | |
312 | 'You do not have permission to view this activity' | |
313 | ); | |
314 | } | |
315 | } | |
316 | ||
526e0834 | 317 | $sql = CRM_Utils_SQL_Select::fragment(); |
bc4b6f0f CW |
318 | $recordTypes = civicrm_api3('ActivityContact', 'getoptions', array('field' => 'record_type_id')); |
319 | $recordTypes = $recordTypes['values']; | |
526e0834 CW |
320 | $activityContactOptions = array( |
321 | 'contact_id' => NULL, | |
bc4b6f0f CW |
322 | 'target_contact_id' => array_search('Activity Targets', $recordTypes), |
323 | 'source_contact_id' => array_search('Activity Source', $recordTypes), | |
324 | 'assignee_contact_id' => array_search('Activity Assignees', $recordTypes), | |
526e0834 CW |
325 | ); |
326 | foreach ($activityContactOptions as $activityContactName => $activityContactValue) { | |
327 | if (!empty($params[$activityContactName])) { | |
526e0834 CW |
328 | if (!is_array($params[$activityContactName])) { |
329 | $params[$activityContactName] = array('=' => $params[$activityContactName]); | |
330 | } | |
331 | $clause = \CRM_Core_DAO::createSQLFilter('contact_id', $params[$activityContactName]); | |
332 | $typeClause = $activityContactValue ? 'record_type_id = #typeId AND ' : ''; | |
333 | $sql->where("a.id IN (SELECT activity_id FROM civicrm_activity_contact WHERE $typeClause !clause)", | |
334 | array('#typeId' => $activityContactValue, '!clause' => $clause) | |
335 | ); | |
6a488035 TO |
336 | } |
337 | } | |
526e0834 | 338 | if (!empty($params['tag_id'])) { |
526e0834 CW |
339 | if (!is_array($params['tag_id'])) { |
340 | $params['tag_id'] = array('=' => $params['tag_id']); | |
1196e086 | 341 | } |
526e0834 CW |
342 | $clause = \CRM_Core_DAO::createSQLFilter('tag_id', $params['tag_id']); |
343 | if ($clause) { | |
344 | $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_activity" AND !clause)', array('!clause' => $clause)); | |
1196e086 | 345 | } |
526e0834 | 346 | } |
40875a91 | 347 | if (!empty($params['file_id'])) { |
40875a91 CW |
348 | if (!is_array($params['file_id'])) { |
349 | $params['file_id'] = array('=' => $params['file_id']); | |
350 | } | |
351 | $clause = \CRM_Core_DAO::createSQLFilter('file_id', $params['file_id']); | |
352 | if ($clause) { | |
353 | $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_file WHERE entity_table = "civicrm_activity" AND !clause)', array('!clause' => $clause)); | |
354 | } | |
355 | } | |
526e0834 | 356 | if (!empty($params['case_id'])) { |
526e0834 CW |
357 | if (!is_array($params['case_id'])) { |
358 | $params['case_id'] = array('=' => $params['case_id']); | |
359 | } | |
360 | $clause = \CRM_Core_DAO::createSQLFilter('case_id', $params['case_id']); | |
361 | if ($clause) { | |
362 | $sql->where('a.id IN (SELECT activity_id FROM civicrm_case_activity WHERE !clause)', array('!clause' => $clause)); | |
0298287b | 363 | } |
6a488035 | 364 | } |
526e0834 | 365 | $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $sql); |
244bbdd8 | 366 | $options = _civicrm_api3_get_options_from_params($params, FALSE, 'Activity', 'get'); |
22e263ad | 367 | if ($options['is_count']) { |
244bbdd8 | 368 | return civicrm_api3_create_success($activities, $params, 'Activity', 'get'); |
e2e3c1ce | 369 | } |
6a488035 | 370 | |
bc4b6f0f | 371 | $activities = _civicrm_api3_activity_get_formatResult($params, $activities, $options); |
ab5fa8f2 | 372 | //legacy custom data get - so previous formatted response is still returned too |
244bbdd8 | 373 | return civicrm_api3_create_success($activities, $params, 'Activity', 'get'); |
ab5fa8f2 TO |
374 | } |
375 | ||
376 | /** | |
61fe4988 | 377 | * Given a list of activities, append any extra data requested about the activities. |
ab5fa8f2 | 378 | * |
b081365f | 379 | * @note Called by civicrm-core and CiviHR |
ab5fa8f2 | 380 | * |
cf470720 TO |
381 | * @param array $params |
382 | * API request parameters. | |
ab5fa8f2 | 383 | * @param array $activities |
61fe4988 | 384 | * |
a6c01b45 | 385 | * @return array |
72b3a70c | 386 | * new activities list |
ab5fa8f2 | 387 | */ |
bc4b6f0f | 388 | function _civicrm_api3_activity_get_formatResult($params, $activities, $options) { |
30db5cbf CW |
389 | if (!$activities) { |
390 | return $activities; | |
391 | } | |
392 | ||
bc4b6f0f | 393 | $returns = $options['return']; |
6a488035 TO |
394 | foreach ($params as $n => $v) { |
395 | if (substr($n, 0, 7) == 'return.') { | |
396 | $returnkey = substr($n, 7); | |
397 | $returns[$returnkey] = $v; | |
398 | } | |
399 | } | |
0298287b | 400 | |
2f3d72cf | 401 | $returns['source_contact_id'] = 1; |
db6e8cb4 CW |
402 | if (!empty($returns['target_contact_name'])) { |
403 | $returns['target_contact_id'] = 1; | |
404 | } | |
405 | if (!empty($returns['assignee_contact_name'])) { | |
406 | $returns['assignee_contact_id'] = 1; | |
407 | } | |
408 | ||
40875a91 CW |
409 | $tagGet = array('tag_id', 'entity_id'); |
410 | foreach (array_keys($returns) as $key) { | |
411 | if (strpos($key, 'tag_id.') === 0) { | |
412 | $tagGet[] = $key; | |
413 | $returns['tag_id'] = 1; | |
414 | } | |
415 | } | |
416 | ||
6a488035 TO |
417 | foreach ($returns as $n => $v) { |
418 | switch ($n) { | |
419 | case 'assignee_contact_id': | |
420 | foreach ($activities as $key => $activityArray) { | |
db6e8cb4 CW |
421 | $cids = $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']); |
422 | if ($cids && !empty($returns['assignee_contact_name'])) { | |
423 | foreach ($cids as $cid) { | |
424 | $activities[$key]['assignee_contact_name'][$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name'); | |
425 | } | |
426 | } | |
6a488035 TO |
427 | } |
428 | break; | |
35671d00 | 429 | |
6a488035 TO |
430 | case 'target_contact_id': |
431 | foreach ($activities as $key => $activityArray) { | |
db6e8cb4 CW |
432 | $cids = $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']); |
433 | if ($cids && !empty($returns['target_contact_name'])) { | |
434 | foreach ($cids as $cid) { | |
435 | $activities[$key]['target_contact_name'][$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name'); | |
436 | } | |
437 | } | |
6a488035 TO |
438 | } |
439 | break; | |
35671d00 | 440 | |
42d30b83 DL |
441 | case 'source_contact_id': |
442 | foreach ($activities as $key => $activityArray) { | |
db6e8cb4 CW |
443 | $cid = $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity::getSourceContactID($activityArray['id']); |
444 | if ($cid && !empty($returns['source_contact_name'])) { | |
445 | $activities[$key]['source_contact_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name'); | |
446 | } | |
42d30b83 DL |
447 | } |
448 | break; | |
35671d00 | 449 | |
30db5cbf CW |
450 | case 'tag_id': |
451 | $tags = civicrm_api3('EntityTag', 'get', array( | |
452 | 'entity_table' => 'civicrm_activity', | |
453 | 'entity_id' => array('IN' => array_keys($activities)), | |
40875a91 | 454 | 'return' => $tagGet, |
30db5cbf CW |
455 | 'options' => array('limit' => 0), |
456 | )); | |
457 | foreach ($tags['values'] as $tag) { | |
40875a91 CW |
458 | $key = (int) $tag['entity_id']; |
459 | unset($tag['entity_id'], $tag['id']); | |
460 | $activities[$key]['tag_id'][$tag['tag_id']] = $tag; | |
461 | } | |
462 | break; | |
463 | ||
464 | case 'file_id': | |
465 | $dao = CRM_Core_DAO::executeQuery("SELECT entity_id, file_id FROM civicrm_entity_file WHERE entity_table = 'civicrm_activity' AND entity_id IN (%1)", | |
466 | array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES))); | |
467 | while ($dao->fetch()) { | |
468 | $activities[$dao->entity_id]['file_id'][] = $dao->file_id; | |
30db5cbf CW |
469 | } |
470 | break; | |
471 | ||
bc4b6f0f CW |
472 | case 'case_id': |
473 | $dao = CRM_Core_DAO::executeQuery("SELECT activity_id, case_id FROM civicrm_case_activity WHERE activity_id IN (%1)", | |
474 | array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES))); | |
475 | while ($dao->fetch()) { | |
476 | $activities[$dao->activity_id]['case_id'] = $dao->case_id; | |
477 | } | |
478 | break; | |
479 | ||
6a488035 TO |
480 | default: |
481 | if (substr($n, 0, 6) == 'custom') { | |
482 | $returnProperties[$n] = $v; | |
483 | } | |
484 | } | |
485 | } | |
526e0834 | 486 | |
40875a91 | 487 | // Legacy extras |
526e0834 CW |
488 | if (!empty($params['contact_id'])) { |
489 | $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get'); | |
490 | $typeOptions = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate'); | |
491 | foreach ($activities as $key => &$activityArray) { | |
492 | if (!empty($activityArray['status_id'])) { | |
493 | $activityArray['status'] = $statusOptions[$activityArray['status_id']]; | |
494 | } | |
495 | if (!empty($activityArray['activity_type_id'])) { | |
496 | $activityArray['activity_name'] = $typeOptions[$activityArray['activity_type_id']]; | |
497 | } | |
498 | } | |
499 | } | |
500 | ||
30db5cbf | 501 | if (!empty($returnProperties) || !empty($params['contact_id'])) { |
6a488035 | 502 | foreach ($activities as $activityId => $values) { |
10114f2d | 503 | //@todo - should possibly load activity type id if not loaded (update with id) |
e9ff5391 | 504 | _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 |
505 | } |
506 | } | |
ab5fa8f2 | 507 | return $activities; |
6a488035 TO |
508 | } |
509 | ||
2f3d72cf | 510 | |
6a488035 TO |
511 | /** |
512 | * Delete a specified Activity. | |
513 | * | |
cf470720 TO |
514 | * @param array $params |
515 | * Array holding 'id' of activity to be deleted. | |
6a488035 | 516 | * |
10114f2d | 517 | * @throws API_Exception |
6a488035 | 518 | * |
61fe4988 | 519 | * @return array |
00f8641b | 520 | * API result array |
6a488035 TO |
521 | */ |
522 | function civicrm_api3_activity_delete($params) { | |
523 | ||
524 | if (CRM_Activity_BAO_Activity::deleteActivity($params)) { | |
244bbdd8 | 525 | return civicrm_api3_create_success(1, $params, 'Activity', 'delete'); |
6a488035 TO |
526 | } |
527 | else { | |
244bbdd8 | 528 | throw new API_Exception('Could not delete Activity'); |
6a488035 TO |
529 | } |
530 | } | |
531 | ||
532 | /** | |
61fe4988 | 533 | * Check for required params. |
6a488035 | 534 | * |
cf470720 TO |
535 | * @param array $params |
536 | * Associated array of fields. | |
10114f2d EM |
537 | * |
538 | * @throws API_Exception | |
539 | * @throws Exception | |
a6c01b45 | 540 | * @return array |
72b3a70c | 541 | * array with errors |
6a488035 TO |
542 | */ |
543 | function _civicrm_api3_activity_check_params(&$params) { | |
544 | ||
545 | $contactIDFields = array_intersect_key($params, | |
7cdbcb16 TO |
546 | array( |
547 | 'source_contact_id' => 1, | |
548 | 'assignee_contact_id' => 1, | |
549 | 'target_contact_id' => 1, | |
550 | ) | |
6a488035 | 551 | ); |
42d30b83 DL |
552 | |
553 | // this should be handled by wrapper layer & probably the api would already manage it | |
554 | //correctly by doing post validation - ie. a failure should result in a roll-back = an error | |
555 | // needs testing | |
6a488035 TO |
556 | if (!empty($contactIDFields)) { |
557 | $contactIds = array(); | |
558 | foreach ($contactIDFields as $fieldname => $contactfield) { | |
559 | if (empty($contactfield)) { | |
560 | continue; | |
561 | } | |
562 | if (is_array($contactfield)) { | |
563 | foreach ($contactfield as $contactkey => $contactvalue) { | |
564 | $contactIds[$contactvalue] = $contactvalue; | |
565 | } | |
566 | } | |
567 | else { | |
568 | $contactIds[$contactfield] = $contactfield; | |
569 | } | |
570 | } | |
571 | ||
6a488035 TO |
572 | $sql = ' |
573 | SELECT count(*) | |
574 | FROM civicrm_contact | |
575 | WHERE id IN (' . implode(', ', $contactIds) . ' )'; | |
576 | if (count($contactIds) != CRM_Core_DAO::singleValueQuery($sql)) { | |
61fe4988 | 577 | throw new API_Exception('Invalid Contact Id'); |
6a488035 TO |
578 | } |
579 | } | |
580 | ||
35671d00 | 581 | $activityIds = array( |
7cdbcb16 TO |
582 | 'activity' => CRM_Utils_Array::value('id', $params), |
583 | 'parent' => CRM_Utils_Array::value('parent_id', $params), | |
584 | 'original' => CRM_Utils_Array::value('original_id', $params), | |
6a488035 TO |
585 | ); |
586 | ||
587 | foreach ($activityIds as $id => $value) { | |
588 | if ($value && | |
589 | !CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id') | |
590 | ) { | |
10114f2d | 591 | throw new API_Exception('Invalid ' . ucfirst($id) . ' Id'); |
6a488035 TO |
592 | } |
593 | } | |
594 | // this should be handled by wrapper layer & probably the api would already manage it | |
595 | //correctly by doing pseudoconstant validation | |
596 | // needs testing | |
a60ed840 | 597 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate'); |
7cdbcb16 TO |
598 | $activityName = CRM_Utils_Array::value('activity_name', $params); |
599 | $activityName = ucfirst($activityName); | |
6a488035 TO |
600 | $activityLabel = CRM_Utils_Array::value('activity_label', $params); |
601 | if ($activityLabel) { | |
a60ed840 | 602 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'create'); |
6a488035 TO |
603 | } |
604 | ||
605 | $activityTypeId = CRM_Utils_Array::value('activity_type_id', $params); | |
606 | ||
607 | if ($activityName || $activityLabel) { | |
608 | $activityTypeIdInList = array_search(($activityName ? $activityName : $activityLabel), $activityTypes); | |
609 | ||
610 | if (!$activityTypeIdInList) { | |
35671d00 | 611 | $errorString = $activityName ? "Invalid Activity Name : $activityName" : "Invalid Activity Type Label"; |
6a488035 TO |
612 | throw new Exception($errorString); |
613 | } | |
614 | elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) { | |
10114f2d | 615 | throw new API_Exception('Mismatch in Activity'); |
6a488035 TO |
616 | } |
617 | $params['activity_type_id'] = $activityTypeIdInList; | |
618 | } | |
619 | elseif ($activityTypeId && | |
620 | !array_key_exists($activityTypeId, $activityTypes) | |
621 | ) { | |
10114f2d | 622 | throw new API_Exception('Invalid Activity Type ID'); |
6a488035 TO |
623 | } |
624 | ||
6a488035 TO |
625 | // check for activity duration minutes |
626 | // this should be validated @ the wrapper layer not here | |
627 | // needs testing | |
628 | if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) { | |
10114f2d | 629 | throw new API_Exception('Invalid Activity Duration (in minutes)'); |
6a488035 TO |
630 | } |
631 | ||
6a488035 TO |
632 | //if adding a new activity & date_time not set make it now |
633 | // this should be managed by the wrapper layer & setting ['api.default'] in speces | |
634 | // needs testing | |
8cc574cf | 635 | if (empty($params['id']) && empty($params['activity_date_time'])) { |
6a488035 TO |
636 | $params['activity_date_time'] = CRM_Utils_Date::processDate(date('Y-m-d H:i:s')); |
637 | } | |
638 | ||
639 | return NULL; | |
640 | } | |
641 | ||
dabf9814 | 642 | /** |
61fe4988 EM |
643 | * Get parameters for activity list. |
644 | * | |
7cdbcb16 | 645 | * @see _civicrm_api3_generic_getlist_params |
dabf9814 | 646 | * |
7cdbcb16 TO |
647 | * @param array $request |
648 | * API request. | |
dabf9814 CW |
649 | */ |
650 | function _civicrm_api3_activity_getlist_params(&$request) { | |
7cdbcb16 TO |
651 | $fieldsToReturn = array( |
652 | 'activity_date_time', | |
653 | 'activity_type_id', | |
654 | 'subject', | |
655 | 'source_contact_id', | |
656 | ); | |
dabf9814 CW |
657 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
658 | $request['params']['options']['sort'] = 'activity_date_time DESC'; | |
659 | $request['params'] += array( | |
660 | 'is_current_revision' => 1, | |
661 | 'is_deleted' => 0, | |
662 | ); | |
663 | } | |
664 | ||
665 | /** | |
61fe4988 EM |
666 | * Get output for activity list. |
667 | * | |
dabf9814 CW |
668 | * @see _civicrm_api3_generic_getlist_output |
669 | * | |
8c6b335b CW |
670 | * @param array $result |
671 | * @param array $request | |
dabf9814 CW |
672 | * |
673 | * @return array | |
674 | */ | |
675 | function _civicrm_api3_activity_getlist_output($result, $request) { | |
676 | $output = array(); | |
677 | if (!empty($result['values'])) { | |
678 | foreach ($result['values'] as $row) { | |
679 | $data = array( | |
680 | 'id' => $row[$request['id_field']], | |
681 | 'label' => $row[$request['label_field']] ? $row[$request['label_field']] : ts('(no subject)'), | |
7cdbcb16 TO |
682 | 'description' => array( |
683 | CRM_Core_Pseudoconstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id']), | |
684 | ), | |
dabf9814 CW |
685 | ); |
686 | if (!empty($row['activity_date_time'])) { | |
687 | $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['activity_date_time']); | |
688 | } | |
689 | if (!empty($row['source_contact_id'])) { | |
7cdbcb16 TO |
690 | $data['description'][] = ts('By %1', array( |
691 | 1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name'), | |
692 | )); | |
dabf9814 | 693 | } |
8a938c69 CW |
694 | // Add repeating info |
695 | $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_activity'); | |
696 | $data['extra']['is_recur'] = FALSE; | |
697 | if ($repeat) { | |
698 | $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1])); | |
699 | $data['extra']['is_recur'] = TRUE; | |
700 | } | |
dabf9814 CW |
701 | $output[] = $data; |
702 | } | |
703 | } | |
704 | return $output; | |
705 | } |