Skip to content

Commit

Permalink
Use Processor to determine the phone_type_id & im_provider_id
Browse files Browse the repository at this point in the history
What we are doing here is actually very simple - despite the code being kinda insane - we are just getting
the  fields from the database for  each column in the mapping.

The fields are all loaded into the processor already & those functions are tested. This switches to  using them
and to passing around less variables...

Follow ons will do the same for other fields (website_type_id, location_type_id but I will add more tests as I do that
  • Loading branch information
eileenmcnaughton committed Aug 22, 2019
1 parent 7b1b7f8 commit 12e1448
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
19 changes: 7 additions & 12 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ public function buildQuickForm() {
//get loaded Mapping Fields
$mappingName = CRM_Utils_Array::value(1, $mappingName);
$mappingLocation = CRM_Utils_Array::value(1, $mappingLocation);
$mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType);
$mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider);
$mappingRelation = CRM_Utils_Array::value(1, $mappingRelation);
$mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType);

Expand Down Expand Up @@ -409,7 +407,7 @@ public function buildQuickForm() {
$sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);

if ($this->get('savedMapping')) {
list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
}
else {
$js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
Expand Down Expand Up @@ -854,17 +852,16 @@ protected function saveMappingField($mapperKeys, array $saveMapping, string $cTy
* @param $mappingRelation
* @param $mappingWebsiteType
* @param $mappingLocation
* @param $mappingPhoneType
* @param $mappingImProvider
* @param array $defaults
* @param string $js
* @param bool $hasColumnNames
* @param array $dataPatterns
* @param array $columnPatterns
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
$jsSet = FALSE;
$formName = $processor->getFormName();
if (isset($mappingName[$i])) {
Expand Down Expand Up @@ -904,9 +901,8 @@ public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation,
$contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i]));
$websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL;
$locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0;
$phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL;
//get provider id from saved mappings
$imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL;
$phoneType = $processor->getPhoneTypeID($i);
$imProvider = $processor->getIMProviderID($i);

if ($websiteTypeId) {
$defaults["mapper[$i]"] = [$relation, $contactDetails, $websiteTypeId];
Expand Down Expand Up @@ -947,9 +943,8 @@ public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation,
$mappingHeader = array_keys((array) $this->_mapperFields, $mappingName[$i]);
$websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL;
$locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0;
$phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL;
// get IM service provider id
$imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL;
$phoneType = $processor->getPhoneTypeID($i);
$imProvider = $processor->getIMProviderID($i);

if ($websiteTypeId) {
$defaults["mapper[$i]"] = [$mappingHeader[0], $websiteTypeId];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/ImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public function getMetadataTitles() {
protected function rekeyBySortedColumnNumbers(array $mappingFields) {
$this->mappingFields = CRM_Utils_Array::rekey($mappingFields, 'column_number');
ksort($this->mappingFields);
return array_values($this->mappingFields);
return $this->mappingFields;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ protected function loadSavedMapping($form, $mappingID, $columnNumber) {
$processor->setFormName('document.forms.MapField');
$processor->setMetadata($this->getContactImportMetadata());

$return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
$return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
return ['defaults' => $return[0], 'js' => $return[1]];
}

Expand Down

0 comments on commit 12e1448

Please sign in to comment.