From 4d9f4d690dc561cd058e7f5b2ae4bac3a926159f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 3 Jun 2022 16:33:40 +1200 Subject: [PATCH] Contact import - extract assignments to be used by other forms --- CRM/Contact/Import/Form/MapField.php | 13 ++------ CRM/Contact/Import/Form/Preview.php | 23 +------------ CRM/Contact/Import/MetadataTrait.php | 4 +-- CRM/Import/Form/Preview.php | 30 +++++++++++++++-- CRM/Import/Forms.php | 49 ++++++++++++++++++++++++++++ CRM/Import/Parser.php | 4 +-- 6 files changed, 84 insertions(+), 39 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 39e4ca1dce..e79fd196b2 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -101,17 +101,8 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { // retrieve and highlight required custom fields $formattedFieldNames = $this->formatCustomFieldName($this->_mapperFields); - $this->assign('highlightedFields', $this->getHighlightedFields()); $this->_formattedFieldNames[$contactType] = $this->_mapperFields = array_merge($this->_mapperFields, $formattedFieldNames); - - $columnNames = $this->getColumnHeaders(); - - $this->_columnCount = $this->getNumberOfColumns(); - $this->_columnNames = $columnNames; - $this->assign('columnNames', $this->getColumnHeaders()); - $this->assign('columnCount', $this->_columnCount); - $this->_dataValues = array_values($this->getDataRows([], 2)); - $this->assign('dataValues', $this->_dataValues); + $this->assignMapFieldVariables(); } /** @@ -512,7 +503,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * * @throws \CRM_Core_Exception */ - private function getHighlightedFields(): array { + protected function getHighlightedFields(): array { $entityFields = [ 'Individual' => ['first_name', 'last_name'], 'Organization' => ['organization_name'], diff --git a/CRM/Contact/Import/Form/Preview.php b/CRM/Contact/Import/Form/Preview.php index 3bf15b4ea2..8dfe8c6509 100644 --- a/CRM/Contact/Import/Form/Preview.php +++ b/CRM/Contact/Import/Form/Preview.php @@ -34,23 +34,9 @@ class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview { * @throws \CRM_Core_Exception */ public function preProcess() { - $columnNames = $this->getColumnHeaders(); + parent::preProcess(); $this->_disableUSPS = $this->getSubmittedValue('disableUSPS'); - //assign column names - $this->assign('columnNames', $columnNames); - - //get the mapping name displayed if the mappingId is set - $mappingId = $this->get('loadMappingId'); - if ($mappingId) { - $mapDAO = new CRM_Core_DAO_Mapping(); - $mapDAO->id = $mappingId; - $mapDAO->find(TRUE); - } - $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL); - - $this->assign('rowDisplayCount', 2); - $groups = CRM_Core_PseudoConstant::nestedGroup(); $this->set('groups', $groups); @@ -59,13 +45,6 @@ class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview { $this->set('tag', $tag); } - $this->assign('downloadErrorRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::ERROR)); - $this->assign('invalidRowCount', $this->getRowCount(CRM_Import_Parser::ERROR)); - $this->assign('validRowCount', $this->getRowCount(CRM_Import_Parser::VALID)); - $this->assign('totalRowCount', $this->getRowCount([])); - $this->assign('mapper', $this->getMappedFieldLabels()); - $this->assign('dataValues', $this->getDataRows([], 2)); - $this->setStatusUrl(); } diff --git a/CRM/Contact/Import/MetadataTrait.php b/CRM/Contact/Import/MetadataTrait.php index fc8d570484..368df0e11a 100644 --- a/CRM/Contact/Import/MetadataTrait.php +++ b/CRM/Contact/Import/MetadataTrait.php @@ -81,7 +81,7 @@ trait CRM_Contact_Import_MetadataTrait { * * @return array */ - public function getHeaderPatterns() { + public function getHeaderPatterns(): array { return CRM_Utils_Array::collect('headerPattern', $this->getContactImportMetadata()); } @@ -90,7 +90,7 @@ trait CRM_Contact_Import_MetadataTrait { * * @return array */ - public function getDataPatterns() { + public function getDataPatterns(): array { return CRM_Utils_Array::collect('dataPattern', $this->getContactImportMetadata()); } diff --git a/CRM/Import/Form/Preview.php b/CRM/Import/Form/Preview.php index 3ba704f339..cce75402e9 100644 --- a/CRM/Import/Form/Preview.php +++ b/CRM/Import/Form/Preview.php @@ -36,8 +36,7 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms { * Assign common values to the template. */ public function preProcess() { - $this->assign('skipColumnHeader', $this->getSubmittedValue('skipColumnHeader')); - $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2); + $this->assignPreviewVariables(); } /** @@ -91,4 +90,31 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms { $this->assign('statusUrl', $statusUrl); } + /** + * Assign smarty variables for the preview screen. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + protected function assignPreviewVariables(): void { + $this->assign('downloadErrorRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::ERROR)); + $this->assign('invalidRowCount', $this->getRowCount(CRM_Import_Parser::ERROR)); + $this->assign('validRowCount', $this->getRowCount(CRM_Import_Parser::VALID)); + $this->assign('totalRowCount', $this->getRowCount([])); + $this->assign('mapper', $this->getMappedFieldLabels()); + $this->assign('dataValues', $this->getDataRows([], 2)); + $this->assign('columnNames', $this->getColumnHeaders()); + //get the mapping name displayed if the mappingId is set + $mappingId = $this->get('loadMappingId'); + if ($mappingId) { + $mapDAO = new CRM_Core_DAO_Mapping(); + $mapDAO->id = $mappingId; + $mapDAO->find(TRUE); + } + $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL); + $this->assign('skipColumnHeader', $this->getSubmittedValue('skipColumnHeader')); + // rowDisplayCount is deprecated - it used to be used with {section} but we have nearly gotten rid of it. + $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2); + } + } diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php index 6c61172567..1512bfc6f1 100644 --- a/CRM/Import/Forms.php +++ b/CRM/Import/Forms.php @@ -586,4 +586,53 @@ class CRM_Import_Forms extends CRM_Core_Form { return $mapper; } + /** + * Assign variables required for the MapField form. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + protected function assignMapFieldVariables(): void { + $this->addExpectedSmartyVariable('highlightedRelFields'); + $this->_columnCount = $this->getNumberOfColumns(); + $this->_columnNames = $this->getColumnHeaders(); + $this->_dataValues = array_values($this->getDataRows([], 2)); + $this->assign('columnNames', $this->getColumnHeaders()); + $this->assign('highlightedFields', $this->getHighlightedFields()); + $this->assign('columnCount', $this->_columnCount ); + $this->assign('dataValues', $this->_dataValues); + } + + /** + * Get the fields to be highlighted in the UI. + * + * The highlighted fields are those used to match + * to an existing entity. + * + * @return array + * + * @throws \CRM_Core_Exception + */ + protected function getHighlightedFields(): array { + return []; + } + + /** + * Get the data patterns to pattern match the incoming data. + * + * @return array + */ + public function getDataPatterns(): array { + return $this->getParser()->getDataPatterns(); + } + + /** + * Get the data patterns to pattern match the incoming data. + * + * @return array + */ + public function getHeaderPatterns(): array { + return $this->getParser()->getHeaderPatterns(); + } + } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index d25e5e1a55..7636bd1e93 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -510,7 +510,7 @@ abstract class CRM_Import_Parser { /** * @return array */ - public function getHeaderPatterns() { + public function getHeaderPatterns(): array { $values = []; foreach ($this->_fields as $name => $field) { if (isset($field->_headerPattern)) { @@ -523,7 +523,7 @@ abstract class CRM_Import_Parser { /** * @return array */ - public function getDataPatterns() { + public function getDataPatterns():array { $values = []; foreach ($this->_fields as $name => $field) { $values[$name] = $field->_dataPattern; -- 2.25.1