Merge pull request #22487 from mattwire/repeattransactiontemplatecontribution
[civicrm-core.git] / CRM / Import / DataSource / CSV.php
index a21a4f01f6911e6c7740018147ee71644286dc5f..79e68a0d7ff21af016d3f4153316d18906047766 100644 (file)
@@ -35,14 +35,6 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
     return ['title' => ts('Comma-Separated Values (CSV)')];
   }
 
-  /**
-   * Set variables up before form is built.
-   *
-   * @param CRM_Core_Form $form
-   */
-  public function preProcess(&$form) {
-  }
-
   /**
    * This is function is called by the form object to get the DataSource's form snippet.
    *
@@ -56,9 +48,7 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
   public function buildQuickForm(&$form) {
     $form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV');
 
-    $config = CRM_Core_Config::singleton();
-
-    $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
+    $uploadFileSize = CRM_Utils_Number::formatUnitSize(Civi::settings()->get('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);
@@ -78,30 +68,22 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
   }
 
   /**
-   * Process the form submission.
-   *
-   * @param array $params
-   * @param string $db
-   * @param \CRM_Core_Form $form
+   * Initialize the datasource, based on the submitted values stored in the user job.
    *
    * @throws \API_Exception
    * @throws \CRM_Core_Exception
    */
-  public function postProcess(&$params, &$db, &$form) {
-    $file = $params['uploadFile']['name'];
-    $firstRowIsColumnHeader = $params['skipColumnHeader'] ?? FALSE;
+  public function initialize(): void {
     $result = self::_CsvToTable(
-      $file,
-      $firstRowIsColumnHeader,
-      CRM_Utils_Array::value('import_table_name', $params),
-      CRM_Utils_Array::value('fieldSeparator', $params, ',')
+      $this->getSubmittedValue('uploadFile')['name'],
+      $this->getSubmittedValue('skipColumnHeader'),
+      $this->getSubmittedValue('fieldSeparator') ?? ','
     );
+    $this->addTrackingFieldsToTable($result['import_table_name']);
 
-    $form->set('originalColHeader', CRM_Utils_Array::value('column_headers', $result));
-    $form->set('importTableName', $result['import_table_name']);
     $this->updateUserJobMetadata('DataSource', [
       'table_name' => $result['import_table_name'],
-      'column_headers' => $firstRowIsColumnHeader ? $result['column_headers'] : [],
+      'column_headers' => $this->getSubmittedValue('skipColumnHeader') ? $result['column_headers'] : [],
       'number_of_columns' => $result['number_of_columns'],
     ]);
   }
@@ -113,8 +95,6 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
    *   File name to load.
    * @param bool $headers
    *   Whether the first row contains headers.
-   * @param string $tableName
-   *   Name of table from which data imported.
    * @param string $fieldSeparator
    *   Character that separates the various columns in the file.
    *
@@ -125,7 +105,6 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
   private static function _CsvToTable(
     $file,
     $headers = FALSE,
-    $tableName = NULL,
     $fieldSeparator = ','
   ) {
     $result = [];
@@ -194,9 +173,6 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
       }
     }
 
-    if ($tableName) {
-      CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS $tableName");
-    }
     $table = CRM_Utils_SQL_TempTable::build()->setDurable();
     $tableName = $table->getName();
     CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS $tableName");