Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple values with same location without swapping primary keys #148

Open
wants to merge 4 commits into
base: 7.x-4.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions includes/wf_crm_webform_postprocess.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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][1]['location_type_id']) ? $contact[$location][1]['location_type_id'] : null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be written more concisely as

Suggested change
$is_primary_location_type = isset($contact[$location][1]['location_type_id']) ? $contact[$location][1]['location_type_id'] : null;
$is_primary_location_type = $contact[$location][1]['location_type_id'] ?? NULL;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw I made this change and it works fine.

But just to note, the is only available on PHP 7.x and I guess its ok for 7.x-5.x branch.


foreach ($contact[$location] as $i => $params) {
// Translate state/prov abbr to id
if (!empty($params['state_province_id'])) {
Expand All @@ -781,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']));
}
}
}
Expand All @@ -796,6 +793,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) {
Expand Down Expand Up @@ -828,17 +826,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);
}
Expand Down