/** * @todo Write sth * @todo Serious FIXMES in the code! File issues. */ function civicrm_contact_update(&$params, $create_new = false) { _civicrm_initialize(); require_once 'CRM/Utils/Array.php'; $contactID = CRM_Utils_Array::value('contact_id', $params); if ($create_new) { // Make sure nothing is screwed up before we create a new contact if (!empty($contactID)) { return civicrm_create_error('Cannot create new contact when contact_id is present'); } if (empty($params['contact_type'])) { return civicrm_create_error('Contact Type not specified'); } $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, false); $values = civicrm_contact_check_params($params, $dupeCheck); if ($values) { return $values; } // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { require_once 'CRM/Core/BAO/LocationType.php'; $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } // FIXME: Some legacy support cruft, should get rid of this in 3.1 $change = array('individual_prefix' => 'prefix', 'prefix' => 'prefix_id', 'individual_suffix' => 'suffix', 'suffix' => 'suffix_id', 'gender' => 'gender_id'); foreach ($change as $field => $changeAs) { if (array_key_exists($field, $params)) { $params[$changeAs] = $params[$field]; unset($params[$field]); } } // End legacy support cruft if (isset($params['suffix_id']) && !is_numeric($params['suffix_id'])) { $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix()); } if (isset($params['prefix_id']) && !is_numeric($params['prefix_id'])) { $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix()); } if (isset($params['gender_id']) && !is_numeric($params['gender_id'])) { $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender()); } $values = array(); $entityId = CRM_Utils_Array::value('contact_id', $params, null); _civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId); $params = array_merge($params, $values); $contact =& _civicrm_contact_update($params, $contactID); if (is_a($contact, 'CRM_Core_Error')) { return civicrm_create_error($contact->_errors[0]['message']); } else { $values = array(); $values['contact_id'] = $contact->id; $values['is_error'] = 0; } return $values; }
/** * method for creating contact * * */ function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = null, $requiredCheck = true) { $dupeCheck = false; $newContact = null; if (is_null($contactId) && $onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { $dupeCheck = (bool) $onDuplicate; } //get the prefix id etc if exists CRM_Contact_BAO_Contact::resolveDefaults($formatted, true); require_once 'api/v2/Contact.php'; // setting required check to false, CRM-2839 // plus we do our own required check in import $error = civicrm_contact_check_params($formatted, $dupeCheck, true, false); if (is_null($error) && civicrm_error(_civicrm_validate_formatted_contact($formatted))) { $error = _civicrm_validate_formatted_contact($formatted); } $newContact = $error; if (is_null($error)) { if ($contactId) { $this->formatParams($formatted, $onDuplicate, (int) $contactId); } // pass doNotResetCache flag since resetting and rebuilding cache could be expensive. $config =& CRM_Core_Config::singleton(); $config->doNotResetCache = 1; $cid = CRM_Contact_BAO_Contact::createProfileContact($formatted, $contactFields, $contactId, null, null, $formatted['contact_type']); $config->doNotResetCache = 0; $contact = array('contact_id' => $cid); $defaults = array(); $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } //get the id of the contact whose street address is not parsable, CRM-5886 if ($this->_parseStreetAddress && $newContact->address) { foreach ($newContact->address as $address) { if ($address['street_address'] && (!CRM_Utils_Array::value('street_number', $address) || !CRM_Utils_Array::value('street_name', $address))) { $this->_unparsedStreetAddressContacts[] = array('id' => $newContact->id, 'streetAddress' => $address['street_address']); } } } return $newContact; }
/** * method for creating contact * * */ function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = null, $requiredCheck = true) { $dupeCheck = false; $newContact = null; if (is_null($contactId) && $onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { $dupeCheck = (bool) $onDuplicate; } //get the prefix id etc if exists CRM_Contact_BAO_Contact::resolveDefaults($formatted, true); require_once 'api/v2/Contact.php'; // setting required check to false, CRM-2839 // plus we do our own required check in import $error = civicrm_contact_check_params($formatted, $dupeCheck, true, false); if (is_null($error) && civicrm_error(_civicrm_validate_formatted_contact($formatted))) { $error = _civicrm_validate_formatted_contact($formatted); } $newContact = $error; if (is_null($error)) { if ($contactId) { $this->formatParams($formatted, $onDuplicate, (int) $contactId); } $cid = CRM_Contact_BAO_Contact::createProfileContact($formatted, $contactFields, $contactId, null, null, $formatted['contact_type']); $contact = array('contact_id' => $cid); $defaults = array(); $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } return $newContact; }
/** * @todo Write sth * @todo Serious FIXMES in the code! File issues. */ function civicrm_contact_update(&$params, $create_new = FALSE) { _civicrm_initialize(); try { civicrm_api_check_permission(__FUNCTION__, $params, TRUE); } catch (Exception $e) { return civicrm_create_error($e->getMessage()); } require_once 'CRM/Utils/Array.php'; $entityId = CRM_Utils_Array::value('contact_id', $params, NULL); if (!CRM_Utils_Array::value('contact_type', $params) && $entityId) { $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId); } $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); $values = civicrm_contact_check_params($params, $dupeCheck); if ($values) { return $values; } if ($create_new) { // Make sure nothing is screwed up before we create a new contact if (!empty($entityId)) { return civicrm_create_error('Cannot create new contact when contact_id is present'); } if (empty($params['contact_type'])) { return civicrm_create_error('Contact Type not specified'); } // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { require_once 'CRM/Core/BAO/LocationType.php'; $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } if ($homeUrl = CRM_Utils_Array::value('home_url', $params)) { require_once 'CRM/Core/PseudoConstant.php'; $websiteTypes = CRM_Core_PseudoConstant::websiteType(); $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), 'url' => $homeUrl)); } // FIXME: Some legacy support cruft, should get rid of this in 3.1 $change = array('individual_prefix' => 'prefix', 'prefix' => 'prefix_id', 'individual_suffix' => 'suffix', 'suffix' => 'suffix_id', 'gender' => 'gender_id'); foreach ($change as $field => $changeAs) { if (array_key_exists($field, $params)) { $params[$changeAs] = $params[$field]; unset($params[$field]); } } // End legacy support cruft if (isset($params['suffix_id']) && !is_numeric($params['suffix_id'])) { $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix()); } if (isset($params['prefix_id']) && !is_numeric($params['prefix_id'])) { $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix()); } if (isset($params['gender_id']) && !is_numeric($params['gender_id'])) { $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender()); } $error = _civicrm_greeting_format_params($params); if (civicrm_error($error)) { return $error; } $values = array(); if (!($csType = CRM_Utils_Array::value('contact_sub_type', $params)) && $entityId) { require_once 'CRM/Contact/BAO/Contact.php'; $csType = CRM_Contact_BAO_Contact::getContactSubType($entityId); } $customValue = civicrm_contact_check_custom_params($params, $csType); if ($customValue) { return $customValue; } _civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId); $params = array_merge($params, $values); $contact =& _civicrm_contact_update($params, $entityId); if (is_a($contact, 'CRM_Core_Error')) { return civicrm_create_error($contact->_errors[0]['message']); } else { $values = array(); $values['contact_id'] = $contact->id; $values['is_error'] = 0; } return $values; }
/** * Test civicrm_contact_check_params with a duplicate * and request the error in array format */ function testCheckParamsWithDuplicateContact2() { // Insert a row in civicrm_contact creating individual contact $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, new PHPUnit_Extensions_Database_DataSet_XMLDataSet(dirname(__FILE__) . '/dataset/contact_17.xml')); $op->execute($this->_dbconn, new PHPUnit_Extensions_Database_DataSet_XMLDataSet(dirname(__FILE__) . '/dataset/email_contact_17.xml')); $params = array('first_name' => 'Test', 'last_name' => 'Contact', 'email' => '*****@*****.**', 'contact_type' => 'Individual'); $contact =& civicrm_contact_check_params($params, true, true); $this->assertEquals(1, $contact['is_error']); $this->assertRegexp("/matching contacts.*17/s", $contact['error_message']['message']); }