[REF] [Import] extract function that sets field metadata
authoreileen <emcnaughton@wikimedia.org>
Wed, 7 Aug 2019 05:39:35 +0000 (17:39 +1200)
committereileen <emcnaughton@wikimedia.org>
Wed, 7 Aug 2019 05:39:49 +0000 (17:39 +1200)
My thinking is that we want to migrate over to a new class (similar to export processor) that does a lot a metadata functions
- perhaps it might be importMetadata. A big part of the reason it's all so crazy appears to be that the functions to
wrangle the metadata are on the first class in the sequence (DataSource) and because they are not shared & do not have a shared
parent they are passed around instead via the form set &  get methods. In fact the metadata & array wrangling functions just
need to be available - hmm sounding like a trait - ok next commit....

CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php

index f8b543c6c5d8fd3505d6e1c43bcf73d2f3627570..a93dfc1ae4000f210e5beca1a4bb3c5abd02f15a 100644 (file)
@@ -146,50 +146,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
    * The initializer code, called before processing.
    */
   public function init() {
-    $contactFields = CRM_Contact_BAO_Contact::importableFields($this->_contactType);
-    // exclude the address options disabled in the Address Settings
-    $fields = CRM_Core_BAO_Address::validateAddressOptions($contactFields);
-
-    //CRM-5125
-    //supporting import for contact subtypes
-    $csType = NULL;
-    if (!empty($this->_contactSubType)) {
-      //custom fields for sub type
-      $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->_contactSubType);
-
-      if (!empty($subTypeFields)) {
-        foreach ($subTypeFields as $customSubTypeField => $details) {
-          $fields[$customSubTypeField] = $details;
-        }
-      }
-    }
-
-    //Relationship importables
-    $this->_relationships = $relations
-      = CRM_Contact_BAO_Relationship::getContactRelationshipType(
-        NULL, NULL, NULL, $this->_contactType,
-        FALSE, 'label', TRUE, $this->_contactSubType
-      );
-    asort($relations);
-
-    foreach ($relations as $key => $var) {
-      list($type) = explode('_', $key);
-      $relationshipType[$key]['title'] = $var;
-      $relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
-      $relationshipType[$key]['import'] = TRUE;
-      $relationshipType[$key]['relationship_type_id'] = $type;
-      $relationshipType[$key]['related'] = TRUE;
-    }
-
-    if (!empty($relationshipType)) {
-      $fields = array_merge($fields, [
-        'related' => [
-          'title' => ts('- related contact info -'),
-        ],
-      ], $relationshipType);
-    }
-
-    foreach ($fields as $name => $field) {
+    $this->setFieldMetadata();
+    foreach ($this->getImportableFieldsMetadata() as $name => $field) {
       $this->addField($name, $field['title'], CRM_Utils_Array::value('type', $field), CRM_Utils_Array::value('headerPattern', $field), CRM_Utils_Array::value('dataPattern', $field), CRM_Utils_Array::value('hasLocationType', $field));
     }
 
@@ -2092,4 +2050,53 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
 
   }
 
+  /**
+   * Set field metadata.
+   */
+  protected function setFieldMetadata() {
+    $contactFields = CRM_Contact_BAO_Contact::importableFields($this->_contactType);
+    // exclude the address options disabled in the Address Settings
+    $fields = CRM_Core_BAO_Address::validateAddressOptions($contactFields);
+
+    //CRM-5125
+    //supporting import for contact subtypes
+    $csType = NULL;
+    if (!empty($this->_contactSubType)) {
+      //custom fields for sub type
+      $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->_contactSubType);
+
+      if (!empty($subTypeFields)) {
+        foreach ($subTypeFields as $customSubTypeField => $details) {
+          $fields[$customSubTypeField] = $details;
+        }
+      }
+    }
+
+    //Relationship importables
+    $this->_relationships = $relations
+      = CRM_Contact_BAO_Relationship::getContactRelationshipType(
+      NULL, NULL, NULL, $this->_contactType,
+      FALSE, 'label', TRUE, $this->_contactSubType
+    );
+    asort($relations);
+
+    foreach ($relations as $key => $var) {
+      list($type) = explode('_', $key);
+      $relationshipType[$key]['title'] = $var;
+      $relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
+      $relationshipType[$key]['import'] = TRUE;
+      $relationshipType[$key]['relationship_type_id'] = $type;
+      $relationshipType[$key]['related'] = TRUE;
+    }
+
+    if (!empty($relationshipType)) {
+      $fields = array_merge($fields, [
+        'related' => [
+          'title' => ts('- related contact info -'),
+        ],
+      ], $relationshipType);
+    }
+    $this->setImportableFieldsMetadata($fields);
+  }
+
 }
index 6fab5498b9c4fa4762164f930e0fe0a88704aaee..3c71a3ca33a1d485a6673d65be1d09759a3d10fe 100644 (file)
@@ -137,6 +137,33 @@ abstract class CRM_Import_Parser {
    */
   protected $_fields;
 
+  /**
+   * Metadata for all available fields, keyed by unique name.
+   *
+   * This is intended to supercede $_fields which uses a special sauce format which
+   * importableFieldsMetadata uses the standard getfields type format.
+   *
+   * @var array
+   */
+  protected $importableFieldsMetadata = [];
+
+  /**
+   * Get metadata for all importable fields in std getfields style format.
+   *
+   * @return array
+   */
+  public function getImportableFieldsMetadata(): array {
+    return $this->importableFieldsMetadata;
+  }
+
+  /**
+   * Set metadata for all importable fields in std getfields style format.
+   * @param array $importableFieldsMetadata
+   */
+  public function setImportableFieldsMetadata(array $importableFieldsMetadata) {
+    $this->importableFieldsMetadata = $importableFieldsMetadata;
+  }
+
   /**
    * Array of the fields that are actually part of the import process
    * the position in the array also dictates their position in the import