Update debugging page text
[civicrm-core.git] / api / v3 / Activity.php
index 951ee68e174a7fcc3a972c7f13ea6cb8fdbb2b35..d3b43a2e28dd1969f9bd6809f08f27313f95598f 100644 (file)
@@ -1,9 +1,7 @@
 <?php
-// $Id$
-
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  */
 
 /**
- * File for the CiviCRM APIv3 activity functions
+ * This api exposes CiviCRM activity.
  *
  * @package CiviCRM_APIv3
- * @subpackage API_Activity
- * @copyright CiviCRM LLC (c) 2004-2014
- * @version $Id: Activity.php 30486 2010-11-02 16:12:09Z shot $
- *
  */
 
 
 /**
- * Creates or updates an Activity. See the example for usage
+ * Creates or updates an Activity.
  *
- * @param array $params Associative array of property name/value
- *                             pairs for the activity.
- * {@getfields activity_create}
+ * @param array $params
+ *   Array per getfields documentation.
  *
  * @throws API_Exception
- * @return array Array containing 'is_error' to denote success or failure and details of the created activity
- *
- * @example ActivityCreate.php Standard create example
- * @example Activity/ContactRefCustomField.php Create example including setting a contact reference custom field
- * {@example ActivityCreate.php 0}
+ * @return array
+ *   Array containing 'is_error' to denote success or failure and details of the created activity.
  */
 function civicrm_api3_activity_create($params) {
 
@@ -59,7 +49,9 @@ function civicrm_api3_activity_create($params) {
     civicrm_api3_verify_one_mandatory($params,
       NULL,
       array(
-        'activity_name', 'activity_type_id', 'activity_label',
+        'activity_name',
+        'activity_type_id',
+        'activity_label',
       )
     );
   }
@@ -75,7 +67,6 @@ function civicrm_api3_activity_create($params) {
     return $errors;
   }
 
-
   // processing for custom data
   $values = $activityArray = array();
   _civicrm_api3_custom_format_params($params, $values, 'Activity');
@@ -91,9 +82,13 @@ function civicrm_api3_activity_create($params) {
   // If this is a case activity, see if there is an existing activity
   // and set it as an old revision. Also retrieve details we'll need.
   // this handling should all be moved to the BAO layer
-  $case_id           = '';
-  $createRevision    = FALSE;
+  $case_id = '';
+  $createRevision = FALSE;
   $oldActivityValues = array();
+  // Lookup case id if not supplied
+  if (!isset($params['case_id']) && !empty($params['id'])) {
+    $params['case_id'] = CRM_Core_DAO::singleValueQuery("SELECT case_id FROM civicrm_case_activity WHERE activity_id = " . (int) $params['id']);
+  }
   if (!empty($params['case_id'])) {
     $case_id = $params['case_id'];
     if (!empty($params['id'])) {
@@ -175,13 +170,17 @@ function civicrm_api3_activity_create($params) {
 }
 
 /**
- * Specify Meta data for create. Note that this data is retrievable via the getfields function
- * and is used for pre-filling defaults and ensuring mandatory requirements are met.
- * @param array $params (reference) array of parameters determined by getfields
+ * Specify Meta data for create.
+ *
+ * Note that this data is retrievable via the getfields function and is used for pre-filling defaults and
+ * ensuring mandatory requirements are met.
+ *
+ * @param array $params
+ *   Array of parameters determined by getfields.
  */
 function _civicrm_api3_activity_create_spec(&$params) {
 
-  //default for source_contact_id = currently logged in user
+  // Default for source_contact_id = currently logged in user.
   $params['source_contact_id']['api.default'] = 'user_contact_id';
 
   $params['status_id']['api.aliases'] = array('activity_status');
@@ -200,32 +199,28 @@ function _civicrm_api3_activity_create_spec(&$params) {
   );
 
   $params['source_contact_id'] = array(
-      'name' => 'source_contact_id',
-      'title' => 'Activity Source Contact',
-      'type' => 1,
-      'FKClassName' => 'CRM_Activity_DAO_ActivityContact',
-      'api.default' => 'user_contact_id',
+    'name' => 'source_contact_id',
+    'title' => 'Activity Source Contact',
+    'type' => 1,
+    'FKClassName' => 'CRM_Activity_DAO_ActivityContact',
+    'api.default' => 'user_contact_id',
   );
 
 }
 
 /**
- * Gets a CiviCRM activity according to parameters
+ * Gets a CiviCRM activity according to parameters.
  *
- * @param array  $params       Associative array of property name/value
- *                             pairs for the activity.
+ * @param array $params
+ *   Array per getfields documentation.
  *
  * @return array
- *
- * {@getfields activity_get}
- * @example ActivityGet.php Basic example
- * @example Activity/DateTimeHigh.php Example get with date filtering
- * {@example ActivityGet.php 0}
  */
 function civicrm_api3_activity_get($params) {
   if (!empty($params['contact_id'])) {
     $activities = CRM_Activity_BAO_Activity::getContactActivity($params['contact_id']);
-    //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
+    // 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.
     foreach ($activities as $key => $activityArray) {
       $activities[$key]['id'] = $key;
     }
@@ -233,8 +228,8 @@ function civicrm_api3_activity_get($params) {
   else {
     $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
   }
-  $options = _civicrm_api3_get_options_from_params($params, FALSE,'activity','get');
-  if($options['is_count']) {
+  $options = _civicrm_api3_get_options_from_params($params, FALSE, 'activity', 'get');
+  if ($options['is_count']) {
     return civicrm_api3_create_success($activities, $params, 'activity', 'get');
   }
 
@@ -244,13 +239,16 @@ function civicrm_api3_activity_get($params) {
 }
 
 /**
- * Given a list of activities, append any extra data requested about the activities
+ * Given a list of activities, append any extra data requested about the activities.
  *
- * NOTE: Called by civicrm-core and CiviHR
+ * @note Called by civicrm-core and CiviHR
  *
- * @param array $params API request parameters
+ * @param array $params
+ *   API request parameters.
  * @param array $activities
- * @return array new activities list
+ *
+ * @return array
+ *   new activities list
  */
 function _civicrm_api3_activity_get_formatResult($params, $activities) {
   $returns = CRM_Utils_Array::value('return', $params, array());
@@ -274,16 +272,19 @@ function _civicrm_api3_activity_get_formatResult($params, $activities) {
           $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']);
         }
         break;
+
       case 'target_contact_id':
         foreach ($activities as $key => $activityArray) {
           $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']);
         }
         break;
+
       case 'source_contact_id':
         foreach ($activities as $key => $activityArray) {
           $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity::getSourceContactID($activityArray['id']);
         }
         break;
+
       default:
         if (substr($n, 0, 6) == 'custom') {
           $returnProperties[$n] = $v;
@@ -303,17 +304,12 @@ function _civicrm_api3_activity_get_formatResult($params, $activities) {
 /**
  * Delete a specified Activity.
  *
- * @param array $params array holding 'id' of activity to be deleted
- * {@getfields activity_delete}
+ * @param array $params
+ *   Array holding 'id' of activity to be deleted.
  *
  * @throws API_Exception
- * @return void|CRM_Core_Error  An error if 'activityName or ID' is invalid,
- *                         permissions are insufficient, etc. or CiviCRM success array
- *
- *
- *
- * @example ActivityDelete.php Standard Delete Example
  *
+ * @return array
  */
 function civicrm_api3_activity_delete($params) {
 
@@ -326,22 +322,24 @@ function civicrm_api3_activity_delete($params) {
 }
 
 /**
- * Check for required params
+ * Check for required params.
  *
- * @param array $params associated array of fields
+ * @param array $params
+ *   Associated array of fields.
  *
  * @throws API_Exception
  * @throws Exception
- * @return array $error array with errors
+ * @return array
+ *   array with errors
  */
 function _civicrm_api3_activity_check_params(&$params) {
 
   $contactIDFields = array_intersect_key($params,
-                     array(
-                       'source_contact_id' => 1,
-                       'assignee_contact_id' => 1,
-                       'target_contact_id' => 1,
-                     )
+    array(
+      'source_contact_id' => 1,
+      'assignee_contact_id' => 1,
+      'target_contact_id' => 1,
+    )
   );
 
   // this should be handled by wrapper layer & probably the api would already manage it
@@ -363,20 +361,19 @@ function _civicrm_api3_activity_check_params(&$params) {
       }
     }
 
-
     $sql = '
 SELECT  count(*)
   FROM  civicrm_contact
  WHERE  id IN (' . implode(', ', $contactIds) . ' )';
     if (count($contactIds) != CRM_Core_DAO::singleValueQuery($sql)) {
-      throw new API_Exception('Invalid ' .  ' Contact Id');
+      throw new API_Exception('Invalid Contact Id');
     }
   }
 
-
-  $activityIds = array('activity' => CRM_Utils_Array::value('id', $params),
-                 'parent' => CRM_Utils_Array::value('parent_id', $params),
-                 'original' => CRM_Utils_Array::value('original_id', $params),
+  $activityIds = array(
+    'activity' => CRM_Utils_Array::value('id', $params),
+    'parent' => CRM_Utils_Array::value('parent_id', $params),
+    'original' => CRM_Utils_Array::value('original_id', $params),
   );
 
   foreach ($activityIds as $id => $value) {
@@ -390,8 +387,8 @@ SELECT  count(*)
   //correctly by doing pseudoconstant validation
   // needs testing
   $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
-  $activityName  = CRM_Utils_Array::value('activity_name', $params);
-  $activityName  = ucfirst($activityName);
+  $activityName = CRM_Utils_Array::value('activity_name', $params);
+  $activityName = ucfirst($activityName);
   $activityLabel = CRM_Utils_Array::value('activity_label', $params);
   if ($activityLabel) {
     $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'create');
@@ -403,7 +400,7 @@ SELECT  count(*)
     $activityTypeIdInList = array_search(($activityName ? $activityName : $activityLabel), $activityTypes);
 
     if (!$activityTypeIdInList) {
-      $errorString = $activityName ? "Invalid Activity Name : $activityName"  : "Invalid Activity Type Label";
+      $errorString = $activityName ? "Invalid Activity Name : $activityName" : "Invalid Activity Type Label";
       throw new Exception($errorString);
     }
     elseif ($activityTypeId && ($activityTypeId != $activityTypeIdInList)) {
@@ -424,7 +421,6 @@ SELECT  count(*)
     throw new API_Exception('Invalid Activity Duration (in minutes)');
   }
 
-
   //if adding a new activity & date_time not set make it now
   // this should be managed by the wrapper layer & setting ['api.default'] in speces
   // needs testing
@@ -436,12 +432,20 @@ SELECT  count(*)
 }
 
 /**
- * @see _civicrm_api3_generic_getlist_params.
+ * Get parameters for activity list.
+ *
+ * @see _civicrm_api3_generic_getlist_params
  *
- * @param $request array
+ * @param array $request
+ *   API request.
  */
 function _civicrm_api3_activity_getlist_params(&$request) {
-  $fieldsToReturn = array('activity_date_time', 'activity_type_id', 'subject', 'source_contact_id');
+  $fieldsToReturn = array(
+    'activity_date_time',
+    'activity_type_id',
+    'subject',
+    'source_contact_id',
+  );
   $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
   $request['params']['options']['sort'] = 'activity_date_time DESC';
   $request['params'] += array(
@@ -451,10 +455,12 @@ function _civicrm_api3_activity_getlist_params(&$request) {
 }
 
 /**
+ * Get output for activity list.
+ *
  * @see _civicrm_api3_generic_getlist_output
  *
- * @param $result array
- * @param $request array
+ * @param array $result
+ * @param array $request
  *
  * @return array
  */
@@ -465,13 +471,17 @@ function _civicrm_api3_activity_getlist_output($result, $request) {
       $data = array(
         'id' => $row[$request['id_field']],
         'label' => $row[$request['label_field']] ? $row[$request['label_field']] : ts('(no subject)'),
-        'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id'])),
+        'description' => array(
+          CRM_Core_Pseudoconstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id']),
+        ),
       );
       if (!empty($row['activity_date_time'])) {
         $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['activity_date_time']);
       }
       if (!empty($row['source_contact_id'])) {
-        $data['description'][] = ts('By %1', array(1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name')));
+        $data['description'][] = ts('By %1', array(
+          1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name'),
+        ));
       }
       foreach ($request['extra'] as $field) {
         $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
@@ -481,4 +491,3 @@ function _civicrm_api3_activity_getlist_output($result, $request) {
   }
   return $output;
 }
-