Merge pull request #9655 from eileenmcnaughton/activities
[civicrm-core.git] / api / v3 / Activity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 /**
29 * This api exposes CiviCRM Activity records.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34
35 /**
36 * Creates or updates an Activity.
37 *
38 * @param array $params
39 * Array per getfields documentation.
40 *
41 * @throws API_Exception
42 * @return array
43 * API result array
44 */
45 function civicrm_api3_activity_create($params) {
46
47 if (empty($params['id'])) {
48 // an update does not require any mandatory parameters
49 civicrm_api3_verify_one_mandatory($params,
50 NULL,
51 array(
52 'activity_name',
53 'activity_type_id',
54 'activity_label',
55 )
56 );
57 }
58
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
70 // processing for custom data
71 $values = $activityArray = array();
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
85 $case_id = '';
86 $createRevision = FALSE;
87 $oldActivityValues = array();
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 }
92 if (!empty($params['case_id'])) {
93 $case_id = $params['case_id'];
94 if (!empty($params['id'])) {
95 $oldActivityParams = array('id' => $params['id']);
96 if (!$oldActivityValues) {
97 CRM_Activity_BAO_Activity::retrieve($oldActivityParams, $oldActivityValues);
98 }
99 if (empty($oldActivityValues)) {
100 throw new API_Exception(ts("Unable to locate existing activity."));
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()) {
107 if (is_object($activityDAO)) {
108 $activityDAO->free();
109 }
110 throw new API_Exception(ts("Unable to revision existing case activity."));
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 {
153 throw new API_Exception(ts("Unable to create new revision of case activity."));
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]);
168 return civicrm_api3_create_success($activityArray, $params, 'Activity', 'get', $activityBAO);
169 }
170 }
171
172 /**
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 *
178 * @param array $params
179 * Array of parameters determined by getfields.
180 */
181 function _civicrm_api3_activity_create_spec(&$params) {
182
183 // Default for source_contact_id = currently logged in user.
184 $params['source_contact_id']['api.default'] = 'user_contact_id';
185
186 $params['status_id']['api.aliases'] = array('activity_status');
187
188 $params['assignee_contact_id'] = array(
189 'name' => 'assignee_id',
190 'title' => 'Activity Assignee',
191 'description' => 'Contact(s) assigned to this activity.',
192 'type' => 1,
193 'FKClassName' => 'CRM_Contact_DAO_Contact',
194 'FKApiName' => 'Contact',
195 );
196 $params['target_contact_id'] = array(
197 'name' => 'target_id',
198 'title' => 'Activity Target',
199 'description' => 'Contact(s) participating in this activity.',
200 'type' => 1,
201 'FKClassName' => 'CRM_Contact_DAO_Contact',
202 'FKApiName' => 'Contact',
203 );
204
205 $params['source_contact_id'] = array(
206 'name' => 'source_contact_id',
207 'title' => 'Activity Source Contact',
208 'description' => 'Person who created this activity. Defaults to current user.',
209 'type' => 1,
210 'FKClassName' => 'CRM_Contact_DAO_Contact',
211 'api.default' => 'user_contact_id',
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',
222 );
223
224 }
225
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(
233 'title' => 'Tags',
234 'description' => 'Find activities with specified tags.',
235 'type' => 1,
236 'FKClassName' => 'CRM_Core_DAO_Tag',
237 'FKApiName' => 'Tag',
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',
246 );
247 $params['case_id'] = array(
248 'title' => 'Cases',
249 'description' => 'Find activities within specified cases.',
250 'type' => 1,
251 'FKClassName' => 'CRM_Case_DAO_Case',
252 'FKApiName' => 'Case',
253 );
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 );
261 $params['target_contact_id'] = array(
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(
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(
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
284 /**
285 * Gets a CiviCRM activity according to parameters.
286 *
287 * @param array $params
288 * Array per getfields documentation.
289 *
290 * @return array API result array
291 * API result array
292 *
293 * @throws \API_Exception
294 * @throws \CiviCRM_API3_Exception
295 * @throws \Civi\API\Exception\UnauthorizedException
296 */
297 function civicrm_api3_activity_get($params) {
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
317 $sql = CRM_Utils_SQL_Select::fragment();
318 // Support search by activity_contact
319 $extraFieldSpecs = array();
320 _civicrm_api3_activity_create_spec($extraFieldSpecs);
321 $options = civicrm_api3('ActivityContact', 'getoptions', array('field' => 'record_type_id'));
322 $options = $options['values'];
323 $activityContactOptions = array(
324 'contact_id' => NULL,
325 'target_contact_id' => array_search('Activity Targets', $options),
326 'source_contact_id' => array_search('Activity Source', $options),
327 'assignee_contact_id' => array_search('Activity Assignees', $options),
328 );
329 foreach ($activityContactOptions as $activityContactName => $activityContactValue) {
330 if (!empty($params[$activityContactName])) {
331 _civicrm_api3_validate_integer($params, $activityContactName, $extraFieldSpecs[$activityContactName], 'Activity');
332 if (!is_array($params[$activityContactName])) {
333 $params[$activityContactName] = array('=' => $params[$activityContactName]);
334 }
335 $clause = \CRM_Core_DAO::createSQLFilter('contact_id', $params[$activityContactName]);
336 $typeClause = $activityContactValue ? 'record_type_id = #typeId AND ' : '';
337 $sql->where("a.id IN (SELECT activity_id FROM civicrm_activity_contact WHERE $typeClause !clause)",
338 array('#typeId' => $activityContactValue, '!clause' => $clause)
339 );
340 }
341 }
342 if (!empty($params['tag_id'])) {
343 _civicrm_api3_validate_integer($params, 'tag_id', $extraFieldSpecs['tag_id'], 'Activity');
344 if (!is_array($params['tag_id'])) {
345 $params['tag_id'] = array('=' => $params['tag_id']);
346 }
347 $clause = \CRM_Core_DAO::createSQLFilter('tag_id', $params['tag_id']);
348 if ($clause) {
349 $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_activity" AND !clause)', array('!clause' => $clause));
350 }
351 }
352 if (!empty($params['file_id'])) {
353 _civicrm_api3_validate_integer($params, 'file_id', $extraFieldSpecs['file_id'], 'Activity');
354 if (!is_array($params['file_id'])) {
355 $params['file_id'] = array('=' => $params['file_id']);
356 }
357 $clause = \CRM_Core_DAO::createSQLFilter('file_id', $params['file_id']);
358 if ($clause) {
359 $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_file WHERE entity_table = "civicrm_activity" AND !clause)', array('!clause' => $clause));
360 }
361 }
362 if (!empty($params['case_id'])) {
363 _civicrm_api3_validate_integer($params, 'case_id', $extraFieldSpecs['case_id'], 'Activity');
364 if (!is_array($params['case_id'])) {
365 $params['case_id'] = array('=' => $params['case_id']);
366 }
367 $clause = \CRM_Core_DAO::createSQLFilter('case_id', $params['case_id']);
368 if ($clause) {
369 $sql->where('a.id IN (SELECT activity_id FROM civicrm_case_activity WHERE !clause)', array('!clause' => $clause));
370 }
371 }
372 $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $sql);
373 $options = _civicrm_api3_get_options_from_params($params, FALSE, 'Activity', 'get');
374 if ($options['is_count']) {
375 return civicrm_api3_create_success($activities, $params, 'Activity', 'get');
376 }
377
378 $activities = _civicrm_api3_activity_get_formatResult($params, $activities);
379 //legacy custom data get - so previous formatted response is still returned too
380 return civicrm_api3_create_success($activities, $params, 'Activity', 'get');
381 }
382
383 /**
384 * Given a list of activities, append any extra data requested about the activities.
385 *
386 * @note Called by civicrm-core and CiviHR
387 *
388 * @param array $params
389 * API request parameters.
390 * @param array $activities
391 *
392 * @return array
393 * new activities list
394 */
395 function _civicrm_api3_activity_get_formatResult($params, $activities) {
396 if (!$activities) {
397 return $activities;
398 }
399
400 $returns = CRM_Utils_Array::value('return', $params, array());
401 if (!is_array($returns)) {
402 $returns = str_replace(' ', '', $returns);
403 $returns = explode(',', $returns);
404 }
405 $returns = array_fill_keys($returns, 1);
406
407 foreach ($params as $n => $v) {
408 if (substr($n, 0, 7) == 'return.') {
409 $returnkey = substr($n, 7);
410 $returns[$returnkey] = $v;
411 }
412 }
413
414 $returns['source_contact_id'] = 1;
415 if (!empty($returns['target_contact_name'])) {
416 $returns['target_contact_id'] = 1;
417 }
418 if (!empty($returns['assignee_contact_name'])) {
419 $returns['assignee_contact_id'] = 1;
420 }
421
422 $tagGet = array('tag_id', 'entity_id');
423 foreach (array_keys($returns) as $key) {
424 if (strpos($key, 'tag_id.') === 0) {
425 $tagGet[] = $key;
426 $returns['tag_id'] = 1;
427 }
428 }
429
430 foreach ($returns as $n => $v) {
431 switch ($n) {
432 case 'assignee_contact_id':
433 foreach ($activities as $key => $activityArray) {
434 $cids = $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']);
435 if ($cids && !empty($returns['assignee_contact_name'])) {
436 foreach ($cids as $cid) {
437 $activities[$key]['assignee_contact_name'][$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
438 }
439 }
440 }
441 break;
442
443 case 'target_contact_id':
444 foreach ($activities as $key => $activityArray) {
445 $cids = $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']);
446 if ($cids && !empty($returns['target_contact_name'])) {
447 foreach ($cids as $cid) {
448 $activities[$key]['target_contact_name'][$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
449 }
450 }
451 }
452 break;
453
454 case 'source_contact_id':
455 foreach ($activities as $key => $activityArray) {
456 $cid = $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity::getSourceContactID($activityArray['id']);
457 if ($cid && !empty($returns['source_contact_name'])) {
458 $activities[$key]['source_contact_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
459 }
460 }
461 break;
462
463 case 'tag_id':
464 $tags = civicrm_api3('EntityTag', 'get', array(
465 'entity_table' => 'civicrm_activity',
466 'entity_id' => array('IN' => array_keys($activities)),
467 'return' => $tagGet,
468 'options' => array('limit' => 0),
469 ));
470 foreach ($tags['values'] as $tag) {
471 $key = (int) $tag['entity_id'];
472 unset($tag['entity_id'], $tag['id']);
473 $activities[$key]['tag_id'][$tag['tag_id']] = $tag;
474 }
475 break;
476
477 case 'file_id':
478 $dao = CRM_Core_DAO::executeQuery("SELECT entity_id, file_id FROM civicrm_entity_file WHERE entity_table = 'civicrm_activity' AND entity_id IN (%1)",
479 array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)));
480 while ($dao->fetch()) {
481 $activities[$dao->entity_id]['file_id'][] = $dao->file_id;
482 }
483 break;
484
485 default:
486 if (substr($n, 0, 6) == 'custom') {
487 $returnProperties[$n] = $v;
488 }
489 }
490 }
491
492 // Legacy extras
493 if (!empty($params['contact_id'])) {
494 $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get');
495 $typeOptions = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
496 foreach ($activities as $key => &$activityArray) {
497 if (!empty($activityArray['status_id'])) {
498 $activityArray['status'] = $statusOptions[$activityArray['status_id']];
499 }
500 if (!empty($activityArray['activity_type_id'])) {
501 $activityArray['activity_name'] = $typeOptions[$activityArray['activity_type_id']];
502 }
503 }
504 }
505
506 if (!empty($returnProperties) || !empty($params['contact_id'])) {
507 foreach ($activities as $activityId => $values) {
508 //@todo - should possibly load activity type id if not loaded (update with id)
509 _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));
510 }
511 }
512 return $activities;
513 }
514
515
516 /**
517 * Delete a specified Activity.
518 *
519 * @param array $params
520 * Array holding 'id' of activity to be deleted.
521 *
522 * @throws API_Exception
523 *
524 * @return array
525 * API result array
526 */
527 function civicrm_api3_activity_delete($params) {
528
529 if (CRM_Activity_BAO_Activity::deleteActivity($params)) {
530 return civicrm_api3_create_success(1, $params, 'Activity', 'delete');
531 }
532 else {
533 throw new API_Exception('Could not delete Activity');
534 }
535 }
536
537 /**
538 * Check for required params.
539 *
540 * @param array $params
541 * Associated array of fields.
542 *
543 * @throws API_Exception
544 * @throws Exception
545 * @return array
546 * array with errors
547 */
548 function _civicrm_api3_activity_check_params(&$params) {
549
550 $contactIDFields = array_intersect_key($params,
551 array(
552 'source_contact_id' => 1,
553 'assignee_contact_id' => 1,
554 'target_contact_id' => 1,
555 )
556 );
557
558 // this should be handled by wrapper layer & probably the api would already manage it
559 //correctly by doing post validation - ie. a failure should result in a roll-back = an error
560 // needs testing
561 if (!empty($contactIDFields)) {
562 $contactIds = array();
563 foreach ($contactIDFields as $fieldname => $contactfield) {
564 if (empty($contactfield)) {
565 continue;
566 }
567 if (is_array($contactfield)) {
568 foreach ($contactfield as $contactkey => $contactvalue) {
569 $contactIds[$contactvalue] = $contactvalue;
570 }
571 }
572 else {
573 $contactIds[$contactfield] = $contactfield;
574 }
575 }
576
577 $sql = '
578 SELECT count(*)
579 FROM civicrm_contact
580 WHERE id IN (' . implode(', ', $contactIds) . ' )';
581 if (count($contactIds) != CRM_Core_DAO::singleValueQuery($sql)) {
582 throw new API_Exception('Invalid Contact Id');
583 }
584 }
585
586 $activityIds = array(
587 'activity' => CRM_Utils_Array::value('id', $params),
588 'parent' => CRM_Utils_Array::value('parent_id', $params),
589 'original' => CRM_Utils_Array::value('original_id', $params),
590 );
591
592 foreach ($activityIds as $id => $value) {
593 if ($value &&
594 !CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id')
595 ) {
596 throw new API_Exception('Invalid ' . ucfirst($id) . ' Id');
597 }
598 }
599 // this should be handled by wrapper layer & probably the api would already manage it
600 //correctly by doing pseudoconstant validation
601 // needs testing
602 $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
603 $activityName = CRM_Utils_Array::value('activity_name', $params);
604 $activityName = ucfirst($activityName);
605 $activityLabel = CRM_Utils_Array::value('activity_label', $params);
606 if ($activityLabel) {
607 $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'create');
608 }
609
610 $activityTypeId = CRM_Utils_Array::value('activity_type_id', $params);
611
612 if ($activityName || $activityLabel) {
613 $activityTypeIdInList = array_search(($activityName ? $activityName : $activityLabel), $activityTypes);
614
615 if (!$activityTypeIdInList) {
616 $errorString = $activityName ? "Invalid Activity Name : $activityName" : "Invalid Activity Type Label";
617 throw new Exception($errorString);
618 }
619 elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) {
620 throw new API_Exception('Mismatch in Activity');
621 }
622 $params['activity_type_id'] = $activityTypeIdInList;
623 }
624 elseif ($activityTypeId &&
625 !array_key_exists($activityTypeId, $activityTypes)
626 ) {
627 throw new API_Exception('Invalid Activity Type ID');
628 }
629
630 // check for activity duration minutes
631 // this should be validated @ the wrapper layer not here
632 // needs testing
633 if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) {
634 throw new API_Exception('Invalid Activity Duration (in minutes)');
635 }
636
637 //if adding a new activity & date_time not set make it now
638 // this should be managed by the wrapper layer & setting ['api.default'] in speces
639 // needs testing
640 if (empty($params['id']) && empty($params['activity_date_time'])) {
641 $params['activity_date_time'] = CRM_Utils_Date::processDate(date('Y-m-d H:i:s'));
642 }
643
644 return NULL;
645 }
646
647 /**
648 * Get parameters for activity list.
649 *
650 * @see _civicrm_api3_generic_getlist_params
651 *
652 * @param array $request
653 * API request.
654 */
655 function _civicrm_api3_activity_getlist_params(&$request) {
656 $fieldsToReturn = array(
657 'activity_date_time',
658 'activity_type_id',
659 'subject',
660 'source_contact_id',
661 );
662 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
663 $request['params']['options']['sort'] = 'activity_date_time DESC';
664 $request['params'] += array(
665 'is_current_revision' => 1,
666 'is_deleted' => 0,
667 );
668 }
669
670 /**
671 * Get output for activity list.
672 *
673 * @see _civicrm_api3_generic_getlist_output
674 *
675 * @param array $result
676 * @param array $request
677 *
678 * @return array
679 */
680 function _civicrm_api3_activity_getlist_output($result, $request) {
681 $output = array();
682 if (!empty($result['values'])) {
683 foreach ($result['values'] as $row) {
684 $data = array(
685 'id' => $row[$request['id_field']],
686 'label' => $row[$request['label_field']] ? $row[$request['label_field']] : ts('(no subject)'),
687 'description' => array(
688 CRM_Core_Pseudoconstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id']),
689 ),
690 );
691 if (!empty($row['activity_date_time'])) {
692 $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['activity_date_time']);
693 }
694 if (!empty($row['source_contact_id'])) {
695 $data['description'][] = ts('By %1', array(
696 1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name'),
697 ));
698 }
699 // Add repeating info
700 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_activity');
701 $data['extra']['is_recur'] = FALSE;
702 if ($repeat) {
703 $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
704 $data['extra']['is_recur'] = TRUE;
705 }
706 $output[] = $data;
707 }
708 }
709 return $output;
710 }