Merge pull request #23959 from totten/master-casexml-acttype
[civicrm-core.git] / CRM / Import / Forms.php
index b2f96352e4cfe06cf232d361c006df5eb987ae48..c9636b25bcff91a516c528a8832d069f97661759 100644 (file)
@@ -373,7 +373,7 @@ class CRM_Import_Forms extends CRM_Core_Form {
     $id = UserJob::create(FALSE)
       ->setValues([
         'created_id' => CRM_Core_Session::getLoggedInContactID(),
-        'type_id:name' => $this->getUserJobType(),
+        'job_type' => $this->getUserJobType(),
         'status_id:name' => 'draft',
         // This suggests the data could be cleaned up after this.
         'expires_date' => '+ 1 week',
@@ -468,9 +468,9 @@ class CRM_Import_Forms extends CRM_Core_Form {
    */
   protected function getOutputRows($statuses = [], int $limit = 0) {
     $statuses = (array) $statuses;
-    return $this->getDataSourceObject()->setLimit($limit)->setStatuses($statuses)
-      ->setSelectFields(array_merge(['_id', '_status_message'], $this->getColumnHeaders()))
-      ->setStatuses($statuses)->getRows();
+    $dataSource = $this->getDataSourceObject()->setLimit($limit)->setStatuses($statuses)->setStatuses($statuses);
+    $dataSource->setSelectFields(array_merge(['_id', '_status_message'], $dataSource->getDataFieldNames()));
+    return $dataSource->getRows();
   }
 
   /**
@@ -512,7 +512,7 @@ class CRM_Import_Forms extends CRM_Core_Form {
    */
   public static function outputCSV(): void {
     $userJobID = CRM_Utils_Request::retrieveValue('user_job_id', 'Integer', NULL, TRUE);
-    $status = CRM_Utils_Request::retrieveValue('status', 'String', NULL, TRUE);
+    $status = (int) CRM_Utils_Request::retrieveValue('status', 'String', NULL, TRUE);
     $saveFileName = CRM_Import_Parser::saveFileName($status);
 
     $form = new CRM_Import_Forms();
@@ -523,13 +523,9 @@ class CRM_Import_Forms extends CRM_Core_Form {
     $writer = Writer::createFromFileObject(new SplTempFileObject());
     $headers = $form->getOutputColumnsHeaders();
     $writer->insertOne($headers);
-    // Note this might be more inefficient that iterating the result
+    // Note this might be more inefficient by iterating the result
     // set & doing insertOne - possibly something to explore later.
     $writer->insertAll($form->getOutputRows($status));
-
-    CRM_Utils_System::setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
-    CRM_Utils_System::setHttpHeader('Content-Description', 'File Transfer');
-    CRM_Utils_System::setHttpHeader('Content-Type', 'text/csv; charset=UTF-8');
     $writer->output($saveFileName);
     CRM_Utils_System::civiExit();
   }
@@ -548,6 +544,32 @@ class CRM_Import_Forms extends CRM_Core_Form {
     ]);
   }
 
+  /**
+   * Get the url to download the relevant csv file.
+   * @param string $status
+   *
+   * @return string
+   */
+
+  /**
+   *
+   * @return array
+   */
+  public function getTrackingSummary(): array {
+    $summary = [];
+    $fields = $this->getParser()->getTrackingFields();
+    $row = $this->getDataSourceObject()->setAggregateFields($fields)->getRow();
+    foreach ($fields as $fieldName => $field) {
+      $summary[] = [
+        'field_name' => $fieldName,
+        'description' => $field['description'],
+        'value' => $row[$fieldName],
+      ];
+    }
+
+    return $summary;
+  }
+
   /**
    * Get the fields available for import selection.
    *
@@ -566,6 +588,14 @@ class CRM_Import_Forms extends CRM_Core_Form {
    * @return \CRM_Contact_Import_Parser_Contact|\CRM_Contribute_Import_Parser_Contribution
    */
   protected function getParser() {
+    foreach (CRM_Core_BAO_UserJob::getTypes() as $jobType) {
+      if ($jobType['id'] === $this->getUserJob()['job_type']) {
+        $className = $jobType['class'];
+        $classObject = new $className();
+        $classObject->setUserJobID($this->getUserJobID());
+        return $classObject;
+      };
+    }
     return NULL;
   }