From 77ef27f9f90889fad0a6bb474fa6436fba4bbe9e Mon Sep 17 00:00:00 2001 From: Beto Aveiga Date: Fri, 13 Jul 2018 07:06:30 -0500 Subject: [PATCH 1/4] improve definition of $is_primary_location_type and param is_primary --- includes/wf_crm_webform_postprocess.inc | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/includes/wf_crm_webform_postprocess.inc b/includes/wf_crm_webform_postprocess.inc index 739c25d4d..a24c5588b 100644 --- a/includes/wf_crm_webform_postprocess.inc +++ b/includes/wf_crm_webform_postprocess.inc @@ -747,10 +747,6 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { * @param int $c */ private function saveContactLocation($contact, $cid, $c) { - // Check which location_type_id is to be set as is_primary=1; - $is_primary_address_location_type = wf_crm_aval($contact, 'address:1:location_type_id'); - $is_primary_email_location_type = wf_crm_aval($contact, 'email:1:location_type_id'); - foreach (wf_crm_location_fields() as $location) { if (!empty($contact[$location])) { $existing = array(); @@ -764,6 +760,9 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { // start array index at 1 $existing = array_merge(array(array()), $result['values']); } + // Check which location_type_id is to be set as is_primary=1; + $is_primary_location_type = isset($contact[$location]) ? $contact[$location][1]['location_type_id'] : null; + foreach ($contact[$location] as $i => $params) { // Translate state/prov abbr to id if (!empty($params['state_province_id'])) { @@ -828,17 +827,10 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { continue; } if ($location != 'website') { - if (empty($params['location_type_id'])) { - $params['location_type_id'] = wf_crm_aval($existing, "$i:location_type_id", 1); - } - $params['is_primary'] = $i == 1 ? 1 : 0; - // Override that just in cases we know - if ($location == 'address' && $params['location_type_id'] == $is_primary_address_location_type) { - $params['is_primary'] = 1; - } - if ($location == 'email' && $params['location_type_id'] == $is_primary_email_location_type) { - $params['is_primary'] = 1; - } + $params['location_type_id'] = empty($params['location_type_id']) ? wf_crm_aval($existing, "$i:location_type_id", 1) : $params['location_type_id']; + // Modify is_primary parameter if location type of the current field is the same + // type of the first field, assuring only first field to be set as primary + $params['is_primary'] = (int) ($params['location_type_id'] == $is_primary_location_type && $i == 1); } wf_civicrm_api($location, 'create', $params); } From f69bafce7a2e8459106a38a70c2b5ad70a63b406 Mon Sep 17 00:00:00 2001 From: Beto Aveiga Date: Fri, 13 Jul 2018 07:09:23 -0500 Subject: [PATCH 2/4] break once the first difference is found --- includes/wf_crm_webform_postprocess.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/wf_crm_webform_postprocess.inc b/includes/wf_crm_webform_postprocess.inc index a24c5588b..50a741c7d 100644 --- a/includes/wf_crm_webform_postprocess.inc +++ b/includes/wf_crm_webform_postprocess.inc @@ -795,6 +795,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { foreach ($params as $param => $val) { if ($val != (string) wf_crm_aval($existing[$i], $param, '')) { $same = FALSE; + break; } } if ($same) { From e7ecd3909abaf35200b4f3279861cfbf98d9e532 Mon Sep 17 00:00:00 2001 From: Beto Aveiga Date: Fri, 13 Jul 2018 07:17:27 -0500 Subject: [PATCH 3/4] use a existent drupal function to get user by email --- includes/wf_crm_webform_postprocess.inc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/includes/wf_crm_webform_postprocess.inc b/includes/wf_crm_webform_postprocess.inc index 50a741c7d..cbb47afd4 100644 --- a/includes/wf_crm_webform_postprocess.inc +++ b/includes/wf_crm_webform_postprocess.inc @@ -780,12 +780,10 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { $uid = wf_crm_user_cid($cid, 'contact'); if ($uid) { $user = user_load($uid); - if ($params['email'] != $user->mail) { - // Verify this email is unique before saving it to user - $args = array(':mail' => $params['email']); - if (!(db_query("SELECT count(uid) FROM {users} WHERE mail = :mail", $args)->fetchField())) { - user_save($user, array('mail' => $params['email'])); - } + // Verify this email is different from current user email + // and unique between Drupal users before updating it + if ($params['email'] != $user->mail && !user_load_by_mail($params['email']) ) { + user_save($user, array('mail' => $params['email'])); } } } From cc8824d498d3893d3b53f738e4773750259af613 Mon Sep 17 00:00:00 2001 From: Beto Aveiga Date: Mon, 6 Aug 2018 10:15:02 -0500 Subject: [PATCH 4/4] fix check of location type id for primary --- includes/wf_crm_webform_postprocess.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/wf_crm_webform_postprocess.inc b/includes/wf_crm_webform_postprocess.inc index cbb47afd4..13b6be321 100644 --- a/includes/wf_crm_webform_postprocess.inc +++ b/includes/wf_crm_webform_postprocess.inc @@ -761,7 +761,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { $existing = array_merge(array(array()), $result['values']); } // Check which location_type_id is to be set as is_primary=1; - $is_primary_location_type = isset($contact[$location]) ? $contact[$location][1]['location_type_id'] : null; + $is_primary_location_type = isset($contact[$location][1]['location_type_id']) ? $contact[$location][1]['location_type_id'] : null; foreach ($contact[$location] as $i => $params) { // Translate state/prov abbr to id