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 * Shared preProcess code.
72 public function preProcess() {
73 $this->assignMapFieldVariables();
74 $this->_mapperFields
= $this->getAvailableFields();
75 asort($this->_mapperFields
);
80 * Process the mapped fields and map it into the uploaded file
81 * preview the file and extract some summary statistics
84 * @noinspection PhpDocSignatureInspection
85 * @noinspection PhpUnhandledExceptionInspection
87 public function postProcess() {
88 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
89 $this->saveMapping($this->getMappingTypeName());
90 $parser = $this->getParser();
96 * Attempt to match header labels with our mapper fields.
98 * @param string $header
99 * @param array $patterns
103 public function defaultFromHeader($header, &$patterns) {
104 foreach ($patterns as $key => $re) {
105 // Skip empty key/patterns
106 if (!$key ||
!$re ||
strlen("$re") < 5) {
110 // Scan through the headerPatterns defined in the schema for a match
111 if (preg_match($re, $header)) {
112 $this->_fieldUsed
[$key] = TRUE;
120 * Guess at the field names given the data and patterns from the schema.
122 * @param array $patterns
123 * @param string $index
127 public function defaultFromData($patterns, $index) {
130 $n = count($this->_dataValues
);
132 foreach ($patterns as $key => $re) {
133 // Skip empty key/patterns
134 if (!$key ||
!$re ||
strlen("$re") < 5) {
138 /* Take a vote over the preview data set */
140 for ($i = 0; $i < $n; $i++
) {
141 if (isset($this->_dataValues
[$i][$index])) {
142 if (preg_match($re, $this->_dataValues
[$i][$index])) {
147 if ($hits > $bestHits) {
154 $this->_fieldUsed
[$best] = TRUE;
160 * Add the saved mapping fields to the form.
162 * @param int|null $savedMappingID
164 * @throws \CiviCRM_API3_Exception
166 protected function buildSavedMappingFields($savedMappingID) {
167 //to save the current mappings
168 if (!$savedMappingID) {
169 $saveDetailsName = ts('Save this field mapping');
170 $this->applyFilter('saveMappingName', 'trim');
171 $this->add('text', 'saveMappingName', ts('Name'));
172 $this->add('text', 'saveMappingDesc', ts('Description'));
175 $savedMapping = $this->get('savedMapping');
177 $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']);
178 $this->set('loadedMapping', $savedMapping);
179 $this->add('hidden', 'mappingId', $savedMappingID);
181 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
182 $saveDetailsName = ts('Save as a new field mapping');
183 $this->add('text', 'saveMappingName', ts('Name'));
184 $this->add('text', 'saveMappingDesc', ts('Description'));
186 $this->assign('savedMappingName', $mappingName ??
NULL);
187 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
191 * Validate that sufficient fields have been supplied to match to a contact.
193 * @param string $contactType
194 * @param array $importKeys
197 * Message if insufficient fields are present. Empty string otherwise.
199 protected static function validateRequiredContactMatchFields(string $contactType, array $importKeys): string {
200 [$ruleFields, $threshold] = CRM_Dedupe_BAO_DedupeRuleGroup
::dedupeRuleFieldsWeight([
201 'used' => 'Unsupervised',
202 'contact_type' => $contactType,
205 foreach ($importKeys as $key => $val) {
206 if (array_key_exists($val, $ruleFields)) {
207 $weightSum +
= $ruleFields[$val];
211 foreach ($ruleFields as $field => $weight) {
212 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
214 if ($weightSum < $threshold) {
215 return $fieldMessage . ' ' . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(
223 * Get the field mapped to the savable format.
225 * @param array $fieldMapping
226 * @param int $mappingID
227 * @param int $columnNumber
230 * @throws \CRM_Core_Exception
232 protected function getMappedField(array $fieldMapping, int $mappingID, int $columnNumber): array {
233 return $this->getParser()->getMappingFieldFromMapperInput($fieldMapping, $mappingID, $columnNumber);
237 * Save the mapping field.
239 * @param int $mappingID
240 * @param int $columnNumber
241 * @param bool $isUpdate
243 * @throws \API_Exception
244 * @throws \CRM_Core_Exception
246 protected function saveMappingField(int $mappingID, int $columnNumber, bool $isUpdate = FALSE): void
{
247 $fieldMapping = (array) $this->getSubmittedValue('mapper')[$columnNumber];
248 $mappedField = $this->getMappedField($fieldMapping, $mappingID, $columnNumber);
249 if (empty($mappedField['name'])) {
250 $mappedField['name'] = 'do_not_import';
253 Civi\Api4\MappingField
::update(FALSE)
254 ->setValues($mappedField)
255 ->addWhere('column_number', '=', $columnNumber)
256 ->addWhere('mapping_id', '=', $mappingID)
260 Civi\Api4\MappingField
::create(FALSE)
261 ->setValues($mappedField)->execute();
266 * Save the Field Mapping.
268 * @param string $mappingType
270 * @throws \API_Exception
271 * @throws \CRM_Core_Exception
273 protected function saveMapping(string $mappingType): void
{
274 //Updating Mapping Records
275 if ($this->getSubmittedValue('updateMapping')) {
276 foreach (array_keys($this->getColumnHeaders()) as $i) {
277 $this->saveMappingField($this->getSubmittedValue('mappingId'), $i, TRUE);
280 //Saving Mapping Details and Records
281 if ($this->getSubmittedValue('saveMapping')) {
283 'name' => $this->getSubmittedValue('saveMappingName'),
284 'description' => $this->getSubmittedValue('saveMappingDesc'),
285 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $mappingType),
287 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
289 foreach (array_keys($this->getColumnHeaders()) as $i) {
290 $this->saveMappingField($saveMapping->id
, $i, FALSE);
292 $this->set('savedMapping', $saveMapping->id
);