From 8bf4c85b9ae9bf0323ada60a114e27a9c5559c48 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 26 May 2022 16:21:19 +1200 Subject: [PATCH] Clean up preview screen code --- CRM/Contribute/Import/Form/Preview.php | 26 +++++++++++++----- CRM/Contribute/Import/Parser/Contribution.php | 27 +++++++++++++++++-- .../CRM/Contribute/Import/Form/MapTable.tpl | 4 --- .../Import/Parser/ContributionTest.php | 2 +- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CRM/Contribute/Import/Form/Preview.php b/CRM/Contribute/Import/Form/Preview.php index 6d2927bcf2..46d77feedb 100644 --- a/CRM/Contribute/Import/Form/Preview.php +++ b/CRM/Contribute/Import/Form/Preview.php @@ -27,9 +27,6 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview { parent::preProcess(); //get the data from the session $dataValues = $this->get('dataValues'); - $mapper = $this->get('mapper'); - $softCreditFields = $this->get('softCreditFields'); - $mapperSoftCreditType = $this->get('mapperSoftCreditType'); $invalidRowCount = $this->get('invalidRowCount'); //get the mapping name displayed if the mappingId is set @@ -47,9 +44,6 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview { } $properties = [ - 'mapper', - 'softCreditFields', - 'mapperSoftCreditType', 'dataValues', 'columnCount', 'totalRowCount', @@ -58,12 +52,32 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview { 'downloadErrorRecordsUrl', ]; $this->setStatusUrl(); + $this->assign('mapper', $this->getMappedFieldLabels()); foreach ($properties as $property) { $this->assign($property, $this->get($property)); } } + /** + * Get the mapped fields as an array of labels. + * + * e.g + * ['First Name', 'Employee Of - First Name', 'Home - Street Address'] + * + * @return array + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + protected function getMappedFieldLabels(): array { + $mapper = []; + $parser = $this->getParser(); + foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) { + $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber)); + } + return $mapper; + } + /** * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics. */ diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index 22ade85dd4..e2f831b64c 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -739,8 +739,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { } $params = $this->getMappedRow($values); - $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE]; - + $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE, 'contribution_id' => $params['id'] ?? NULL]; //CRM-10994 if (isset($params['total_amount']) && $params['total_amount'] == 0) { $params['total_amount'] = '0.00'; @@ -1539,4 +1538,28 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { return $emails->first()['contact_id']; } + /** + * @param array $mappedField + * Field detail as would be saved in field_mapping table + * or as returned from getMappingFieldFromMapperInput + * + * @return string + * @throws \API_Exception + */ + public function getMappedFieldLabel(array $mappedField): string { + if (empty($this->importableFieldsMetadata)) { + $this->setFieldMetadata(); + } + $title = []; + $title[] = $this->getFieldMetadata($mappedField['name'])['title']; + if ($mappedField['soft_credit_match_field']) { + $title[] = $this->getFieldMetadata($mappedField['soft_credit_match_field'])['title']; + } + if ($mappedField['soft_credit_type_id']) { + $title[] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $mappedField['soft_credit_type_id']); + } + + return implode(' - ', $title); + } + } diff --git a/templates/CRM/Contribute/Import/Form/MapTable.tpl b/templates/CRM/Contribute/Import/Form/MapTable.tpl index 41c6fb2399..458bf3b4fe 100644 --- a/templates/CRM/Contribute/Import/Form/MapTable.tpl +++ b/templates/CRM/Contribute/Import/Form/MapTable.tpl @@ -44,11 +44,7 @@ {* Display mapper