function &crm_create_contact_formatted(&$params, $onDuplicate) { _crm_initialize(); // return error if we have no params if (empty($params)) { return _crm_error('Input Parameters empty'); } $error = _crm_required_formatted_contact($params); if (is_a($error, 'CRM_Core_Error')) { return $error; } $error = _crm_validate_formatted_contact($params); if (is_a($error, 'CRM_Core_Error')) { return $error; } require_once 'CRM/Import/Parser.php'; if ($onDuplicate != CRM_IMPORT_PARSER_DUPLICATE_NOCHECK) { $error = _crm_duplicate_formatted_contact($params); if (is_a($error, 'CRM_Core_Error')) { return $error; } } $ids = array(); CRM_Contact_BAO_Contact::resolveDefaults($params, true); $contact = CRM_Contact_BAO_Contact::create($params, $ids, count($params['location'])); return $contact; }
/** * handle the values in import mode * * @param int $onDuplicate the code for what action to take on duplicates * @param array $values the array of values belonging to this line * * @return boolean the result of this processing * @access public */ function import($onDuplicate, &$values) { // first make sure this is a valid line $response = $this->summary($values); if ($response != CRM_CONTRIBUTE_IMPORT_PARSER_VALID) { return $response; } $params =& $this->getActiveFieldParams(); //for date-Formats $session =& CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); foreach ($params as $key => $val) { if ($val) { switch ($key) { case 'receive_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; case 'cancel_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; case 'receipt_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; case 'thankyou_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; } } } //date-Format part ends $formatted = array(); if ($GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['indieFields'] == null) { require_once 'CRM/Contribute/DAO/Contribution.php'; $tempIndieFields =& CRM_Contribute_DAO_Contribution::import(); $GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['indieFields'] = $tempIndieFields; } foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } $value = array($key => $field); _crm_add_formatted_contrib_param($value, $formatted); } if ($this->_contactIdIndex < 0) { if ($GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['cIndieFields'] == null) { require_once 'CRM/Contact/BAO/Contact.php'; $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields('Individual', null); $GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['cIndieFields'] = $cTempIndieFields; } foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } if (is_array($field)) { foreach ($field as $value) { $break = false; if (is_array($value)) { foreach ($value as $name => $testForEmpty) { if ($name !== 'phone_type' && ($testForEmpty === '' || $testForEmpty == null)) { $break = true; break; } } } else { $break = true; } if (!$break) { _crm_add_formatted_param($value, $contactFormatted); } } continue; } $value = array($key => $field); if (array_key_exists($key, $GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['cIndieFields'])) { $value['contact_type'] = 'Individual'; } _crm_add_formatted_param($value, $contactFormatted); } $contactFormatted['contact_type'] = 'Individual'; $error = _crm_duplicate_formatted_contact($contactFormatted); $matchedIDs = explode(',', $error->_errors[0]['params'][0]); if (CRM_Contribute_Import_Parser_Contribution::isDuplicate($error)) { if (count($matchedIDs) > 1) { array_unshift($values, "Multiple matching contact records detected for this row. The contribution was not imported"); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } else { $cid = $matchedIDs[0]; $formatted['contact_id'] = $cid; $newContribution = crm_create_contribution_formatted($formatted, $onDuplicate); if (is_a($newContribution, CRM_Core_Error)) { array_unshift($values, $newContribution->_errors[0]['message']); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } $this->_newContributions[] = $newContribution->id; return CRM_CONTRIBUTE_IMPORT_PARSER_VALID; } } else { require_once 'CRM/Core/DAO/DupeMatch.php'; $dao =& new CRM_Core_DAO_DupeMatch(); $dao->find(true); $fieldsArray = explode('AND', $dao->rule); foreach ($fieldsArray as $value) { if (array_key_exists(trim($value), $params)) { $paramValue = $params[trim($value)]; if (is_array($paramValue)) { $disp .= $params[trim($value)][0][trim($value)] . " "; } else { $disp .= $params[trim($value)] . " "; } } } array_unshift($values, "No matching Contact found for (" . $disp . ")"); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } } else { $newContribution = crm_create_contribution_formatted($formatted, $onDuplicate); if (is_a($newContribution, CRM_Core_Error)) { array_unshift($values, $newContribution->_errors[0]['message']); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } $this->_newContributions[] = $newContribution->id; return CRM_CONTRIBUTE_IMPORT_PARSER_VALID; } }
/** * handle the values in import mode * * @param int $onDuplicate the code for what action to take on duplicates * @param array $values the array of values belonging to this line * * @return boolean the result of this processing * @access public */ function import($onDuplicate, &$values) { // first make sure this is a valid line //$this->_updateWithId = false; $response = $this->summary($values); if ($response != CRM_IMPORT_PARSER_VALID) { return $response; } $params =& $this->getActiveFieldParams(); $formatted = array('contact_type' => $this->_contactType); //for date-Formats $session =& CRM_Core_Session::singleton(); $dateType = $session->get("dateType"); $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type']); foreach ($params as $key => $val) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { if ($customFields[$customFieldID][2] == 'Date') { CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); } } if ($key == 'birth_date') { if ($val) { CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); } } } //date-Format part ends if ($GLOBALS['_CRM_IMPORT_PARSER_CONTACT']['indieFields'] == null) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $this->_contactType) . ".php"; eval('$tempIndieFields =& CRM_Contact_DAO_' . $this->_contactType . '::import();'); //modified for PHP4 issue $GLOBALS['_CRM_IMPORT_PARSER_CONTACT']['indieFields'] = $tempIndieFields; } foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } if (is_array($field)) { foreach ($field as $value) { $break = false; if (is_array($value)) { foreach ($value as $name => $testForEmpty) { if ($name !== 'phone_type' && ($testForEmpty === '' || $testForEmpty == null)) { $break = true; break; } } } else { $break = true; } if (!$break) { _crm_add_formatted_param($value, $formatted); } } continue; } $value = array($key => $field); if (array_key_exists($key, $GLOBALS['_CRM_IMPORT_PARSER_CONTACT']['indieFields'])) { $value['contact_type'] = $this->_contactType; } _crm_add_formatted_param($value, $formatted); } /*if (in_array('id',$this->_mapperKeys)) { $this->_updateWithId = true; }*/ $relationship = false; // Support Match and Update Via Contact ID if ($this->_updateWithId) { $error = _crm_duplicate_formatted_contact($formatted); if (CRM_Import_Parser_Contact::isDuplicate($error)) { $matchedIDs = explode(',', $error->_errors[0]['params'][0]); if (count($matchedIDs) >= 1) { $updateflag = true; foreach ($matchedIDs as $contactId) { if ($params['id'] == $contactId) { $paramsValues = array('contact_id' => $contactId); $contactExits = crm_get_contact($paramsValues); if ($formatted['contact_type'] == $contactExits->contact_type) { $newContact = crm_update_contact_formatted($contactId, $formatted, true); $updateflag = false; $this->_retCode = CRM_IMPORT_PARSER_VALID; } else { $message = "Mismatched contact Types :"; array_unshift($values, $message); $updateflag = false; $this->_retCode = CRM_IMPORT_PARSER_NO_MATCH; } } } if ($updateflag) { $message = "Mismatched contact IDs OR Mismatched contact Types :"; array_unshift($values, $message); $this->_retCode = CRM_IMPORT_PARSER_NO_MATCH; } } } else { $paramsValues = array('contact_id' => $params['id']); $contact = crm_get_contact($paramsValues); if (is_a($contact, CRM_Contact_BAO_Contact)) { if ($formatted['contact_type'] == $contact->contact_type) { $newContact = crm_update_contact_formatted($contact->id, $formatted, true); $this->_retCode = CRM_IMPORT_PARSER_VALID; } else { $message = "Mismatched contact Types :"; array_unshift($values, $message); $this->_retCode = CRM_IMPORT_PARSER_NO_MATCH; } } else { $message = "No contact found for this contact ID:" . $params['id']; array_unshift($values, $message); $this->_retCode = CRM_IMPORT_PARSER_NO_MATCH; } } if (is_a($newContact, CRM_Contact_BAO_Contact)) { $relationship = true; } else { if (is_a($error, CRM_Core_Error)) { $newContact = $error; $relationship = true; } } if ($newContact && !is_a($newContact, CRM_Core_Error)) { $this->_newContacts[] = $newContact->id; } } else { $newContact = crm_create_contact_formatted($formatted, $onDuplicate); $relationship = true; } if ($relationship) { if (CRM_Import_Parser_Contact::isDuplicate($newContact)) { foreach ($newContact->_errors[0]['params'] as $cid) { $primaryContactId = $cid; } } else { $primaryContactId = $newContact->id; } if (CRM_Import_Parser_Contact::isDuplicate($newContact) || is_a($newContact, CRM_Contact_BAO_Contact)) { //relationship contact insert foreach ($params as $key => $field) { list($id, $first, $second) = explode('_', $key); if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) { continue; } $relationType = new CRM_Contact_DAO_RelationshipType(); $relationType->id = $id; $relationType->find(true); $name_a_b = $relationType->name_a_b; if ($params[$key]['contact_type']) { $formatting = array('contact_type' => $params[$key]['contact_type']); } else { $fld = array_keys($params[$key]); foreach (CRM_Core_SelectValues::contactType() as $cType => $val) { if ($cType) { $contactFields =& CRM_Contact_BAO_Contact::importableFields($cType); if (array_key_exists($fld[0], $contactFields)) { $formatting['contact_type'] = $cType; $params[$key]['contact_type'] = $cType; $field['contact_type'] = $cType; break; } } } } $contactFields = null; if ($contactFields == null) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $params[$key]['contact_type']) . ".php"; eval('$contactFields =& CRM_Contact_DAO_' . $params[$key]['contact_type'] . '::import();'); } foreach ($field as $k => $v) { if ($v == null || $v === '') { continue; } if (is_array($v)) { foreach ($v as $value) { $break = false; foreach ($value as $testForEmpty) { if ($testForEmpty === '' || $testForEmpty == null) { $break = true; break; } } if (!$break) { _crm_add_formatted_param($value, $formatting); } } continue; } $value = array($k => $v); if (array_key_exists($k, $contactFields)) { $value['contact_type'] = $params[$key]['contact_type']; } _crm_add_formatted_param($value, $formatting); } $relatedNewContact = crm_create_contact_formatted($formatting, $onDuplicate); if (CRM_Import_Parser_Contact::isDuplicate($relatedNewContact)) { foreach ($relatedNewContact->_errors[0]['params'] as $cid) { $relContactId = $cid; } } else { $relContactId = $relatedNewContact->id; $this->_newRelatedContacts[] = $relContactId; } if (CRM_Import_Parser_Contact::isDuplicate($relatedNewContact) || is_a($relatedNewContact, CRM_Contact_BAO_Contact)) { //store the related contact id for groups //$this->_newRelatedContacts[] = $relContactId; // now create the relationship record $relationParams = array(); $relationParams = array('relationship_type_id' => $key, 'contact_check' => array($relContactId => 1)); $relationIds = array('contact' => $primaryContactId); CRM_Contact_BAO_Relationship::create($relationParams, $relationIds); //check if the two contacts are related and of type individual if ($params[$key]['contact_type'] == 'Individual' && $this->_contactType == 'Individual') { if ($name_a_b == 'Spouse of' || $name_a_b == 'Child of' || $name_a_b == 'Sibling of') { $householdName = "The " . $formatting['last_name'] . " household"; $householdFormatting = array('contact_type' => 'Household', 'household_name' => $householdName); $householdContact = crm_create_contact_formatted($householdFormatting, $onDuplicate); if (CRM_Import_Parser_Contact::isDuplicate($householdContact)) { foreach ($householdContact->_errors[0]['params'] as $cid) { $householdId = $cid; } } else { $householdId = $householdContact->id; $this->_newRelatedContacts[] = $householdId; } //Household contact is created //for two related individual contacts waiting confirmation whether //to add it in a group //$this->_newRelatedContacts[] = $householdId; $relationParams = array(); // adding household relationship $relType = '7_' . $second . '_' . $first; $relationParams = array('relationship_type_id' => $relType, 'contact_check' => array($relContactId => 1, $primaryContactId => 1)); $relationIds = array('contact' => $householdId); CRM_Contact_BAO_Relationship::create($relationParams, $relationIds); } } } } } } if ($this->_updateWithId) { return $this->_retCode; } //dupe checking if (is_a($newContact, CRM_Core_Error)) { $code = $newContact->_errors[0]['code']; if ($code == CRM_CORE_ERROR_DUPLICATE_CONTACT) { $urls = array(); foreach ($newContact->_errors[0]['params'] as $cid) { $urls[] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $cid, true); } $url_string = implode("\n", $urls); array_unshift($values, $url_string); /* If we duplicate more than one record, skip no matter what */ if (count($newContact->_errors[0]['params']) > 1) { array_unshift($values, ts('Record duplicates multiple contacts')); return CRM_IMPORT_PARSER_ERROR; } /* Params only had one id, so shift it out */ $contactId = array_shift($newContact->_errors[0]['params']); if ($onDuplicate == CRM_IMPORT_PARSER_DUPLICATE_REPLACE) { $newContact = crm_replace_contact_formatted($contactId, $formatted); } else { if ($onDuplicate == CRM_IMPORT_PARSER_DUPLICATE_UPDATE) { $newContact = crm_update_contact_formatted($contactId, $formatted, true); } else { if ($onDuplicate == CRM_IMPORT_PARSER_DUPLICATE_FILL) { $newContact = crm_update_contact_formatted($contactId, $formatted, false); } } } // else skip does nothing and just returns an error code. if ($newContact && !is_a($newContact, CRM_Core_Error)) { $this->_newContacts[] = $newContact->id; } //CRM-262 No Duplicate Checking if ($onDuplicate == CRM_IMPORT_PARSER_DUPLICATE_SKIP) { return CRM_IMPORT_PARSER_DUPLICATE; } return CRM_IMPORT_PARSER_VALID; } else { /* Not a dupe, so we had an error */ array_unshift($values, $newContact->_errors[0]['message']); return CRM_IMPORT_PARSER_ERROR; } } if ($newContact && !is_a($newContact, CRM_Core_Error)) { $this->_newContacts[] = $newContact->id; } return CRM_IMPORT_PARSER_VALID; }