Fix fieldSeparator field missing / notice regression
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 26 Jan 2023 00:41:03 +0000 (13:41 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 26 Jan 2023 00:41:03 +0000 (13:41 +1300)
CRM/Contact/Import/Form/DataSource.php
CRM/Import/DataSource/CSV.php
CRM/Import/Form/DataSource.php
CRM/Import/Form/DataSourceConfig.php
templates/CRM/Contact/Import/Form/CSV.tpl
templates/CRM/Import/Form/DataSource.tpl

index 3a0d52336e110abae3fd41e1e70b2564dc08b7ad..3c65435a907a2c3d1841adeb10d768e0b07b78a9 100644 (file)
@@ -93,8 +93,6 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Form_DataSource {
       $this->addElement('checkbox', 'doGeocodeAddress', ts('Geocode addresses during import?'));
     }
 
-    $this->addElement('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2]);
-
     if (Civi::settings()->get('address_standardization_provider') === 'USPS') {
       $this->addElement('checkbox', 'disableUSPS', ts('Disable USPS address validation during import?'));
     }
index 17a57912dd29985317ca1008bef68f2f1b335c10..bc87dcbba4ff05231f5a34cfe941954d1beecf2b 100644 (file)
@@ -41,7 +41,7 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
    * It should add all fields necessary to get the data
    * uploaded to the temporary table in the DB.
    *
-   * @param CRM_Core_Form $form
+   * @param CRM_Contact_Import_Form_DataSource|\CRM_Import_Form_DataSourceConfig $form
    *
    * @throws \CRM_Core_Exception
    */
@@ -56,6 +56,7 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
     $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
     $form->assign('uploadSize', $uploadSize);
     $form->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
+    $form->addElement('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2]);
     $form->setMaxFileSize($uploadFileSize);
     $form->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
       1 => $uploadSize,
@@ -63,7 +64,7 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
     ]), 'maxfilesize', $uploadFileSize);
     $form->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
     $form->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-
+    $form->setDataSourceDefaults($this->getDefaultValues());
     $form->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
   }
 
@@ -260,4 +261,17 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
     return preg_replace("/^(\u{a0})+|(\u{a0})+$/", '', $string);
   }
 
+  /**
+   * Get default values for csv dataSource fields.
+   *
+   * @return array
+   */
+  public function getDefaultValues(): array {
+    return [
+      'fieldSeparator' => CRM_Core_Config::singleton()->fieldSeparator,
+      'skipColumnHeader' => 1,
+    ];
+
+  }
+
 }
index 5be9b595fe8d60073f183f10a9a798fa7227e8f0..29008b234a030a9da4be318ead32a938a047e5f9 100644 (file)
@@ -96,11 +96,10 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
   }
 
   public function setDefaultValues() {
-    return [
+    return array_merge($this->dataSourceDefaults, [
       'dataSource' => $this->getDefaultDataSource(),
       'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
-      'fieldSeparator' => CRM_Core_Config::singleton()->fieldSeparator,
-    ];
+    ]);
 
   }
 
@@ -191,4 +190,23 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
     $this->getDataSourceObject()->initialize();
   }
 
+  /**
+   * Default values for datasource fields.
+   *
+   * @var array
+   */
+  protected $dataSourceDefaults = [];
+
+  /**
+   * Set dataSource default values.
+   *
+   * @param array $dataSourceDefaults
+   *
+   * @return self
+   */
+  public function setDataSourceDefaults(array $dataSourceDefaults): self {
+    $this->dataSourceDefaults = $dataSourceDefaults;
+    return $this;
+  }
+
 }
index 697de6c997245279b0c849687c7c60c57a0b76cf..b8bd425ba455d7dedaa34593b5a9404007ba221b 100644 (file)
  */
 class CRM_Import_Form_DataSourceConfig extends CRM_Import_Forms {
 
+  /**
+   * Default values for datasource fields.
+   *
+   * @var array
+   */
+  protected $dataSourceDefaults = [];
+
+  /**
+   * Set dataSource default values.
+   *
+   * @param array $dataSourceDefaults
+   *
+   * @return CRM_Import_Form_DataSourceConfig
+   */
+  public function setDataSourceDefaults(array $dataSourceDefaults): CRM_Import_Form_DataSourceConfig {
+    $this->dataSourceDefaults = $dataSourceDefaults;
+    return $this;
+  }
+
   /**
    * Set variables up before form is built.
    *
@@ -57,6 +76,9 @@ class CRM_Import_Form_DataSourceConfig extends CRM_Import_Forms {
         $defaults[$fieldName] = $this->getSubmittedValue($fieldName);
       }
     }
+    else {
+      $defaults = array_merge($this->dataSourceDefaults, $defaults);
+    }
     return $defaults;
   }
 
index 59c0cdee9a1ee718473ec0c8d8deffab595367aa..18c838740d65e7952fdb34a0270c0174a8555dd3 100644 (file)
@@ -22,5 +22,9 @@
             <div class="description">{ts}Check this box if the first row of your file consists of field names (Example: 'First Name','Last Name','Email'){/ts}</div>
         </td>
     </tr>
+    <tr class="crm-import-datasource-form-block-fieldSeparator">
+      <td class="label">{$form.fieldSeparator.label} {help id='id-fieldSeparator' file='CRM/Contact/Import/Form/DataSource'}</td>
+      <td>{$form.fieldSeparator.html}</td>
+    </tr>
   </table>
 
index 7aef23417d242734d745da28f4f04fba1782ebce..5da2f56a9fe43da1cdac9715f997d8423966480a 100644 (file)
           <td><span>{$form.multipleCustomData.html}</span> </td>
         </tr>
       {/if}
-        <tr class="crm-import-datasource-form-block-fieldSeparator">
-          <td class="label">{$form.fieldSeparator.label} {help id='id-fieldSeparator' file='CRM/Contact/Import/Form/DataSource'}</td>
-          <td>{$form.fieldSeparator.html}</td>
-        </tr>
        <tr class="crm-import-uploadfile-form-block-date">{include file="CRM/Core/Date.tpl"}</tr>
        {if array_key_exists('savedMapping', $form)}
          <tr class="crm-import-uploadfile-form-block-savedMapping">