Merge pull request #10431 from JMAConsulting/NoticeFixes-1
[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, add to case(s)
163 foreach ((array) $case_id as $singleCaseId) {
164 $caseActivityParams = array('activity_id' => $activityBAO->id, 'case_id' => $singleCaseId);
165 CRM_Case_BAO_Case::processCaseActivity($caseActivityParams);
166 }
167 }
168
169 _civicrm_api3_object_to_array($activityBAO, $activityArray[$activityBAO->id]);
170 return civicrm_api3_create_success($activityArray, $params, 'Activity', 'get', $activityBAO);
171 }
172 }
173
174 /**
175 * Specify Meta data for create.
176 *
177 * Note that this data is retrievable via the getfields function and is used for pre-filling defaults and
178 * ensuring mandatory requirements are met.
179 *
180 * @param array $params
181 * Array of parameters determined by getfields.
182 */
183 function _civicrm_api3_activity_create_spec(&$params) {
184
185 $params['status_id']['api.aliases'] = array('activity_status');
186
187 $params['assignee_contact_id'] = array(
188 'name' => 'assignee_id',
189 'title' => 'Activity Assignee',
190 'description' => 'Contact(s) assigned to this activity.',
191 'type' => 1,
192 'FKClassName' => 'CRM_Contact_DAO_Contact',
193 'FKApiName' => 'Contact',
194 );
195 $params['target_contact_id'] = array(
196 'name' => 'target_id',
197 'title' => 'Activity Target',
198 'description' => 'Contact(s) participating in this activity.',
199 'type' => 1,
200 'FKClassName' => 'CRM_Contact_DAO_Contact',
201 'FKApiName' => 'Contact',
202 );
203
204 $params['source_contact_id'] = array(
205 'name' => 'source_contact_id',
206 'title' => 'Activity Source Contact',
207 'description' => 'Person who created this activity. Defaults to current user.',
208 'type' => 1,
209 'FKClassName' => 'CRM_Contact_DAO_Contact',
210 'api.default' => 'user_contact_id',
211 'FKApiName' => 'Contact',
212 'api.required' => TRUE,
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
299 $sql = CRM_Utils_SQL_Select::fragment();
300 $recordTypes = civicrm_api3('ActivityContact', 'getoptions', array('field' => 'record_type_id'));
301 $recordTypes = $recordTypes['values'];
302 $activityContactOptions = array(
303 'contact_id' => NULL,
304 'target_contact_id' => array_search('Activity Targets', $recordTypes),
305 'source_contact_id' => array_search('Activity Source', $recordTypes),
306 'assignee_contact_id' => array_search('Activity Assignees', $recordTypes),
307 );
308 if (empty($params['target_contact_id']) && empty($params['source_contact_id'])
309 && empty($params['assignee_contact_id']) &&
310 !empty($params['check_permissions']) && !CRM_Core_Permission::check('view all activities')
311 && !CRM_Core_Permission::check('view all contacts')
312 ) {
313 // Force join on the activity contact table.
314 // @todo get this & other acl filters to work, remove check further down.
315 //$params['contact_id'] = array('IS NOT NULL' => TRUE);
316 }
317
318 foreach ($activityContactOptions as $activityContactName => $activityContactValue) {
319 if (!empty($params[$activityContactName])) {
320 if (!is_array($params[$activityContactName])) {
321 $params[$activityContactName] = array('=' => $params[$activityContactName]);
322 }
323 $clause = \CRM_Core_DAO::createSQLFilter('contact_id', $params[$activityContactName]);
324 $typeClause = $activityContactValue ? 'record_type_id = #typeId AND ' : '';
325 $sql->where("a.id IN (SELECT activity_id FROM civicrm_activity_contact WHERE $typeClause !clause)",
326 array('#typeId' => $activityContactValue, '!clause' => $clause)
327 );
328 }
329 }
330
331 // Define how to handle filters on some related entities.
332 // Subqueries are nice in (a) avoiding duplicates and (b) when the result
333 // list is expected to be bite-sized. Joins are nice (a) with larger
334 // datasets and (b) checking for non-existent relations.
335 $rels = array(
336 'tag_id' => array(
337 'subquery' => 'a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_activity" AND !clause)',
338 'join' => '!joinType civicrm_entity_tag !alias ON (!alias.entity_table = "civicrm_activity" AND !alias.entity_id = a.id)',
339 'column' => 'tag_id',
340 ),
341 'file_id' => array(
342 'subquery' => 'a.id IN (SELECT entity_id FROM civicrm_entity_file WHERE entity_table = "civicrm_activity" AND !clause)',
343 'join' => '!joinType civicrm_entity_file !alias ON (!alias.entity_table = "civicrm_activity" AND !alias.entity_id = a.id)',
344 'column' => 'file_id',
345 ),
346 'case_id' => array(
347 'subquery' => 'a.id IN (SELECT activity_id FROM civicrm_case_activity WHERE !clause)',
348 'join' => '!joinType civicrm_case_activity !alias ON (!alias.activity_id = a.id)',
349 'column' => 'case_id',
350 ),
351 );
352 foreach ($rels as $filter => $relSpec) {
353 if (!empty($params[$filter])) {
354 if (!is_array($params[$filter])) {
355 $params[$filter] = array('=' => $params[$filter]);
356 }
357 // $mode is one of ('LEFT JOIN', 'INNER JOIN', 'SUBQUERY')
358 $mode = isset($params[$filter]['IS NULL']) ? 'LEFT JOIN' : 'SUBQUERY';
359 if ($mode === 'SUBQUERY') {
360 $clause = \CRM_Core_DAO::createSQLFilter($relSpec['column'], $params[$filter]);
361 if ($clause) {
362 $sql->where($relSpec['subquery'], array('!clause' => $clause));
363 }
364 }
365 else {
366 $alias = 'actjoin_' . $filter;
367 $clause = \CRM_Core_DAO::createSQLFilter($alias . "." . $relSpec['column'], $params[$filter]);
368 if ($clause) {
369 $sql->join($alias, $relSpec['join'], array('!alias' => $alias, 'joinType' => $mode));
370 $sql->where($clause);
371 }
372 }
373 }
374 }
375 $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $sql);
376 if (!empty($params['check_permissions']) && !CRM_Core_Permission::check('view all activities')) {
377 // @todo get this to work at the query level - see contact_id join above.
378 foreach ($activities as $activity) {
379 if (!CRM_Activity_BAO_Activity::checkPermission($activity['id'], CRM_Core_Action::VIEW)) {
380 unset($activities[$activity['id']]);
381 }
382 }
383 }
384 $options = _civicrm_api3_get_options_from_params($params, FALSE, 'Activity', 'get');
385 if ($options['is_count']) {
386 return civicrm_api3_create_success($activities, $params, 'Activity', 'get');
387 }
388
389 $activities = _civicrm_api3_activity_get_formatResult($params, $activities, $options);
390 //legacy custom data get - so previous formatted response is still returned too
391 return civicrm_api3_create_success($activities, $params, 'Activity', 'get');
392 }
393
394 /**
395 * Given a list of activities, append any extra data requested about the activities.
396 *
397 * @note Called by civicrm-core and CiviHR
398 *
399 * @param array $params
400 * API request parameters.
401 * @param array $activities
402 *
403 * @return array
404 * new activities list
405 */
406 function _civicrm_api3_activity_get_formatResult($params, $activities, $options) {
407 if (!$activities) {
408 return $activities;
409 }
410
411 $returns = $options['return'];
412 foreach ($params as $n => $v) {
413 if (substr($n, 0, 7) == 'return.') {
414 $returnkey = substr($n, 7);
415 $returns[$returnkey] = $v;
416 }
417 }
418
419 $returns['source_contact_id'] = 1;
420 if (!empty($returns['target_contact_name'])) {
421 $returns['target_contact_id'] = 1;
422 }
423 if (!empty($returns['assignee_contact_name'])) {
424 $returns['assignee_contact_id'] = 1;
425 }
426
427 $tagGet = array('tag_id', 'entity_id');
428 foreach (array_keys($returns) as $key) {
429 if (strpos($key, 'tag_id.') === 0) {
430 $tagGet[] = $key;
431 $returns['tag_id'] = 1;
432 }
433 }
434
435 foreach ($returns as $n => $v) {
436 switch ($n) {
437 case 'assignee_contact_id':
438 foreach ($activities as $key => $activityArray) {
439 $cids = $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']);
440 if ($cids && !empty($returns['assignee_contact_name'])) {
441 foreach ($cids as $cid) {
442 $activities[$key]['assignee_contact_name'][$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
443 }
444 }
445 }
446 break;
447
448 case 'target_contact_id':
449 foreach ($activities as $key => $activityArray) {
450 $cids = $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']);
451 if ($cids && !empty($returns['target_contact_name'])) {
452 foreach ($cids as $cid) {
453 $activities[$key]['target_contact_name'][$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
454 }
455 }
456 }
457 break;
458
459 case 'source_contact_id':
460 foreach ($activities as $key => $activityArray) {
461 $cid = $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity::getSourceContactID($activityArray['id']);
462 if ($cid && !empty($returns['source_contact_name'])) {
463 $activities[$key]['source_contact_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
464 }
465 }
466 break;
467
468 case 'tag_id':
469 $tags = civicrm_api3('EntityTag', 'get', array(
470 'entity_table' => 'civicrm_activity',
471 'entity_id' => array('IN' => array_keys($activities)),
472 'return' => $tagGet,
473 'options' => array('limit' => 0),
474 ));
475 foreach ($tags['values'] as $tag) {
476 $key = (int) $tag['entity_id'];
477 unset($tag['entity_id'], $tag['id']);
478 $activities[$key]['tag_id'][$tag['tag_id']] = $tag;
479 }
480 break;
481
482 case 'file_id':
483 $dao = CRM_Core_DAO::executeQuery("SELECT entity_id, file_id FROM civicrm_entity_file WHERE entity_table = 'civicrm_activity' AND entity_id IN (%1)",
484 array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)));
485 while ($dao->fetch()) {
486 $activities[$dao->entity_id]['file_id'][] = $dao->file_id;
487 }
488 break;
489
490 case 'case_id':
491 $dao = CRM_Core_DAO::executeQuery("SELECT activity_id, case_id FROM civicrm_case_activity WHERE activity_id IN (%1)",
492 array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)));
493 while ($dao->fetch()) {
494 $activities[$dao->activity_id]['case_id'][] = $dao->case_id;
495 }
496 break;
497
498 default:
499 if (substr($n, 0, 6) == 'custom') {
500 $returnProperties[$n] = $v;
501 }
502 }
503 }
504
505 // Legacy extras
506 if (!empty($params['contact_id'])) {
507 $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get');
508 $typeOptions = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
509 foreach ($activities as $key => &$activityArray) {
510 if (!empty($activityArray['status_id'])) {
511 $activityArray['status'] = $statusOptions[$activityArray['status_id']];
512 }
513 if (!empty($activityArray['activity_type_id'])) {
514 $activityArray['activity_name'] = $typeOptions[$activityArray['activity_type_id']];
515 }
516 }
517 }
518
519 if (!empty($returnProperties) || !empty($params['contact_id'])) {
520 foreach ($activities as $activityId => $values) {
521 //@todo - should possibly load activity type id if not loaded (update with id)
522 _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));
523 }
524 }
525 return $activities;
526 }
527
528
529 /**
530 * Delete a specified Activity.
531 *
532 * @param array $params
533 * Array holding 'id' of activity to be deleted.
534 *
535 * @throws API_Exception
536 *
537 * @return array
538 * API result array
539 */
540 function civicrm_api3_activity_delete($params) {
541
542 if (CRM_Activity_BAO_Activity::deleteActivity($params)) {
543 return civicrm_api3_create_success(1, $params, 'Activity', 'delete');
544 }
545 else {
546 throw new API_Exception('Could not delete Activity');
547 }
548 }
549
550 /**
551 * Check for required params.
552 *
553 * @param array $params
554 * Associated array of fields.
555 *
556 * @throws API_Exception
557 * @throws Exception
558 * @return array
559 * array with errors
560 */
561 function _civicrm_api3_activity_check_params(&$params) {
562 $activityIds = array(
563 'activity' => CRM_Utils_Array::value('id', $params),
564 'parent' => CRM_Utils_Array::value('parent_id', $params),
565 'original' => CRM_Utils_Array::value('original_id', $params),
566 );
567
568 foreach ($activityIds as $id => $value) {
569 if ($value &&
570 !CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id')
571 ) {
572 throw new API_Exception('Invalid ' . ucfirst($id) . ' Id');
573 }
574 }
575 // this should be handled by wrapper layer & probably the api would already manage it
576 //correctly by doing pseudoconstant validation
577 // needs testing
578 $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
579 $activityName = CRM_Utils_Array::value('activity_name', $params);
580 $activityName = ucfirst($activityName);
581 $activityLabel = CRM_Utils_Array::value('activity_label', $params);
582 if ($activityLabel) {
583 $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'create');
584 }
585
586 $activityTypeId = CRM_Utils_Array::value('activity_type_id', $params);
587
588 if ($activityName || $activityLabel) {
589 $activityTypeIdInList = array_search(($activityName ? $activityName : $activityLabel), $activityTypes);
590
591 if (!$activityTypeIdInList) {
592 $errorString = $activityName ? "Invalid Activity Name : $activityName" : "Invalid Activity Type Label";
593 throw new Exception($errorString);
594 }
595 elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) {
596 throw new API_Exception('Mismatch in Activity');
597 }
598 $params['activity_type_id'] = $activityTypeIdInList;
599 }
600 elseif ($activityTypeId &&
601 !array_key_exists($activityTypeId, $activityTypes)
602 ) {
603 throw new API_Exception('Invalid Activity Type ID');
604 }
605
606 // check for activity duration minutes
607 // this should be validated @ the wrapper layer not here
608 // needs testing
609 if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) {
610 throw new API_Exception('Invalid Activity Duration (in minutes)');
611 }
612
613 //if adding a new activity & date_time not set make it now
614 // this should be managed by the wrapper layer & setting ['api.default'] in speces
615 // needs testing
616 if (empty($params['id']) && empty($params['activity_date_time'])) {
617 $params['activity_date_time'] = CRM_Utils_Date::processDate(date('Y-m-d H:i:s'));
618 }
619
620 return NULL;
621 }
622
623 /**
624 * Get parameters for activity list.
625 *
626 * @see _civicrm_api3_generic_getlist_params
627 *
628 * @param array $request
629 * API request.
630 */
631 function _civicrm_api3_activity_getlist_params(&$request) {
632 $fieldsToReturn = array(
633 'activity_date_time',
634 'activity_type_id',
635 'subject',
636 'source_contact_id',
637 );
638 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
639 $request['params']['options']['sort'] = 'activity_date_time DESC';
640 $request['params'] += array(
641 'is_current_revision' => 1,
642 'is_deleted' => 0,
643 );
644 }
645
646 /**
647 * Get output for activity list.
648 *
649 * @see _civicrm_api3_generic_getlist_output
650 *
651 * @param array $result
652 * @param array $request
653 *
654 * @return array
655 */
656 function _civicrm_api3_activity_getlist_output($result, $request) {
657 $output = array();
658 if (!empty($result['values'])) {
659 foreach ($result['values'] as $row) {
660 $data = array(
661 'id' => $row[$request['id_field']],
662 'label' => $row[$request['label_field']] ? $row[$request['label_field']] : ts('(no subject)'),
663 'description' => array(
664 CRM_Core_Pseudoconstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id']),
665 ),
666 );
667 if (!empty($row['activity_date_time'])) {
668 $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['activity_date_time']);
669 }
670 if (!empty($row['source_contact_id'])) {
671 $data['description'][] = ts('By %1', array(
672 1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name'),
673 ));
674 }
675 // Add repeating info
676 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_activity');
677 $data['extra']['is_recur'] = FALSE;
678 if ($repeat) {
679 $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
680 $data['extra']['is_recur'] = TRUE;
681 }
682 $output[] = $data;
683 }
684 }
685 return $output;
686 }