5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.5 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2014 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 activity functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Activity
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: Activity.php 30486 2010-11-02 16:12:09Z shot $
42 * Creates or updates an Activity. See the example for usage
44 * @param array $params Associative array of property name/value
45 * pairs for the activity.
46 * {@getfields activity_create}
48 * @return array Array containing 'is_error' to denote success or failure and details of the created activity
50 * @example ActivityCreate.php Standard create example
51 * @example Activity/ContactRefCustomField.php Create example including setting a contact reference custom field
52 * {@example ActivityCreate.php 0}
55 function civicrm_api3_activity_create($params) {
57 if (empty($params['id'])) {
58 // an update does not require any mandatory parameters
59 civicrm_api3_verify_one_mandatory($params,
62 'activity_name', 'activity_type_id', 'activity_label',
69 // check for various error and required conditions
70 // note that almost all the processing in there should be managed by the wrapper layer
71 // & should be removed - needs testing
72 $errors = _civicrm_api3_activity_check_params($params);
74 // this should not be required as should throw exception rather than return errors -
76 if (!empty($errors)) {
81 // processing for custom data
83 _civicrm_api3_custom_format_params($params, $values, 'Activity');
85 if (!empty($values['custom'])) {
86 $params['custom'] = $values['custom'];
89 // this should be set as a default rather than hard coded
91 $params['skipRecentView'] = TRUE;
93 // If this is a case activity, see if there is an existing activity
94 // and set it as an old revision. Also retrieve details we'll need.
95 // this handling should all be moved to the BAO layer
97 $createRevision = FALSE;
98 $oldActivityValues = array();
99 if (!empty($params['case_id'])) {
100 $case_id = $params['case_id'];
101 if (!empty($params['id'])) {
102 $oldActivityParams = array('id' => $params['id']);
103 if (!$oldActivityValues) {
104 CRM_Activity_BAO_Activity
::retrieve($oldActivityParams, $oldActivityValues);
106 if (empty($oldActivityValues)) {
107 return civicrm_api3_create_error(ts("Unable to locate existing activity."));
110 $activityDAO = new CRM_Activity_DAO_Activity();
111 $activityDAO->id
= $params['id'];
112 $activityDAO->is_current_revision
= 0;
113 if (!$activityDAO->save()) {
114 if (is_object($activityDAO)) {
115 $activityDAO->free();
117 return civicrm_api3_create_error(ts("Unable to revision existing case activity."));
119 $createRevision = TRUE;
124 $deleteActivityAssignment = FALSE;
125 if (isset($params['assignee_contact_id'])) {
126 $deleteActivityAssignment = TRUE;
129 $deleteActivityTarget = FALSE;
130 if (isset($params['target_contact_id'])) {
131 $deleteActivityTarget = TRUE;
134 // this should all be handled at the BAO layer
135 $params['deleteActivityAssignment'] = CRM_Utils_Array
::value('deleteActivityAssignment', $params, $deleteActivityAssignment);
136 $params['deleteActivityTarget'] = CRM_Utils_Array
::value('deleteActivityTarget', $params, $deleteActivityTarget);
138 if ($case_id && $createRevision) {
139 // This is very similar to the copy-to-case action.
140 if (!CRM_Utils_Array
::crmIsEmptyArray($oldActivityValues['target_contact'])) {
141 $oldActivityValues['targetContactIds'] = implode(',', array_unique($oldActivityValues['target_contact']));
143 if (!CRM_Utils_Array
::crmIsEmptyArray($oldActivityValues['assignee_contact'])) {
144 $oldActivityValues['assigneeContactIds'] = implode(',', array_unique($oldActivityValues['assignee_contact']));
146 $oldActivityValues['mode'] = 'copy';
147 $oldActivityValues['caseID'] = $case_id;
148 $oldActivityValues['activityID'] = $oldActivityValues['id'];
149 $oldActivityValues['contactID'] = $oldActivityValues['source_contact_id'];
151 $copyToCase = CRM_Activity_Page_AJAX
::_convertToCaseActivity($oldActivityValues);
152 if (empty($copyToCase['error_msg'])) {
153 // now fix some things that are different from copy-to-case
154 // then fall through to the create below to update with the passed in params
155 $params['id'] = $copyToCase['newId'];
156 $params['is_auto'] = 0;
157 $params['original_id'] = empty($oldActivityValues['original_id']) ?
$oldActivityValues['id'] : $oldActivityValues['original_id'];
160 return civicrm_api3_create_error(ts("Unable to create new revision of case activity."));
165 $activityBAO = CRM_Activity_BAO_Activity
::create($params);
167 if (isset($activityBAO->id
)) {
168 if ($case_id && !$createRevision) {
169 // If this is a brand new case activity we need to add this
170 $caseActivityParams = array('activity_id' => $activityBAO->id
, 'case_id' => $case_id);
171 CRM_Case_BAO_Case
::processCaseActivity($caseActivityParams);
174 _civicrm_api3_object_to_array($activityBAO, $activityArray[$activityBAO->id
]);
175 return civicrm_api3_create_success($activityArray, $params, 'activity', 'get', $activityBAO);
180 * Specify Meta data for create. Note that this data is retrievable via the getfields function
181 * and is used for pre-filling defaults and ensuring mandatory requirements are met.
182 * @param array $params (reference) array of parameters determined by getfields
184 function _civicrm_api3_activity_create_spec(&$params) {
186 //default for source_contact_id = currently logged in user
187 $params['source_contact_id']['api.default'] = 'user_contact_id';
189 $params['status_id']['api.aliases'] = array('activity_status');
191 $params['assignee_contact_id'] = array(
192 'name' => 'assignee_id',
193 'title' => 'assigned to',
195 'FKClassName' => 'CRM_Activity_DAO_ActivityContact',
197 $params['target_contact_id'] = array(
198 'name' => 'target_id',
199 'title' => 'Activity Target',
201 'FKClassName' => 'CRM_Activity_DAO_ActivityContact',
204 $params['source_contact_id'] = array(
205 'name' => 'source_contact_id',
206 'title' => 'Activity Source Contact',
208 'FKClassName' => 'CRM_Activity_DAO_ActivityContact',
209 'api.default' => 'user_contact_id',
215 * Gets a CiviCRM activity according to parameters
217 * @param array $params Associative array of property name/value
218 * pairs for the activity.
222 * {@getfields activity_get}
223 * @example ActivityGet.php Basic example
224 * @example Activity/DateTimeHigh.php Example get with date filtering
225 * {@example ActivityGet.php 0}
227 function civicrm_api3_activity_get($params) {
228 if (!empty($params['contact_id'])) {
229 $activities = CRM_Activity_BAO_Activity
::getContactActivity($params['contact_id']);
230 //BAO function doesn't actually return a contact ID - hack api for now & add to test so when api re-write happens it won't get missed
231 foreach ($activities as $key => $activityArray) {
232 $activities[$key]['id'] = $key;
236 $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE);
238 $options = _civicrm_api3_get_options_from_params($params, FALSE,'activity','get');
239 if($options['is_count']) {
240 return civicrm_api3_create_success($activities, $params, 'activity', 'get');
243 $activities = _civicrm_api3_activity_get_formatResult($params, $activities);
244 //legacy custom data get - so previous formatted response is still returned too
245 return civicrm_api3_create_success($activities, $params, 'activity', 'get');
249 * Given a list of activities, append any extra data requested about the activities
251 * NOTE: Called by civicrm-core and CiviHR
253 * @param array $params API request parameters
254 * @param array $activities
255 * @return array new activities list
257 function _civicrm_api3_activity_get_formatResult($params, $activities) {
258 $returns = CRM_Utils_Array
::value('return', $params, array());
259 if (!is_array($returns)) {
260 $returns = str_replace(' ', '', $returns);
261 $returns = explode(',', $returns);
263 $returns = array_fill_keys($returns, 1);
265 foreach ($params as $n => $v) {
266 if (substr($n, 0, 7) == 'return.') {
267 $returnkey = substr($n, 7);
268 $returns[$returnkey] = $v;
271 $returns['source_contact_id'] = 1;
272 foreach ($returns as $n => $v) {
274 case 'assignee_contact_id':
275 foreach ($activities as $key => $activityArray) {
276 $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment
::retrieveAssigneeIdsByActivityId($activityArray['id']);
279 case 'target_contact_id':
280 foreach ($activities as $key => $activityArray) {
281 $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget
::retrieveTargetIdsByActivityId($activityArray['id']);
284 case 'source_contact_id':
285 foreach ($activities as $key => $activityArray) {
286 $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity
::getSourceContactID($activityArray['id']);
290 if (substr($n, 0, 6) == 'custom') {
291 $returnProperties[$n] = $v;
295 if (!empty($activities) && (!empty($returnProperties) ||
!empty($params['contact_id']))) {
296 foreach ($activities as $activityId => $values) {
297 _civicrm_api3_custom_data_get($activities[$activityId], 'Activity', $activityId, NULL, $values['activity_type_id']);
306 * Delete a specified Activity.
308 * @param array $params array holding 'id' of activity to be deleted
309 * {@getfields activity_delete}
311 * @return void|CRM_Core_Error An error if 'activityName or ID' is invalid,
312 * permissions are insufficient, etc. or CiviCRM success array
316 * @example ActivityDelete.php Standard Delete Example
320 function civicrm_api3_activity_delete($params) {
322 if (CRM_Activity_BAO_Activity
::deleteActivity($params)) {
323 return civicrm_api3_create_success(1, $params, 'activity', 'delete');
326 return civicrm_api3_create_error('Could not delete activity');
331 * Function to check for required params
333 * @param array $params associated array of fields
334 * @param boolean $addMode true for add mode
336 * @return array $error array with errors
338 function _civicrm_api3_activity_check_params(&$params) {
340 $contactIDFields = array_intersect_key($params,
342 'source_contact_id' => 1,
343 'assignee_contact_id' => 1,
344 'target_contact_id' => 1,
348 // this should be handled by wrapper layer & probably the api would already manage it
349 //correctly by doing post validation - ie. a failure should result in a roll-back = an error
351 if (!empty($contactIDFields)) {
352 $contactIds = array();
353 foreach ($contactIDFields as $fieldname => $contactfield) {
354 if (empty($contactfield)) {
357 if (is_array($contactfield)) {
358 foreach ($contactfield as $contactkey => $contactvalue) {
359 $contactIds[$contactvalue] = $contactvalue;
363 $contactIds[$contactfield] = $contactfield;
371 WHERE id IN (' . implode(', ', $contactIds) . ' )';
372 if (count($contactIds) != CRM_Core_DAO
::singleValueQuery($sql)) {
373 return civicrm_api3_create_error('Invalid ' . ' Contact Id');
378 $activityIds = array('activity' => CRM_Utils_Array
::value('id', $params),
379 'parent' => CRM_Utils_Array
::value('parent_id', $params),
380 'original' => CRM_Utils_Array
::value('original_id', $params),
383 foreach ($activityIds as $id => $value) {
385 !CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id')
387 return civicrm_api3_create_error('Invalid ' . ucfirst($id) . ' Id');
390 // this should be handled by wrapper layer & probably the api would already manage it
391 //correctly by doing pseudoconstant validation
393 $activityTypes = CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate');
394 $activityName = CRM_Utils_Array
::value('activity_name', $params);
395 $activityName = ucfirst($activityName);
396 $activityLabel = CRM_Utils_Array
::value('activity_label', $params);
397 if ($activityLabel) {
398 $activityTypes = CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'create');
401 $activityTypeId = CRM_Utils_Array
::value('activity_type_id', $params);
403 if ($activityName ||
$activityLabel) {
404 $activityTypeIdInList = array_search(($activityName ?
$activityName : $activityLabel), $activityTypes);
406 if (!$activityTypeIdInList) {
407 $errorString = $activityName ?
"Invalid Activity Name : $activityName" : "Invalid Activity Type Label";
408 throw new Exception($errorString);
410 elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) {
411 return civicrm_api3_create_error('Mismatch in Activity');
413 $params['activity_type_id'] = $activityTypeIdInList;
415 elseif ($activityTypeId &&
416 !array_key_exists($activityTypeId, $activityTypes)
418 return civicrm_api3_create_error('Invalid Activity Type ID');
421 // check for activity duration minutes
422 // this should be validated @ the wrapper layer not here
424 if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) {
425 return civicrm_api3_create_error('Invalid Activity Duration (in minutes)');
429 //if adding a new activity & date_time not set make it now
430 // this should be managed by the wrapper layer & setting ['api.default'] in speces
432 if (empty($params['id']) && empty($params['activity_date_time'])) {
433 $params['activity_date_time'] = CRM_Utils_Date
::processDate(date('Y-m-d H:i:s'));