From 77a9ae2474f9151ca94841ec8f83008bf7551f5f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 4 Jun 2022 11:27:30 +1200 Subject: [PATCH] Extract saveFieldMapping --- CRM/Contribute/Import/Form/MapField.php | 29 +++------------- CRM/Import/Form/MapField.php | 33 +++++++++++++++++++ .../CRM/Contribute/Import/Form/MapField.tpl | 2 +- .../CRM/Contribute/Import/Form/MapTable.tpl | 11 ------- 4 files changed, 38 insertions(+), 37 deletions(-) delete mode 100644 templates/CRM/Contribute/Import/Form/MapTable.tpl diff --git a/CRM/Contribute/Import/Form/MapField.php b/CRM/Contribute/Import/Form/MapField.php index b45767d8b1..7b3c7fdbfb 100644 --- a/CRM/Contribute/Import/Form/MapField.php +++ b/CRM/Contribute/Import/Form/MapField.php @@ -73,8 +73,6 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { */ public function preProcess() { parent::preProcess(); - $this->_mapperFields = $this->getAvailableFields(); - asort($this->_mapperFields); $this->_columnCount = $this->get('columnCount'); $skipColumnHeader = $this->getSubmittedValue('skipColumnHeader'); @@ -181,7 +179,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL); $jsSet = FALSE; if ($this->get('savedMapping')) { - list($mappingName, $mappingContactType) = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID); + [$mappingName, $mappingContactType] = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID); $mappingName = $mappingName[1]; $mappingContactType = $mappingContactType[1]; @@ -326,7 +324,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { 'used' => 'Unsupervised', 'contact_type' => $contactTypes[$contactTypeId] ?? '', ]; - list($ruleFields, $threshold) = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params); + [$ruleFields, $threshold] = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { @@ -445,27 +443,8 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { // store mapping Id to display it in the preview page $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); - //Updating Mapping Records - if (!empty($params['updateMapping'])) { - for ($i = 0; $i < $this->_columnCount; $i++) { - $this->saveMappingField($params['mappingId'], $i, TRUE); - } - } - - //Saving Mapping Details and Records - if (!empty($params['saveMapping'])) { - $mappingParams = [ - 'name' => $params['saveMappingName'], - 'description' => $params['saveMappingDesc'], - 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'), - ]; - $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); - - for ($i = 0; $i < $this->_columnCount; $i++) { - $this->saveMappingField($saveMapping->id, $i, FALSE); - } - $this->set('savedMapping', $saveMapping->id); - } + $mappingType = 'Import Contribution'; + $this->saveMapping($mappingType); $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeysMain); $parser->setUserJobID($this->getUserJobID()); diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index 1f9187c35e..11581f2a46 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -71,6 +71,8 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { */ public function preProcess() { $this->assignMapFieldVariables(); + $this->_mapperFields = $this->getAvailableFields(); + asort($this->_mapperFields); parent::preProcess(); } @@ -241,4 +243,35 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { } } + /** + * Save the Field Mapping. + * + * @param string $mappingType + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + protected function saveMapping(string $mappingType): void { + //Updating Mapping Records + if ($this->getSubmittedValue('updateMapping')) { + foreach (array_keys($this->getColumnHeaders()) as $i) { + $this->saveMappingField($this->getSubmittedValue('mappingId'), $i, TRUE); + } + } + //Saving Mapping Details and Records + if ($this->getSubmittedValue('saveMapping')) { + $mappingParams = [ + 'name' => $this->getSubmittedValue('saveMappingName'), + 'description' => $this->getSubmittedValue('saveMappingDesc'), + 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $mappingType), + ]; + $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); + + foreach (array_keys($this->getColumnHeaders()) as $i) { + $this->saveMappingField($saveMapping->id, $i, FALSE); + } + $this->set('savedMapping', $saveMapping->id); + } + } + } diff --git a/templates/CRM/Contribute/Import/Form/MapField.tpl b/templates/CRM/Contribute/Import/Form/MapField.tpl index 6ab1ca0585..defcc4c38e 100644 --- a/templates/CRM/Contribute/Import/Form/MapField.tpl +++ b/templates/CRM/Contribute/Import/Form/MapField.tpl @@ -19,7 +19,7 @@
{include file="CRM/common/formButtons.tpl" location="top"}
{* Table for mapping data to CRM fields *} - {include file="CRM/Contribute/Import/Form/MapTable.tpl"} +{include file="CRM/Import/Form/MapTableCommon.tpl" mapper=$form.mapper}
{include file="CRM/common/formButtons.tpl" location="bottom"}
{$initHideBoxes|smarty:nodefaults} diff --git a/templates/CRM/Contribute/Import/Form/MapTable.tpl b/templates/CRM/Contribute/Import/Form/MapTable.tpl deleted file mode 100644 index 3b9bab3228..0000000000 --- a/templates/CRM/Contribute/Import/Form/MapTable.tpl +++ /dev/null @@ -1,11 +0,0 @@ -{* - +--------------------------------------------------------------------+ - | Copyright CiviCRM LLC. All rights reserved. | - | | - | This work is published under the GNU AGPLv3 license with some | - | permitted exceptions and without any warranty. For full license | - | and copyright information, see https://civicrm.org/licensing | - +--------------------------------------------------------------------+ -*} -{* Contribution Import Wizard - Data Mapping table used by MapFields.tpl and Preview.tpl *} -{include file="CRM/Import/Form/MapTableCommon.tpl" mapper=$form.mapper} -- 2.25.1