2 // $Id: Activity.php 45502 2013-02-08 13:32:55Z kurund $
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
32 * File for the CiviCRM APIv2 activity functions
34 * @package CiviCRM_APIv2
35 * @subpackage API_Activity
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: Activity.php 45502 2013-02-08 13:32:55Z kurund $
42 * Include common API util functions
44 require_once 'api/v2/utils.php';
46 require_once 'CRM/Activity/BAO/Activity.php';
47 require_once 'CRM/Core/DAO/OptionGroup.php';
49 // require these to call new function names from deprecated ones in here
50 require_once 'api/v2/ActivityType.php';
51 require_once 'api/v2/ActivityContact.php';
54 * Create a new Activity.
56 * Creates a new Activity record and returns the newly created
57 * activity object (including the contact_id property). Minimum
58 * required data values for the various contact_type are:
60 * Properties which have administratively assigned sets of values
61 * If an unrecognized value is passed, an error
64 * Modules may invoke crm_get_contact_values($contactID) to
65 * retrieve a list of currently available values for a given
68 * @param array $params Associative array of property name/value
69 * pairs to insert in new contact.
70 * @param string $activity_type Which class of contact is being created.
71 * Valid values = 'SMS', 'Meeting', 'Event', 'PhoneCall'.
72 * {@schema Activity/Activity.xml}
74 * @return CRM_Activity|CRM_Error Newly created Activity object
76 function &civicrm_activity_create(&$params) {
77 _civicrm_initialize();
81 // check for various error and required conditions
82 $errors = _civicrm_activity_check_params($params, TRUE);
84 if (!empty($errors)) {
88 // processing for custom data
90 _civicrm_custom_format_params($params, $values, 'Activity');
91 if (!empty($values['custom'])) {
92 $params['custom'] = $values['custom'];
96 $activity = CRM_Activity_BAO_Activity
::create($params);
98 if (!is_a($activity, 'CRM_Core_Error') && isset($activity->id
)) {
99 $activityArray = array('is_error' => 0);
102 $activityArray = array('is_error' => 1);
105 _civicrm_object_to_array($activity, $activityArray);
107 return $activityArray;
112 * @param <type> $params
113 * @param <type> $returnCustom
117 function civicrm_activity_get($params, $returnCustom = FALSE) {
118 _civicrm_initialize();
120 $activityId = CRM_Utils_Array
::value('activity_id', $params);
121 if (empty($activityId)) {
122 return civicrm_create_error(ts("Required parameter not found"));
125 if (!is_numeric($activityId)) {
126 return civicrm_create_error(ts("Invalid activity Id"));
129 $activity = _civicrm_activity_get($activityId, $returnCustom);
132 return civicrm_create_success($activity);
135 return civicrm_create_error(ts('Invalid Data'));
140 * Wrapper to make this function compatible with the REST API
142 * Obsolete now; if no one is using this, it should be removed. -- Wes Morgan
144 function civicrm_activity_get_contact($params) {
145 // TODO: Spit out deprecation warning here
146 return civicrm_activities_get_contact($params);
150 * Retrieve a set of activities, specific to given input params.
152 * @param array $params (reference ) input parameters.
153 * @deprecated from 3.4 - use civicrm_activity_contact_get
155 * @return array (reference) array of activities / error message.
158 function civicrm_activities_get_contact($params) {
159 // TODO: Spit out deprecation warning here
160 return civicrm_activity_contact_get($params);
164 * Update a specified activity.
166 * Updates activity with the values passed in the 'params' array. An
167 * error is returned if an invalid id or activity Name is passed
169 * @param CRM_Activity $activity A valid Activity object
170 * @param array $params Associative array of property
171 * name/value pairs to be updated.
173 * @return CRM_Activity|CRM_Core_Error Return the updated ActivtyType Object else
174 * Error Object (if integrity violation)
179 function &civicrm_activity_update(&$params) {
181 //check for various error and required conditions
182 $errors = _civicrm_activity_check_params($params);
184 if (!empty($errors)) {
188 // processing for custom data
190 _civicrm_custom_format_params($params, $values, 'Activity');
191 if (!empty($values['custom'])) {
192 $params['custom'] = $values['custom'];
195 $activity = CRM_Activity_BAO_Activity
::create($params);
196 $activityArray = array();
197 _civicrm_object_to_array($activity, $activityArray);
199 return $activityArray;
203 * Delete a specified Activity.
205 * @param CRM_Activity $activity Activity object to be deleted
207 * @return void|CRM_Core_Error An error if 'activityName or ID' is invalid,
208 * permissions are insufficient, etc.
213 function civicrm_activity_delete(&$params) {
214 _civicrm_initialize();
218 //check for various error and required conditions
219 $errors = _civicrm_activity_check_params($params);
221 if (!empty($errors)) {
225 if (CRM_Activity_BAO_Activity
::deleteActivity($params)) {
226 return civicrm_create_success();
229 return civicrm_create_error(ts('Could not delete activity'));
234 * Retrieve a specific Activity by Id.
236 * @param int $activityId
238 * @return array (reference) activity object
241 function _civicrm_activity_get($activityId, $returnCustom = FALSE) {
242 $dao = new CRM_Activity_BAO_Activity();
243 $dao->id
= $activityId;
244 if ($dao->find(TRUE)) {
246 _civicrm_object_to_array($dao, $activity);
248 //also return custom data if needed.
249 if ($returnCustom && !empty($activity)) {
250 $customdata = civicrm_activity_custom_get(array(
251 'activity_id' => $activityId,
252 'activity_type_id' => $activity['activity_type_id'],
254 $activity = array_merge($activity, $customdata);
265 * Function to check for required params
267 * @param array $params associated array of fields
268 * @param boolean $addMode true for add mode
270 * @return array $error array with errors
272 function _civicrm_activity_check_params(&$params, $addMode = FALSE) {
273 // return error if we do not get any params
274 if (empty($params)) {
275 return civicrm_create_error(ts('Input Parameters empty'));
278 $contactIds = array('source' => CRM_Utils_Array
::value('source_contact_id', $params),
279 'assignee' => CRM_Utils_Array
::value('assignee_contact_id', $params),
280 'target' => CRM_Utils_Array
::value('target_contact_id', $params),
283 foreach ($contactIds as $key => $value) {
287 $valueIds = array($value);
288 if (is_array($value)) {
290 foreach ($value as $id) {
291 if (is_numeric($id)) {
292 $valueIds[$id] = $id;
296 elseif (!is_numeric($value)) {
297 return civicrm_create_error(ts('Invalid %1 Contact Id', array(
303 if (empty($valueIds)) {
310 WHERE id IN (' . implode(', ', $valueIds) . ' )';
311 if (count($valueIds) != CRM_Core_DAO
::singleValueQuery($sql)) {
312 return civicrm_create_error(ts('Invalid %1 Contact Id', array(1 => ucfirst($key))));
316 $activityIds = array('activity' => CRM_Utils_Array
::value('id', $params),
317 'parent' => CRM_Utils_Array
::value('parent_id', $params),
318 'original' => CRM_Utils_Array
::value('original_id', $params),
321 foreach ($activityIds as $id => $value) {
323 !CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id')
325 return civicrm_create_error(ts('Invalid %1 Id', array(1 => ucfirst($id))));
329 if (!$addMode && !isset($params['id'])) {
330 return civicrm_create_error(ts('Required parameter "id" not found'));
333 // check for source contact id
334 if ($addMode && empty($params['source_contact_id'])) {
335 return civicrm_create_error(ts('Missing Source Contact'));
338 // check for activity subject if add mode
339 if ($addMode && !isset($params['subject'])) {
340 return civicrm_create_error(ts('Missing Subject'));
343 if (!$addMode && $params['id'] && !is_numeric($params['id'])) {
344 return civicrm_create_error(ts('Invalid activity "id"'));
347 require_once 'CRM/Core/PseudoConstant.php';
348 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name');
350 // check if activity type_id is passed in
351 if ($addMode && !isset($params['activity_name']) && !isset($params['activity_type_id'])) {
352 //when name AND id are both absent
353 return civicrm_create_error(ts('Missing Activity Type'));
356 $activityName = CRM_Utils_Array
::value('activity_name', $params);
357 $activityTypeId = CRM_Utils_Array
::value('activity_type_id', $params);
360 $activityNameId = array_search(ucfirst($activityName), $activityTypes);
362 if (!$activityNameId) {
363 return civicrm_create_error(ts('Invalid Activity Name'));
365 elseif ($activityTypeId && ($activityTypeId != $activityNameId)) {
366 return civicrm_create_error(ts('Mismatch in Activity'));
368 $params['activity_type_id'] = $activityNameId;
370 elseif ($activityTypeId &&
371 !array_key_exists($activityTypeId, $activityTypes)
373 return civicrm_create_error(ts('Invalid Activity Type ID'));
377 // check for activity status is passed in
378 if (isset($params['status_id'])) {
379 require_once "CRM/Core/PseudoConstant.php";
380 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
382 if (is_numeric($params['status_id']) && !array_key_exists($params['status_id'], $activityStatus)) {
383 return civicrm_create_error(ts('Invalid Activity Status'));
385 elseif (!is_numeric($params['status_id'])) {
386 $statusId = array_search($params['status_id'], $activityStatus);
388 if (!is_numeric($statusId)) {
389 return civicrm_create_error(ts('Invalid Activity Status'));
394 if (isset($params['priority_id']) && is_numeric($params['priority_id'])) {
395 require_once "CRM/Core/PseudoConstant.php";
396 $activityPriority = CRM_Core_PseudoConstant
::priority();
397 if (!array_key_exists($params['priority_id'], $activityPriority)) {
398 return civicrm_create_error(ts('Invalid Priority'));
402 // check for activity duration minutes
403 if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) {
404 return civicrm_create_error(ts('Invalid Activity Duration (in minutes)'));
408 !CRM_Utils_Array
::value('activity_date_time', $params)
410 $params['activity_date_time'] = CRM_Utils_Date
::processDate(date('Y-m-d H:i:s'));
413 if (CRM_Utils_Array
::value('activity_date_time', $params)) {
414 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time']);
422 * Convert an email file to an activity
424 function civicrm_activity_processemail($file, $activityTypeID, $result = array(
426 // do not parse if result array already passed (towards EmailProcessor..)
427 if (empty($result)) {
428 // might want to check that email is ok here
429 if (!file_exists($file) ||
432 return CRM_Core_Error
::createAPIError(ts('File %1 does not exist or is not readable',
438 require_once 'CRM/Utils/Mail/Incoming.php';
439 $result = CRM_Utils_Mail_Incoming
::parse($file);
440 if ($result['is_error']) {
444 $params = _civicrm_activity_buildmailparams($result, $activityTypeID);
445 return civicrm_activity_create($params);
450 * @param <type> $result
451 * @param <type> $activityTypeID
455 function _civicrm_activity_buildmailparams($result, $activityTypeID) {
456 // get ready for collecting data about activity to be created
459 $params['activity_type_id'] = $activityTypeID;
460 $params['status_id'] = 2;
461 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
462 $params['target_contact_id'] = array();
463 $keys = array('to', 'cc', 'bcc');
464 foreach ($keys as $key) {
465 if (is_array($result[$key])) {
466 foreach ($result[$key] as $key => $keyValue) {
467 if (!empty($keyValue['id'])) {
468 $params['target_contact_id'][] = $keyValue['id'];
473 $params['subject'] = $result['subject'];
474 $params['activity_date_time'] = $result['date'];
475 $params['details'] = $result['body'];
477 for ($i = 1; $i <= 5; $i++
) {
478 if (isset($result["attachFile_$i"])) {
479 $params["attachFile_$i"] = $result["attachFile_$i"];
488 * @param <type> $file
489 * @param <type> $activityTypeID
492 * @deprecated since 3.4 use civicrm_activity_processemail
494 function civicrm_activity_process_email($file, $activityTypeID) {
495 // TODO: Spit out deprecation warning here
496 return civicrm_activity_processemail($file, $activityTypeID);
500 * @deprecated since 3.4 use civicrm_activity_type_get
504 function civicrm_activity_get_types() {
505 // TODO: Spit out deprecation warning here
506 return civicrm_activity_type_get();
510 * Function retrieve activity custom data.
512 * @param array $params key => value array.
514 * @return array $customData activity custom data
518 function civicrm_activity_custom_get($params) {
520 $customData = array();
521 if (!CRM_Utils_Array
::value('activity_id', $params)) {
525 require_once 'CRM/Core/BAO/CustomGroup.php';
526 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Activity',
527 CRM_Core_DAO
::$_nullObject,
528 $params['activity_id'],
530 CRM_Utils_Array
::value('activity_type_id', $params)
532 //get the group count.
534 foreach ($groupTree as $key => $value) {
535 if ($key === 'info') {
540 $formattedGroupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree,
542 CRM_Core_DAO
::$_nullObject
545 CRM_Core_BAO_CustomGroup
::setDefaults($formattedGroupTree, $defaults);
546 if (!empty($defaults)) {
547 foreach ($defaults as $key => $val) {
548 $customData[$key] = $val;