Merge pull request #23080 from eileenmcnaughton/isset
[civicrm-core.git] / CRM / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class gets the name of the file to upload.
19 *
20 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
21 */
22 abstract class CRM_Import_Form_MapField extends CRM_Import_Forms {
23
24 /**
25 * Cache of preview data values
26 *
27 * @var array
28 */
29 protected $_dataValues;
30
31 /**
32 * Mapper fields
33 *
34 * @var array
35 */
36 protected $_mapperFields;
37
38 /**
39 * Number of columns in import file
40 *
41 * @var int
42 */
43 protected $_columnCount;
44
45 /**
46 * Column headers, if we have them
47 *
48 * @var array
49 */
50 protected $_columnHeaders;
51
52 /**
53 * An array of booleans to keep track of whether a field has been used in
54 * form building already.
55 *
56 * @var array
57 */
58 protected $_fieldUsed;
59
60 /**
61 * Return a descriptive name for the page, used in wizard header.
62 *
63 * @return string
64 */
65 public function getTitle() {
66 return ts('Match Fields');
67 }
68
69 /**
70 * Attempt to match header labels with our mapper fields.
71 *
72 * @param string $header
73 * @param array $patterns
74 *
75 * @return string
76 */
77 public function defaultFromHeader($header, &$patterns) {
78 foreach ($patterns as $key => $re) {
79 // Skip empty key/patterns
80 if (!$key || !$re || strlen("$re") < 5) {
81 continue;
82 }
83
84 // Scan through the headerPatterns defined in the schema for a match
85 if (preg_match($re, $header)) {
86 $this->_fieldUsed[$key] = TRUE;
87 return $key;
88 }
89 }
90 return '';
91 }
92
93 /**
94 * Guess at the field names given the data and patterns from the schema.
95 *
96 * @param array $patterns
97 * @param string $index
98 *
99 * @return string
100 */
101 public function defaultFromData($patterns, $index) {
102 $best = '';
103 $bestHits = 0;
104 $n = count($this->_dataValues);
105
106 foreach ($patterns as $key => $re) {
107 // Skip empty key/patterns
108 if (!$key || !$re || strlen("$re") < 5) {
109 continue;
110 }
111
112 /* Take a vote over the preview data set */
113 $hits = 0;
114 for ($i = 0; $i < $n; $i++) {
115 if (isset($this->_dataValues[$i][$index])) {
116 if (preg_match($re, $this->_dataValues[$i][$index])) {
117 $hits++;
118 }
119 }
120 }
121 if ($hits > $bestHits) {
122 $bestHits = $hits;
123 $best = $key;
124 }
125 }
126
127 if ($best != '') {
128 $this->_fieldUsed[$best] = TRUE;
129 }
130 return $best;
131 }
132
133 /**
134 * Add the saved mapping fields to the form.
135 *
136 * @param int|null $savedMappingID
137 *
138 * @throws \CiviCRM_API3_Exception
139 */
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'));
147 }
148 else {
149 $savedMapping = $this->get('savedMapping');
150
151 $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']);
152 $this->set('loadedMapping', $savedMapping);
153 $this->add('hidden', 'mappingId', $savedMappingID);
154
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'));
159 }
160 $this->assign('savedMappingName', $mappingName ?? NULL);
161 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
162 }
163
164 /**
165 * Validate that sufficient fields have been supplied to match to a contact.
166 *
167 * @param string $contactType
168 * @param array $importKeys
169 *
170 * @return string
171 * Message if insufficient fields are present. Empty string otherwise.
172 */
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,
177 ]);
178 $weightSum = 0;
179 foreach ($importKeys as $key => $val) {
180 if (array_key_exists($val, $ruleFields)) {
181 $weightSum += $ruleFields[$val];
182 }
183 }
184 $fieldMessage = '';
185 foreach ($ruleFields as $field => $weight) {
186 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
187 }
188 if ($weightSum < $threshold) {
189 return $fieldMessage . ' ' . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(
190 1 => $threshold,
191 ));
192 }
193 return '';
194 }
195
196 }