Merge pull request #1984 from dlobo/CRM-13621
[civicrm-core.git] / api / v3 / Contact.php
index e9fa2b4baaf226f956e1a80ca62d7086a3cd0204..1e9d0bebb00da51fa5c82c745a992ac1c6b480a8 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.3                                                |
+  | CiviCRM version 4.4                                                |
   +--------------------------------------------------------------------+
   | Copyright CiviCRM LLC (c) 2004-2013                                |
   +--------------------------------------------------------------------+
@@ -83,42 +83,26 @@ function civicrm_api3_contact_create($params) {
     );
   }
 
-  if (isset($params['suffix_id']) && !(is_numeric($params['suffix_id']))) {
-    $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'));
-  }
-
-  if (isset($params['prefix_id']) && !(is_numeric($params['prefix_id']))) {
-    $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'));
-  }
-
-  if (isset($params['gender_id']) && !(is_numeric($params['gender_id']))) {
-    $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'));
-  }
-
-  $error = _civicrm_api3_greeting_format_params($params);
-  if (civicrm_error($error)) {
-    return $error;
-  }
+  _civicrm_api3_greeting_format_params($params);
 
   $values = array();
-  $entityId = $contactID;
 
-  if (empty($params['contact_type']) && $entityId) {
-    $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId);
+  if (empty($params['contact_type']) && $contactID) {
+    $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID);
   }
 
-  if (!isset($params['contact_sub_type']) && $entityId) {
-    $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($entityId);
+  if (!isset($params['contact_sub_type']) && $contactID) {
+    $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID);
   }
 
-  _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $entityId);
+  _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID);
 
   $params = array_merge($params, $values);
-
+  //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao
   $contact = _civicrm_api3_contact_update($params, $contactID);
 
   if (is_a($contact, 'CRM_Core_Error')) {
-    return civicrm_api3_create_error($contact->_errors[0]['message']);
+    throw new API_Exception($contact->_errors[0]['message']);
   }
   else {
     $values = array();
@@ -139,11 +123,14 @@ function _civicrm_api3_contact_create_spec(&$params) {
   $params['current_employer'] = array(
     'title' => 'Current Employer',
     'description' => 'Name of Current Employer',
+    'type' => CRM_Utils_Type::T_STRING,
   );
   $params['dupe_check'] = array(
     'title' => 'Check for Duplicates',
     'description' => 'Throw error if contact create matches dedupe rule',
   );
+  $params['prefix_id']['api.aliases'] = array('individual_prefix', 'individual_prefix_id');
+  $params['suffix_id']['api.aliases'] = array('individual_suffix', 'individual_suffix_id');
 }
 
 /**
@@ -163,26 +150,7 @@ 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);
-
-  // CRM-7929 Quick fix by colemanw
-  // TODO: Figure out what function is responsible for prepending 'individual_' to these keys
-  // and sort it out there rather than going to all this trouble here.
-  // Eileen's note - not sure anymore if we went the right path stripping the 'individual' here
-  // as is arguable whether it makes more sense now I think it would make more sense to rename the table field  or uniquefield to have
-  // the individual prefix but we are stuck with it now for apiv3 series.
-  $returnContacts = array();
-  if (is_array($contacts)) {
-    foreach ($contacts as $cid => $contact) {
-      if (is_array($contact)) {
-        $returnContacts[$cid] = array();
-        foreach ($contact as $key => $value) {
-          $key = str_replace(array('individual_prefix', 'individual_suffix'), array('prefix', 'suffix'), $key);
-          $returnContacts[$cid][$key] = $value;
-        }
-      }
-    }
-  }
-  return civicrm_api3_create_success($returnContacts, $params, 'contact');
+  return civicrm_api3_create_success($contacts, $params, 'contact');
 }
 
 function civicrm_api3_contact_getcount($params) {
@@ -214,15 +182,18 @@ function _civicrm_api3_contact_get_spec(&$params) {
   $params['state_province_id']['title'] = 'Primary Address State Province ID';
   $params['state_province_name']['title'] = 'Primary Address State Province Name';
   $params['state_province']['title'] = 'Primary Address State Province';
-  $params['country_id']['title'] = 'Primary Address State Province ID';
+  $params['country_id']['title'] = 'Primary Address Country ID';
   $params['country']['title'] = 'Primary Address country';
   $params['worldregion_id']['title'] = 'Primary Address World Region ID';
   $params['worldregion']['title'] = 'Primary Address World Region';
   $params['phone_id']['title'] = 'Primary Phone ID';
+  $params['phone']['title'] = 'Primary Phone';
   $params['phone_type_id']['title'] = 'Primary Phone Type ID';
   $params['provider_id']['title'] = 'Primary Phone Provider ID';
   $params['email_id']['title'] = 'Primary Email ID';
   $params['email']['title'] = 'Primary Email';
+  $params['gender_id']['title'] = 'Gender ID';
+  $params['gender']['title'] = 'Gender';
   $params['on_hold']['title'] = 'Primary Email On Hold';
   $params['im']['title'] = 'Primary Instant Messanger';
   $params['im_id']['title'] = 'Primary Instant Messanger ID';
@@ -298,7 +269,7 @@ function civicrm_api3_contact_delete($params) {
   // restrict permanent delete if a contact has financial trxn associated with it
   $error = NULL;
   if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($contactID), $error)) {
-    return civicrm_api3_create_error($error['_qf_default']);  
+    return civicrm_api3_create_error($error['_qf_default']);
   }
   if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) {
     return civicrm_api3_create_success();
@@ -330,9 +301,14 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
     break;
   }
 
+  // Fixme: This really needs to be handled at a lower level. @See CRM-13123
+  if (isset($params['preferred_communication_method'])) {
+    $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 (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) {
-        return civicrm_api3_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$params['contact_sub_type']));
+        throw new API_Exception("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$params['contact_sub_type']));
       }
     }
 
@@ -391,17 +367,12 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
  * @static
  */
 function _civicrm_api3_contact_update($params, $contactID = NULL) {
-  $transaction = new CRM_Core_Transaction();
-
+  //@todo - doesn't contact create support 'id' which is already set- check & remove
   if ($contactID) {
     $params['contact_id'] = $contactID;
   }
 
-  $contact = CRM_Contact_BAO_Contact::create($params);
-
-  $transaction->commit();
-
-  return $contact;
+  return CRM_Contact_BAO_Contact::create($params);
 }
 
 /**
@@ -458,7 +429,7 @@ function _civicrm_api3_greeting_format_params($params) {
     if ($customGreeting && $greetingId &&
       ($greetingId != array_search('Customized', $greetings))
     ) {
-      return civicrm_api3_create_error(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting',
+      throw new API_Exception(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting',
           array(1 => $key)
         ));
     }
@@ -466,7 +437,7 @@ function _civicrm_api3_greeting_format_params($params) {
     if ($greetingVal && $greetingId &&
       ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings))
     ) {
-      return civicrm_api3_create_error(ts('Mismatch in %1 greeting id and %1 greeting',
+      throw new API_Exception(ts('Mismatch in %1 greeting id and %1 greeting',
           array(1 => $key)
         ));
     }
@@ -474,11 +445,11 @@ function _civicrm_api3_greeting_format_params($params) {
     if ($greetingId) {
 
       if (!array_key_exists($greetingId, $greetings)) {
-        return civicrm_api3_create_error(ts('Invalid %1 greeting Id', array(1 => $key)));
+        throw new API_Exception(ts('Invalid %1 greeting Id', array(1 => $key)));
       }
 
       if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) {
-        return civicrm_api3_create_error(ts('Please provide a custom value for %1 greeting',
+        throw new API_Exception(ts('Please provide a custom value for %1 greeting',
             array(1 => $key)
           ));
       }
@@ -486,7 +457,7 @@ function _civicrm_api3_greeting_format_params($params) {
     elseif ($greetingVal) {
 
       if (!in_array($greetingVal, $greetings)) {
-        return civicrm_api3_create_error(ts('Invalid %1 greeting', array(1 => $key)));
+        throw new API_Exception(ts('Invalid %1 greeting', array(1 => $key)));
       }
 
       $greetingId = CRM_Utils_Array::key($greetingVal, $greetings);
@@ -496,10 +467,13 @@ function _civicrm_api3_greeting_format_params($params) {
       $greetingId = CRM_Utils_Array::key('Customized', $greetings);
     }
 
-    $customValue = $params['contact_id'] ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
-      $params['contact_id'],
-      "{$key}{$greeting}_custom"
-    ) : FALSE;
+    $customValue = isset($params['contact_id']) ?
+      CRM_Core_DAO::getFieldValue(
+        'CRM_Contact_DAO_Contact',
+        $params['contact_id'],
+        "{$key}{$greeting}_custom"
+      ) :
+      FALSE;
 
     if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) {
       $nullValue = TRUE;
@@ -545,7 +519,7 @@ function civicrm_api3_contact_quicksearch($params) {
 
 function civicrm_api3_contact_getquick($params) {
   civicrm_api3_verify_mandatory($params, NULL, array('name'));
-  $name = CRM_Utils_Type::escape($params['name'], 'String');
+  $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String');
 
   // get the autocomplete options from settings
   $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR,
@@ -592,6 +566,9 @@ function civicrm_api3_contact_getquick($params) {
       case 'phone':
       case 'email':
         $actualSelectElements[] = $select[] = ($value == 'address') ? $selectText : $value;
+        if ($value == 'phone') {
+          $actualSelectElements[] = $select[] = 'phone_ext';
+        }
         $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) ";
         break;