/** * Combine all the importable fields from the lower levels object. * * The ordering is important, since currently we do not have a weight * scheme. Adding weight is super important and should be done in the * next week or so, before this can be called complete. * * @param string $contactType * @param bool $status * * @return array * array of importable Fields */ public static function &importableFields($contactType = 'Individual', $status = TRUE) { if (!self::$_importableFields) { if (!self::$_importableFields) { self::$_importableFields = array(); } if (!$status) { $fields = array('' => array('title' => ts('- do not import -'))); } else { $fields = array('' => array('title' => ts('- Contribution Fields -'))); } $note = CRM_Core_DAO_Note::import(); $tmpFields = CRM_Contribute_DAO_Contribution::import(); unset($tmpFields['option_value']); $optionFields = CRM_Core_OptionValue::getFields($mode = 'contribute'); $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); // Using new Dedupe rule. $ruleParams = array('contact_type' => $contactType, 'used' => 'Unsupervised'); $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); $tmpContactField = array(); if (is_array($fieldsArray)) { foreach ($fieldsArray as $value) { //skip if there is no dupe rule if ($value == 'none') { continue; } $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $value, 'id', 'column_name'); $value = $customFieldId ? 'custom_' . $customFieldId : $value; $tmpContactField[trim($value)] = $contactFields[trim($value)]; if (!$status) { $title = $tmpContactField[trim($value)]['title'] . ' ' . ts('(match to contact)'); } else { $title = $tmpContactField[trim($value)]['title']; } $tmpContactField[trim($value)]['title'] = $title; } } $tmpContactField['external_identifier'] = $contactFields['external_identifier']; $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . ' ' . ts('(match to contact)'); $tmpFields['contribution_contact_id']['title'] = $tmpFields['contribution_contact_id']['title'] . ' ' . ts('(match to contact)'); $fields = array_merge($fields, $tmpContactField); $fields = array_merge($fields, $tmpFields); $fields = array_merge($fields, $note); $fields = array_merge($fields, $optionFields); $fields = array_merge($fields, CRM_Financial_DAO_FinancialType::export()); $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); self::$_importableFields = $fields; } return self::$_importableFields; }
/** * combine all the importable fields from the lower levels object * * The ordering is important, since currently we do not have a weight * scheme. Adding weight is super important and should be done in the * next week or so, before this can be called complete. * * @return array array of importable Fields * @access public */ function &importableFields($contacType = 'Individual', $status = true) { if (!self::$_importableFields) { if (!self::$_importableFields) { self::$_importableFields = array(); } if (!$status) { $fields = array('' => array('title' => ts('- do not import -'))); } else { $fields = array('' => array('title' => ts('- Contribution Fields -'))); } require_once 'CRM/Core/DAO/Note.php'; $note = CRM_Core_DAO_Note::import(); $tmpFields = CRM_Contribute_DAO_Contribution::import(); unset($tmpFields['option_value']); require_once 'CRM/Core/OptionValue.php'; $optionFields = CRM_Core_OptionValue::getFields($mode = 'contribute'); require_once 'CRM/Contact/BAO/Contact.php'; $contactFields = CRM_Contact_BAO_Contact::importableFields($contacType, null); // Using new Dedupe rule. $ruleParams = array('contact_type' => $contacType, 'level' => 'Strict'); require_once 'CRM/Dedupe/BAO/Rule.php'; $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); $tmpConatctField = array(); if (is_array($fieldsArray)) { foreach ($fieldsArray as $value) { //skip if there is no dupe rule if ($value == 'none') { continue; } $tmpConatctField[trim($value)] = $contactFields[trim($value)]; if (!$status) { $title = $tmpConatctField[trim($value)]['title'] . " (match to contact)"; } else { $title = $tmpConatctField[trim($value)]['title']; } $tmpConatctField[trim($value)]['title'] = $title; } } $tmpConatctField['external_identifier'] = $contactFields['external_identifier']; $tmpConatctField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " (match to contact)"; $tmpFields['contribution_contact_id']['title'] = $tmpFields['contribution_contact_id']['title'] . " (match to contact)"; $fields = array_merge($fields, $tmpConatctField); $fields = array_merge($fields, $tmpFields); $fields = array_merge($fields, $note); $fields = array_merge($fields, $optionFields); require_once 'CRM/Contribute/DAO/ContributionType.php'; $fields = array_merge($fields, CRM_Contribute_DAO_ContributionType::export()); $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); self::$_importableFields = $fields; } return self::$_importableFields; }
/** * 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 bool * the result of this processing */ public function import($onDuplicate, &$values) { // first make sure this is a valid line $response = $this->summary($values); if ($response != CRM_Import_Parser::VALID) { return $response; } $params =& $this->getActiveFieldParams(); $formatted = array('version' => 3); // don't add to recent items, CRM-4399 $formatted['skipRecentView'] = TRUE; //for date-Formats $session = CRM_Core_Session::singleton(); $dateType = $session->get('dateTypes'); $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Contribution'; $customFields = CRM_Core_BAO_CustomField::getFields($customDataType); //CRM-10994 if (isset($params['total_amount']) && $params['total_amount'] == 0) { $params['total_amount'] = '0.00'; } foreach ($params as $key => $val) { if ($val) { switch ($key) { case 'receive_date': case 'cancel_date': case 'receipt_date': case 'thankyou_date': $params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType); break; case 'pledge_payment': $params[$key] = CRM_Utils_String::strtobool($val); break; } if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { if ($customFields[$customFieldID]['data_type'] == 'Date') { CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); unset($params[$key]); } elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { $params[$key] = CRM_Utils_String::strtoboolstr($val); } } } } //date-Format part ends static $indieFields = NULL; if ($indieFields == NULL) { $tempIndieFields = CRM_Contribute_DAO_Contribution::import(); $indieFields = $tempIndieFields; } $paramValues = array(); foreach ($params as $key => $field) { if ($field == NULL || $field === '') { continue; } $paramValues[$key] = $field; } //import contribution record according to select contact type if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP && (!empty($paramValues['contribution_contact_id']) || !empty($paramValues['external_identifier']))) { $paramValues['contact_type'] = $this->_contactType; } elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))) { $paramValues['contact_type'] = $this->_contactType; } elseif (!empty($params['soft_credit'])) { $paramValues['contact_type'] = $this->_contactType; } elseif (!empty($paramValues['pledge_payment'])) { $paramValues['contact_type'] = $this->_contactType; } //need to pass $onDuplicate to check import mode. if (!empty($paramValues['pledge_payment'])) { $paramValues['onDuplicate'] = $onDuplicate; } require_once 'CRM/Utils/DeprecatedUtils.php'; $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE, $onDuplicate); if ($formatError) { array_unshift($values, $formatError['error_message']); if (CRM_Utils_Array::value('error_data', $formatError) == 'soft_credit') { return CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR; } elseif (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') { return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR; } return CRM_Import_Parser::ERROR; } if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, NULL, 'Contribution'); } else { //fix for CRM-2219 - Update Contribution // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) { $dupeIds = array('id' => CRM_Utils_Array::value('contribution_id', $paramValues), 'trxn_id' => CRM_Utils_Array::value('trxn_id', $paramValues), 'invoice_id' => CRM_Utils_Array::value('invoice_id', $paramValues)); $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds); if ($ids['contribution']) { $formatted['id'] = $ids['contribution']; $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, $formatted['id'], 'Contribution'); //process note if (!empty($paramValues['note'])) { $noteID = array(); $contactID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id'); $daoNote = new CRM_Core_BAO_Note(); $daoNote->entity_table = 'civicrm_contribution'; $daoNote->entity_id = $ids['contribution']; if ($daoNote->find(TRUE)) { $noteID['id'] = $daoNote->id; } $noteParams = array('entity_table' => 'civicrm_contribution', 'note' => $paramValues['note'], 'entity_id' => $ids['contribution'], 'contact_id' => $contactID); CRM_Core_BAO_Note::add($noteParams, $noteID); unset($formatted['note']); } //need to check existing soft credit contribution, CRM-3968 if (!empty($formatted['soft_credit'])) { $dupeSoftCredit = array('contact_id' => $formatted['soft_credit'], 'contribution_id' => $ids['contribution']); //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']); if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) { foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) { if (!empty($existingSoftCreditValues['soft_credit_id'])) { $deleteParams = array('id' => $existingSoftCreditValues['soft_credit_id'], 'pcp_id' => NULL); CRM_Contribute_BAO_ContributionSoft::del($deleteParams); } } } } $newContribution = CRM_Contribute_BAO_Contribution::create($formatted, $ids); $this->_newContributions[] = $newContribution->id; //return soft valid since we need to show how soft credits were added if (!empty($formatted['soft_credit'])) { return CRM_Contribute_Import_Parser::SOFT_CREDIT; } // process pledge payment assoc w/ the contribution return self::processPledgePayments($formatted); return CRM_Import_Parser::VALID; } else { $labels = array('id' => 'Contribution ID', 'trxn_id' => 'Transaction ID', 'invoice_id' => 'Invoice ID'); foreach ($dupeIds as $k => $v) { if ($v) { $errorMsg[] = "{$labels[$k]} {$v}"; } } $errorMsg = implode(' AND ', $errorMsg); array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.'); return CRM_Import_Parser::ERROR; } } } if ($this->_contactIdIndex < 0) { // set the contact type if its not set if (!isset($paramValues['contact_type'])) { $paramValues['contact_type'] = $this->_contactType; } $paramValues['version'] = 3; //retrieve contact id using contact dedupe rule require_once 'CRM/Utils/DeprecatedUtils.php'; $error = _civicrm_api3_deprecated_check_contact_dedupe($paramValues); if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { $matchedIDs = explode(',', $error['error_message']['params'][0]); if (count($matchedIDs) > 1) { array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported'); return CRM_Import_Parser::ERROR; } else { $cid = $matchedIDs[0]; $formatted['contact_id'] = $cid; $newContribution = civicrm_api('contribution', 'create', $formatted); if (civicrm_error($newContribution)) { if (is_array($newContribution['error_message'])) { array_unshift($values, $newContribution['error_message']['message']); if ($newContribution['error_message']['params'][0]) { return CRM_Import_Parser::DUPLICATE; } } else { array_unshift($values, $newContribution['error_message']); return CRM_Import_Parser::ERROR; } } $this->_newContributions[] = $newContribution['id']; $formatted['contribution_id'] = $newContribution['id']; //return soft valid since we need to show how soft credits were added if (!empty($formatted['soft_credit'])) { return CRM_Contribute_Import_Parser::SOFT_CREDIT; } // process pledge payment assoc w/ the contribution return self::processPledgePayments($formatted); return CRM_Import_Parser::VALID; } } else { // Using new Dedupe rule. $ruleParams = array('contact_type' => $this->_contactType, 'used' => 'Unsupervised'); $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); $disp = NULL; 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)] . " "; } } } if (!empty($params['external_identifier'])) { if ($disp) { $disp .= "AND {$params['external_identifier']}"; } else { $disp = $params['external_identifier']; } } array_unshift($values, 'No matching Contact found for (' . $disp . ')'); return CRM_Import_Parser::ERROR; } } else { if (!empty($paramValues['external_identifier'])) { $checkCid = new CRM_Contact_DAO_Contact(); $checkCid->external_identifier = $paramValues['external_identifier']; $checkCid->find(TRUE); if ($checkCid->id != $formatted['contact_id']) { array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); return CRM_Import_Parser::ERROR; } } $newContribution = civicrm_api('contribution', 'create', $formatted); if (civicrm_error($newContribution)) { if (is_array($newContribution['error_message'])) { array_unshift($values, $newContribution['error_message']['message']); if ($newContribution['error_message']['params'][0]) { return CRM_Import_Parser::DUPLICATE; } } else { array_unshift($values, $newContribution['error_message']); return CRM_Import_Parser::ERROR; } } $this->_newContributions[] = $newContribution['id']; $formatted['contribution_id'] = $newContribution['id']; //return soft valid since we need to show how soft credits were added if (!empty($formatted['soft_credit'])) { return CRM_Contribute_Import_Parser::SOFT_CREDIT; } // process pledge payment assoc w/ the contribution return self::processPledgePayments($formatted); return CRM_Import_Parser::VALID; } }
/** * combine all the importable fields from the lower levels object * * The ordering is important, since currently we do not have a weight * scheme. Adding weight is super important and should be done in the * next week or so, before this can be called complete. * * @return array array of importable Fields * @access public */ function &importableFields() { if (!$GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['_importableFields']) { if (!$GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['_importableFields']) { $GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['_importableFields'] = array(); } if (!$status) { $fields = array('' => array('title' => ts('- do not import -'))); } else { $fields = array('' => array('title' => ts('- Contribution Fields -'))); } $tmpFields = CRM_Contribute_DAO_Contribution::import(); $contactFields = CRM_Contact_BAO_Contact::importableFields('Individual', null); require_once 'CRM/Core/DAO/DupeMatch.php'; $dao =& new CRM_Core_DAO_DupeMatch(); $dao->find(true); $fieldsArray = explode('AND', $dao->rule); $tmpConatctField = array(); if (is_array($fieldsArray)) { foreach ($fieldsArray as $value) { $tmpConatctField[trim($value)] = $contactFields[trim($value)]; $tmpConatctField[trim($value)]['title'] = $tmpConatctField[trim($value)]['title'] . " (match to contact)"; } } $fields = array_merge($fields, $tmpConatctField); $fields = array_merge($fields, $tmpFields); $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); $GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['_importableFields'] = $fields; } return $GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['_importableFields']; }
/** * 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 $response = $this->summary($values); if ($response != CRM_Contribute_Import_Parser::VALID) { return $response; } $params =& $this->getActiveFieldParams(); $formatted = array(); // don't add to recent items, CRM-4399 $formatted['skipRecentView'] = true; //for date-Formats $session =& CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $params)); 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; case 'pledge_payment': $params[$key] = CRM_Utils_String::strtobool($val); break; } if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { if ($customFields[$customFieldID]['data_type'] == 'Date') { CRM_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); unset($params[$key]); } else { if ($customFields[$customFieldID]['data_type'] == 'Boolean') { $params[$key] = CRM_Utils_String::strtoboolstr($val); } } } } } //date-Format part ends static $indieFields = null; if ($indieFields == null) { require_once 'CRM/Contribute/DAO/Contribution.php'; $tempIndieFields =& CRM_Contribute_DAO_Contribution::import(); $indieFields = $tempIndieFields; } $paramValues = array(); foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } $paramValues[$key] = $field; } //import contribution record according to select contact type if ($onDuplicate == CRM_Contribute_Import_Parser::DUPLICATE_SKIP && ($paramValues['contribution_contact_id'] || $paramValues['external_identifier'])) { $paramValues['contact_type'] = $this->_contactType; } else { if ($onDuplicate == CRM_Contribute_Import_Parser::DUPLICATE_UPDATE && ($paramValues['contribution_id'] || $values['trxn_id'] || $paramValues['invoice_id'])) { $paramValues['contact_type'] = $this->_contactType; } else { if (!empty($params['soft_credit'])) { $paramValues['contact_type'] = $this->_contactType; } else { if (CRM_Utils_Array::value('pledge_payment', $paramValues)) { $paramValues['contact_type'] = $this->_contactType; } } } } //need to pass $onDuplicate to check import mode. if (CRM_Utils_Array::value('pledge_payment', $paramValues)) { $paramValues['onDuplicate'] = $onDuplicate; } $formatError = _civicrm_contribute_formatted_param($paramValues, $formatted, true); if ($formatError) { array_unshift($values, $formatError['error_message']); if (CRM_Utils_Array::value('error_data', $formatError) == 'soft_credit') { return CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR; } else { if (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') { return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR; } } return CRM_Contribute_Import_Parser::ERROR; } if ($onDuplicate != CRM_Contribute_Import_Parser::DUPLICATE_UPDATE) { $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, CRM_Core_DAO::$_nullObject, null, 'Contribution'); } else { //fix for CRM-2219 - Update Contribution // onDuplicate == CRM_Contribute_Import_Parser::DUPLICATE_UPDATE if ($paramValues['invoice_id'] || $paramValues['trxn_id'] || $paramValues['contribution_id']) { require_once 'CRM/Contribute/BAO/Contribution.php'; $dupeIds = array('id' => CRM_Utils_Array::value('contribution_id', $paramValues), 'trxn_id' => CRM_Utils_Array::value('trxn_id', $paramValues), 'invoice_id' => CRM_Utils_Array::value('invoice_id', $paramValues)); $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds); if ($ids['contribution']) { $formatted['id'] = $ids['contribution']; $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, CRM_Core_DAO::$_nullObject, $formatted['id'], 'Contribution'); //process note if ($paramValues['note']) { $noteID = array(); $contactID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id'); require_once 'CRM/Core/BAO/Note.php'; $daoNote =& new CRM_Core_BAO_Note(); $daoNote->entity_table = 'civicrm_contribution'; $daoNote->entity_id = $ids['contribution']; if ($daoNote->find(true)) { $noteID['id'] = $daoNote->id; } $noteParams = array('entity_table' => 'civicrm_contribution', 'note' => $paramValues['note'], 'entity_id' => $ids['contribution'], 'contact_id' => $contactID); CRM_Core_BAO_Note::add($noteParams, $noteID); unset($formatted['note']); } //need to check existing soft credit contribution, CRM-3968 if (CRM_Utils_Array::value('soft_credit_to', $formatted)) { $dupeSoftCredit = array('contact_id' => $formatted['soft_credit_to'], 'contribution_id' => $ids['contribution']); $existingSoftCredit = CRM_Contribute_BAO_Contribution::getSoftContribution($dupeSoftCredit); if (CRM_Utils_Array::value('soft_credit_id', $existingSoftCredit)) { $formatted['softID'] = $existingSoftCredit['soft_credit_id']; } } $newContribution =& CRM_Contribute_BAO_Contribution::create($formatted, $ids); $this->_newContributions[] = $newContribution->id; //return soft valid since we need to show how soft credits were added if (CRM_Utils_Array::value('soft_credit_to', $formatted)) { return CRM_Contribute_Import_Parser::SOFT_CREDIT; } // process pledge payment assoc w/ the contribution return self::processPledgePayments($formatted); return CRM_Contribute_Import_Parser::VALID; } else { $labels = array('id' => 'Contribution ID', 'trxn_id' => 'Transaction ID', 'invoice_id' => 'Invoice ID'); foreach ($dupeIds as $k => $v) { if ($v) { $errorMsg[] = "{$labels[$k]} {$v}"; } } $errorMsg = implode(' AND ', $errorMsg); array_unshift($values, "Matching Contribution record not found for " . $errorMsg . ". Row was skipped."); return CRM_Contribute_Import_Parser::ERROR; } } } if ($this->_contactIdIndex < 0) { // set the contact type if its not set if (!isset($paramValues['contact_type'])) { $paramValues['contact_type'] = $this->_contactType; } //retrieve contact id using contact dedupe rule $error = civicrm_check_contact_dedupe($paramValues); if (civicrm_duplicate($error)) { $matchedIDs = explode(',', $error['error_message']['params'][0]); 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 = civicrm_contribution_format_create($formatted); if (civicrm_error($newContribution)) { if (is_array($newContribution['error_message'])) { array_unshift($values, $newContribution['error_message']['message']); if ($newContribution['error_message']['params'][0]) { return CRM_Contribute_Import_Parser::DUPLICATE; } } else { array_unshift($values, $newContribution['error_message']); return CRM_Contribute_Import_Parser::ERROR; } } $this->_newContributions[] = $newContribution['id']; //return soft valid since we need to show how soft credits were added if (CRM_Utils_Array::value('soft_credit_to', $formatted)) { return CRM_Contribute_Import_Parser::SOFT_CREDIT; } // process pledge payment assoc w/ the contribution return self::processPledgePayments($formatted); return CRM_Contribute_Import_Parser::VALID; } } else { // Using new Dedupe rule. $ruleParams = array('contact_type' => $this->_contactType, 'level' => 'Strict'); require_once 'CRM/Dedupe/BAO/Rule.php'; $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); 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)] . " "; } } } if (CRM_Utils_Array::value('external_identifier', $params)) { if ($disp) { $disp .= "AND {$params['external_identifier']}"; } else { $disp = $params['external_identifier']; } } array_unshift($values, "No matching Contact found for (" . $disp . ")"); return CRM_Contribute_Import_Parser::ERROR; } } else { if ($paramValues['external_identifier']) { $checkCid = new CRM_Contact_DAO_Contact(); $checkCid->external_identifier = $paramValues['external_identifier']; $checkCid->find(true); if ($checkCid->id != $formatted['contact_id']) { array_unshift($values, "Mismatch of External identifier :" . $paramValues['external_identifier'] . " and Contact Id:" . $formatted['contact_id']); return CRM_Contribute_Import_Parser::ERROR; } } $newContribution = civicrm_contribution_format_create($formatted); if (civicrm_error($newContribution)) { if (is_array($newContribution['error_message'])) { array_unshift($values, $newContribution['error_message']['message']); if ($newContribution['error_message']['params'][0]) { return CRM_Contribute_Import_Parser::DUPLICATE; } } else { array_unshift($values, $newContribution['error_message']); return CRM_Contribute_Import_Parser::ERROR; } } $this->_newContributions[] = $newContribution['id']; //return soft valid since we need to show how soft credits were added if (CRM_Utils_Array::value('soft_credit_to', $formatted)) { return CRM_Contribute_Import_Parser::SOFT_CREDIT; } // process pledge payment assoc w/ the contribution return self::processPledgePayments($formatted); return CRM_Contribute_Import_Parser::VALID; } }
function &exportableFields() { if (!self::$_exportableFields) { if (!self::$_exportableFields) { self::$_exportableFields = array(); } require_once 'CRM/Core/OptionValue.php'; require_once 'CRM/Contribute/DAO/Product.php'; require_once 'CRM/Contribute/DAO/ContributionProduct.php'; require_once 'CRM/Contribute/DAO/ContributionType.php'; $impFields = CRM_Contribute_DAO_Contribution::import(); $expFieldProduct = CRM_Contribute_DAO_Product::export(); $expFieldsContrib = CRM_Contribute_DAO_ContributionProduct::export(); $typeField = CRM_Contribute_DAO_ContributionType::export(); $optionField = CRM_Core_OptionValue::getFields($mode = 'contribute'); $fields = array_merge($impFields, $typeField); $fields = array_merge($fields, $expFieldProduct); $fields = array_merge($fields, $expFieldsContrib); $fields = array_merge($fields, $optionField); $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); self::$_exportableFields = $fields; } return self::$_exportableFields; }