Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Import] [Membership] Cleanup, fix form breakage (unreleased regression) #23689

Merged
merged 11 commits into from
Jun 6, 2022
11 changes: 1 addition & 10 deletions CRM/Contact/Import/Form/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,7 @@ public function preProcess() {

$this->assign('groupAdditions', $this->getUserJob()['metadata']['summary_info']['groups']);
$this->assign('tagAdditions', $this->getUserJob()['metadata']['summary_info']['tags']);
$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));
$this->assignOutputURLs();
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/import/contact', 'reset=1'));
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
* The initializer code, called before processing.
*/
public function init() {
// Force re-load of user job.
unset($this->userJob);
$this->setFieldMetadata();
foreach ($this->getImportableFieldsMetadata() as $name => $field) {
$this->addField($name, $field['title'], CRM_Utils_Array::value('type', $field), CRM_Utils_Array::value('headerPattern', $field), CRM_Utils_Array::value('dataPattern', $field), CRM_Utils_Array::value('hasLocationType', $field));
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ protected function storeFormValues($names) {
* Entity to set for paraser currently only for custom import
*/
protected function submitFileForMapping($parserClassName, $entity = NULL) {
$this->controller->resetPage('MapField');
CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats'));
$this->processDatasource();

Expand All @@ -181,6 +180,7 @@ protected function submitFileForMapping($parserClassName, $entity = NULL) {

// add all the necessary variables to the form
$parser->set($this);
$this->controller->resetPage('MapField');
}

/**
Expand Down
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));
}

}
48 changes: 42 additions & 6 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 Expand Up @@ -1807,8 +1843,8 @@ protected function getSubtypes($contactType) {
* @param int|null $entityID
* Optional created entity ID
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
protected function setImportStatus(int $id, string $status, string $message, ?int $entityID = NULL): void {
$this->getDataSourceObject()->updateStatus($id, $status, $message, $entityID);
Expand Down
Loading