/** * Global validation rules for the form. * * @param array $fields * Posted values of the form. * * @param $files * @param $self * * @return array * list of errors to be posted back to the form */ public static function formRule($fields, $files, $self) { $errors = array(); if (!array_key_exists('savedMapping', $fields)) { $importKeys = array(); foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } // FIXME: should use the schema titles, not redeclare them $requiredFields = array('membership_contact_id' => ts('Contact ID'), 'membership_type_id' => ts('Membership Type'), 'membership_start_date' => ts('Membership Start Date')); $contactTypeId = $self->get('contactType'); $contactTypes = array(CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization'); $params = array('used' => 'Unsupervised', 'contact_type' => $contactTypes[$contactTypeId]); list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { $weightSum += $ruleFields[$val]; } } $fieldMessage = ''; foreach ($ruleFields as $field => $weight) { $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; } foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if ($field == 'membership_contact_id') { if (($weightSum >= $threshold || in_array('external_identifier', $importKeys)) && $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE || in_array('membership_id', $importKeys)) { continue; } else { if (!isset($errors['_qf_default'])) { $errors['_qf_default'] = ''; } $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " {$fieldMessage} " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(1 => $threshold)) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />'; } } else { if (!isset($errors['_qf_default'])) { $errors['_qf_default'] = ''; } $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)) . '<br />'; } } } } if (!empty($fields['saveMapping'])) { $nameField = CRM_Utils_Array::value('saveMappingName', $fields); if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Membership', 'name'); if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name'); } } } if (!empty($errors)) { if (!empty($errors['saveMappingName'])) { $_flag = 1; $assignError = new CRM_Core_Page(); $assignError->assign('mappingDetailsError', $_flag); } return $errors; } return TRUE; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule($fields, $files, $self) { $errors = array(); $fieldMessage = NULL; if (!array_key_exists('savedMapping', $fields)) { $importKeys = array(); foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } // FIXME: should use the schema titles, not redeclare them $requiredFields = array('participant_contact_id' => ts('Contact ID'), 'event_id' => ts('Event ID')); $contactTypeId = $self->get('contactType'); $contactTypes = array(CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization'); $params = array('used' => 'Unsupervised', 'contact_type' => $contactTypes[$contactTypeId]); list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { $weightSum += $ruleFields[$val]; } } foreach ($ruleFields as $field => $weight) { $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; } foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if ($field == 'participant_contact_id') { if ($weightSum >= $threshold || in_array('external_identifier', $importKeys) || in_array('participant_id', $importKeys)) { continue; } if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { $errors['_qf_default'] .= ts('Missing required field: Provide Particiapnt ID') . '<br />'; } else { $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " {$fieldMessage} " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(1 => $threshold)) . ' ' . ts('Or Provide Contact Id or External Identifier.') . '<br />'; } } elseif (!in_array('event_title', $importKeys)) { $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2', array(1 => $title, 2 => 'Event Title')) . '<br />'; } } } } if (CRM_Utils_Array::value('saveMapping', $fields)) { $nameField = CRM_Utils_Array::value('saveMappingName', $fields); if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Participant', 'name'); if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name'); } } } //display Error if loaded mapping is not selected if (array_key_exists('loadMapping', $fields)) { $getMapName = CRM_Utils_Array::value('savedMapping', $fields); if (empty($getMapName)) { $errors['savedMapping'] = ts('Select saved mapping'); } } if (!empty($errors)) { if (!empty($errors['saveMappingName'])) { $_flag = 1; $assignError = new CRM_Core_Page(); $assignError->assign('mappingDetailsError', $_flag); } return $errors; } return TRUE; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule($fields) { $errors = array(); // define so we avoid notices below $errors['_qf_default'] = ''; $fieldMessage = NULL; if (!array_key_exists('savedMapping', $fields)) { $importKeys = array(); foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } // FIXME: should use the schema titles, not redeclare them $requiredFields = array('target_contact_id' => ts('Contact ID'), 'activity_date_time' => ts('Activity Date'), 'activity_subject' => ts('Activity Subject'), 'activity_type_id' => ts('Activity Type Id')); $params = array('used' => 'Unsupervised', 'contact_type' => 'Individual'); list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { $weightSum += $ruleFields[$val]; } } foreach ($ruleFields as $field => $weight) { $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; } foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if ($field == 'target_contact_id') { if ($weightSum >= $threshold || in_array('external_identifier', $importKeys)) { continue; } else { $errors['_qf_default'] .= ts('Missing required contact matching fields.') . $fieldMessage . ' ' . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(1 => $threshold)) . '<br />'; } } elseif ($field == 'activity_type_id') { if (in_array('activity_label', $importKeys)) { continue; } else { $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2', array(1 => $title, 2 => 'Activity Type Label')) . '<br />'; } } else { $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)) . '<br />'; } } } } if (!empty($fields['saveMapping'])) { $nameField = CRM_Utils_Array::value('saveMappingName', $fields); if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Activity', 'name'); if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { $errors['saveMappingName'] = ts('Duplicate Import Mapping Name'); } } } if (empty($errors['_qf_default'])) { unset($errors['_qf_default']); } if (!empty($errors)) { if (!empty($errors['saveMappingName'])) { $_flag = 1; $assignError = new CRM_Core_Page(); $assignError->assign('mappingDetailsError', $_flag); } return $errors; } return TRUE; }
/** * Global validation rules for the form. * * @param array $fields * Posted values of the form. * * @param $files * @param $self * * @return array * list of errors to be posted back to the form */ public static function formRule($fields, $files, $self) { $errors = array(); $fieldMessage = NULL; $contactORContributionId = $self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE ? 'contribution_id' : 'contribution_contact_id'; if (!array_key_exists('savedMapping', $fields)) { $importKeys = array(); foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } $contactTypeId = $self->get('contactType'); $contactTypes = array(CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization'); $params = array('used' => 'Unsupervised', 'contact_type' => isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : ''); list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { $weightSum += $ruleFields[$val]; } if ($val == "soft_credit") { $mapperKey = CRM_Utils_Array::key('soft_credit', $importKeys); if (empty($fields['mapper'][$mapperKey][1])) { if (empty($errors['_qf_default'])) { $errors['_qf_default'] = ''; } $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />'; } } } foreach ($ruleFields as $field => $weight) { $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; } // FIXME: should use the schema titles, not redeclare them $requiredFields = array($contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'), 'total_amount' => ts('Total Amount'), 'financial_type' => ts('Financial Type')); foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if (empty($errors['_qf_default'])) { $errors['_qf_default'] = ''; } if ($field == $contactORContributionId) { if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) && $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " {$fieldMessage} " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(1 => $threshold)) . '<br />'; } elseif ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && !(in_array('invoice_id', $importKeys) || in_array('trxn_id', $importKeys) || in_array('contribution_id', $importKeys))) { $errors['_qf_default'] .= ts('Invoice ID or Transaction ID or Contribution ID are required to match to the existing contribution records in Update mode.') . '<br />'; } } else { $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)) . '<br />'; } } } //at least one field should be mapped during update. if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { $atleastOne = FALSE; foreach ($self->_mapperFields as $key => $field) { if (in_array($key, $importKeys) && !in_array($key, array('doNotImport', 'contribution_id', 'invoice_id', 'trxn_id'))) { $atleastOne = TRUE; break; } } if (!$atleastOne) { $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />'; } } } if (!empty($fields['saveMapping'])) { $nameField = CRM_Utils_Array::value('saveMappingName', $fields); if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Contribution', 'name'); if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { $errors['saveMappingName'] = ts('Duplicate Import Contribution Mapping Name'); } } } if (!empty($errors)) { if (!empty($errors['saveMappingName'])) { $_flag = 1; $assignError = new CRM_Core_Page(); $assignError->assign('mappingDetailsError', $_flag); } if (!empty($errors['_qf_default'])) { CRM_Core_Session::setStatus($errors['_qf_default'], ts("Error"), "error"); return $errors; } } return TRUE; }
/** * Check if the profiles collect enough information to dedupe. * * @param $profileIds * @param int $rgId * @return bool */ public static function canProfilesDedupe($profileIds, $rgId = 0) { // find the unsupervised rule $rgParams = array('used' => 'Unsupervised', 'contact_type' => 'Individual'); if ($rgId > 0) { $rgParams['id'] = $rgId; } $activeRg = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($rgParams); // get the combinations that could be a match for the rule $okCombos = $combos = array(); CRM_Dedupe_BAO_RuleGroup::combos($activeRg[0], $activeRg[1], $combos); // create an index of what combinations involve each field $index = array(); foreach ($combos as $comboid => $combo) { foreach ($combo as $cfield) { $index[$cfield][$comboid] = TRUE; } $combos[$comboid] = array_fill_keys($combo, 0); $okCombos[$comboid] = array_fill_keys($combo, 2); } // get profiles and see if they have the necessary combos $profileReqFields = array(); foreach ($profileIds as $profileId) { if ($profileId && is_numeric($profileId)) { $fields = CRM_Core_BAO_UFGroup::getFields($profileId); // walk through the fields in the profile foreach ($fields as $field) { // check each of the fields in the index against the profile field foreach ($index as $ifield => $icombos) { if (strpos($field['name'], $ifield) !== FALSE) { // we found the field in the profile, now record it in the index foreach ($icombos as $icombo => $dontcare) { $combos[$icombo][$ifield] = $combos[$icombo][$ifield] != 2 && !$field['is_required'] ? 1 : 2; if ($combos[$icombo] == $okCombos[$icombo]) { // if any combo is complete with 2s (all fields are present and required), we can go home return 2; } } } } } } } // check the combos to see if everything is > 0 foreach ($combos as $comboid => $combo) { $complete = FALSE; foreach ($combo as $cfield) { if ($cfield > 0) { $complete = TRUE; } else { // this combo isn't complete--skip to the next combo continue 2; } } if ($complete) { return 1; } } // no combo succeeded return 0; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @param $files * @param $self * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule($fields, $files, $self) { $errors = array(); $fieldMessage = NULL; if (!array_key_exists('savedMapping', $fields)) { $importKeys = array(); foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } $contactTypeId = $self->get('contactType'); $contactTypes = array(CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization'); $params = array('used' => 'Unsupervised', 'contact_type' => $contactTypes[$contactTypeId]); list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { $weightSum += $ruleFields[$val]; } } foreach ($ruleFields as $field => $weight) { $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; } } if (CRM_Utils_Array::value('saveMapping', $fields)) { $nameField = CRM_Utils_Array::value('saveMappingName', $fields); if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $this->_mappingType, 'name'); if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { $errors['saveMappingName'] = ts('Duplicate ' . $this->_mappingType . 'Mapping Name'); } } } //display Error if loaded mapping is not selected if (array_key_exists('loadMapping', $fields)) { $getMapName = CRM_Utils_Array::value('savedMapping', $fields); if (empty($getMapName)) { $errors['savedMapping'] = ts('Select saved mapping'); } } if (!empty($errors)) { if (!empty($errors['saveMappingName'])) { $_flag = 1; $assignError = new CRM_Core_Page(); $assignError->assign('mappingDetailsError', $_flag); } return $errors; } return TRUE; }