CRM-15168 open up test to include a few more entities
[civicrm-core.git] / api / v3 / Contact.php
index 1efb42a13da38ff03c96391970897d3642417255..4ec440ef9b27c2766ff28e20de00a8fb8ff77eea 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.4                                                |
+  | CiviCRM version 4.5                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2013                                |
+  | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -32,7 +32,7 @@
  *
  * @package CiviCRM_APIv3
  * @subpackage API_Contact
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $
  *
  */
 /**
  * Create or update a contact (note you should always call this via civicrm_api() & never directly)
  *
- * @param  array   $params   input parameters
+ * @param  array $params input parameters
  *
  * Allowed @params array keys are:
  * {@getfields contact_create}
  *
  *
+ * @throws API_Exception
  * @example ContactCreate.php Example of Create Call
  *
  * @return array  API Result Array
@@ -153,6 +154,11 @@ function civicrm_api3_contact_get($params) {
   return civicrm_api3_create_success($contacts, $params, 'contact');
 }
 
+/**
+ * @param $params
+ *
+ * @return int
+ */
 function civicrm_api3_contact_getcount($params) {
   $options = array();
   _civicrm_api3_contact_get_supportanomalies($params, $options);
@@ -267,8 +273,8 @@ function civicrm_api3_contact_delete($params) {
   if ($contactID == $session->get('userID')) {
     return civicrm_api3_create_error('This contact record is linked to the currently logged in user account - and cannot be deleted.');
   }
-  $restore = CRM_Utils_Array::value('restore', $params) ? $params['restore'] : FALSE;
-  $skipUndelete = CRM_Utils_Array::value('skip_undelete', $params) ? $params['skip_undelete'] : FALSE;
+  $restore = !empty($params['restore']) ? $params['restore'] : FALSE;
+  $skipUndelete = !empty($params['skip_undelete']) ? $params['skip_undelete'] : FALSE;
 
   // CRM-12929
   // restrict permanent delete if a contact has financial trxn associated with it
@@ -285,6 +291,17 @@ function civicrm_api3_contact_delete($params) {
 }
 
 
+/**
+ * @param $params
+ * @param bool $dupeCheck
+ * @param bool $dupeErrorArray
+ * @param bool $obsoletevalue
+ * @param null $dedupeRuleGroupID
+ *
+ * @return null
+ * @throws API_Exception
+ * @throws CiviCRM_API3_Exception
+ */
 function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeErrorArray = false, $obsoletevalue = true, $dedupeRuleGroupID = null )
 {
 
@@ -311,7 +328,7 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
     $params['preferred_communication_method'] = CRM_Utils_Array::implodePadded($params['preferred_communication_method']);
   }
 
-  if (CRM_Utils_Array::value('contact_sub_type', $params) && CRM_Utils_Array::value('contact_type', $params)) {
+  if (!empty($params['contact_sub_type']) && !empty($params['contact_type'])) {
       if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) {
         throw new API_Exception("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$params['contact_sub_type']));
       }
@@ -329,17 +346,18 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
       $dedupeParams['check_permission'] = $params['check_permission'];
     }
 
-    $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Strict', array());
+    $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array());
 
     if (count($ids) >0) {
       throw new API_Exception("Found matching contacts: ". implode(',',$ids),"duplicate",array("ids"=>$ids));
     }
   }
 
-  //check for organisations with same name
+  // The BAO no longer supports the legacy param "current_employer" so here is a shim for api backward-compatability
   if (!empty($params['current_employer'])) {
-    $organizationParams = array();
-    $organizationParams['organization_name'] = $params['current_employer'];
+    $organizationParams = array(
+      'organization_name' => $params['current_employer'],
+    );
 
     $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization');
 
@@ -355,6 +373,17 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
     if (empty($params['employer_id']) && (count($dupeIds) > 1)) {
       throw new API_Exception('Found more than one Organisation with same Name.');
     }
+
+    if ($dupeIds) {
+      $params['employer_id'] = $dupeIds[0];
+    }
+    else {
+      $result = civicrm_api3('contact', 'create', array(
+        'organization_name' => $params['current_employer'],
+        'contact_type' => 'Organization'
+      ));
+      $params['employer_id'] = $result['id'];
+    }
   }
 
   return NULL;
@@ -383,10 +412,10 @@ function _civicrm_api3_contact_update($params, $contactID = NULL) {
 /**
  * Validate the addressee or email or postal greetings
  *
- * @param  $params                   Associative array of property name/value
+ * @param  array $params  Associative array of property name/value
  *                                   pairs to insert in new contact.
  *
- * @return array (reference )        null on success, error message otherwise
+ * @throws API_Exception
  *
  * @access public
  */
@@ -510,18 +539,13 @@ function _civicrm_api3_greeting_format_params($params) {
 }
 
 /**
- * Contact quick search api
+ * Old contact quick search api
  *
- * @access public
+ * @deprecated
  *
  * {@example ContactGetquick.php 0}
  *
  */
-function civicrm_api3_contact_quicksearch($params) {
-  // kept as an alias for compatibility reasons.  CRM-11136
-  return civicrm_api3_contact_getquick($params);
-}
-
 function civicrm_api3_contact_getquick($params) {
   civicrm_api3_verify_mandatory($params, NULL, array('name'));
   $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String');
@@ -538,7 +562,7 @@ function civicrm_api3_contact_getquick($params) {
 
   $list = array();
   foreach ($acpref as $value) {
-    if ($value && CRM_Utils_Array::value($value, $acOptions)) {
+    if ($value && !empty($acOptions[$value])) {
       $list[$value] = $acOptions[$value];
     }
   }
@@ -623,13 +647,13 @@ function civicrm_api3_contact_getquick($params) {
     $where .= " AND $aclWhere ";
   }
 
-  if (CRM_Utils_Array::value('org', $params)) {
+  if (!empty($params['org'])) {
     $where .= " AND contact_type = \"Organization\"";
 
     // CRM-7157, hack: get current employer details when
     // employee_id is present.
     $currEmpDetails = array();
-    if (CRM_Utils_Array::value('employee_id', $params)) {
+    if (!empty($params['employee_id'])) {
       if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
           (int) $params['employee_id'],
           'employer_id'
@@ -654,23 +678,28 @@ function civicrm_api3_contact_getquick($params) {
     }
   }
 
-  if (CRM_Utils_Array::value('contact_sub_type', $params)) {
+  if (!empty($params['contact_sub_type'])) {
     $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String');
     $where .= " AND cc.contact_sub_type = '{$contactSubType}'";
   }
 
+  if (!empty($params['contact_type'])) {
+    $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String');
+    $where .= " AND cc.contact_type LIKE '{$contactType}'";
+  }
+
   //set default for current_employer or return contact with particular id
-  if (CRM_Utils_Array::value('id', $params)) {
+  if (!empty($params['id'])) {
     $where .= " AND cc.id = " . (int) $params['id'];
   }
 
-  if (CRM_Utils_Array::value('cid', $params)) {
+  if (!empty($params['cid'])) {
     $where .= " AND cc.id <> " . (int) $params['cid'];
   }
 
   //contact's based of relationhip type
   $relType = NULL;
-  if (CRM_Utils_Array::value('rel', $params)) {
+  if (!empty($params['rel'])) {
     $relation = explode('_', CRM_Utils_Array::value('rel', $params));
     $relType  = CRM_Utils_Type::escape($relation[0], 'Integer');
     $rel      = CRM_Utils_Type::escape($relation[2], 'String');
@@ -723,7 +752,7 @@ function civicrm_api3_contact_getquick($params) {
   }
 
   // check if only CMS users are requested
-  if (CRM_Utils_Array::value('cmsuser', $params)) {
+  if (!empty($params['cmsuser'])) {
     $additionalFrom = "
       INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id)
       ";
@@ -776,7 +805,7 @@ LIMIT    0, {$limit}
     }
     $t['data'] = $dao->data;
     $contactList[] = $t;
-    if (CRM_Utils_Array::value('org', $params) &&
+    if (!empty($params['org']) &&
       !empty($currEmpDetails) &&
       $dao->id == $currEmpDetails['id']
     ) {
@@ -786,7 +815,7 @@ LIMIT    0, {$limit}
 
   //return organization name if doesn't exist in db
   if (empty($contactList)) {
-    if (CRM_Utils_Array::value('org', $params)) {
+    if (!empty($params['org'])) {
       if ($listCurrentEmployer && !empty($currEmpDetails)) {
         $contactList = array(
           array(
@@ -843,12 +872,21 @@ function civicrm_api3_contact_merge($params) {
   }
 }
 
+/**
+ * @param $params
+ */
 function _civicrm_api3_contact_proximity_spec(&$params) {
   $params['latitude']['api.required'] = 1;
   $params['longitude']['api.required'] = 1;
   $params['unit']['api.default'] = 'meter';
 }
 
+/**
+ * @param $params
+ *
+ * @return array
+ * @throws Exception
+ */
 function civicrm_api3_contact_proximity($params) {
   $latitude  = CRM_Utils_Array::value('latitude', $params);
   $longitude = CRM_Utils_Array::value('longitude', $params);
@@ -893,3 +931,93 @@ WHERE     $whereClause
   return civicrm_api3_create_success($contacts, $params, 'contact', 'get_by_location', $dao);
 }
 
+
+/**
+ * @see _civicrm_api3_generic_getlist_params
+ *
+ * @param $request array
+ */
+function _civicrm_api3_contact_getlist_params(&$request) {
+  // get the autocomplete options from settings
+  $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR,
+    CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+      'contact_autocomplete_options'
+    )
+  );
+
+  // get the option values for contact autocomplete
+  $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name');
+
+  $list = array();
+  foreach ($acpref as $value) {
+    if ($value && !empty($acOptions[$value])) {
+      $list[] = $acOptions[$value];
+    }
+  }
+  // If we are doing quicksearch by a field other than name, make sure that field is added to results
+  $field_name = CRM_Utils_String::munge($request['search_field']);
+  // Unique name contact_id = id
+  if ($field_name == 'contact_id') {
+    $field_name = 'id';
+  }
+  // phone_numeric should be phone
+  $searchField = str_replace('_numeric', '', $field_name);
+  if(!in_array($searchField, $list)) {
+    $list[] = $searchField;
+  }
+  $request['description_field'] = $list;
+  $list[] = 'contact_type';
+  $request['params']['return'] = array_unique(array_merge($list, $request['extra']));
+  $request['params']['options']['sort'] = 'sort_name';
+  // Contact api doesn't support array(LIKE => 'foo') syntax
+  if (!empty($request['input'])) {
+    $request['params'][$request['search_field']] = $request['input'];
+  }
+}
+
+/**
+ * @see _civicrm_api3_generic_getlist_output
+ *
+ * @param $result array
+ * @param $request array
+ *
+ * @return array
+ */
+function _civicrm_api3_contact_getlist_output($result, $request) {
+  $output = array();
+  if (!empty($result['values'])) {
+    $addressFields = array_intersect(array('street_address', 'city', 'state_province', 'country'), $request['params']['return']);
+    foreach ($result['values'] as $row) {
+      $data = array(
+        'id' => $row[$request['id_field']],
+        'label' => $row[$request['label_field']],
+        'description' => array(),
+      );
+      foreach ($request['description_field'] as $item) {
+        if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) {
+          $data['description'][] = $row[$item];
+        }
+      }
+      $address = array();
+      foreach($addressFields as $item) {
+        if (!empty($row[$item])) {
+          $address[] = $row[$item];
+        }
+      }
+      if ($address) {
+        $data['description'][] = implode(' ', $address);
+      }
+      if (!empty($request['image_field'])) {
+        $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : '';
+      }
+      else {
+        $data['icon_class'] = $row['contact_type'];
+      }
+      foreach ($request['extra'] as $field) {
+        $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
+      }
+      $output[] = $data;
+    }
+  }
+  return $output;
+}