Skip to content

Commit

Permalink
Merge pull request #10554 from eileenmcnaughton/crazy_vars
Browse files Browse the repository at this point in the history
[NFC] Cleanup towards CRM-20759, define variables more legibly & consistently.
  • Loading branch information
eileenmcnaughton authored Jun 27, 2017
2 parents 5502c5c + 1881b7b commit 3b32e27
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 152 deletions.
105 changes: 31 additions & 74 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,39 +717,15 @@ public function formatCustomFieldName(&$fields) {
* @return \CRM_Contact_Import_Parser_Contact
*/
public function submit($params, $mapperKeys) {
$mapper = $mapperKeysMain = array();
$mapper = $mapperKeysMain = $locations = array();
$parserParameters = CRM_Contact_Import_Parser_Contact::getParameterForParser($this->_columnCount);

$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
$imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
$websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');

//these mapper params need to set key as array and val as null.
$mapperParams = array(
'related' => 'relatedVal',
'locations' => 'locationsVal',
'mapperLocType' => 'mapperLocTypeVal',
'mapperPhoneType' => 'mapperPhoneTypeVal',
'mapperImProvider' => 'mapperImProviderVal',
'mapperWebsiteType' => 'mapperWebsiteTypeVal',
'relatedContactType' => 'relatedContactTypeVal',
'relatedContactDetails' => 'relatedContactDetailsVal',
'relatedContactLocType' => 'relatedContactLocTypeVal',
'relatedContactPhoneType' => 'relatedContactPhoneTypeVal',
'relatedContactImProvider' => 'relatedContactImProviderVal',
'relatedContactWebsiteType' => 'relatedContactWebsiteTypeVal',
);

//set respective mapper params to array.
foreach (array_keys($mapperParams) as $mapperParam) {
$$mapperParam = array();
}

for ($i = 0; $i < $this->_columnCount; $i++) {
//set respective mapper value to null
foreach (array_values($mapperParams) as $mapperParam) {
$$mapperParam = NULL;
}

$fldName = CRM_Utils_Array::value(0, $mapperKeys[$i]);
$selOne = CRM_Utils_Array::value(1, $mapperKeys[$i]);
Expand All @@ -761,17 +737,17 @@ public function submit($params, $mapperKeys) {
//need to differentiate non location elements.
if ($selOne && is_numeric($selOne)) {
if ($fldName == 'url') {
$mapperWebsiteTypeVal = $websiteTypes[$selOne];
$parserParameters['mapperWebsiteType'][$i] = $websiteTypes[$selOne];
}
else {
$locationsVal = $locationTypes[$selOne];
$mapperLocTypeVal = $selOne;
$locations[$i] = $locationTypes[$selOne];
$parserParameters['mapperLocType'][$i] = $selOne;
if ($selTwo && is_numeric($selTwo)) {
if ($fldName == 'phone') {
$mapperPhoneTypeVal = $phoneTypes[$selTwo];
$parserParameters['mapperPhoneType'][$i] = $phoneTypes[$selTwo];
}
elseif ($fldName == 'im') {
$mapperImProviderVal = $imProviders[$selTwo];
$parserParameters['mapperImProvider'][$i] = $imProviders[$selTwo];
}
}
}
Expand All @@ -782,19 +758,19 @@ public function submit($params, $mapperKeys) {
if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')
) {
$relatedVal = $this->_mapperFields[$fldName];
$parserParameters['mapperRelated'][$i] = $this->_mapperFields[$fldName];
if ($selOne) {
if ($selOne == 'url') {
$relatedContactWebsiteTypeVal = $websiteTypes[$selTwo];
$parserParameters['relatedContactWebsiteType'][$i] = $websiteTypes[$selTwo];
}
else {
$relatedContactLocTypeVal = CRM_Utils_Array::value($selTwo, $locationTypes);
$parserParameters['relatedContactLocType'][$i] = CRM_Utils_Array::value($selTwo, $locationTypes);
if ($selThree) {
if ($selOne == 'phone') {
$relatedContactPhoneTypeVal = $phoneTypes[$selThree];
$parserParameters['relatedContactPhoneType'][$i] = $phoneTypes[$selThree];
}
elseif ($selOne == 'im') {
$relatedContactImProviderVal = $imProviders[$selThree];
$parserParameters['relatedContactImProvider'][$i] = $imProviders[$selThree];
}
}
}
Expand All @@ -803,44 +779,25 @@ public function submit($params, $mapperKeys) {
$relationType = new CRM_Contact_DAO_RelationshipType();
$relationType->id = $id;
$relationType->find(TRUE);
$relatedContactTypeVal = $relationType->{"contact_type_$second"};
$relatedContactDetailsVal = $this->_formattedFieldNames[$relatedContactTypeVal][$selOne];
$parserParameters['relatedContactType'][$i] = $relationType->{"contact_type_$second"};
$parserParameters['relatedContactDetails'][$i] = $this->_formattedFieldNames[$parserParameters['relatedContactType'][$i]][$selOne];
}
}

//set the respective mapper param array values.
foreach ($mapperParams as $mapperParamKey => $mapperParamVal) {
${$mapperParamKey}[$i] = $$mapperParamVal;
}
}

$this->set('columnNames', $this->_columnNames);

//set main contact properties.
$properties = array(
'ims' => 'mapperImProvider',
'mapper' => 'mapper',
'phones' => 'mapperPhoneType',
'websites' => 'mapperWebsiteType',
'locations' => 'locations',
);
foreach ($properties as $propertyName => $propertyVal) {
$this->set($propertyName, $$propertyVal);
}

//set related contact propeties.
$relProperties = array(
'related',
'relatedContactType',
'relatedContactDetails',
'relatedContactLocType',
'relatedContactPhoneType',
'relatedContactImProvider',
'relatedContactWebsiteType',
);
foreach ($relProperties as $relProperty) {
$this->set($relProperty, $$relProperty);
}
$this->set('websites', $parserParameters['mapperWebsiteType']);
$this->set('locations', $locations);
$this->set('phones', $parserParameters['mapperPhoneType']);
$this->set('ims', $parserParameters['mapperImProvider']);
$this->set('related', $parserParameters['mapperRelated']);
$this->set('relatedContactType', $parserParameters['relatedContactType']);
$this->set('relatedContactDetails', $parserParameters['relatedContactDetails']);
$this->set('relatedContactLocType', $parserParameters['relatedContactLocType']);
$this->set('relatedContactPhoneType', $parserParameters['relatedContactPhoneType']);
$this->set('relatedContactImProvider', $parserParameters['relatedContactImProvider']);
$this->set('relatedContactWebsiteType', $parserParameters['relatedContactWebsiteType']);
$this->set('mapper', $mapper);

// store mapping Id to display it in the preview page
$this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
Expand Down Expand Up @@ -995,11 +952,11 @@ public function submit($params, $mapperKeys) {
$this->set('savedMapping', $saveMappingFields->mapping_id);
}

$parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $mapperLocType, $mapperPhoneType,
$mapperImProvider, $related, $relatedContactType,
$relatedContactDetails, $relatedContactLocType,
$relatedContactPhoneType, $relatedContactImProvider,
$mapperWebsiteType, $relatedContactWebsiteType
$parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $parserParameters['mapperLocType'], $parserParameters['mapperPhoneType'],
$parserParameters['mapperImProvider'], $parserParameters['mapperRelated'], $parserParameters['relatedContactType'],
$parserParameters['relatedContactDetails'], $parserParameters['relatedContactLocType'],
$parserParameters['relatedContactPhoneType'], $parserParameters['relatedContactImProvider'],
$parserParameters['mapperWebsiteType'], $parserParameters['relatedContactWebsiteType']
);

$primaryKeyName = $this->get('primaryKeyName');
Expand Down
103 changes: 25 additions & 78 deletions CRM/Contact/Import/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,7 @@ class CRM_Contact_Import_ImportJob {
protected $_allTags;

protected $_mapper;
protected $_mapperKeys;
protected $_mapperLocTypes;
protected $_mapperPhoneTypes;
protected $_mapperImProviders;
protected $_mapperWebsiteTypes;
protected $_mapperRelated;
protected $_mapperRelatedContactType;
protected $_mapperRelatedContactDetails;
protected $_mapperRelatedContactLocType;
protected $_mapperRelatedContactPhoneType;
protected $_mapperRelatedContactImProvider;
protected $_mapperRelatedContactWebsiteType;
protected $_mapperKeys = array();
protected $_mapFields;

protected $_parser;
Expand Down Expand Up @@ -101,25 +90,6 @@ public function __construct($tableName = NULL, $createSql = NULL, $createTable =
}

$this->_tableName = $tableName;

//initialize the properties.
$properties = array(
'mapperKeys',
'mapperRelated',
'mapperLocTypes',
'mapperPhoneTypes',
'mapperImProviders',
'mapperWebsiteTypes',
'mapperRelatedContactType',
'mapperRelatedContactDetails',
'mapperRelatedContactLocType',
'mapperRelatedContactPhoneType',
'mapperRelatedContactImProvider',
'mapperRelatedContactWebsiteType',
);
foreach ($properties as $property) {
$this->{"_$property"} = array();
}
}

/**
Expand Down Expand Up @@ -168,32 +138,14 @@ public function setJobParams(&$params) {
*/
public function runImport(&$form, $timeout = 55) {
$mapper = $this->_mapper;
$mapperFields = array();
$mapperFields;
$parserParameters = CRM_Contact_Import_Parser_Contact::getParameterForParser(count($mapper));
$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
$imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
$websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');

//initialize mapper perperty value.
$mapperPeroperties = array(
'mapperRelated' => 'mapperRelatedVal',
'mapperLocTypes' => 'mapperLocTypesVal',
'mapperPhoneTypes' => 'mapperPhoneTypesVal',
'mapperImProviders' => 'mapperImProvidersVal',
'mapperWebsiteTypes' => 'mapperWebsiteTypesVal',
'mapperRelatedContactType' => 'mapperRelatedContactTypeVal',
'mapperRelatedContactDetails' => 'mapperRelatedContactDetailsVal',
'mapperRelatedContactLocType' => 'mapperRelatedContactLocTypeVal',
'mapperRelatedContactPhoneType' => 'mapperRelatedContactPhoneTypeVal',
'mapperRelatedContactImProvider' => 'mapperRelatedContactImProviderVal',
'mapperRelatedContactWebsiteType' => 'mapperRelatedContactWebsiteTypeVal',
);

foreach ($mapper as $key => $value) {
//set respective mapper value to null.
foreach (array_values($mapperPeroperties) as $perpertyVal) {
$$perpertyVal = NULL;
}

$fldName = CRM_Utils_Array::value(0, $mapper[$key]);
$header = array($this->_mapFields[$fldName]);
Expand All @@ -206,19 +158,19 @@ public function runImport(&$form, $timeout = 55) {
if ($selOne && is_numeric($selOne)) {
if ($fldName == 'url') {
$header[] = $websiteTypes[$selOne];
$mapperWebsiteTypesVal = $selOne;
$parserParameters['mapperWebsiteType'][$key] = $selOne;
}
else {
$header[] = $locationTypes[$selOne];
$mapperLocTypesVal = $selOne;
$parserParameters['mapperLocType'][$key] = $selOne;
if ($selTwo && is_numeric($selTwo)) {
if ($fldName == 'phone') {
$header[] = $phoneTypes[$selTwo];
$mapperPhoneTypesVal = $selTwo;
$parserParameters['mapperPhoneType'][$key] = $selTwo;
}
elseif ($fldName == 'im') {
$header[] = $imProviders[$selTwo];
$mapperImProvidersVal = $selTwo;
$parserParameters['mapperImProvider'][$key] = $selTwo;
}
}
}
Expand All @@ -237,54 +189,49 @@ public function runImport(&$form, $timeout = 55) {
$relationType = new CRM_Contact_DAO_RelationshipType();
$relationType->id = $id;
$relationType->find(TRUE);
$mapperRelatedContactTypeVal = $relationType->{"contact_type_$second"};
$parserParameters['relatedContactType'][$key] = $relationType->{"contact_type_$second"};

$mapperRelatedVal = $fldName;
$parserParameters['mapperRelated'][$key] = $fldName;
if ($selOne) {
$mapperRelatedContactDetailsVal = $selOne;
$parserParameters['relatedContactDetails'][$key] = $selOne;
if ($selTwo) {
if ($selOne == 'url') {
$header[] = $websiteTypes[$selTwo];
$mapperRelatedContactWebsiteTypeVal = $selTwo;
$parserParameters[$key]['relatedContactWebsiteType'][$key] = $selTwo;
}
else {
$header[] = $locationTypes[$selTwo];
$mapperRelatedContactLocTypeVal = $selTwo;
$parserParameters['relatedContactLocType'][$key] = $selTwo;
if ($selThree) {
if ($selOne == 'phone') {
$header[] = $phoneTypes[$selThree];
$mapperRelatedContactPhoneTypeVal = $selThree;
$parserParameters['relatedContactPhoneType'][$key] = $selThree;
}
elseif ($selOne == 'im') {
$header[] = $imProviders[$selThree];
$mapperRelatedContactImProviderVal = $selThree;
$parserParameters['relatedContactImProvider'][$key] = $selThree;
}
}
}
}
}
}
$mapperFields[] = implode(' - ', $header);

//set the respective mapper param array values.
foreach ($mapperPeroperties as $mapperProKey => $mapperProVal) {
$this->{"_$mapperProKey"}[$key] = $$mapperProVal;
}
}

$this->_parser = new CRM_Contact_Import_Parser_Contact(
$this->_mapperKeys,
$this->_mapperLocTypes,
$this->_mapperPhoneTypes,
$this->_mapperImProviders,
$this->_mapperRelated,
$this->_mapperRelatedContactType,
$this->_mapperRelatedContactDetails,
$this->_mapperRelatedContactLocType,
$this->_mapperRelatedContactPhoneType,
$this->_mapperRelatedContactImProvider,
$this->_mapperWebsiteTypes,
$this->_mapperRelatedContactWebsiteType
$parserParameters['mapperLocType'],
$parserParameters['mapperPhoneType'],
$parserParameters['mapperImProvider'],
$parserParameters['mapperRelated'],
$parserParameters['relatedContactType'],
$parserParameters['relatedContactDetails'],
$parserParameters['relatedContactLocType'],
$parserParameters['relatedContactPhoneType'],
$parserParameters['relatedContactImProvider'],
$parserParameters['mapperWebsiteType'],
$parserParameters['relatedContactWebsiteType']
);

$this->_parser->run($this->_tableName, $mapperFields,
Expand Down
29 changes: 29 additions & 0 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2222,4 +2222,33 @@ protected function getPossibleContactMatches($params) {
return array($extIDMatch);
}

/**
* Format the form mapping parameters ready for the parser.
*
* @param int $count
* Number of rows.
*
* @return array $parserParameters
*/
public static function getParameterForParser($count) {
$baseArray = array();
for ($i = 0; $i < $count; $i++) {
$baseArray[$i] = NULL;
}
$parserParameters['mapperLocType'] = $baseArray;
$parserParameters['mapperPhoneType'] = $baseArray;
$parserParameters['mapperImProvider'] = $baseArray;
$parserParameters['mapperWebsiteType'] = $baseArray;
$parserParameters['mapperRelated'] = $baseArray;
$parserParameters['relatedContactType'] = $baseArray;
$parserParameters['relatedContactDetails'] = $baseArray;
$parserParameters['relatedContactLocType'] = $baseArray;
$parserParameters['relatedContactPhoneType'] = $baseArray;
$parserParameters['relatedContactImProvider'] = $baseArray;
$parserParameters['relatedContactWebsiteType'] = $baseArray;

return $parserParameters;

}

}

0 comments on commit 3b32e27

Please sign in to comment.