Merge pull request #10506 from seamuslee001/CRM-20253-test
[civicrm-core.git] / api / v3 / Contact.php
index 773068f4bbf23e73965e62061b47433ad044e25f..2d1da21a38d19f36b470d779284c0ddc60fe590a 100644 (file)
@@ -55,8 +55,14 @@ function civicrm_api3_contact_create($params) {
     throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify contact record');
   }
 
-  $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE);
-  $values = _civicrm_api3_contact_check_params($params, $dupeCheck);
+  if (!empty($params['dupe_check'])) {
+    $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', array(), $params['check_permission']);
+    if (count($ids) > 0) {
+      throw new API_Exception("Found matching contacts: " . implode(',', $ids), "duplicate", array("ids" => $ids));
+    }
+  }
+
+  $values = _civicrm_api3_contact_check_params($params);
   if ($values) {
     return $values;
   }
@@ -123,6 +129,8 @@ function civicrm_api3_contact_create($params) {
     _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]);
   }
 
+  $values = _civicrm_api3_contact_formatResult($params, $values);
+
   return civicrm_api3_create_success($values, $params, 'Contact', 'create');
 }
 
@@ -162,9 +170,39 @@ function civicrm_api3_contact_get($params) {
   $options = array();
   _civicrm_api3_contact_get_supportanomalies($params, $options);
   $contacts = _civicrm_api3_get_using_query_object('Contact', $params, $options);
+  $contacts = _civicrm_api3_contact_formatResult($params, $contacts);
   return civicrm_api3_create_success($contacts, $params, 'Contact');
 }
 
+/**
+ * Filter the result.
+ *
+ * @param array $result
+ *
+ * @return array
+ * @throws \CRM_Core_Exception
+ */
+function _civicrm_api3_contact_formatResult($params, $result) {
+  $apiKeyPerms = array('edit api keys', 'administer CiviCRM');
+  $allowApiKey = empty($params['check_permissions']) || CRM_Core_Permission::check(array($apiKeyPerms));
+  if (!$allowApiKey) {
+    if (is_array($result)) {
+      // Single-value $result
+      if (isset($result['api_key'])) {
+        unset($result['api_key']);
+      }
+
+      // Multi-value $result
+      foreach ($result as $key => $row) {
+        if (is_array($row)) {
+          unset($result[$key]['api_key']);
+        }
+      }
+    }
+  }
+  return $result;
+}
+
 /**
  * Get number of contacts matching the supplied criteria.
  *
@@ -206,6 +244,10 @@ function _civicrm_api3_contact_get_spec(&$params) {
     'title' => 'Primary Address Supplemental Address 2',
     'type' => CRM_Utils_Type::T_STRING,
   );
+  $params['supplemental_address_3'] = array(
+    'title' => 'Primary Address Supplemental Address 3',
+    'type' => CRM_Utils_Type::T_STRING,
+  );
   $params['current_employer'] = array(
     'title' => 'Current Employer',
     'type' => CRM_Utils_Type::T_STRING,
@@ -455,13 +497,12 @@ function civicrm_api3_contact_delete($params) {
  * This function is on it's way out.
  *
  * @param array $params
- * @param bool $dupeCheck
  *
  * @return null
  * @throws API_Exception
  * @throws CiviCRM_API3_Exception
  */
-function _civicrm_api3_contact_check_params(&$params, $dupeCheck) {
+function _civicrm_api3_contact_check_params(&$params) {
 
   switch (strtolower(CRM_Utils_Array::value('contact_type', $params))) {
     case 'household':
@@ -494,35 +535,13 @@ function _civicrm_api3_contact_check_params(&$params, $dupeCheck) {
     }
   }
 
-  if ($dupeCheck) {
-    // check for record already existing
-    $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']);
-
-    // CRM-6431
-    // setting 'check_permission' here means that the dedupe checking will be carried out even if the
-    // person does not have permission to carry out de-dupes
-    // this is similar to the front end form
-    if (isset($params['check_permission'])) {
-      $dedupeParams['check_permission'] = $params['check_permission'];
-    }
-
-    $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));
-    }
-  }
-
   // 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(
       'organization_name' => $params['current_employer'],
     );
 
-    $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization');
-
-    $dedupParams['check_permission'] = FALSE;
-    $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised');
+    $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', array(), FALSE);
 
     // check for mismatch employer name and id
     if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) {
@@ -1104,11 +1123,10 @@ function _civicrm_api3_contact_deprecation() {
  *   -int main_id: main contact id with whom merge has to happen
  *   -int other_id: duplicate contact which would be deleted after merge operation
  *   -string mode: "safe" skips the merge if there are no conflicts. Does a force merge otherwise.
- *   -boolean auto_flip: whether to let api decide which contact to retain and which to delete.
  *
  * @return array
  *   API Result Array
- * @throws CiviCRM_API3_Exception
+ * @throws API_Exception
  */
 function civicrm_api3_contact_merge($params) {
   if (($result = CRM_Dedupe_Merger::merge(array(
@@ -1119,7 +1137,7 @@ function civicrm_api3_contact_merge($params) {
     ), array(), $params['mode'])) != FALSE) {
     return civicrm_api3_create_success($result, $params);
   }
-  throw new CiviCRM_API3_Exception('Merge failed');
+  throw new API_Exception('Merge failed');
 }
 
 /**
@@ -1268,6 +1286,11 @@ function _civicrm_api3_contact_getlist_params(&$request) {
   // Contact api doesn't support array(LIKE => 'foo') syntax
   if (!empty($request['input'])) {
     $request['params'][$request['search_field']] = $request['input'];
+    // Temporarily override wildcard setting
+    if (Civi::settings()->get('includeWildCardInName') != $request['add_wildcard']) {
+      Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard'] = !$request['add_wildcard'];
+      Civi::settings()->set('includeWildCardInName', $request['add_wildcard']);
+    }
   }
 }
 
@@ -1320,6 +1343,11 @@ function _civicrm_api3_contact_getlist_output($result, $request) {
       $output[] = $data;
     }
   }
+  // Restore wildcard override by _civicrm_api3_contact_getlist_params
+  if (isset(Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard'])) {
+    Civi::settings()->set('includeWildCardInName', Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard']);
+    unset(Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard']);
+  }
   return $output;
 }
 
@@ -1333,17 +1361,14 @@ function _civicrm_api3_contact_getlist_output($result, $request) {
  *   API formatted array
  */
 function civicrm_api3_contact_duplicatecheck($params) {
-  $dedupeParams = CRM_Dedupe_Finder::formatParams($params['match'], $params['match']['contact_type']);
-
-  // CRM-6431
-  // setting 'check_permission' here means that the dedupe checking will be carried out even if the
-  // person does not have permission to carry out de-dupes
-  // this is similar to the front end form
-  if (isset($params['check_permission'])) {
-    $dedupeParams['check_permission'] = $params['check_permission'];
-  }
-
-  $dupes = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['match']['contact_type'], 'Unsupervised', array(), CRM_Utils_Array::value('dedupe_rule_id', $params));
+  $dupes = CRM_Contact_BAO_Contact::getDuplicateContacts(
+    $params['match'],
+    $params['match']['contact_type'],
+    'Unsupervised',
+    array(),
+    CRM_Utils_Array::value('check_permissions', $params),
+    CRM_Utils_Array::value('dedupe_rule_id', $params)
+  );
   $values = empty($dupes) ? array() : array_fill_keys($dupes, array());
   return civicrm_api3_create_success($values, $params, 'Contact', 'duplicatecheck');
 }