dev/core#3665 import summary fixes
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Jun 2022 05:27:49 +0000 (17:27 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Jun 2022 21:15:19 +0000 (09:15 +1200)
m

CRM/Contact/Import/Parser/Contact.php
CRM/Import/DataSource.php
CRM/Import/Form/Summary.php
CRM/Import/Forms.php
templates/CRM/Contact/Import/Form/Summary.tpl

index a64158677ab85145acc9d1d1a20df0f1483b46f8..53f0e868cd87ab3e62ade1d6fd9b4feac25a3bec 100644 (file)
@@ -92,8 +92,18 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
    */
   public function getTrackingFields(): array {
     return [
-      'related_contact_created' => 'INT COMMENT "Number of related contacts created"',
-      'related_contact_matched' => 'INT COMMENT "Number of related contacts found (& potentially updated)"',
+      'related_contact_created' => [
+        'name' => 'related_contact_created',
+        'operation' => 'SUM',
+        'type' => 'INT',
+        'description' => ts('Number of related contacts created'),
+      ],
+      'related_contact_matched' => [
+        'name' => 'related_contact_matched',
+        'operation' => 'SUM',
+        'type' => 'INT',
+        'description' => ts('Number of related contacts found (and potentially updated)'),
+      ],
     ];
   }
 
index 3e551746f51700e8aa672d29a404906993f8960b..12924c3d3e6a3e1f5946c356702c2d258d2c5a03 100644 (file)
@@ -74,6 +74,13 @@ abstract class CRM_Import_DataSource {
    */
   private $selectFields;
 
+  /**
+   * Fields to select as aggregates.
+   *
+   * @var array
+   */
+  private $aggregateFields;
+
   /**
    * The name of the import table.
    *
@@ -98,6 +105,23 @@ abstract class CRM_Import_DataSource {
     return $this;
   }
 
+  /**
+   * @param array $fields
+   *
+   * @return CRM_Import_DataSource
+   */
+  public function setAggregateFields(array $fields): CRM_Import_DataSource {
+    $this->aggregateFields = $fields;
+    return $this;
+  }
+
+  /**
+   * @return array|null
+   */
+  public function getAggregateFields(): ?array {
+    return $this->aggregateFields;
+  }
+
   /**
    * Current row.
    *
@@ -521,7 +545,7 @@ abstract class CRM_Import_DataSource {
     $sql = '';
     $fields = $this->getParser()->getTrackingFields();
     foreach ($fields as $fieldName => $spec) {
-      $sql .= 'ADD COLUMN  _' . $fieldName . ' ' . $spec . ',';
+      $sql .= 'ADD COLUMN  _' . $fieldName . ' ' . $spec['type'] . ',';
     }
     return $sql;
   }
@@ -606,6 +630,13 @@ abstract class CRM_Import_DataSource {
    * @return string
    */
   private function getSelectClause(): string {
+    if ($this->getAggregateFields()) {
+      $fields = [];
+      foreach ($this->getAggregateFields() as $field) {
+        $fields[] = $field['operation'] . '(_' . $field['name'] . ') as ' . $field['name'];
+      }
+      return implode(',', $fields);
+    }
     return $this->getSelectFields() ? '`' . implode('`, `', $this->getSelectFields()) . '`' : '*';
   }
 
index f758a30f9ea833dc4fd2103cb27c594c0fa069f5..8cf4e734016744bc07bd79b748319d7a94e21cc1 100644 (file)
@@ -62,6 +62,8 @@ abstract class CRM_Import_Form_Summary extends CRM_Import_Forms {
       $this->assign('downloadAddressRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING));
       $this->assign('downloadPledgePaymentErrorRecordsUrl', $this->getDownloadURL(CRM_Contribute_Import_Parser_Contribution::PLEDGE_PAYMENT_ERROR));
       $this->assign('downloadSoftCreditErrorRecordsUrl', $this->getDownloadURL(CRM_Contribute_Import_Parser_Contribution::SOFT_CREDIT_ERROR));
+      $this->assign('trackingSummary', $this->getTrackingSummary());
+
       $userJobID = CRM_Utils_Request::retrieve('user_job_id', 'String', $this, TRUE);
       $userJob = UserJob::get(TRUE)
         ->addWhere('id', '=', $userJobID)
index 633bb5cb34ecf04bfab76b2f0c914a33aa59bb78..54add7e8a35bd96a615a3eec05ac734db7d22836 100644 (file)
@@ -544,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.
    *
@@ -562,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()['type_id']) {
+        $className = $jobType['class'];
+        $classObject = new $className();
+        $classObject->setUserJobID($this->getUserJobID());
+        return $classObject;
+      };
+    }
     return NULL;
   }
 
index 3c1a278b7233ce8a76c7db9e53ac06ee5cce9744..9c57dfa77fcbf2849ff40c150fd2cc0d94ba9c2d 100644 (file)
     {/if}
 
     <tr>
-      <td class="label crm-grid-cell">{ts}Total Contacts{/ts}</td>
+      <td class="label crm-grid-cell">{ts}Total Rows Imported{/ts}</td>
       <td class="data">{$validRowCount}</td>
-      <td class="explanation">{ts}Total number of contact records created or modified during the import.{/ts}</td>
+      <td class="explanation">{ts}Total number of primary records created or modified during the import.{/ts}</td>
     </tr>
+    {foreach from=$trackingSummary item="summaryRow"}
+      <tr>
+        <td class="label crm-grid-cell"></td>
+        <td class="data">{$summaryRow.value}</td>
+        <td class="explanation">{$summaryRow.description}</td>
+      </tr>
+    {/foreach}
 
     {if $groupAdditions}
     <tr><td class="label crm-grid-cell">{ts}Import to Groups{/ts}</td>