From 79d21b5b9ced5e5dedb4fd9c70c67a0bdc49f228 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 22 Apr 2022 10:11:41 +1200 Subject: [PATCH] [REF] [Import] Remove last instance of passing fieldTypes through get->set --- CRM/Contact/Import/Form/DataSource.php | 3 --- CRM/Contact/Import/Form/MapField.php | 19 ++++++++++++++++--- CRM/Contact/Import/Parser/Contact.php | 2 -- CRM/Import/Parser.php | 8 +++++++- ...ndividual_valid_with_related_no_email.csv# | 1 - 5 files changed, 23 insertions(+), 10 deletions(-) delete mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/.~lock.individual_valid_with_related_no_email.csv# diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index c03b1cfb86..9ebefb05d4 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -222,9 +222,6 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Forms { CRM_Import_Parser::MODE_MAPFIELD ); - // add all the necessary variables to the form - $parser->set($this); - } /** diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index fb5e8c0126..7cbc6ff1ac 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -133,7 +133,6 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $defaults = []; $mapperKeys = array_keys($this->_mapperFields); $hasColumnNames = !empty($this->_columnNames); - $hasLocationTypes = $this->get('fieldTypes'); $this->_location_types = ['Primary' => ts('Primary')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); @@ -206,7 +205,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $values = []; foreach ($relatedFields as $name => $field) { $values[$name] = $field['title']; - if (isset($hasLocationTypes[$name])) { + if ($this->isLocationTypeRequired($name)) { $sel3[$key][$name] = $this->_location_types; } elseif ($name === 'url') { @@ -270,7 +269,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { } else { $options = NULL; - if (!empty($hasLocationTypes[$key])) { + if ($this->isLocationTypeRequired($key)) { $options = $this->_location_types; } elseif ($key === 'url') { @@ -650,4 +649,18 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { return $highlightedFields; } + /** + * Get an array of fields with TRUE or FALSE to reflect need for location type. + * + * e.g ['first_name' => FALSE, 'email' => TRUE, 'street_address' => TRUE'] + * + * @return bool + */ + private function isLocationTypeRequired($name): bool { + if (!isset(Civi::$statics[__CLASS__]['location_fields'])) { + Civi::$statics[__CLASS__]['location_fields'] = (new CRM_Contact_Import_Parser_Contact())->setUserJobID($this->getUserJobID())->getSelectTypes(); + } + return (bool) (Civi::$statics[__CLASS__]['location_fields'][$name] ?? FALSE); + } + } diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 9412f4387d..382358e167 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -2537,8 +2537,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { * @param int $mode */ public function set($store, $mode = self::MODE_SUMMARY) { - // To be removed in https://github.com/civicrm/civicrm-core/pull/23281 - $store->set('fieldTypes', $this->getSelectTypes()); } /** diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 739cd6552e..d2860ce585 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -64,9 +64,12 @@ abstract class CRM_Import_Parser { * Set user job ID. * * @param int $userJobID + * + * @return self */ - public function setUserJobID(int $userJobID): void { + public function setUserJobID(int $userJobID): self { $this->userJobID = $userJobID; + return $this; } /** @@ -431,6 +434,9 @@ abstract class CRM_Import_Parser { */ public function getSelectTypes() { $values = []; + // This is only called from the MapField form in isolation now, + // so we need to set the metadata. + $this->init(); foreach ($this->_fields as $name => $field) { if (isset($field->_hasLocationType)) { $values[$name] = $field->_hasLocationType; diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/.~lock.individual_valid_with_related_no_email.csv# b/tests/phpunit/CRM/Contact/Import/Form/data/.~lock.individual_valid_with_related_no_email.csv# deleted file mode 100644 index 8d0a2114c2..0000000000 --- a/tests/phpunit/CRM/Contact/Import/Form/data/.~lock.individual_valid_with_related_no_email.csv# +++ /dev/null @@ -1 +0,0 @@ -,eileen,eileen-laptop,11.05.2022 11:24,file:///home/eileen/.config/libreoffice/4; \ No newline at end of file -- 2.25.1