Skip to content

Commit

Permalink
Merge pull request #14762 from eileenmcnaughton/mapping_whomp
Browse files Browse the repository at this point in the history
[REF] Extract mapping converter function, kinda brutally
  • Loading branch information
mattwire authored Jul 9, 2019
2 parents bf6a7db + 257195f commit 33c6a83
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 59 deletions.
138 changes: 79 additions & 59 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,80 @@ public static function addComponentFields(&$fields, $mappingType, $exportMode) {
return $compArray;
}

/**
* Get the parameters for a mapping field in a saveable format from the quickform mapping format.
*
* @param array $params
* @param int $mappingId
* @param array $v
* @param string $k
* @param string $key
* @param int $colCnt
*
* @return array
*/
protected static function getMappingParams($params, $mappingId, $v, $k, $key, $colCnt) {
$locationTypeId = NULL;
$phoneTypeId = NULL;
$saveMappingFields = new CRM_Core_DAO_MappingField();

$saveMappingFields->mapping_id = $mappingId;
$saveMappingFields->name = CRM_Utils_Array::value('1', $v);
$saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v);
$locationId = CRM_Utils_Array::value('2', $v);
$saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL;

if ($v[1] == 'phone') {
$saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v);
}
elseif ($v[1] == 'im') {
$saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v);
}

if (!empty($params['operator'])) {
$saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]);
}
if (!empty($params['value'])) {
$saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]);
}
// Handle mapping for 'related contact' fields
if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) {
list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v));
if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) {

if (!empty($v['2'])) {
$saveMappingFields->name = CRM_Utils_Array::value('2', $v);
}
elseif (!empty($v['4'])) {
$saveMappingFields->name = CRM_Utils_Array::value('4', $v);
}

if (is_numeric(CRM_Utils_Array::value('3', $v))) {
$locationTypeId = CRM_Utils_Array::value('3', $v);
}
elseif (is_numeric(CRM_Utils_Array::value('5', $v))) {
$locationTypeId = CRM_Utils_Array::value('5', $v);
}

if (is_numeric(CRM_Utils_Array::value('4', $v))) {
$phoneTypeId = CRM_Utils_Array::value('4', $v);
}
elseif (is_numeric(CRM_Utils_Array::value('6', $v))) {
$phoneTypeId = CRM_Utils_Array::value('6', $v);
}

$saveMappingFields->location_type_id = is_numeric($locationTypeId) ? $locationTypeId : NULL;
$saveMappingFields->phone_type_id = is_numeric($phoneTypeId) ? $phoneTypeId : NULL;
$saveMappingFields->relationship_type_id = $id;
$saveMappingFields->relationship_direction = "{$first}_{$second}";
}
}

$saveMappingFields->grouping = $key;
$saveMappingFields->column_number = $colCnt;
return (array) $saveMappingFields;
}

/**
* Function returns all custom fields with group title and
* field label
Expand Down Expand Up @@ -1208,7 +1282,7 @@ public static function &returnProperties(&$params) {
*
* @return NULL
*/
public static function saveMappingFields(&$params, $mappingId) {
public static function saveMappingFields($params, $mappingId) {
//delete mapping fields records for existing mapping
$mappingFields = new CRM_Core_DAO_MappingField();
$mappingFields->mapping_id = $mappingId;
Expand All @@ -1224,65 +1298,11 @@ public static function saveMappingFields(&$params, $mappingId) {
foreach ($value as $k => $v) {

if (!empty($v['1'])) {
$saveMappingFields = new CRM_Core_DAO_MappingField();

$saveMappingFields->mapping_id = $mappingId;
$saveMappingFields->name = CRM_Utils_Array::value('1', $v);
$saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v);
$locationId = CRM_Utils_Array::value('2', $v);
$saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL;

if ($v[1] == 'phone') {
$saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v);
}
elseif ($v[1] == 'im') {
$saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v);
}

if (!empty($params['operator'])) {
$saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]);
}
if (!empty($params['value'])) {
$saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]);
}
// Handle mapping for 'related contact' fields
if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) {
list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v));
if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) {

if (!empty($v['2'])) {
$saveMappingFields->name = CRM_Utils_Array::value('2', $v);
}
elseif (!empty($v['4'])) {
$saveMappingFields->name = CRM_Utils_Array::value('4', $v);
}

if (is_numeric(CRM_Utils_Array::value('3', $v))) {
$locationTypeid = CRM_Utils_Array::value('3', $v);
}
elseif (is_numeric(CRM_Utils_Array::value('5', $v))) {
$locationTypeid = CRM_Utils_Array::value('5', $v);
}

if (is_numeric(CRM_Utils_Array::value('4', $v))) {
$phoneTypeid = CRM_Utils_Array::value('4', $v);
}
elseif (is_numeric(CRM_Utils_Array::value('6', $v))) {
$phoneTypeid = CRM_Utils_Array::value('6', $v);
}

$saveMappingFields->location_type_id = is_numeric($locationTypeid) ? $locationTypeid : NULL;
$saveMappingFields->phone_type_id = is_numeric($phoneTypeid) ? $phoneTypeid : NULL;
$saveMappingFields->relationship_type_id = $id;
$saveMappingFields->relationship_direction = "{$first}_{$second}";
}
}

$saveMappingFields->grouping = $key;
$saveMappingFields->column_number = $colCnt;
$saveMappingFields->save();
$saveMappingParams = self::getMappingParams($params, $mappingId, $v, $k, $key, $colCnt);
$saveMappingField = new CRM_Core_DAO_MappingField();
$saveMappingField->copyValues($saveMappingParams, TRUE);
$saveMappingField->save();
$colCnt++;
$locationTypeid = $phoneTypeid = NULL;
}
}
}
Expand Down
145 changes: 145 additions & 0 deletions tests/phpunit/CRM/Core/BAO/MappingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/**
* Class CRM_Core_BAO_MappingTest.
*
* @group headless
*/
class CRM_Core_BAO_MappingTest extends CiviUnitTestCase {

/**
* Test calling saveMapping.
*/
public function testSaveMappingFields() {
$mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'teest']);
$params = [
'qfKey' => '8d3bae0f77b62314516c1253176a1c1a_6756',
'entryURL' => 'http://dmaster.local/civicrm/contribute/search?reset=1',
'saveMappingName' => 'test',
'saveMappingDesc' => '',
'saveMapping' => '1',
'mapper' => [
1 =>
[
['Individual', '10_b_a', 'id'],
['Individual', 'city', ' '],
['Individual', 'contact_sub_type'],
['Student', 'custom_27'],
['Individual', 'current_employer'],
['Individual', 'phone', '1', '2'],
['Individual', 'postal_code', '2'],
['Individual', 'im', '1', '1'],
['Individual', 'url'],
['Individual', '1_b_a', 'phone', '5', '1'],
],
],
];
CRM_Core_BAO_Mapping::saveMappingFields($params, $mapping['id']);
$expected = [
1 =>
[
'id' => '1',
'mapping_id' => '1',
'name' => 'id',
'contact_type' => 'Individual',
'column_number' => '0',
'relationship_type_id' => '10',
'relationship_direction' => 'b_a',
'grouping' => '1',
],
2 =>
[
'id' => '2',
'mapping_id' => '1',
'name' => 'city',
'contact_type' => 'Individual',
'column_number' => '1',
'grouping' => '1',
],
3 =>
[
'id' => '3',
'mapping_id' => '1',
'name' => 'contact_sub_type',
'contact_type' => 'Individual',
'column_number' => '2',
'grouping' => '1',
],
4 =>
[
'id' => '4',
'mapping_id' => '1',
'name' => 'custom_27',
'contact_type' => 'Student',
'column_number' => '3',
'grouping' => '1',
],
5 =>
[
'id' => '5',
'mapping_id' => '1',
'name' => 'current_employer',
'contact_type' => 'Individual',
'column_number' => '4',
'grouping' => '1',
],
6 =>
[
'id' => '6',
'mapping_id' => '1',
'name' => 'phone',
'contact_type' => 'Individual',
'column_number' => '5',
'location_type_id' => '1',
'phone_type_id' => '2',
'grouping' => '1',
],
7 =>
[
'id' => '7',
'mapping_id' => '1',
'name' => 'postal_code',
'contact_type' => 'Individual',
'column_number' => '6',
'location_type_id' => '2',
'grouping' => '1',
],
8 =>
[
'id' => '8',
'mapping_id' => '1',
'name' => 'im',
'contact_type' => 'Individual',
'column_number' => '7',
'location_type_id' => '1',
'im_provider_id' => '1',
'grouping' => '1',
],
9 =>
[
'id' => '9',
'mapping_id' => '1',
'name' => 'url',
'contact_type' => 'Individual',
'column_number' => '8',
'grouping' => '1',
],
10 =>
[
'id' => '10',
'mapping_id' => '1',
'name' => 'phone',
'contact_type' => 'Individual',
'column_number' => '9',
'location_type_id' => '5',
'phone_type_id' => '1',
'relationship_type_id' => '1',
'relationship_direction' => 'b_a',
'grouping' => '1',
],
];
$saved = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']])['values'];
$this->assertEquals($expected, $saved);
}

}

0 comments on commit 33c6a83

Please sign in to comment.