Skip to content

Commit

Permalink
[Import] Add some not-yet-used functions that are prone to conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 26, 2022
1 parent d409829 commit eb3ccff
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 29 deletions.
9 changes: 9 additions & 0 deletions CRM/Activity/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ public function postProcess() {
}
}

/**
* @return CRM_Activity_Import_Parser_Activity
*/
protected function getParser(): CRM_Activity_Import_Parser_Activity {
$parser = new CRM_Activity_Import_Parser_Activity();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
20 changes: 0 additions & 20 deletions CRM/Contact/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,6 @@ public function postProcess() {
$importJob->isComplete();
}

/**
* Get the mapped fields as an array of labels.
*
* e.g
* ['First Name', 'Employee Of - First Name', 'Home - Street Address']
*
* @return array
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function getMappedFieldLabels(): array {
$mapper = [];
$parser = new CRM_Contact_Import_Parser_Contact();
$parser->setUserJobID($this->getUserJobID());
foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
$mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
}
return $mapper;
}

/**
* @return \CRM_Contact_Import_Parser_Contact
*/
Expand Down
9 changes: 9 additions & 0 deletions CRM/Contribute/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,13 @@ public function postProcess() {
}
}

/**
* @return \CRM_Contribute_Import_Parser_Contribution
*/
protected function getParser(): CRM_Contribute_Import_Parser_Contribution {
$parser = new CRM_Contribute_Import_Parser_Contribution();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
9 changes: 9 additions & 0 deletions CRM/Custom/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,13 @@ public function postProcess() {
}
}

/**
* @return CRM_Custom_Import_Parser_Api
*/
protected function getParser(): CRM_Custom_Import_Parser_Api {
$parser = new CRM_Custom_Import_Parser_Api();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
9 changes: 9 additions & 0 deletions CRM/Event/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,13 @@ public function postProcess() {
$parser->set($this);
}

/**
* @return CRM_Event_Import_Parser_Participant
*/
protected function getParser(): CRM_Event_Import_Parser_Participant {
$parser = new CRM_Event_Import_Parser_Participant();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
9 changes: 9 additions & 0 deletions CRM/Event/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,13 @@ public function postProcess() {
}
}

/**
* @return CRM_Event_Import_Parser_Participant
*/
protected function getParser(): CRM_Event_Import_Parser_Participant {
$parser = new CRM_Event_Import_Parser_Participant();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
19 changes: 19 additions & 0 deletions CRM/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@ public function setStatusUrl() {
$this->assign('statusUrl', $statusUrl);
}

/**
* Get the mapped fields as an array of labels.
*
* e.g
* ['First Name', 'Employee Of - First Name', 'Home - Street Address']
*
* @return array
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function getMappedFieldLabels(): array {
$mapper = [];
$parser = $this->getParser();
foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
$mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
}
return $mapper;
}

}
23 changes: 23 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,27 @@ protected function validateCustomField($customFieldID, $value, array $fieldMetaD
return NULL;
}

/**
* Get the civicrm_mapping_field appropriate layout for the mapper input.
*
* For simple parsers (not contribution or contact) the input looks like
* ['first_name', 'custom_32']
* and it is converted to
*
* ['name' => 'first_name', 'mapping_id' => 1, 'column_number' => 5],
*
* @param array $fieldMapping
* @param int $mappingID
* @param int $columnNumber
*
* @return array
*/
public function getMappingFieldFromMapperInput(array $fieldMapping, int $mappingID, int $columnNumber): array {
return [
'name' => $fieldMapping[0],
'mapping_id' => $mappingID,
'column_number' => $columnNumber,
];
}

}
9 changes: 9 additions & 0 deletions CRM/Member/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,13 @@ public function postProcess() {
$parser->set($this);
}

/**
* @return \CRM_Member_Import_Parser_Membership
*/
protected function getParser(): CRM_Member_Import_Parser_Membership {
$parser = new CRM_Member_Import_Parser_Membership();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
9 changes: 9 additions & 0 deletions CRM/Member/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,13 @@ public function postProcess() {
}
}

/**
* @return \CRM_Member_Import_Parser_Membership
*/
protected function getParser(): CRM_Member_Import_Parser_Membership {
$parser = new CRM_Member_Import_Parser_Membership();
$parser->setUserJobID($this->getUserJobID());
return $parser;
}

}
29 changes: 20 additions & 9 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,7 @@ public static function exportCSV($fileName, $header, $data) {
* @return void
*/
public function init() {
$this->fieldMetadata = CRM_Member_BAO_Membership::importableFields($this->_contactType, FALSE);

foreach ($this->fieldMetadata as $name => $field) {
// @todo - we don't really need to do all this.... fieldMetadata is just fine to use as is.
$field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT);
$field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//');
$field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//');
$this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
}
$this->setFieldMetadata();

$this->_newMemberships = [];

Expand Down Expand Up @@ -1045,4 +1037,23 @@ protected function isContactIDColumnPresent(): bool {
return in_array('membership_contact_id', $this->_mapperKeys, TRUE);
}

/**
* Set field metadata.
*/
protected function setFieldMetadata(): void {
if (empty($this->importableFieldsMetadata)) {
$metadata = CRM_Member_BAO_Membership::importableFields($this->_contactType, FALSE);

foreach ($this->fieldMetadata as $name => $field) {
// @todo - we don't really need to do all this.... fieldMetadata is just fine to use as is.
$field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT);
$field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//');
$field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//');
$this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
}
// We are consolidating on `importableFieldsMetadata` - but both still used.
$this->importableFieldsMetadata = $this->fieldMetadata = $metadata;
}
}

}

0 comments on commit eb3ccff

Please sign in to comment.