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