Participant import fix - broken uniqueName fields, mapping saving, event_id
[civicrm-core.git] / CRM / Import / Form / DataSourceConfig.php
index 93b34e739f99b9f6aad934972ecf0688f0e85505..e5ddfc870bb85ce1db7069b3648bc0539c36f585 100644 (file)
@@ -29,13 +29,52 @@ class CRM_Import_Form_DataSourceConfig extends CRM_Import_Forms {
     $dataSourcePath = explode('_', $this->getDataSourceClassName());
     $templateFile = 'CRM/Contact/Import/Form/' . $dataSourcePath[3] . '.tpl';
     $this->assign('dataSourceFormTemplateFile', $templateFile ?? NULL);
+    if (CRM_Utils_Request::retrieveValue('user_job_id', 'Integer')) {
+      $this->setUserJobID(CRM_Utils_Request::retrieveValue('user_job_id', 'Integer'));
+    }
   }
 
   /**
    * Build the form object.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function buildQuickForm(): void {
     $this->buildDataSourceFields();
   }
 
+  /**
+   * Set defaults.
+   *
+   * @return array
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public function setDefaultValues() {
+    $defaults = [];
+    if ($this->userJobID) {
+      foreach ($this->getDataSourceFields() as $fieldName) {
+        $defaults[$fieldName] = $this->getSubmittedValue($fieldName);
+      }
+    }
+    return $defaults;
+  }
+
+  /**
+   * Get the submitted value, as saved in the user job.
+   *
+   * This form is not in the same flow as the DataSource but
+   * the value we want is saved to the userJob so load it from there.
+   *
+   * @param string $fieldName
+   *
+   * @return mixed|null
+   * @throws \API_Exception
+   */
+  public function getSubmittedValue(string $fieldName) {
+    $userJob = $this->getUserJob();
+    return $userJob['metadata']['submitted_values'][$fieldName];
+  }
+
 }