From bccb1d034f72779f3477cc3abe50d59367ea605d Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 20 Jun 2022 17:27:49 +1200 Subject: [PATCH] dev/core#3665 import summary fixes m --- CRM/Contact/Import/Parser/Contact.php | 14 ++++++-- CRM/Import/DataSource.php | 33 +++++++++++++++++- CRM/Import/Form/Summary.php | 2 ++ CRM/Import/Forms.php | 34 +++++++++++++++++++ templates/CRM/Contact/Import/Form/Summary.tpl | 11 ++++-- 5 files changed, 89 insertions(+), 5 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index a64158677a..53f0e868cd 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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)'), + ], ]; } diff --git a/CRM/Import/DataSource.php b/CRM/Import/DataSource.php index 3e551746f5..12924c3d3e 100644 --- a/CRM/Import/DataSource.php +++ b/CRM/Import/DataSource.php @@ -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()) . '`' : '*'; } diff --git a/CRM/Import/Form/Summary.php b/CRM/Import/Form/Summary.php index f758a30f9e..8cf4e73401 100644 --- a/CRM/Import/Form/Summary.php +++ b/CRM/Import/Form/Summary.php @@ -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) diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php index 633bb5cb34..54add7e8a3 100644 --- a/CRM/Import/Forms.php +++ b/CRM/Import/Forms.php @@ -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; } diff --git a/templates/CRM/Contact/Import/Form/Summary.tpl b/templates/CRM/Contact/Import/Form/Summary.tpl index 3c1a278b72..9c57dfa77f 100644 --- a/templates/CRM/Contact/Import/Form/Summary.tpl +++ b/templates/CRM/Contact/Import/Form/Summary.tpl @@ -90,10 +90,17 @@ {/if} - {ts}Total Contacts{/ts} + {ts}Total Rows Imported{/ts} {$validRowCount} - {ts}Total number of contact records created or modified during the import.{/ts} + {ts}Total number of primary records created or modified during the import.{/ts} + {foreach from=$trackingSummary item="summaryRow"} + + + {$summaryRow.value} + {$summaryRow.description} + + {/foreach} {if $groupAdditions} {ts}Import to Groups{/ts} -- 2.25.1