From 4f28e9fa7ecdecff8db16cca963b71ac024f6784 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 22 May 2018 12:52:59 -0400 Subject: [PATCH] Added ability for contacts to update their own E-Mail address and higher level users to update others. --- models/admin/contacts/index.php | 53 ++++++++++++++++++++++- models/admin/profile/index.php | 74 ++++++++++++++++++++++++++------- views/admin/contacts/edit.html | 38 +++++++++++++++-- views/admin/profile/index.html | 38 ++++++++++++++++- 4 files changed, 182 insertions(+), 21 deletions(-) diff --git a/models/admin/contacts/index.php b/models/admin/contacts/index.php index e8b13ca..a96072e 100644 --- a/models/admin/contacts/index.php +++ b/models/admin/contacts/index.php @@ -126,6 +126,8 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts $userDeleted = false; $wpUserDeleted = false; $contactMembers = array(); + $newEmail = false; + $newEmailError = false; $numbContacts = false; $numbDisplayed = false; $lastDisplayed = false; @@ -493,6 +495,54 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts $option = 'edit'; $view = 'edit.html'; + // Check for a new E-Mail address submission + if (trim($_REQUEST['new_email']) != '') { + + // Is it a valid format? + $newEmail = is_email(trim($_REQUEST['new_email'])); + if (!$newEmail) { + $newEmailError = 'BAD_FORMAT'; + $newEmail = $_REQUEST['new_email']; + } else { + + // Sanitize submitted address + $newEmail2 = sanitize_email($newEmail); + if ($newEmail != $newEmail2) { + $newEmailError = 'BAD_CHARACTERS'; + } else { + + // Check for existing contact in Wordpress and Contacts using the new address + $contactCheck = $this->checkContact($newEmail); + + if ($contactCheck['wordpressEmail'] || $contactCheck['contactsEmail']) { + $newEmailError = 'IN_USE'; + } else { + + // Update address for contact + $this->wpdb->query(" + UPDATE ".GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts + SET email = '$newEmail' + WHERE id = ".$contactID."; + "); + + + // Get the wordpress user ID + $wpUser = get_user_by('email', $contactInfo['fieldData']['email']); + + // Update address for WordPress user + if ($wpUser->ID > 0) { + $user_id = wp_update_user( array( 'ID' => $wpUser->ID, 'user_email' => $newEmail ) ); + } + + // Display new address in current form + $contactInfo['fieldData']['email'] = $newEmail; + $newEmail = ''; + + } + } + } + } + break; case 'delete': @@ -709,6 +759,8 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts 'userDeleted' => $userDeleted, 'wpUserDeleted' => $wpUserDeleted, 'contactMembers' => $contactMembers, + 'new_email' => $newEmail, + 'newEmailError' => $newEmailError, 'numbDisplayed' => $numbDisplayed, 'lastDisplayed' => $lastDisplayed, 'paging' => $paging, @@ -718,7 +770,6 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts 'limit' => $limit, 'namesList' => $namesList, 'EntityManagerRole' => $this->config['contact_role_numb']['EntityManager'] - ); // Return status, any suggested view, and any data to controller diff --git a/models/admin/profile/index.php b/models/admin/profile/index.php index cef0d9e..10c22ee 100644 --- a/models/admin/profile/index.php +++ b/models/admin/profile/index.php @@ -117,10 +117,12 @@ class GlmMembersAdmin_profile_index extends GlmDataContacts public function modelAction($actionData = false) { - $option = 'edit'; + $option = 'edit'; $contactUpdated = false; $contactMembers = false; - $membersList = false; + $membersList = false; + $newEmail = false; + $newEmailError = false; $view = 'admin/profile/index.html'; @@ -178,9 +180,51 @@ class GlmMembersAdmin_profile_index extends GlmDataContacts } - $option = 'edit'; + $option = 'edit'; + } + // Check for a new E-Mail address submission + if (trim($_REQUEST['new_email']) != '') { + + // Is it a valid format? + $newEmail = is_email(trim($_REQUEST['new_email'])); + if (!$newEmail) { + $newEmailError = 'BAD_FORMAT'; + $newEmail = $_REQUEST['new_email']; + } else { + + // Sanitize submitted address + $newEmail2 = sanitize_email($newEmail); + if ($newEmail != $newEmail2) { + $newEmailError = 'BAD_CHARACTERS'; + } else { + + // Check for existing contact in Wordpress and Contacts using the new address + $contactCheck = $this->checkContact($newEmail); + if ($contactCheck['wordpressEmail'] || $contactCheck['contactsEmail']) { + $newEmailError = 'IN_USE'; + } else { + + // Update address for contact + $this->wpdb->query(" + UPDATE ".GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts + SET email = '$newEmail' + WHERE id = ".$this->contactID."; + "); + + // Update address for WordPress user + if ($this->wpUserID > 0) { + $user_id = wp_update_user( array( 'ID' => $this->wpUserID, 'user_email' => $newEmail ) ); + } + + // Display new address in current form + $this->contactInfo['fieldData']['email'] = $newEmail; + $newEmail = ''; + + } + } + } } break; @@ -216,21 +260,23 @@ class GlmMembersAdmin_profile_index extends GlmDataContacts // Compile template data $templateData = array( - 'option' => $option, - 'contactID' => $this->contactID, - 'contactInfo' => $this->contactInfo, - 'contactUpdated' => $contactUpdated, - 'contactMembers' => $contactMembers, - 'membersList' => $membersList, - 'EntityManagerRole' => $this->config['contact_role_numb']['EntityManager'] + 'option' => $option, + 'contactID' => $this->contactID, + 'contactInfo' => $this->contactInfo, + 'contactUpdated' => $contactUpdated, + 'contactMembers' => $contactMembers, + 'membersList' => $membersList, + 'EntityManagerRole' => $this->config['contact_role_numb']['EntityManager'], + 'new_email' => $newEmail, + 'newEmailError' => $newEmailError ); // Return status, any suggested view, and any data to controller return array( - 'status' => true, - 'modelRedirect' => false, - 'view' => $view, - 'data' => $templateData + 'status' => true, + 'modelRedirect' => false, + 'view' => $view, + 'data' => $templateData ); } diff --git a/views/admin/contacts/edit.html b/views/admin/contacts/edit.html index 8cffdd1..9755165 100644 --- a/views/admin/contacts/edit.html +++ b/views/admin/contacts/edit.html @@ -76,7 +76,11 @@ {if $option == 'create'}

Add New Contact

{else} + {if $newEmailError != ''} +

NOTE: Your new E-Mail address was not valid. Please see below.

+ {else} {if $contactUpdated}

Contact Updated

{/if} + {/if} Delete this Contact
@@ -221,19 +225,45 @@ {if $contactInfo.fieldFail.access}

{$contactInfo.fieldFail.access}

{/if} - {if $option == 'create'} + Email Address:
NOTE: This field is only required for users who will have login privileges. {if $contactInfo.fieldFail.email}

{$contactInfo.fieldFail.email}

{/if} + {else} - Email Address:: - {$contactInfo.fieldData.email} - {/if} + + Email Address::{$contactInfo.fieldData.email} + + New Email Address: + + + {if $newEmailError == 'BAD_CHARACTERS'} +
+ The E-Mail address you submitted contained invalid characters. {$new_email} + Please check the address and submit again. + + {/if} + {if $newEmailError == 'BAD_FORMAT'} +
+ The E-Mail address you submitted is formatted incorrectly. An example of a correctly formatted E-Mail address is "name@domain.com". + Please check the address and submit again. + + {/if} + {if $newEmailError == 'IN_USE'} +
+ The E-Mail address you submitted is already in use for this site. + You may only change your address to one that is not currently in use. + + {/if} +
To change your E-Mail address, enter your new address here. This address must not be used by any other contact or user in this site. + + + {/if} {if $option == 'create'} diff --git a/views/admin/profile/index.html b/views/admin/profile/index.html index fa830d6..2815a64 100644 --- a/views/admin/profile/index.html +++ b/views/admin/profile/index.html @@ -2,13 +2,20 @@ {if apply_filters('glm_members_permit_admin_profile_index_edit_profile', true)} -

Why are there items below that I can't edit?
Some of the information below, such as your Username and Email address, are used by the system to identify your profile and may not be altered after the contact profile has been created. There are also certain items that relate to permissions you have to access and change other data.

- {if $contactUpdated}

Contact Updated

{/if} + {if $newEmailError != ''} +

NOTE: Your new E-Mail address was not valid. Please see below.

+ {else} + {if $contactUpdated} +

Contact Updated

+ {/if} + {/if} + +

 

@@ -47,7 +54,34 @@ Contact Type:{$contactInfo.fieldData.contact_type.name} Permissions:{$contactInfo.fieldData.contact_role.name} Display/Moderate/Archive:{$contactInfo.fieldData.access.name} + Email Address::{$contactInfo.fieldData.email} + + New Email Address: + + + {if $newEmailError == 'BAD_CHARACTERS'} +
+ The E-Mail address you submitted contained invalid characters. {$new_email} + Please check the address and submit again. + + {/if} + {if $newEmailError == 'BAD_FORMAT'} +
+ The E-Mail address you submitted is formatted incorrectly. An example of a correctly formatted E-Mail address is "name@domain.com". + Please check the address and submit again. + + {/if} + {if $newEmailError == 'IN_USE'} +
+ The E-Mail address you submitted is already in use for this site. + You may only change your address to one that is not currently in use. + + {/if} +
To change your E-Mail address, enter your new address here. This address must not be used by any other contact or user in this site. + + + Login Username:{$contactInfo.fieldData.username} Login password: -- 2.17.1