[REF] [Import] Remove last instance of passing fieldTypes through get->set
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 21 Apr 2022 22:11:41 +0000 (10:11 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 12 May 2022 20:21:33 +0000 (08:21 +1200)
CRM/Contact/Import/Form/DataSource.php
CRM/Contact/Import/Form/MapField.php
CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php
tests/phpunit/CRM/Contact/Import/Form/data/.~lock.individual_valid_with_related_no_email.csv# [deleted file]

index c03b1cfb8627c607bae063f75928771dfe46166a..9ebefb05d4daa48406fee8d858e20c04ef37e3f1 100644 (file)
@@ -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);
-
   }
 
   /**
index fb5e8c0126500041eaf90f73e654580530fc0785..7cbc6ff1ac65a1fa7727eff5be0a8d7845771fd0 100644 (file)
@@ -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);
+  }
+
 }
index 9412f4387df83be2d46b15005cb964fe76cba563..382358e167fb854b041caf97c0280f6a1f747548 100644 (file)
@@ -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());
   }
 
   /**
index 739cd6552e74aef5d8b49976b8d1d3dfe68e637c..d2860ce585a8648313191810dd580167af45c52e 100644 (file)
@@ -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 (file)
index 8d0a211..0000000
+++ /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