From 3d07fff345e902ec4fb12670a32c5feadc0ae664 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 6 Jun 2022 15:18:53 +1200 Subject: [PATCH] Add extra shared import functions This is an attempt to get away from fixing known breakage being blocked by https://github.com/civicrm/civicrm-core/pull/23689 These functions should be mostly as-yet-unused, or only minorly changed but required to fix the known issues --- CRM/Import/Form/MapField.php | 19 ++++++++++++++++ CRM/Import/Form/Preview.php | 10 ++++++++ CRM/Import/Form/Summary.php | 22 ++++++++++++++++++ CRM/Import/Parser.php | 44 ++++++++++++++++++++++++++++++++---- 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index 11581f2a4699..26f90307782b 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -76,6 +76,22 @@ public function preProcess() { parent::preProcess(); } + /** + * Process the mapped fields and map it into the uploaded file + * preview the file and extract some summary statistics + * + * @return void + * @noinspection PhpDocSignatureInspection + * @noinspection PhpUnhandledExceptionInspection + */ + public function postProcess() { + $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues()); + $this->saveMapping($this->getMappingTypeName()); + $parser = $this->getParser(); + $parser->init(); + $parser->validate(); + } + /** * Attempt to match header labels with our mapper fields. * @@ -230,6 +246,9 @@ protected function getMappedField(array $fieldMapping, int $mappingID, int $colu protected function saveMappingField(int $mappingID, int $columnNumber, bool $isUpdate = FALSE): void { $fieldMapping = (array) $this->getSubmittedValue('mapper')[$columnNumber]; $mappedField = $this->getMappedField($fieldMapping, $mappingID, $columnNumber); + if (empty($mappedField['name'])) { + $mappedField['name'] = 'do_not_import'; + } if ($isUpdate) { Civi\Api4\MappingField::update(FALSE) ->setValues($mappedField) diff --git a/CRM/Import/Form/Preview.php b/CRM/Import/Form/Preview.php index cce75402e928..69e302d8c99a 100644 --- a/CRM/Import/Form/Preview.php +++ b/CRM/Import/Form/Preview.php @@ -117,4 +117,14 @@ protected function assignPreviewVariables(): void { $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2); } + /** + * Process the mapped fields and map it into the uploaded file + * preview the file and extract some summary statistics + * + * @return void + */ + public function postProcess() { + CRM_Import_Parser::runImport(NULL, $this->getUserJobID(), 0); + } + } diff --git a/CRM/Import/Form/Summary.php b/CRM/Import/Form/Summary.php index b27af617eea1..72c8be2f2744 100644 --- a/CRM/Import/Form/Summary.php +++ b/CRM/Import/Form/Summary.php @@ -23,6 +23,15 @@ */ abstract class CRM_Import_Form_Summary extends CRM_Import_Forms { + /** + * Set variables up before form is built. + * + * @return void + */ + public function preProcess() { + $this->assignOutputURLs(); + } + /** * Build the form object. */ @@ -45,4 +54,17 @@ public function getTitle() { return ts('Summary'); } + protected function assignOutputURLs(): void { + $this->assign('totalRowCount', $this->getRowCount()); + $this->assign('validRowCount', $this->getRowCount(CRM_Import_Parser::VALID) + $this->getRowCount(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING)); + $this->assign('invalidRowCount', $this->getRowCount(CRM_Import_Parser::ERROR)); + $this->assign('duplicateRowCount', $this->getRowCount(CRM_Import_Parser::DUPLICATE)); + $this->assign('unMatchCount', $this->getRowCount(CRM_Import_Parser::NO_MATCH)); + $this->assign('unparsedAddressCount', $this->getRowCount(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING)); + $this->assign('downloadDuplicateRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::DUPLICATE)); + $this->assign('downloadErrorRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::ERROR)); + $this->assign('downloadMismatchRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::NO_MATCH)); + $this->assign('downloadAddressRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING)); + } + } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 30b8c8ae5ff4..ef1e94583cda 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -635,8 +635,8 @@ protected function validateRequiredContactFields(string $contactType, array $par protected function doPostImportActions() { $userJob = $this->getUserJob(); - $summaryInfo = $userJob['metadata']['summary_info']; - $actions = $userJob['metadata']['post_actions']; + $summaryInfo = $userJob['metadata']['summary_info'] ?? []; + $actions = $userJob['metadata']['post_actions'] ?? []; if (!empty($actions['group'])) { $groupAdditions = $this->addImportedContactsToNewGroup($this->createdContacts, $actions['group']); foreach ($actions['group'] as $groupID) { @@ -1575,7 +1575,8 @@ public function validate(): void { protected function getInvalidValues($value, string $key = '', string $prefixString = ''): array { $errors = []; if ($value === 'invalid_import_value') { - $errors[] = $prefixString . $this->getFieldMetadata($key)['title']; + $metadata = $this->getFieldMetadata($key); + $errors[] = $prefixString . ($metadata['html']['label'] ?? $metadata['title']); } elseif (is_array($value)) { foreach ($value as $innerKey => $innerValue) { @@ -1666,6 +1667,40 @@ public function getMappingFieldFromMapperInput(array $fieldMapping, int $mapping ]; } + /** + * @param array $mappedField + * Field detail as would be saved in field_mapping table + * or as returned from getMappingFieldFromMapperInput + * + * @return string + * @throws \API_Exception + */ + public function getMappedFieldLabel(array $mappedField): string { + $this->setFieldMetadata(); + return $this->getFieldMetadata($mappedField['name'])['title']; + } + + /** + * Get the row from the csv mapped to our parameters. + * + * @param array $values + * + * @return array + * @throws \API_Exception + */ + public function getMappedRow(array $values): array { + $params = []; + foreach ($this->getFieldMappings() as $i => $mappedField) { + if ($mappedField['name'] === 'do_not_import') { + continue; + } + if ($mappedField['name']) { + $params[$this->getFieldMetadata($mappedField['name'])['name']] = $this->getTransformedFieldValue($mappedField['name'], $values[$i]); + } + } + return $params; + } + /** * Get the field mappings for the import. * @@ -1678,7 +1713,8 @@ public function getMappingFieldFromMapperInput(array $fieldMapping, int $mapping */ protected function getFieldMappings(): array { $mappedFields = []; - foreach ($this->getSubmittedValue('mapper') as $i => $mapperRow) { + $mapper = $this->getSubmittedValue('mapper'); + foreach ($mapper as $i => $mapperRow) { $mappedField = $this->getMappingFieldFromMapperInput($mapperRow, 0, $i); // Just for clarity since 0 is a pseudo-value unset($mappedField['mapping_id']);