Merge pull request #23701 from eileenmcnaughton/unused
[civicrm-core.git] / CRM / Import / Form / DataSource.php
index bf8c60984d054f90a60d65511cf5bce93e71fd12..306fe2d755236bf756056be91e08ce8a535f4e98 100644 (file)
@@ -63,7 +63,10 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
    */
   public function buildQuickForm() {
     $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".
@@ -155,8 +158,8 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
    *   Entity to set for paraser currently only for custom import
    */
   protected function submitFileForMapping($parserClassName, $entity = NULL) {
-    $this->controller->resetPage('MapField');
     CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
+    $this->processDatasource();
 
     $mapper = [];
 
@@ -165,6 +168,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
       $parser->setEntity($this->get($entity));
     }
     $parser->setMaxLinesToProcess(100);
+    $parser->setUserJobID($this->getUserJobID());
     $parser->run(
       $this->getSubmittedValue('uploadFile'),
       $this->getSubmittedValue('fieldSeparator'),
@@ -176,6 +180,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
 
     // add all the necessary variables to the form
     $parser->set($this);
+    $this->controller->resetPage('MapField');
   }
 
   /**
@@ -187,4 +192,33 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms {
     return ts('Upload Data');
   }
 
+  /**
+   * Process the datasource submission - setting up the job and data source.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  protected function processDatasource(): void {
+    if (!$this->getUserJobID()) {
+      $this->createUserJob();
+    }
+    else {
+      $this->flushDataSource();
+      $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
+    }
+    $this->instantiateDataSource();
+  }
+
+  /**
+   * Instantiate the datasource.
+   *
+   * This gives the datasource a chance to do any table creation etc.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  private function instantiateDataSource(): void {
+    $this->getDataSourceObject()->initialize();
+  }
+
 }