Contact import - extract assignments to be used by other forms
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Jun 2022 04:33:40 +0000 (16:33 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Jun 2022 04:47:48 +0000 (16:47 +1200)
CRM/Contact/Import/Form/MapField.php
CRM/Contact/Import/Form/Preview.php
CRM/Contact/Import/MetadataTrait.php
CRM/Import/Form/Preview.php
CRM/Import/Forms.php
CRM/Import/Parser.php

index 39e4ca1dce8d7211325d1136e01d5113fb0bad3d..e79fd196b20d715fb32aed5b3da9680275b99d99 100644 (file)
@@ -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'],
index 3bf15b4ea251f89694a74f98e87f10c4e73273ea..8dfe8c6509a30c4e68b58b6ad103369d59c08e73 100644 (file)
@@ -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();
   }
 
index fc8d570484274cd190bb43f7df7e381d39768296..368df0e11a937353f26eafd0f2897fe1b2b58b59 100644 (file)
@@ -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());
   }
 
index 3ba704f33987488a37eea24cfb7404794bfe1608..cce75402e92894a8b428262955b9768ed5f33c1d 100644 (file)
@@ -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);
+  }
+
 }
index 6c61172567b3ea50fd81b406cfa7f9dab2a3b854..1512bfc6f1dfe16aa092b79ae0347c3f0753ab8b 100644 (file)
@@ -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();
+  }
+
 }
index d25e5e1a55b1e6c09a5d2f4c285412d036ba0dfb..7636bd1e93a1ea03f4e951a81441a39a8367090c 100644 (file)
@@ -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;