Add datasource selector to generic import (so it can be used eg. with contribution...
authorMatthew Wire <mjw@mjwconsult.co.uk>
Tue, 13 Dec 2022 11:48:41 +0000 (11:48 +0000)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Tue, 13 Dec 2022 11:54:19 +0000 (11:54 +0000)
CRM/Contact/Import/Form/DataSource.php
CRM/Contribute/Import/Form/DataSource.php
CRM/Custom/Import/Form/DataSource.php
CRM/Event/Import/Form/DataSource.php
CRM/Import/Form/DataSource.php
CRM/Member/Import/Form/DataSource.php
templates/CRM/Import/Form/DataSource.tpl

index ee4b414e7edef59cfc21d595030d207a801046be..3a0d52336e110abae3fd41e1e70b2564dc08b7ad 100644 (file)
@@ -121,13 +121,9 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Form_DataSource {
    *   reference to the array of default values
    */
   public function setDefaultValues() {
-    $defaults = [
-      'dataSource' => $this->getDefaultDataSource(),
-      'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
-      'contactType' => 'Individual',
-      'fieldSeparator' => CRM_Core_Config::singleton()->fieldSeparator,
-      'disableUSPS' => TRUE,
-    ];
+    $defaults = parent::setDefaultValues();
+    $defaults['contactType'] = 'Individual';
+    $defaults['disableUSPS'] = TRUE;
 
     if ($this->get('loadedMapping')) {
       $defaults['savedMapping'] = $this->get('loadedMapping');
index 6de2b35a8d001034a2c8e7c657eafc1756d0c852..c161cc6529150dc8bb64d04342b9c48287024901 100644 (file)
@@ -44,8 +44,6 @@ class CRM_Contribute_Import_Form_DataSource extends CRM_Import_Form_DataSource {
       CRM_Import_Parser::DUPLICATE_UPDATE => ts('Update existing contributions'),
     ]);
 
-    $this->setDefaults(['onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP]);
-
     $this->addElement('xbutton', 'loadMapping', ts('Load Mapping'), [
       'type' => 'submit',
       'onclick' => 'checkSelect()',
index 326a759a1c6675b378581898f8ee2fe9af05ae4c..4f780de882a6ad4dee245e6c85eca30224c32887 100644 (file)
@@ -99,13 +99,10 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource {
    * @throws \CRM_Core_Exception
    */
   public function setDefaultValues(): array {
-    $config = CRM_Core_Config::singleton();
-    $defaults = [
-      'contactType' => 'Individual',
-      'fieldSeparator' => $config->fieldSeparator,
-      // Perhaps never used, but permits url passing of the group.
-      'multipleCustomData' => CRM_Utils_Request::retrieve('id', 'Positive', $this),
-    ];
+    parent::setDefaultValues();
+    $defaults['contactType'] = 'Individual';
+    // Perhaps never used, but permits url passing of the group.
+    $defaults['multipleCustomData'] = CRM_Utils_Request::retrieve('id', 'Positive', $this);
 
     $loadedMapping = $this->get('loadedMapping');
     if ($loadedMapping) {
index 70d9fb68fd8ceaddc363d91b7a31c41e9397169c..4817f0540083a130123bb407d70d285f1260c34f 100644 (file)
@@ -46,7 +46,6 @@ class CRM_Event_Import_Form_DataSource extends CRM_Import_Form_DataSource {
       CRM_Import_Parser::DUPLICATE_UPDATE => ts('Update'),
       CRM_Import_Parser::DUPLICATE_NOCHECK => ts('No Duplicate Checking'),
     ]);
-    $this->setDefaults(['onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP]);
 
     $this->addContactTypeSelector();
   }
index 63fce727a474f596d8a5039ede9ea5b5b3a1d8ae..36d6bb4bd8d9bca81effc47c696f62f29926657a 100644 (file)
@@ -59,33 +59,14 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
   public function buildQuickForm() {
     $this->assign('errorMessage', $this->getErrorMessage());
     $config = CRM_Core_Config::singleton();
-    // When we switch to using the DataSource.tpl used by Contact we can remove this in
-    // favour of the one used by Contact - I was trying to consolidate
-    // first & got stuck on https://github.com/civicrm/civicrm-core/pull/23458
-    $this->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV');
-    $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
-
-    //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
-    if (empty($uploadFileSize)) {
-      $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
-    }
-    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
-
-    $this->assign('uploadSize', $uploadSize);
 
-    $this->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE);
-    $this->setMaxFileSize($uploadFileSize);
-    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', [
-      1 => $uploadSize,
-      2 => $uploadFileSize,
-    ]), 'maxfilesize', $uploadFileSize);
-    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
+    $this->assign('urlPath', 'civicrm/import/datasource');
+    $this->assign('urlPathVar', 'snippet=4&user_job_id=' . $this->get('user_job_id'));
 
-    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
+    $this->add('select', 'dataSource', ts('Data Source'), $this->getDataSources(), TRUE,
+      ['onchange' => 'buildDataSourceFormBlock(this.value);']
+    );
 
-    $this->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE);
-    $this->setDefaults(['fieldSeparator' => $config->fieldSeparator]);
     $mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
     $this->add('select', 'savedMapping', ts('Saved Field Mapping'), ['' => ts('- select -')] + $mappingArray);
 
@@ -96,6 +77,8 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
     //build date formats
     CRM_Core_Form_Date::buildAllowedDateFormats($this);
 
+    $this->buildDataSourceFields();
+
     $this->addButtons([
         [
           'type' => 'upload',
@@ -110,6 +93,15 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
     ]);
   }
 
+  public function setDefaultValues() {
+    return [
+      'dataSource' => $this->getDefaultDataSource(),
+      'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
+      'fieldSeparator' => CRM_Core_Config::singleton()->fieldSeparator,
+    ];
+
+  }
+
   /**
    * Get an error message to assign to the template.
    *
index 8eaabae0475ee5a299f73702fcac02483c520bd0..d8a7da6d5aa1fb434583556ddeb26a4c251bcd88 100644 (file)
@@ -45,9 +45,6 @@ class CRM_Member_Import_Form_DataSource extends CRM_Import_Form_DataSource {
       CRM_Import_Parser::DUPLICATE_SKIP => ts('Insert new Membership'),
       CRM_Import_Parser::DUPLICATE_UPDATE => ts('Update existing Membership'),
     ]);
-    $this->setDefaults([
-      'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
-    ]);
 
     $this->addContactTypeSelector();
   }
index 23fdcb965ab33ded53e86db15fd88806997304af..7aef23417d242734d745da28f4f04fba1782ebce 100644 (file)
   </div>
 
   <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
-    <table class="form-layout-compressed">
-      <tr class="crm-import-uploadfile-from-block-uploadFile">
-        <td class="label">{$form.uploadFile.label}</td>
-        <td class="html-adjust"> {$form.uploadFile.html}<br />
-          <span class="description">{ts}File format must be comma-separated-values (CSV).{/ts}</span>
-          <br /><span>{ts 1=$uploadSize}Maximum Upload File Size: %1 MB{/ts}</span>
-        </td>
+  <div id="choose-data-source" class="form-item">
+    <h3>{ts}Choose Data Source{/ts}</h3>
+    <table class="form-layout">
+      <tr class="crm-import-datasource-form-block-dataSource">
+        <td class="label">{$form.dataSource.label}</td>
+        <td>{$form.dataSource.html} {help id='data-source-selection'}</td>
       </tr>
-       <tr class="crm-import-uploadfile-from-block-skipColumnHeader">
-         <td class="label"></td><td>{$form.skipColumnHeader.html}{$form.skipColumnHeader.label}<br />
-           <span class="description">
-             {ts}Check this box if the first row of your file consists of field names (Example: 'Contact ID', 'Amount').{/ts}
-           </span>
-         </td>
-       </tr>
+    </table>
+  </div>
+
+    {* Data source form pane is injected here when the data source is selected. *}
+  <div id="data-source-form-block">
+  </div>
+    <table class="form-layout-compressed">
       {if array_key_exists('contactType', $form)}
         <tr class="crm-import-uploadfile-from-block-contactType">
           <td class="label">{$form.contactType.label}</td>
     <div class="spacer"></div>
 
   <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
+{literal}
+  <script type="text/javascript">
+    CRM.$(function($) {
+      // build data source form block
+      buildDataSourceFormBlock();
+    });
+
+    function buildDataSourceFormBlock(dataSource) {
+      var dataUrl = {/literal}"{crmURL p=$urlPath h=0 q=$urlPathVar|smarty:nodefaults}"{literal};
+
+      if (!dataSource) {
+        var dataSource = CRM.$("#dataSource").val();
+      }
+
+      if (dataSource) {
+        dataUrl = dataUrl + '&dataSource=' + dataSource;
+      } else {
+        CRM.$("#data-source-form-block").html('');
+        return;
+      }
+
+      CRM.$("#data-source-form-block").load(dataUrl);
+    }
+  </script>
+{/literal}
 </div>