3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM Activity records.
31 * @package CiviCRM_APIv3
36 * Creates or updates an Activity.
38 * @param array $params
39 * Array per getfields documentation.
41 * @throws API_Exception
45 function civicrm_api3_activity_create($params) {
46 $isNew = empty($params['id']);
48 if (empty($params['id'])) {
49 // an update does not require any mandatory parameters
50 civicrm_api3_verify_one_mandatory($params,
60 // check for various error and required conditions
61 // note that almost all the processing in there should be managed by the wrapper layer
62 // & should be removed - needs testing
63 $errors = _civicrm_api3_activity_check_params($params);
65 // this should not be required as should throw exception rather than return errors -
67 if (!empty($errors)) {
71 // processing for custom data
72 $values = $activityArray = array();
73 _civicrm_api3_custom_format_params($params, $values, 'Activity');
75 if (!empty($values['custom'])) {
76 $params['custom'] = $values['custom'];
79 // this should be set as a default rather than hard coded
81 $params['skipRecentView'] = TRUE;
83 // If this is a case activity, see if there is an existing activity
84 // and set it as an old revision. Also retrieve details we'll need.
85 // this handling should all be moved to the BAO layer
87 $createRevision = FALSE;
88 $oldActivityValues = array();
89 // Lookup case id if not supplied
90 if (!isset($params['case_id']) && !empty($params['id'])) {
91 $params['case_id'] = CRM_Core_DAO
::singleValueQuery("SELECT case_id FROM civicrm_case_activity WHERE activity_id = " . (int) $params['id']);
93 if (!empty($params['case_id'])) {
94 $case_id = $params['case_id'];
95 if (!empty($params['id']) && Civi
::settings()->get('civicaseActivityRevisions')) {
96 $oldActivityParams = array('id' => $params['id']);
97 if (!$oldActivityValues) {
98 CRM_Activity_BAO_Activity
::retrieve($oldActivityParams, $oldActivityValues);
100 if (empty($oldActivityValues)) {
101 throw new API_Exception(ts("Unable to locate existing activity."));
104 $activityDAO = new CRM_Activity_DAO_Activity();
105 $activityDAO->id
= $params['id'];
106 $activityDAO->is_current_revision
= 0;
107 if (!$activityDAO->save()) {
108 throw new API_Exception(ts("Unable to revision existing case activity."));
110 $createRevision = TRUE;
115 $deleteActivityAssignment = FALSE;
116 if (isset($params['assignee_contact_id'])) {
117 $deleteActivityAssignment = TRUE;
120 $deleteActivityTarget = FALSE;
121 if (isset($params['target_contact_id'])) {
122 $deleteActivityTarget = TRUE;
125 // this should all be handled at the BAO layer
126 $params['deleteActivityAssignment'] = CRM_Utils_Array
::value('deleteActivityAssignment', $params, $deleteActivityAssignment);
127 $params['deleteActivityTarget'] = CRM_Utils_Array
::value('deleteActivityTarget', $params, $deleteActivityTarget);
129 if ($case_id && $createRevision) {
130 // This is very similar to the copy-to-case action.
131 if (!CRM_Utils_Array
::crmIsEmptyArray($oldActivityValues['target_contact'])) {
132 $oldActivityValues['targetContactIds'] = implode(',', array_unique($oldActivityValues['target_contact']));
134 if (!CRM_Utils_Array
::crmIsEmptyArray($oldActivityValues['assignee_contact'])) {
135 $oldActivityValues['assigneeContactIds'] = implode(',', array_unique($oldActivityValues['assignee_contact']));
137 $oldActivityValues['mode'] = 'copy';
138 $oldActivityValues['caseID'] = $case_id;
139 $oldActivityValues['activityID'] = $oldActivityValues['id'];
140 $oldActivityValues['contactID'] = $oldActivityValues['source_contact_id'];
142 $copyToCase = CRM_Activity_Page_AJAX
::_convertToCaseActivity($oldActivityValues);
143 if (empty($copyToCase['error_msg'])) {
144 // now fix some things that are different from copy-to-case
145 // then fall through to the create below to update with the passed in params
146 $params['id'] = $copyToCase['newId'];
147 $params['is_auto'] = 0;
148 $params['original_id'] = empty($oldActivityValues['original_id']) ?
$oldActivityValues['id'] : $oldActivityValues['original_id'];
151 throw new API_Exception(ts("Unable to create new revision of case activity."));
156 $activityBAO = CRM_Activity_BAO_Activity
::create($params);
158 if (isset($activityBAO->id
)) {
159 if ($case_id && $isNew && !$createRevision) {
160 // If this is a brand new case activity, add to case(s)
161 foreach ((array) $case_id as $singleCaseId) {
162 $caseActivityParams = array('activity_id' => $activityBAO->id
, 'case_id' => $singleCaseId);
163 CRM_Case_BAO_Case
::processCaseActivity($caseActivityParams);
167 _civicrm_api3_object_to_array($activityBAO, $activityArray[$activityBAO->id
]);
168 return civicrm_api3_create_success($activityArray, $params, 'Activity', 'get', $activityBAO);
173 * Specify Meta data for create.
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.
178 * @param array $params
179 * Array of parameters determined by getfields.
181 function _civicrm_api3_activity_create_spec(&$params) {
183 $params['status_id']['api.aliases'] = array('activity_status');
185 $params['assignee_contact_id'] = array(
186 'name' => 'assignee_id',
187 'title' => 'Activity Assignee',
188 'description' => 'Contact(s) assigned to this activity.',
190 'FKClassName' => 'CRM_Contact_DAO_Contact',
191 'FKApiName' => 'Contact',
193 $params['target_contact_id'] = array(
194 'name' => 'target_id',
195 'title' => 'Activity Target',
196 'description' => 'Contact(s) participating in this activity.',
198 'FKClassName' => 'CRM_Contact_DAO_Contact',
199 'FKApiName' => 'Contact',
202 $params['source_contact_id'] = array(
203 'name' => 'source_contact_id',
204 'title' => 'Activity Source Contact',
205 'description' => 'Person who created this activity. Defaults to current user.',
207 'FKClassName' => 'CRM_Contact_DAO_Contact',
208 'api.default' => 'user_contact_id',
209 'FKApiName' => 'Contact',
210 'api.required' => TRUE,
213 $params['case_id'] = array(
215 'title' => 'Case ID',
216 'description' => 'For creating an activity as part of a case.',
218 'FKClassName' => 'CRM_Case_DAO_Case',
219 'FKApiName' => 'Case',
225 * Specify Metadata for get.
227 * @param array $params
229 function _civicrm_api3_activity_get_spec(&$params) {
230 $params['tag_id'] = array(
232 'description' => 'Find activities with specified tags.',
233 'type' => CRM_Utils_Type
::T_INT
,
234 'FKClassName' => 'CRM_Core_DAO_Tag',
235 'FKApiName' => 'Tag',
236 'supports_joins' => TRUE,
238 $params['file_id'] = array(
239 'title' => 'Attached Files',
240 'description' => 'Find activities with attached files.',
241 'type' => CRM_Utils_Type
::T_INT
,
242 'FKClassName' => 'CRM_Core_DAO_File',
243 'FKApiName' => 'File',
245 $params['case_id'] = array(
247 'description' => 'Find activities within specified cases.',
248 'type' => CRM_Utils_Type
::T_INT
,
249 'FKClassName' => 'CRM_Case_DAO_Case',
250 'FKApiName' => 'Case',
251 'supports_joins' => TRUE,
253 $params['contact_id'] = array(
254 'title' => 'Activity Contact ID',
255 'description' => 'Find activities involving this contact (as target, source, OR assignee).',
256 'type' => CRM_Utils_Type
::T_INT
,
257 'FKClassName' => 'CRM_Contact_DAO_Contact',
258 'FKApiName' => 'Contact',
260 $params['target_contact_id'] = array(
261 'title' => 'Target Contact ID',
262 'description' => 'Find activities with specified target contact.',
263 'type' => CRM_Utils_Type
::T_INT
,
264 'FKClassName' => 'CRM_Contact_DAO_Contact',
265 'FKApiName' => 'Contact',
267 $params['source_contact_id'] = array(
268 'title' => 'Source Contact ID',
269 'description' => 'Find activities with specified source contact.',
270 'type' => CRM_Utils_Type
::T_INT
,
271 'FKClassName' => 'CRM_Contact_DAO_Contact',
272 'FKApiName' => 'Contact',
274 $params['assignee_contact_id'] = array(
275 'title' => 'Assignee Contact ID',
276 'description' => 'Find activities with specified assignee contact.',
277 'type' => CRM_Utils_Type
::T_INT
,
278 'FKClassName' => 'CRM_Contact_DAO_Contact',
279 'FKApiName' => 'Contact',
281 $params['is_overdue'] = array(
282 'title' => 'Is Activity Overdue',
283 'description' => 'Incomplete activities with a past date.',
284 'type' => CRM_Utils_Type
::T_BOOLEAN
,
289 * Gets a CiviCRM activity according to parameters.
291 * @param array $params
292 * Array per getfields documentation.
294 * @return array API result array
297 * @throws \API_Exception
298 * @throws \CiviCRM_API3_Exception
299 * @throws \Civi\API\Exception\UnauthorizedException
301 function civicrm_api3_activity_get($params) {
302 $options = _civicrm_api3_get_options_from_params($params, FALSE, 'Activity', 'get');
303 $sql = CRM_Utils_SQL_Select
::fragment();
305 if (empty($params['target_contact_id']) && empty($params['source_contact_id'])
306 && empty($params['assignee_contact_id']) &&
307 !empty($params['check_permissions']) && !CRM_Core_Permission
::check('view all activities')
308 && !CRM_Core_Permission
::check('view all contacts')
310 // Force join on the activity contact table.
311 // @todo get this & other acl filters to work, remove check further down.
312 //$params['contact_id'] = array('IS NOT NULL' => TRUE);
315 _civicrm_api3_activity_get_extraFilters($params, $sql);
317 // Handle is_overdue sort
318 if (!empty($options['sort'])) {
319 $sort = explode(', ', $options['sort']);
321 foreach ($sort as $index => &$sortString) {
322 // Get sort field and direction
323 list($sortField, $dir) = array_pad(explode(' ', $sortString), 2, 'ASC');
324 if ($sortField == 'is_overdue') {
325 $incomplete = implode(',', array_keys(CRM_Activity_BAO_Activity
::getStatusesByType(CRM_Activity_BAO_Activity
::INCOMPLETE
)));
326 $sql->orderBy("IF((a.activity_date_time >= NOW() OR a.status_id NOT IN ($incomplete)), 0, 1) $dir", NULL, $index);
327 // Replace the sort with a placeholder which will be ignored by sql
331 $params['options']['sort'] = implode(', ', $sort);
334 // Ensure there's enough data for calculating is_overdue
335 if (!empty($options['return']['is_overdue']) && (empty($options['return']['status_id']) ||
empty($options['return']['activity_date_time']))) {
336 $options['return']['status_id'] = $options['return']['activity_date_time'] = 1;
337 $params['return'] = array_keys($options['return']);
340 $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Activity', $sql);
341 if ($options['is_count']) {
342 return civicrm_api3_create_success($activities, $params, 'Activity', 'get');
345 if (!empty($params['check_permissions']) && !CRM_Core_Permission
::check('view all activities')) {
346 // @todo get this to work at the query level - see contact_id join above.
347 foreach ($activities as $activity) {
348 if (!CRM_Activity_BAO_Activity
::checkPermission($activity['id'], CRM_Core_Action
::VIEW
)) {
349 unset($activities[$activity['id']]);
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');
360 * Support filters beyond what basic_get can do.
362 * @param array $params
363 * @param CRM_Utils_SQL_Select $sql
364 * @throws \CiviCRM_API3_Exception
367 function _civicrm_api3_activity_get_extraFilters(&$params, &$sql) {
368 // Filter by activity contacts
369 $activityContactOptions = array(
370 'contact_id' => NULL,
371 'target_contact_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'),
372 'source_contact_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source'),
373 'assignee_contact_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Assignees'),
375 foreach ($activityContactOptions as $activityContactName => $activityContactValue) {
376 if (!empty($params[$activityContactName])) {
377 if (!is_array($params[$activityContactName])) {
378 $params[$activityContactName] = array('=' => $params[$activityContactName]);
380 $clause = \CRM_Core_DAO
::createSQLFilter('contact_id', $params[$activityContactName]);
381 $typeClause = $activityContactValue ?
'record_type_id = #typeId AND ' : '';
382 $sql->where("a.id IN (SELECT activity_id FROM civicrm_activity_contact WHERE $typeClause !clause)",
383 array('#typeId' => $activityContactValue, '!clause' => $clause)
388 // Handle is_overdue filter
389 // Boolean calculated field - does not support operators
390 if (isset($params['is_overdue'])) {
391 $incomplete = implode(',', array_keys(CRM_Activity_BAO_Activity
::getStatusesByType(CRM_Activity_BAO_Activity
::INCOMPLETE
)));
392 if ($params['is_overdue']) {
393 $sql->where('a.activity_date_time < NOW()');
394 $sql->where("a.status_id IN ($incomplete)");
397 $sql->where("(a.activity_date_time >= NOW() OR a.status_id NOT IN ($incomplete))");
401 // Define how to handle filters on some related entities.
402 // Subqueries are nice in (a) avoiding duplicates and (b) when the result
403 // list is expected to be bite-sized. Joins are nice (a) with larger
404 // datasets and (b) checking for non-existent relations.
407 'subquery' => 'a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_activity" AND !clause)',
408 'join' => '!joinType civicrm_entity_tag !alias ON (!alias.entity_table = "civicrm_activity" AND !alias.entity_id = a.id)',
409 'column' => 'tag_id',
412 'subquery' => 'a.id IN (SELECT entity_id FROM civicrm_entity_file WHERE entity_table = "civicrm_activity" AND !clause)',
413 'join' => '!joinType civicrm_entity_file !alias ON (!alias.entity_table = "civicrm_activity" AND !alias.entity_id = a.id)',
414 'column' => 'file_id',
417 'subquery' => 'a.id IN (SELECT activity_id FROM civicrm_case_activity WHERE !clause)',
418 'join' => '!joinType civicrm_case_activity !alias ON (!alias.activity_id = a.id)',
419 'column' => 'case_id',
422 foreach ($rels as $filter => $relSpec) {
423 if (!empty($params[$filter])) {
424 if (!is_array($params[$filter])) {
425 $params[$filter] = array('=' => $params[$filter]);
427 // $mode is one of ('LEFT JOIN', 'INNER JOIN', 'SUBQUERY')
428 $mode = isset($params[$filter]['IS NULL']) ?
'LEFT JOIN' : 'SUBQUERY';
429 if ($mode === 'SUBQUERY') {
430 $clause = \CRM_Core_DAO
::createSQLFilter($relSpec['column'], $params[$filter]);
432 $sql->where($relSpec['subquery'], array('!clause' => $clause));
436 $alias = 'actjoin_' . $filter;
437 $clause = \CRM_Core_DAO
::createSQLFilter($alias . "." . $relSpec['column'], $params[$filter]);
439 $sql->join($alias, $relSpec['join'], array('!alias' => $alias, 'joinType' => $mode));
440 $sql->where($clause);
448 * Given a list of activities, append any extra data requested about the activities.
450 * @note Called by civicrm-core and CiviHR
452 * @param array $params
453 * API request parameters.
454 * @param array $activities
457 * new activities list
459 function _civicrm_api3_activity_get_formatResult($params, $activities, $options) {
464 $returns = $options['return'];
465 foreach ($params as $n => $v) {
466 if (substr($n, 0, 7) == 'return.') {
467 $returnkey = substr($n, 7);
468 $returns[$returnkey] = $v;
472 $returns['source_contact_id'] = 1;
473 if (!empty($returns['target_contact_name'])) {
474 $returns['target_contact_id'] = 1;
476 if (!empty($returns['assignee_contact_name'])) {
477 $returns['assignee_contact_id'] = 1;
480 $tagGet = array('tag_id', 'entity_id');
481 $caseGet = $caseIds = array();
482 foreach (array_keys($returns) as $key) {
483 if (strpos($key, 'tag_id.') === 0) {
485 $returns['tag_id'] = 1;
487 if (strpos($key, 'case_id.') === 0) {
488 $caseGet[] = str_replace('case_id.', '', $key);
489 $returns['case_id'] = 1;
493 foreach ($returns as $n => $v) {
495 case 'assignee_contact_id':
496 foreach ($activities as $key => $activityArray) {
497 $cids = $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment
::retrieveAssigneeIdsByActivityId($activityArray['id']);
498 if ($cids && !empty($returns['assignee_contact_name'])) {
499 foreach ($cids as $cid) {
500 $activities[$key]['assignee_contact_name'][$cid] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
506 case 'target_contact_id':
507 foreach ($activities as $key => $activityArray) {
508 $cids = $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget
::retrieveTargetIdsByActivityId($activityArray['id']);
509 if ($cids && !empty($returns['target_contact_name'])) {
510 foreach ($cids as $cid) {
511 $activities[$key]['target_contact_name'][$cid] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
517 case 'source_contact_id':
518 foreach ($activities as $key => $activityArray) {
519 $cid = $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity
::getSourceContactID($activityArray['id']);
520 if ($cid && !empty($returns['source_contact_name'])) {
521 $activities[$key]['source_contact_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
527 $tags = civicrm_api3('EntityTag', 'get', array(
528 'entity_table' => 'civicrm_activity',
529 'entity_id' => array('IN' => array_keys($activities)),
531 'options' => array('limit' => 0),
533 foreach ($tags['values'] as $tag) {
534 $key = (int) $tag['entity_id'];
535 unset($tag['entity_id'], $tag['id']);
536 $activities[$key]['tag_id'][$tag['tag_id']] = $tag;
541 $dao = CRM_Core_DAO
::executeQuery("SELECT entity_id, file_id FROM civicrm_entity_file WHERE entity_table = 'civicrm_activity' AND entity_id IN (%1)",
542 array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
)));
543 while ($dao->fetch()) {
544 $activities[$dao->entity_id
]['file_id'][] = $dao->file_id
;
549 $dao = CRM_Core_DAO
::executeQuery("SELECT activity_id, case_id FROM civicrm_case_activity WHERE activity_id IN (%1)",
550 array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
)));
551 while ($dao->fetch()) {
552 $activities[$dao->activity_id
]['case_id'][] = $dao->case_id
;
553 $caseIds[$dao->case_id
] = $dao->case_id
;
558 foreach ($activities as $key => $activityArray) {
559 $activities[$key]['is_overdue'] = (int) CRM_Activity_BAO_Activity
::isOverdue($activityArray);
564 if (substr($n, 0, 6) == 'custom') {
565 $returnProperties[$n] = $v;
570 // Fetch case fields via the join syntax
571 // Note this is limited to the first case if the activity belongs to more than one
572 if ($caseGet && $caseIds) {
573 $cases = civicrm_api3('Case', 'get', array(
574 'id' => array('IN' => $caseIds),
575 'options' => array('limit' => 0),
576 'check_permissions' => !empty($params['check_permissions']),
577 'return' => $caseGet,
579 foreach ($activities as &$activity) {
580 if (!empty($activity['case_id'])) {
581 $case = CRM_Utils_Array
::value($activity['case_id'][0], $cases['values']);
583 foreach ($case as $key => $value) {
585 $activity['case_id.' . $key] = $value;
594 if (!empty($params['contact_id'])) {
595 $statusOptions = CRM_Activity_BAO_Activity
::buildOptions('status_id', 'get');
596 $typeOptions = CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate');
597 foreach ($activities as $key => &$activityArray) {
598 if (!empty($activityArray['status_id'])) {
599 $activityArray['status'] = $statusOptions[$activityArray['status_id']];
601 if (!empty($activityArray['activity_type_id'])) {
602 $activityArray['activity_name'] = $typeOptions[$activityArray['activity_type_id']];
607 if (!empty($returnProperties) ||
!empty($params['contact_id'])) {
608 foreach ($activities as $activityId => $values) {
609 //@todo - should possibly load activity type id if not loaded (update with id)
610 _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));
618 * Delete a specified Activity.
620 * @param array $params
621 * Array holding 'id' of activity to be deleted.
623 * @throws API_Exception
628 function civicrm_api3_activity_delete($params) {
630 if (CRM_Activity_BAO_Activity
::deleteActivity($params)) {
631 return civicrm_api3_create_success(1, $params, 'Activity', 'delete');
634 throw new API_Exception('Could not delete Activity: ' . (int) $params['id']);
639 * Check for required params.
641 * @param array $params
642 * Associated array of fields.
644 * @throws API_Exception
649 function _civicrm_api3_activity_check_params(&$params) {
650 $activityIds = array(
651 'activity' => CRM_Utils_Array
::value('id', $params),
652 'parent' => CRM_Utils_Array
::value('parent_id', $params),
653 'original' => CRM_Utils_Array
::value('original_id', $params),
656 foreach ($activityIds as $id => $value) {
658 !CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id')
660 throw new API_Exception('Invalid ' . ucfirst($id) . ' Id');
663 // this should be handled by wrapper layer & probably the api would already manage it
664 //correctly by doing pseudoconstant validation
666 $activityTypes = CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate');
667 $activityName = CRM_Utils_Array
::value('activity_name', $params);
668 $activityName = ucfirst($activityName);
669 $activityLabel = CRM_Utils_Array
::value('activity_label', $params);
670 if ($activityLabel) {
671 $activityTypes = CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'create');
674 $activityTypeId = CRM_Utils_Array
::value('activity_type_id', $params);
676 if ($activityName ||
$activityLabel) {
677 $activityTypeIdInList = array_search(($activityName ?
$activityName : $activityLabel), $activityTypes);
679 if (!$activityTypeIdInList) {
680 $errorString = $activityName ?
"Invalid Activity Name : $activityName" : "Invalid Activity Type Label";
681 throw new Exception($errorString);
683 elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) {
684 throw new API_Exception('Mismatch in Activity');
686 $params['activity_type_id'] = $activityTypeIdInList;
688 elseif ($activityTypeId &&
689 !array_key_exists($activityTypeId, $activityTypes)
691 throw new API_Exception('Invalid Activity Type ID');
694 // check for activity duration minutes
695 // this should be validated @ the wrapper layer not here
697 if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) {
698 throw new API_Exception('Invalid Activity Duration (in minutes)');
701 //if adding a new activity & date_time not set make it now
702 // this should be managed by the wrapper layer & setting ['api.default'] in speces
704 if (empty($params['id']) && empty($params['activity_date_time'])) {
705 $params['activity_date_time'] = CRM_Utils_Date
::processDate(date('Y-m-d H:i:s'));
712 * Get parameters for activity list.
714 * @see _civicrm_api3_generic_getlist_params
716 * @param array $request
719 function _civicrm_api3_activity_getlist_params(&$request) {
720 $fieldsToReturn = array(
721 'activity_date_time',
726 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
727 $request['params']['options']['sort'] = 'activity_date_time DESC';
728 $request['params'] +
= array(
729 'is_current_revision' => 1,
735 * Get output for activity list.
737 * @see _civicrm_api3_generic_getlist_output
739 * @param array $result
740 * @param array $request
744 function _civicrm_api3_activity_getlist_output($result, $request) {
746 if (!empty($result['values'])) {
747 foreach ($result['values'] as $row) {
749 'id' => $row[$request['id_field']],
750 'label' => $row[$request['label_field']] ?
$row[$request['label_field']] : ts('(no subject)'),
751 'description' => array(
752 CRM_Core_Pseudoconstant
::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id']),
755 if (!empty($row['activity_date_time'])) {
756 $data['description'][0] .= ': ' . CRM_Utils_Date
::customFormat($row['activity_date_time']);
758 if (!empty($row['source_contact_id'])) {
759 $data['description'][] = ts('By %1', array(
760 1 => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name'),
763 // Add repeating info
764 $repeat = CRM_Core_BAO_RecurringEntity
::getPositionAndCount($row['id'], 'civicrm_activity');
765 $data['extra']['is_recur'] = FALSE;
767 $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
768 $data['extra']['is_recur'] = TRUE;