X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FDeprecatedUtils.php;h=85effe9287b184f17a9fc54602b14904483470b3;hb=d44c681d9105af668449d16d9f53832d7982f47e;hp=42e389d7b910ed9efdbbd986c6a03f36b1e5e9ae;hpb=ab5d9fd76d951deb50097435f0c808f226ca50d6;p=civicrm-core.git diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index 42e389d7b9..85effe9287 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -1,9 +1,9 @@ singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", + $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value", $qParams ); - if (!$svq) { + if (!isset($svq)) { return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); + } else if ($svq == 1) { + return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact."); } $values['contact_id'] = $values['contribution_contact_id']; @@ -311,17 +316,17 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F } } } - elseif ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id']) { + elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) { //when update mode check contribution id or trxn id or //invoice id $contactId = new CRM_Contribute_DAO_Contribution(); - if ($params['contribution_id']) { + if (!empty($params['contribution_id'])) { $contactId->id = $params['contribution_id']; } - elseif ($params['trxn_id']) { + elseif (!empty($params['trxn_id'])) { $contactId->trxn_id = $params['trxn_id']; } - elseif ($params['invoice_id']) { + elseif (!empty($params['invoice_id'])) { $contactId->invoice_id = $params['invoice_id']; } if ($contactId->find(TRUE)) { @@ -333,6 +338,14 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F } } } + else { + if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { + return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped."); + } + else { + return civicrm_api3_create_error("Empty Contact and External ID. Row was skipped."); + } + } break; case 'receive_date': @@ -368,7 +381,7 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F break; } } - if (!CRM_Utils_Array::value('financial_type_id', $values)) { + if (empty($values['financial_type_id'])) { return civicrm_api3_create_error("Financial Type is not valid: $value"); } break; @@ -376,7 +389,7 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F case 'payment_instrument': require_once 'CRM/Core/OptionGroup.php'; $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value); - if (!CRM_Utils_Array::value('payment_instrument_id', $values)) { + if (empty($values['payment_instrument_id'])) { return civicrm_api3_create_error("Payment Instrument is not valid: $value"); } break; @@ -388,69 +401,68 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F } break; - case 'honor_type_id': - require_once 'CRM/Core/OptionGroup.php'; - $values['honor_type_id'] = CRM_Core_OptionGroup::getValue('honor_type', $value); - if (!CRM_Utils_Array::value('honor_type_id', $values)) { - return civicrm_api3_create_error("Honor Type is not valid: $value"); - } - break; - case 'soft_credit': //import contribution record according to select contact type - // validate contact id and external identifier. - $contactId = CRM_Utils_Array::value('contact_id', $params['soft_credit']); - $externalId = CRM_Utils_Array::value('external_identifier', $params['soft_credit']); - if ($contactId || $externalId) { - require_once 'CRM/Contact/DAO/Contact.php'; - $contact = new CRM_Contact_DAO_Contact(); - $contact->id = $contactId; - $contact->external_identifier = $externalId; - - $errorMsg = NULL; - if (!$contact->find(TRUE)) { - $errorMsg = ts("No match found for specified Soft Credit contact data. Row was skipped."); - } - - if ($errorMsg) { - return civicrm_api3_create_error($errorMsg, 'soft_credit'); - } - - // finally get soft credit contact id. - $values['soft_credit_to'] = $contact->id; - } - else { - // get the contact id from duplicate contact rule, if more than one contact is returned - // we should return error, since current interface allows only one-one mapping - - $softParams = $params['soft_credit']; - $softParams['contact_type'] = $params['contact_type']; - - $error = _civicrm_api3_deprecated_duplicate_formatted_contact($softParams); + $value[$key] = $mismatchContactType = $softCreditContactIds = ''; + if (isset($params[$key]) && is_array($params[$key])) { + foreach ($params[$key] as $softKey => $softParam) { + $contactId = CRM_Utils_Array::value('contact_id', $softParam); + $externalId = CRM_Utils_Array::value('external_identifier', $softParam); + $email = CRM_Utils_Array::value('email', $softParam); + if ($contactId || $externalId) { + require_once 'CRM/Contact/DAO/Contact.php'; + $contact = new CRM_Contact_DAO_Contact(); + $contact->id = $contactId; + $contact->external_identifier = $externalId; + $errorMsg = NULL; + if (!$contact->find(TRUE)) { + $field = $contactId ? ts('Contact ID') : ts('External ID'); + $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.", + array(1 => $field, 2 => $contactId ? $contactId : $externalId)); + } - if (isset($error['error_message']['params'][0])) { - $matchedIDs = explode(',', $error['error_message']['params'][0]); + if ($errorMsg) { + return civicrm_api3_create_error($errorMsg, $value[$key]); + } - // check if only one contact is found - if (count($matchedIDs) > 1) { - return civicrm_api3_create_error($error['error_message']['message'], 'soft_credit'); + // finally get soft credit contact id. + $values[$key][$softKey] = $softParam; + $values[$key][$softKey]['contact_id'] = $contact->id; } - else { - $values['soft_credit_to'] = $matchedIDs[0]; + elseif ($email) { + if (!CRM_Utils_Rule::email($email)) { + return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped"); + } + + // get the contact id from duplicate contact rule, if more than one contact is returned + // we should return error, since current interface allows only one-one mapping + $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']); + $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams); + if (!$checkDedupe['is_error']) { + return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped"); + } + else { + $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]); + if (count($matchingContactIds) > 1) { + return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped"); + } + elseif (count($matchingContactIds) == 1) { + $contactId = $matchingContactIds[0]; + unset($softParam['email']); + $values[$key][$softKey] = $softParam + array('contact_id' => $contactId); + } + } } } - else { - return civicrm_api3_create_error('No match found for specified Soft Credit contact data. Row was skipped.', 'soft_credit'); - } - } - break; + } + break; case 'pledge_payment': case 'pledge_id': //giving respect to pledge_payment flag. - if (!CRM_Utils_Array::value('pledge_payment', $params)) { + if (empty($params['pledge_payment'])) { continue; } @@ -489,10 +501,10 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F } else { // first get the contact id for given contribution record. - if (CRM_Utils_Array::value('contribution_contact_id', $params)) { + if (!empty($params['contribution_contact_id'])) { $contributionContactID = $params['contribution_contact_id']; } - elseif (CRM_Utils_Array::value('external_identifier', $params)) { + elseif (!empty($params['external_identifier'])) { require_once 'CRM/Contact/DAO/Contact.php'; $contact = new CRM_Contact_DAO_Contact(); $contact->external_identifier = $params['external_identifier']; @@ -524,7 +536,7 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F } } - if (CRM_Utils_Array::value('pledge_id', $params)) { + if (!empty($params['pledge_id'])) { if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) { return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.', 'pledge_payment'); } @@ -584,7 +596,7 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F } /** - * Function to check duplicate contacts based on de-deupe parameters + * check duplicate contacts based on de-deupe parameters */ function _civicrm_api3_deprecated_check_contact_dedupe($params) { static $cIndieFields = NULL; @@ -658,11 +670,11 @@ function _civicrm_api3_deprecated_check_contact_dedupe($params) { * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * - * @param array $params Associative array of property name/value + * @param array $params Associative array of property name/value * pairs to insert in new contact. - * @param array $values The reformatted properties that we can use internally + * @param array $values The reformatted properties that we can use internally * - * @param array $create Is the formatted Values array going to + * @param array|bool $create Is the formatted Values array going to * be used for CRM_Activity_BAO_Activity::create() * * @return array|CRM_Error @@ -782,7 +794,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } if (isset($values['individual_prefix'])) { - if (CRM_Utils_Array::value('prefix_id', $params)) { + if (!empty($params['prefix_id'])) { $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); $params['prefix'] = $prefixes[$params['prefix_id']]; } @@ -793,7 +805,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } if (isset($values['individual_suffix'])) { - if (CRM_Utils_Array::value('suffix_id', $params)) { + if (!empty($params['suffix_id'])) { $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); $params['suffix'] = $suffixes[$params['suffix_id']]; } @@ -805,7 +817,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { //CRM-4575 if (isset($values['email_greeting'])) { - if (CRM_Utils_Array::value('email_greeting_id', $params)) { + if (!empty($params['email_greeting_id'])) { $emailGreetingFilter = array( 'contact_type' => CRM_Utils_Array::value('contact_type', $params), 'greeting_type' => 'email_greeting', @@ -821,7 +833,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } if (isset($values['postal_greeting'])) { - if (CRM_Utils_Array::value('postal_greeting_id', $params)) { + if (!empty($params['postal_greeting_id'])) { $postalGreetingFilter = array( 'contact_type' => CRM_Utils_Array::value('contact_type', $params), 'greeting_type' => 'postal_greeting', @@ -836,7 +848,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } if (isset($values['addressee'])) { - if (CRM_Utils_Array::value('addressee_id', $params)) { + if (!empty($params['addressee_id'])) { $addresseeFilter = array( 'contact_type' => CRM_Utils_Array::value('contact_type', $params), 'greeting_type' => 'addressee', @@ -851,7 +863,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } if (isset($values['gender'])) { - if (CRM_Utils_Array::value('gender_id', $params)) { + if (!empty($params['gender_id'])) { $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); $params['gender'] = $genders[$params['gender_id']]; } @@ -861,7 +873,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { return TRUE; } - if (isset($values['preferred_communication_method'])) { + if (!empty($values['preferred_communication_method'])) { $comm = array(); $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); @@ -878,7 +890,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } //format the website params. - if (CRM_Utils_Array::value('url', $values)) { + if (!empty($values['url'])) { static $websiteFields; if (!is_array($websiteFields)) { require_once 'CRM/Core/DAO/Website.php'; @@ -899,7 +911,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 - if (CRM_Utils_Array::value('location_type_id', $values)) { + if (!empty($values['location_type_id'])) { _civicrm_api3_deprecated_add_formatted_location_blocks($values, $params); return TRUE; } @@ -931,7 +943,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { /* Check for custom field values */ - if (!CRM_Utils_Array::value('custom', $fields)) { + if (empty($fields['custom'])) { $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE ); @@ -998,7 +1010,7 @@ function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$para $params[$name][++$blockCnt] ); - if (!CRM_Utils_Array::value('id', $params) && ($blockCnt == 1)) { + if (empty($params['id']) && ($blockCnt == 1)) { $params[$name][$blockCnt]['is_primary'] = TRUE; } @@ -1030,7 +1042,7 @@ function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$para // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving // the address in CRM_Core_BAO_Address::create method - if (CRM_Utils_Array::value('location_type_id', $values)) { + if (!empty($values['location_type_id'])) { static $customFields = array(); if (empty($customFields)) { $customFields = CRM_Core_BAO_CustomField::getFields('Address'); @@ -1102,7 +1114,7 @@ function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$para * * @param $params * - * @return + * @return array */ function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { $id = CRM_Utils_Array::value('id', $params); @@ -1165,7 +1177,7 @@ function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { } //check for loc type id. - if (!CRM_Utils_Array::value('location_type_id', $values)) { + if (empty($values['location_type_id'])) { return civicrm_api3_create_error('Location Type Id missing.'); } } @@ -1197,14 +1209,13 @@ function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { } - /** * @deprecated - this is part of the import parser not the API & needs to be moved on out * - * @param $params - * @param $onDuplicate + * @param array $params + * @param $onDuplicate * - * @return + * @return array|bool */ function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) { require_once 'CRM/Event/Import/Parser.php'; @@ -1223,12 +1234,14 @@ function _civicrm_api3_deprecated_create_participant_formatted($params, $onDupli * * @param $params * - * @return + * @param bool $checkDuplicate + * + * @return array|bool */ function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) { //check if participant id is valid or not - if (CRM_Utils_Array::value('id', $params)) { + if (!empty($params['id'])) { $participant = new CRM_Event_BAO_Participant(); $participant->id = $params['id']; if (!$participant->find(TRUE)) { @@ -1237,7 +1250,7 @@ function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplic } require_once 'CRM/Contact/BAO/Contact.php'; //check if contact id is valid or not - if (CRM_Utils_Array::value('contact_id', $params)) { + if (!empty($params['contact_id'])) { $contact = new CRM_Contact_BAO_Contact(); $contact->id = $params['contact_id']; if (!$contact->find(TRUE)) { @@ -1246,10 +1259,10 @@ function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplic } //check that event id is not an template - if (CRM_Utils_Array::value('event_id', $params)) { + if (!empty($params['event_id'])) { $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); if (!empty($isTemplate)) { - return civicrm_api3_create_error(ts('Event templates are not meant to be registered')); + return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); } } @@ -1313,6 +1326,15 @@ function _civicrm_api3_deprecated_contact_check_custom_params($params, $csType = } } +/** + * @param array $params + * @param bool $dupeCheck + * @param bool $dupeErrorArray + * @param bool $requiredCheck + * @param null $dedupeRuleGroupID + * + * @return array|null + */ function _civicrm_api3_deprecated_contact_check_params( &$params, $dupeCheck = TRUE, @@ -1351,18 +1373,18 @@ function _civicrm_api3_deprecated_contact_check_params( if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) { - return civicrm_api3_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$csType)); + return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array)$csType)); } } - if (!CRM_Utils_Array::value('contact_id', $params) && CRM_Utils_Array::value('id', $params)) { + if (empty($params['contact_id']) && !empty($params['id'])) { $valid = FALSE; $error = ''; foreach ($fields as $field) { if (is_array($field)) { $valid = TRUE; foreach ($field as $element) { - if (!CRM_Utils_Array::value($element, $params)) { + if (empty($params[$element])) { $valid = FALSE; $error .= $element; break; @@ -1370,7 +1392,7 @@ function _civicrm_api3_deprecated_contact_check_params( } } else { - if (CRM_Utils_Array::value($field, $params)) { + if (!empty($params[$field])) { $valid = TRUE; } } @@ -1414,7 +1436,7 @@ function _civicrm_api3_deprecated_contact_check_params( } //check for organisations with same name - if (CRM_Utils_Array::value('current_employer', $params)) { + if (!empty($params['current_employer'])) { $organizationParams = array(); $organizationParams['organization_name'] = $params['current_employer']; @@ -1425,15 +1447,13 @@ function _civicrm_api3_deprecated_contact_check_params( $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); // check for mismatch employer name and id - if (CRM_Utils_Array::value('employer_id', $params) - && !in_array($params['employer_id'], $dupeIds) + if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) ) { return civicrm_api3_create_error('Employer name and Employer id Mismatch'); } // show error if multiple organisation with same name exist - if (!CRM_Utils_Array::value('employer_id', $params) - && (count($dupeIds) > 1) + if (empty($params['employer_id']) && (count($dupeIds) > 1) ) { return civicrm_api3_create_error('Found more than one Organisation with same Name.'); } @@ -1444,10 +1464,10 @@ function _civicrm_api3_deprecated_contact_check_params( /** * - * @param $result - * @param $activityTypeID + * @param $result + * @param $activityTypeID * - * @return $params + * @return array $params */ function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) { // get ready for collecting data about activity to be created