From 1881b7b0f02ae69a70ef8bc91e0d0796da21512e Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 24 Jun 2017 10:09:17 +1200 Subject: [PATCH] Cleanup towards CRM-20759, define variables more legibly & consistently. I really want the 2 copy & paste variants of this code loop to be combinable but I think it takes a numebr of small changes to get there in a readable way --- CRM/Contact/Import/Form/MapField.php | 105 ++++++++------------------ CRM/Contact/Import/ImportJob.php | 103 ++++++------------------- CRM/Contact/Import/Parser/Contact.php | 29 +++++++ 3 files changed, 85 insertions(+), 152 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 9ba6188f00..3d74293792 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -717,39 +717,15 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @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]); @@ -761,17 +737,17 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { //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]; } } } @@ -782,19 +758,19 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { 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]; } } } @@ -803,44 +779,25 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $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)); @@ -995,11 +952,11 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $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'); diff --git a/CRM/Contact/Import/ImportJob.php b/CRM/Contact/Import/ImportJob.php index 7c278e6380..0b37b80c2b 100644 --- a/CRM/Contact/Import/ImportJob.php +++ b/CRM/Contact/Import/ImportJob.php @@ -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; @@ -101,25 +90,6 @@ class CRM_Contact_Import_ImportJob { } $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(); - } } /** @@ -168,32 +138,14 @@ class CRM_Contact_Import_ImportJob { */ 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]); @@ -206,19 +158,19 @@ class CRM_Contact_Import_ImportJob { 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; } } } @@ -237,27 +189,27 @@ class CRM_Contact_Import_ImportJob { $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; } } } @@ -265,26 +217,21 @@ class CRM_Contact_Import_ImportJob { } } $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, diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 2c30bc3c68..961fb57bb4 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -2220,4 +2220,33 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { 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; + + } + } -- 2.25.1