buildSavedMappingFields($this->getSubmittedValue('savedMapping')); $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this); //-------- end of saved mapping stuff --------- $defaults = []; $columnHeaders = $this->getColumnHeaders(); $hasHeaders = $this->getSubmittedValue('skipColumnHeader'); $headerPatterns = $this->getHeaderPatterns(); $dataPatterns = $this->getDataPatterns(); // For most fields using the html label is a good thing // but for contact ID we really want to specify ID. $this->_mapperFields['membership_contact_id'] = ts('Contact ID'); $sel1 = $this->_mapperFields; if (!$this->getSubmittedValue('onDuplicate')) { // If not updating then do not allow membership id. unset($sel1['membership_id']); } $sel2[''] = NULL; $js = "\n"; $this->assign('initHideBoxes', $js); //set warning if mismatch in more than if (isset($mappingName)) { if (($this->_columnCount != count($mappingName))) { $warning++; } } if ($warning != 0 && $this->get('savedMapping')) { $session = CRM_Core_Session::singleton(); $session::setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.')); } else { $session = CRM_Core_Session::singleton(); $session::setStatus(NULL); } $this->setDefaults($defaults); $this->addButtons(array( array( 'type' => 'back', 'name' => ts('Previous'), ), array( 'type' => 'next', 'name' => ts('Continue'), 'spacing' => '          ', 'isDefault' => TRUE, ), array( 'type' => 'cancel', 'name' => ts('Cancel'), ), )); } /** * Global validation rules for the form. * * @param array $fields * Posted values of the form. * * @param $files * @param self $self * * @return array|bool * list of errors to be posted back to the form */ public static function formRule($fields, $files, $self) { $errors = []; $importKeys = []; 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'), ); $params = array( 'used' => 'Unsupervised', 'contact_type' => $self->getContactType(), ); [$ruleFields, $threshold] = CRM_Dedupe_BAO_DedupeRuleGroup::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->getSubmittedValue('onDuplicate') != CRM_Import_Parser::DUPLICATE_UPDATE ) || in_array('membership_id', $importKeys) ) { continue; } 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.)') . '
'; } else { if (!isset($errors['_qf_default'])) { $errors['_qf_default'] = ''; } $errors['_qf_default'] .= ts('Missing required field: %1', array( 1 => $title, )) . '
'; } } } if (!empty($fields['saveMapping'])) { $nameField = $fields['saveMappingName'] ?? NULL; if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) { $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; } /** * Get the mapping name per the civicrm_mapping_field.type_id option group. * * @return string */ public function getMappingTypeName(): string { return 'Import Membership'; } /** * @return \CRM_Member_Import_Parser_Membership */ protected function getParser(): CRM_Member_Import_Parser_Membership { if (!$this->parser) { $this->parser = new CRM_Member_Import_Parser_Membership(); $this->parser->setUserJobID($this->getUserJobID()); $this->parser->init(); } return $this->parser; } /** * Get the fields to be highlighted in the UI. * * @return array * @throws \CRM_Core_Exception */ protected function getHighlightedFields(): array { $highlightedFields = []; //CRM-2219 removing other required fields since for update only //membership id is required. if ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_UPDATE) { $remove = [ 'membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier', ]; foreach ($remove as $value) { unset($this->_mapperFields[$value]); } $highlightedFieldsArray = [ 'membership_id', 'membership_start_date', 'membership_type_id', ]; foreach ($highlightedFieldsArray as $name) { $highlightedFields[] = $name; } } elseif ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser::DUPLICATE_SKIP) { unset($this->_mapperFields['membership_id']); $highlightedFieldsArray = [ 'membership_contact_id', 'email', 'external_identifier', 'membership_start_date', 'membership_type_id', ]; foreach ($highlightedFieldsArray as $name) { $highlightedFields[] = $name; } } return $highlightedFields; } }