Centralise skipColumnHeader to one function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 8 Apr 2022 00:40:07 +0000 (12:40 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 11 Apr 2022 20:14:15 +0000 (08:14 +1200)
CRM/Activity/Import/Form/Preview.php
CRM/Contact/Import/Form/Preview.php
CRM/Contribute/Import/Form/Preview.php
CRM/Custom/Import/Form/Preview.php
CRM/Event/Import/Form/Preview.php
CRM/Import/Form/Preview.php
CRM/Import/Forms.php [new file with mode: 0644]
CRM/Member/Import/Form/Preview.php

index b12ffef3660149169966f08f52293cbc576c1d4a..0526eb43386ad4fb5ee961670d2ec7f26e974e5f 100644 (file)
@@ -24,8 +24,7 @@ class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview {
    * Set variables up before form is built.
    */
   public function preProcess() {
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
-
+    parent::preProcess();
     // Get the data from the session.
     $dataValues = $this->get('dataValues');
     $mapper = $this->get('mapper');
@@ -42,14 +41,6 @@ class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
 
-    if ($skipColumnHeader) {
-      $this->assign('skipColumnHeader', $skipColumnHeader);
-      $this->assign('rowDisplayCount', 3);
-    }
-    else {
-      $this->assign('rowDisplayCount', 2);
-    }
-
     if ($invalidRowCount) {
       $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
       $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
@@ -92,7 +83,6 @@ class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview {
   public function postProcess() {
     $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
     $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
     $invalidRowCount = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate = $this->get('onDuplicate');
@@ -117,7 +107,7 @@ class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $parser->run($fileName, $separator,
       $mapperFields,
-      $skipColumnHeader,
+      $this->getSubmittedValue('skipColumnHeader'),
       CRM_Import_Parser::MODE_IMPORT,
       $onDuplicate,
       $this->get('statusID'),
index 2e8028a01ecb943c19af54acd481c6353449ab02..e8d082b64169f40b620afe92851123f267aa2455 100644 (file)
@@ -108,7 +108,7 @@ class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview {
 
     $showColNames = TRUE;
     if ('CRM_Import_DataSource_CSV' == $this->get('dataSource') &&
-      !$this->get('skipColumnHeader')
+      !$this->getSubmittedValue('skipColumnHeader')
     ) {
       $showColNames = FALSE;
     }
index a1849368a6fad13fde673609c6993a613c8995a1..d71fccf323a6091692aff9f228c115ff13ebcf9f 100644 (file)
@@ -24,8 +24,7 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
    * Set variables up before form is built.
    */
   public function preProcess() {
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
-
+    parent::preProcess();
     //get the data from the session
     $dataValues = $this->get('dataValues');
     $mapper = $this->get('mapper');
@@ -44,14 +43,6 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
 
-    if ($skipColumnHeader) {
-      $this->assign('skipColumnHeader', $skipColumnHeader);
-      $this->assign('rowDisplayCount', 3);
-    }
-    else {
-      $this->assign('rowDisplayCount', 2);
-    }
-
     if ($invalidRowCount) {
       $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
       $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
@@ -94,7 +85,6 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
   public function postProcess() {
     $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
     $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
     $invalidRowCount = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate = $this->get('onDuplicate');
@@ -129,7 +119,7 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $parser->run($fileName, $separator,
       $mapperFields,
-      $skipColumnHeader,
+      $this->getSubmittedValue('skipColumnHeader'),
       CRM_Import_Parser::MODE_IMPORT,
       $this->get('contactType'),
       $onDuplicate,
index 0abfd721953c38e1b06faf0216f338b2c77967d6..cc77ef8a703abdf32f77153b3812e1a7c4649bbf 100644 (file)
@@ -13,8 +13,7 @@ class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
    * @return void
    */
   public function preProcess() {
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
-
+    parent::preProcess();
     //get the data from the session
     $dataValues = $this->get('dataValues');
     $mapper = $this->get('mapper');
@@ -32,14 +31,6 @@ class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
 
-    if ($skipColumnHeader) {
-      $this->assign('skipColumnHeader', $skipColumnHeader);
-      $this->assign('rowDisplayCount', 3);
-    }
-    else {
-      $this->assign('rowDisplayCount', 2);
-    }
-
     if ($invalidRowCount) {
       $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
       $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
@@ -82,7 +73,6 @@ class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
   public function postProcess() {
     $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
     $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
     $invalidRowCount = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate = $this->get('onDuplicate');
@@ -109,7 +99,7 @@ class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $parser->run($fileName, $separator,
       $mapperFields,
-      $skipColumnHeader,
+      $this->getSubmittedValue('skipColumnHeader'),
       CRM_Import_Parser::MODE_IMPORT,
       $this->get('contactType'),
       $onDuplicate
index ccec35738ac2e8284a9132163164c9288b8d2097..e39652bbf21ff14b164c2a637b278629539f2a50 100644 (file)
@@ -27,7 +27,7 @@ class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
    * @return void
    */
   public function preProcess() {
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
+    parent::preProcess();
 
     //get the data from the session
     $dataValues = $this->get('dataValues');
@@ -45,14 +45,6 @@ class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
 
-    if ($skipColumnHeader) {
-      $this->assign('skipColumnHeader', $skipColumnHeader);
-      $this->assign('rowDisplayCount', 3);
-    }
-    else {
-      $this->assign('rowDisplayCount', 2);
-    }
-
     if ($invalidRowCount) {
       $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
       $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
@@ -95,7 +87,6 @@ class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
   public function postProcess() {
     $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
     $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
     $invalidRowCount = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate = $this->get('onDuplicate');
@@ -120,7 +111,7 @@ class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $parser->run($fileName, $separator,
       $mapperFields,
-      $skipColumnHeader,
+      $this->getSubmittedValue('skipColumnHeader'),
       CRM_Import_Parser::MODE_IMPORT,
       $this->get('contactType'),
       $onDuplicate
index 5067a22c4813b91bc298b50c07768ddfbef18955..3ba704f33987488a37eea24cfb7404794bfe1608 100644 (file)
@@ -21,7 +21,7 @@
  * TODO: CRM-11254 - if preProcess and postProcess functions can be reconciled between the 5 child classes,
  * those classes can be removed entirely and this class will not need to be abstract
  */
-abstract class CRM_Import_Form_Preview extends CRM_Core_Form {
+abstract class CRM_Import_Form_Preview extends CRM_Import_Forms {
 
   /**
    * Return a descriptive name for the page, used in wizard header.
@@ -32,6 +32,14 @@ abstract class CRM_Import_Form_Preview extends CRM_Core_Form {
     return ts('Preview');
   }
 
+  /**
+   * Assign common values to the template.
+   */
+  public function preProcess() {
+    $this->assign('skipColumnHeader', $this->getSubmittedValue('skipColumnHeader'));
+    $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2);
+  }
+
   /**
    * Build the form object.
    */
diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php
new file mode 100644 (file)
index 0000000..c1fcd9e
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+/**
+ * This class helps the forms within the import flow access submitted & parsed values.
+ */
+class CRM_Import_Forms extends CRM_Core_Form {
+
+  /**
+   * Get the submitted value, accessing it from whatever form in the flow it is submitted on.
+   * @param string $fieldName
+   *
+   * @return mixed|null
+   */
+  public function getSubmittedValue(string $fieldName) {
+    $mappedValues = [
+      'skipColumnHeader' => 'DataSource',
+    ];
+    if (array_key_exists($fieldName, $mappedValues)) {
+      return $this->controller->exportValue($mappedValues[$fieldName], $fieldName);
+    }
+    return parent::getSubmittedValue($fieldName);
+
+  }
+
+}
index dd84e13468dfba78f6efe40e6eabf7a90762e34f..6502770d7a448579d518a428de3b12e8d36e3a20 100644 (file)
@@ -27,8 +27,7 @@ class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
    * @return void
    */
   public function preProcess() {
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
-
+    parent::preProcess();
     //get the data from the session
     $dataValues = $this->get('dataValues');
     $mapper = $this->get('mapper');
@@ -45,14 +44,6 @@ class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
 
-    if ($skipColumnHeader) {
-      $this->assign('skipColumnHeader', $skipColumnHeader);
-      $this->assign('rowDisplayCount', 3);
-    }
-    else {
-      $this->assign('rowDisplayCount', 2);
-    }
-
     if ($invalidRowCount) {
       $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser_Membership';
       $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
@@ -96,7 +87,6 @@ class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
   public function postProcess() {
     $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
     $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
-    $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
     $invalidRowCount = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate = $this->get('onDuplicate');
@@ -140,7 +130,7 @@ class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
     }
     $parser->run($fileName, $separator,
       $mapperFields,
-      $skipColumnHeader,
+      $this->getSubmittedValue('skipColumnHeader'),
       CRM_Import_Parser::MODE_IMPORT,
       $this->get('contactType'),
       $onDuplicate,