3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * This class gets the name of the file to upload.
20 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
22 abstract class CRM_Import_Form_MapField
extends CRM_Import_Forms
{
25 * Cache of preview data values
29 protected $_dataValues;
36 protected $_mapperFields;
39 * Number of columns in import file
43 protected $_columnCount;
46 * Column headers, if we have them
50 protected $_columnHeaders;
53 * An array of booleans to keep track of whether a field has been used in
54 * form building already.
58 protected $_fieldUsed;
61 * Return a descriptive name for the page, used in wizard header.
65 public function getTitle() {
66 return ts('Match Fields');
70 * Attempt to match header labels with our mapper fields.
72 * @param string $header
73 * @param array $patterns
77 public function defaultFromHeader($header, &$patterns) {
78 foreach ($patterns as $key => $re) {
79 // Skip empty key/patterns
80 if (!$key ||
!$re ||
strlen("$re") < 5) {
84 // Scan through the headerPatterns defined in the schema for a match
85 if (preg_match($re, $header)) {
86 $this->_fieldUsed
[$key] = TRUE;
94 * Guess at the field names given the data and patterns from the schema.
96 * @param array $patterns
97 * @param string $index
101 public function defaultFromData($patterns, $index) {
104 $n = count($this->_dataValues
);
106 foreach ($patterns as $key => $re) {
107 // Skip empty key/patterns
108 if (!$key ||
!$re ||
strlen("$re") < 5) {
112 /* Take a vote over the preview data set */
114 for ($i = 0; $i < $n; $i++
) {
115 if (isset($this->_dataValues
[$i][$index])) {
116 if (preg_match($re, $this->_dataValues
[$i][$index])) {
121 if ($hits > $bestHits) {
128 $this->_fieldUsed
[$best] = TRUE;
134 * Add the saved mapping fields to the form.
136 * @param int|null $savedMappingID
138 * @throws \CiviCRM_API3_Exception
140 protected function buildSavedMappingFields($savedMappingID) {
141 //to save the current mappings
142 if (!$savedMappingID) {
143 $saveDetailsName = ts('Save this field mapping');
144 $this->applyFilter('saveMappingName', 'trim');
145 $this->add('text', 'saveMappingName', ts('Name'));
146 $this->add('text', 'saveMappingDesc', ts('Description'));
149 $savedMapping = $this->get('savedMapping');
151 $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']);
152 $this->set('loadedMapping', $savedMapping);
153 $this->add('hidden', 'mappingId', $savedMappingID);
155 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
156 $saveDetailsName = ts('Save as a new field mapping');
157 $this->add('text', 'saveMappingName', ts('Name'));
158 $this->add('text', 'saveMappingDesc', ts('Description'));
160 $this->assign('savedMappingName', $mappingName ??
NULL);
161 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
165 * Validate that sufficient fields have been supplied to match to a contact.
167 * @param string $contactType
168 * @param array $importKeys
171 * Message if insufficient fields are present. Empty string otherwise.
173 protected static function validateRequiredContactMatchFields(string $contactType, array $importKeys): string {
174 [$ruleFields, $threshold] = CRM_Dedupe_BAO_DedupeRuleGroup
::dedupeRuleFieldsWeight([
175 'used' => 'Unsupervised',
176 'contact_type' => $contactType,
179 foreach ($importKeys as $key => $val) {
180 if (array_key_exists($val, $ruleFields)) {
181 $weightSum +
= $ruleFields[$val];
185 foreach ($ruleFields as $field => $weight) {
186 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
188 if ($weightSum < $threshold) {
189 return $fieldMessage . ' ' . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(