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