Skip to content

Commit

Permalink
Add extra shared import functions
Browse files Browse the repository at this point in the history
This is an attempt to get away from fixing known breakage
being blocked by
civicrm#23689

These functions should be mostly as-yet-unused, or only minorly changed
but required to fix the known issues
  • Loading branch information
eileenmcnaughton committed Jun 6, 2022
1 parent 2b19182 commit 3d07fff
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
19 changes: 19 additions & 0 deletions CRM/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions CRM/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
22 changes: 22 additions & 0 deletions CRM/Import/Form/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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));
}

}
44 changes: 40 additions & 4 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*
Expand All @@ -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']);
Expand Down

0 comments on commit 3d07fff

Please sign in to comment.