Eileen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/364311 )

Change subject: CRM-20759 enable Primary as an import location type. Upstream 
Prs https://github.com/civicrm/civicrm-core/pull/10594 
https://github.com/civicrm/civicrm-core/pull/10565
......................................................................

CRM-20759 enable Primary as an import location type.
Upstream Prs
https://github.com/civicrm/civicrm-core/pull/10594
https://github.com/civicrm/civicrm-core/pull/10565

Note the PRs that were merged prior to 4.7.21 being released
I have already included in the upgrade commit

Primary goes to the default location type if the contact has no primary, or 
overwrites primary,
in keeping with profile behaviour

Change-Id: I16530b770ee0fe1fd00db87eca47ba6a5f8d7543
---
M CRM/Contact/BAO/Contact.php
M CRM/Contact/Import/Form/MapField.php
M CRM/Contact/Import/ImportJob.php
M CRM/Contact/Import/Parser/Contact.php
M CRM/Core/BAO/Mapping.php
5 files changed, 87 insertions(+), 57 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/11/364311/1

diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php
index 0f89c03..c5f82fc 100644
--- a/CRM/Contact/BAO/Contact.php
+++ b/CRM/Contact/BAO/Contact.php
@@ -3587,4 +3587,23 @@
     return $ids[0];
   }
 
+  /**
+   * Check if a field is associated with an entity that has a location type.
+   *
+   * (ie. is an address, phone, email etc field).
+   *
+   * @param string $fieldTitle
+   *   Title of the field (not the name - create a new function for that if 
required).
+   *
+   * @return bool
+   */
+  public static function isFieldHasLocationType($fieldTitle) {
+    foreach (CRM_Contact_BAO_Contact::importableFields() as $key => $field) {
+      if ($field['title'] === $fieldTitle) {
+        return CRM_Utils_Array::value('hasLocationType', $field);
+      }
+    }
+    return FALSE;
+  }
+
 }
diff --git a/CRM/Contact/Import/Form/MapField.php 
b/CRM/Contact/Import/Form/MapField.php
index 3d74293..838efaa 100644
--- a/CRM/Contact/Import/Form/MapField.php
+++ b/CRM/Contact/Import/Form/MapField.php
@@ -209,11 +209,10 @@
     else {
       $savedMapping = $this->get('savedMapping');
 
-      list($mappingName, $mappingContactType, $mappingLocation, 
$mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, 
$mappingValue, $mappingWebsiteType) = 
CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
+      list($mappingName, $mappingContactType, $mappingLocation, 
$mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, 
$mappingValue, $mappingWebsiteType) = 
CRM_Core_BAO_Mapping::getMappingFields($savedMapping, TRUE);
 
       //get loaded Mapping Fields
       $mappingName = CRM_Utils_Array::value(1, $mappingName);
-      $mappingContactType = CRM_Utils_Array::value(1, $mappingContactType);
       $mappingLocation = CRM_Utils_Array::value(1, $mappingLocation);
       $mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType);
       $mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider);
@@ -250,8 +249,7 @@
     $dataPatterns = $this->get('dataPatterns');
     $hasLocationTypes = $this->get('fieldTypes');
 
-    $this->_location_types = 
CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
-
+    $this->_location_types = array_merge(array('Primary' => ts('Primary')), 
CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'));
     $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
 
     // Pass default location to js
@@ -724,6 +722,7 @@
     $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');
+    $locationTypes['Primary'] = ts('Primary');
 
     for ($i = 0; $i < $this->_columnCount; $i++) {
 
@@ -735,7 +734,7 @@
       $mapperKeysMain[$i] = $fldName;
 
       //need to differentiate non location elements.
-      if ($selOne && is_numeric($selOne)) {
+      if ($selOne && (is_numeric($selOne) || $selOne === 'Primary')) {
         if ($fldName == 'url') {
           $parserParameters['mapperWebsiteType'][$i] = $websiteTypes[$selOne];
         }
@@ -805,8 +804,6 @@
     //Updating Mapping Records
     if (!empty($params['updateMapping'])) {
 
-      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 
'location_type_id');
-
       $mappingFields = new CRM_Core_DAO_MappingField();
       $mappingFields->mapping_id = $params['mappingId'];
       $mappingFields->find();
@@ -863,8 +860,9 @@
             elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') {
               $updateMappingFields->im_provider_id = isset($mapperKeys[$i][2]) 
? $mapperKeys[$i][2] : NULL;
             }
-            $location = array_keys($locationTypes, $locations[$i]);
-            $updateMappingFields->location_type_id = (isset($location) && 
isset($location[0])) ? $location[0] : NULL;
+            $locationTypeID = $parserParameters['mapperLocType'][$i];
+            // location_type_id is NULL for non-location fields, and for 
Primary location.
+            $updateMappingFields->location_type_id = 
is_numeric($locationTypeID) ? $locationTypeID : 'null';
           }
         }
         $updateMappingFields->save();
@@ -876,15 +874,11 @@
       $mappingParams = array(
         'name' => $params['saveMappingName'],
         'description' => $params['saveMappingDesc'],
-        'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
-          'Import Contact',
-          'name'
-        ),
+        'mapping_type_id' => 'Import Contact',
       );
 
-      $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
+      $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams);
 
-      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 
'location_type_id');
       $contactType = $this->get('contactType');
       switch ($contactType) {
         case CRM_Import_Parser::CONTACT_INDIVIDUAL:
@@ -901,7 +895,7 @@
 
       for ($i = 0; $i < $this->_columnCount; $i++) {
         $saveMappingFields = new CRM_Core_DAO_MappingField();
-        $saveMappingFields->mapping_id = $saveMapping->id;
+        $saveMappingFields->mapping_id = $saveMapping['id'];
         $saveMappingFields->contact_type = $cType;
         $saveMappingFields->column_number = $i;
 
@@ -925,12 +919,12 @@
             elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') {
               $saveMappingFields->im_provider_id = isset($mapperKeys[$i][3]) ? 
$mapperKeys[$i][3] : NULL;
             }
-            $saveMappingFields->location_type_id = isset($mapperKeys[$i][2]) ? 
$mapperKeys[$i][2] : NULL;
+            $saveMappingFields->location_type_id = (isset($mapperKeys[$i][2]) 
&& $mapperKeys[$i][2] !== 'Primary') ? $mapperKeys[$i][2] : NULL;
           }
         }
         else {
           $saveMappingFields->name = $mapper[$i];
-          $location_id = array_keys($locationTypes, $locations[$i]);
+          $locationTypeID = $parserParameters['mapperLocType'][$i];
           // to get phoneType id and provider id separately
           // before saving mappingFields of phone and IM, CRM-3140
           if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') {
@@ -943,7 +937,7 @@
             elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') {
               $saveMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? 
$mapperKeys[$i][2] : NULL;
             }
-            $saveMappingFields->location_type_id = isset($location_id[0]) ? 
$location_id[0] : NULL;
+            $saveMappingFields->location_type_id = is_numeric($locationTypeID) 
? $locationTypeID : NULL;
           }
           $saveMappingFields->relationship_type_id = NULL;
         }
diff --git a/CRM/Contact/Import/ImportJob.php b/CRM/Contact/Import/ImportJob.php
index 0b37b80..a8bb859 100644
--- a/CRM/Contact/Import/ImportJob.php
+++ b/CRM/Contact/Import/ImportJob.php
@@ -138,12 +138,12 @@
    */
   public function runImport(&$form, $timeout = 55) {
     $mapper = $this->_mapper;
-    $mapperFields;
+    $mapperFields = array();
     $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');
+    $locationTypes = array_merge(array('Primary' => ts('Primary')), 
CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'));
 
     foreach ($mapper as $key => $value) {
 
@@ -155,7 +155,8 @@
       $this->_mapperKeys[$key] = $fldName;
 
       //need to differentiate non location elements.
-      if ($selOne && is_numeric($selOne)) {
+      // @todo merge this with duplicate code on MapField class.
+      if ($selOne && (is_numeric($selOne) || $selOne === 'Primary')) {
         if ($fldName == 'url') {
           $header[] = $websiteTypes[$selOne];
           $parserParameters['mapperWebsiteType'][$key] = $selOne;
diff --git a/CRM/Contact/Import/Parser/Contact.php 
b/CRM/Contact/Import/Parser/Contact.php
index a4f101d..b93ac56 100644
--- a/CRM/Contact/Import/Parser/Contact.php
+++ b/CRM/Contact/Import/Parser/Contact.php
@@ -1675,7 +1675,6 @@
     //get the prefix id etc if exists
     CRM_Contact_BAO_Contact::resolveDefaults($formatted, TRUE);
 
-    require_once 'CRM/Utils/DeprecatedUtils.php';
     //@todo direct call to API function not supported.
     // setting required check to false, CRM-2839
     // plus we do our own required check in import
@@ -1953,7 +1952,7 @@
           }
 
           if (!$break) {
-            list($value, $formatted) = $this->formatContactParameters();
+            $this->formatContactParameters($value, $formatted);
           }
         }
         if (!$isAddressCustomField) {
@@ -2276,14 +2275,9 @@
     //      Custom
 
     // Cache the various object fields
-    static $fields = NULL;
-
-    if ($fields == NULL) {
-      $fields = array();
-    }
+    static $fields = array();
 
     // first add core contact values since for other Civi modules they are not 
added
-    require_once 'CRM/Contact/BAO/Contact.php';
     $contactFields = CRM_Contact_DAO_Contact::fields();
     _civicrm_api3_store_values($contactFields, $values, $params);
 
@@ -2415,50 +2409,50 @@
 
     // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
     if (!empty($values['location_type_id'])) {
-      static $fields = NULL;
-      if ($fields == NULL) {
-        $fields = array();
-      }
-
-      foreach (array(
-                 'Phone',
-                 'Email',
-                 'IM',
-                 'OpenID',
-                 'Phone_Ext',
-               ) as $block) {
-        $name = strtolower($block);
-        if (!array_key_exists($name, $values)) {
+      $blockTypes = array(
+        'phone' => 'Phone',
+        'email' => 'Email',
+        'im' => 'IM',
+        'openid' => 'OpenID',
+        'phone_ext' => 'Phone',
+      );
+      foreach ($blockTypes as $blockFieldName => $block) {
+        if (!array_key_exists($blockFieldName, $values)) {
           continue;
         }
 
-        if ($name == 'phone_ext') {
-          $block = 'Phone';
-        }
-
         // block present in value array.
-        if (!array_key_exists($name, $params) || !is_array($params[$name])) {
-          $params[$name] = array();
+        if (!array_key_exists($blockFieldName, $params) || 
!is_array($params[$blockFieldName])) {
+          $params[$blockFieldName] = array();
         }
 
         if (!array_key_exists($block, $fields)) {
           $className = "CRM_Core_DAO_$block";
-          $fields[$block] =& $className::fields();
+          $fields[$block] = $className::fields();
         }
 
-        $blockCnt = count($params[$name]);
+        $blockCnt = count($params[$blockFieldName]);
 
         // copy value to dao field name.
-        if ($name == 'im') {
-          $values['name'] = $values[$name];
+        if ($blockFieldName == 'im') {
+          $values['name'] = $values[$blockFieldName];
         }
 
         _civicrm_api3_store_values($fields[$block], $values,
-          $params[$name][++$blockCnt]
+          $params[$blockFieldName][++$blockCnt]
         );
 
+        if ($values['location_type_id'] === 'Primary') {
+          if (!empty($params['id'])) {
+            $primary = civicrm_api3($block, 'get', array('return' => 
'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 
'sequential' => 1));
+          }
+          $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
+          $values['location_type_id'] = (isset($primary) && $primary['count']) 
? $primary['values'][0]['location_type_id'] : $defaultLocationType->id;
+          $values['is_primary'] = 1;
+        }
+
         if (empty($params['id']) && ($blockCnt == 1)) {
-          $params[$name][$blockCnt]['is_primary'] = TRUE;
+          $params[$blockFieldName][$blockCnt]['is_primary'] = TRUE;
         }
 
         // we only process single block at a time.
@@ -2554,6 +2548,16 @@
         }
       }
 
+      if ($values['location_type_id'] === 'Primary') {
+        if (!empty($params['id'])) {
+          $primary = civicrm_api3('Address', 'get', array('return' => 
'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 
'sequential' => 1));
+        }
+        $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
+        $params['address'][$addressCnt]['location_type_id'] = (isset($primary) 
&& $primary['count']) ? $primary['values'][0]['location_type_id'] : 
$defaultLocationType->id;
+        $params['address'][$addressCnt]['is_primary'] = 1;
+
+      }
+
       if ($addressCnt == 1) {
 
         $params['address'][$addressCnt]['is_primary'] = TRUE;
diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php
index e64452f..4aeb934 100644
--- a/CRM/Core/BAO/Mapping.php
+++ b/CRM/Core/BAO/Mapping.php
@@ -130,10 +130,14 @@
    * @param int $mappingId
    *   Mapping id.
    *
+   * @param bool $addPrimary
+   *   Add the key 'Primary' when the field is a location field AND there is
+   *   no location type (meaning Primary)?
+   *
    * @return array
    *   array of mapping fields
    */
-  public static function getMappingFields($mappingId) {
+  public static function getMappingFields($mappingId, $addPrimary = FALSE) {
     //mapping is to be loaded from database
     $mapping = new CRM_Core_DAO_MappingField();
     $mapping->mapping_id = $mappingId;
@@ -149,6 +153,14 @@
       if (!empty($mapping->location_type_id)) {
         $mappingLocation[$mapping->grouping][$mapping->column_number] = 
$mapping->location_type_id;
       }
+      elseif ($addPrimary) {
+        if (CRM_Contact_BAO_Contact::isFieldHasLocationType($mapping->name)) {
+          $mappingLocation[$mapping->grouping][$mapping->column_number] = 
ts('Primary');
+        }
+        else {
+          $mappingLocation[$mapping->grouping][$mapping->column_number] = NULL;
+        }
+      }
 
       if (!empty($mapping->phone_type_id)) {
         $mappingPhoneType[$mapping->grouping][$mapping->column_number] = 
$mapping->phone_type_id;

-- 
To view, visit https://gerrit.wikimedia.org/r/364311
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I16530b770ee0fe1fd00db87eca47ba6a5f8d7543
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to