Fixing contact create form submission issues.
authorChuck Scott <cscott@gaslightmedia.com>
Fri, 3 Jun 2016 16:51:04 +0000 (12:51 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Fri, 3 Jun 2016 16:51:04 +0000 (12:51 -0400)
classes/data/dataContacts.php
models/admin/contacts/index.php
views/admin/contacts/edit.html
views/admin/contacts/index.html

index 77e49b9..c1272f3 100644 (file)
@@ -172,7 +172,7 @@ class GlmDataContacts extends GlmDataAbstract
                 'type' => 'list',
                 'list' => $this->config['contact_role'],
                 'required' => true,
-                'default' => $this->config['contact_role_numb']['RestrictedContact'],
+                'default' => $this->config['contact_role_numb']['EntityManager'],
                 'force_list' => true,
                 'view_only' => $limitedEdit,
                 'use' => 'a'
@@ -614,6 +614,7 @@ class GlmDataContacts extends GlmDataAbstract
             'id' => $savedFields['id'],
             'fname' => $savedFields['fname'],
             'lname' => $savedFields['lname'],
+            'username' => $savedFields['username'],
             'ref_dest' => $savedFields['ref_dest'],
             'ref_dest_name' => $savedFields['ref_dest_name']
         );
index 9234270..1c132c8 100644 (file)
@@ -92,11 +92,13 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
         $memberID = false;
         $memberData = false;
         $memberName = false;
+        $membersList = false;
         $contactsList = false;
         $haveContacts = false;
         $contactID = false;
         $contactInfo = false;
-        $newContactExists = false;
+        $newContactEmailExists = false;
+        $newContactUsernameExists = false;
         $misMatchedWpUsers = false;
         $newContactCreated = false;
         $usingExistingWPContact = false;
@@ -148,7 +150,7 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
 
         }
 
-        // If we're coming from the Member Events Menu/Tab
+        // If we're coming from the Member Contacts Menu/Tab
         if (defined('GLM_CONTACTS_MEMBER_MENU')) {
 
             $fromMemberMenu = true;
@@ -167,10 +169,12 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
 
         }
 
+        // Load members data class
+        require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMembers.php');
+        $Members = new GlmDataMembers($this->wpdb, $this->config);
+
         // If we have a member ID - Get Member information
         if ($memberID) {
-            require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMembers.php');
-            $Members = new GlmDataMembers($this->wpdb, $this->config);
             $memberData = $Members->getEntry($memberID);
         }
 
@@ -200,6 +204,11 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
                 $EasyPassword = new EasyPassword();
                 $contactInfo['fieldData']['password'] = $EasyPassword->generateEasyPassword('firstlast');
 
+                // If we don't have a member, the get a list of member IDs for selection
+                if (!$haveMember) {
+                    $membersList = $Members->getList('TRUE', 'name');
+                }
+
                 $view = 'edit.html';
 
                 break;
@@ -215,23 +224,30 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
                 $contactCheck = $this->checkContact($_REQUEST['email'], $_REQUEST['username']);
 
                 // If there's already a contact with this E-Mail address, don't create the contact
-                if ($contactCheck['contactsEmail'] || $contactCheck['contactsUsername']) {
-                    $newContactExists = true;
-                    break;
+                if ((trim($_REQUEST['email']) != '' && $contactCheck['contactsEmail'])) {
+                    $newContactEmailExists = true;
+                    $_POST['email'] = '';
+                    $_REQUEST['email'] = '';
+                }
+                if ((trim($_REQUEST['username']) != '' && $contactCheck['contactsUsername'])) {
+                    $newContactUsernameExists = true;
+                    $_POST['username'] = '';
+                    $_REQUEST['username'] = '';
                 }
 
-                // If there is already a WordPress user with the requested Email address and a different user with the requeted Username
+                // If there is already a WordPress user with the requested Email address and a different user with the requested Username
                 if ($contactCheck['wordpressEmail'] && $contactCheck['wordpressLogin']
                     && $contactCheck['wpUserEmail']->ID != $contactCheck['wpUserLogin']->ID) {
                     $misMatchedWpUsers = true;
-                    break;
+                    $_POST['username'] = '';
+                    $_REQUEST['username'] = '';
                 }
 
                 // Try to insert the new contact
                 $contactInfo = $this->insertEntry();
 
                 // If that was successful
-                if ($contactInfo['status']) {
+                if ($contactInfo['status'] && !$newContactEmailExists && !$newContactUsernameExists && !$misMatchedWpUsers) {
 
                     // If there's an existing WordPress user matching the E-Mail address but that has a different username
                     if ($contactCheck['wordpressEmail']
@@ -319,12 +335,28 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
                 $contactInfo['fieldData']['password'] = $_REQUEST['password'];
                 $refType = $_REQUEST['ref_type'];
                 $refTypeName = $this->config['ref_type'][$refType];
-                $view = 'edit.html';
+                $view = 'add.html';
 
                 // addNew was unsuccessful, fall through to edit
 
             case 'edit':
 
+                // Check for no member selected
+                if (!$haveMember) {
+
+                    // Get members list for selection
+                    $membersList = $Members->getList('TRUE', 'name');
+
+                    // If ref_dest is submitted, set that to default
+                    if (isset($_REQUEST['ref_dest'])) {
+                        $refDest = $_REQUEST['ref_dest']-0;
+                        if ($refDest && isset($membersList[$refDest])) {
+                            $membersList[$refDest]['selected'] = true;
+                        }
+                    }
+
+                }
+
                 // Get contact ID to edit
                 if (isset($_REQUEST['contact']) && ($_REQUEST['contact'] -0) > 0) {
 
@@ -332,14 +364,30 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
                     $contactID = $_REQUEST['contact'] - 0;
                     $contactInfo = $this->editEntry($contactID);
 
-                    // If the contact wasn't found, then set ID to false
-                    if (!$contactInfo['status']) {
-                        $contactID = false;
+                    // Check if we have a good member ID
+                    if ($contactInfo['fieldData']['ref_dest']) {
+
+                        // Get member information
+                        $memberData = $Members->getEntry($contactInfo['fieldData']['ref_dest']);
+
+                        // Check that we have good member data
+                        if (is_array($memberData) && isset($memberData['id']) && $memberData['id'] > 0) {
+                            $memberID = $memberData['id'];
+                            $haveMember = true;
+                            $memberName = $memberData['name'];
+                        }
+
                     }
 
-                    $view = 'edit.html';
                 }
 
+                // If the contact wasn't found, then set ID to false
+                if (!$contactInfo['status']) {
+                    $contactID = false;
+                }
+
+                $view = 'edit.html';
+
                 break;
 
             case 'submit':
@@ -354,6 +402,21 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
 
                 $contactInfo = $this->updateEntry($contactID);
 
+                // Check if we have a good member ID
+                if ($contactInfo['fieldData']['ref_dest']) {
+
+                    // Get member information
+                    $memberData = $Members->getEntry($contactInfo['fieldData']['ref_dest']);
+
+                    // Check that we have good member data
+                    if (is_array($memberData) && isset($memberData['id']) && $memberData['id'] > 0) {
+                        $memberID = $memberData['id'];
+                        $haveMember = true;
+                        $memberName = $memberData['name'];
+                    }
+
+                }
+
                 if ($contactInfo['status']) {
                     $contactInfo = $this->editEntry(($_REQUEST['id']-0));
                     $contactUpdated = true;
@@ -576,6 +639,7 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
             'memberID' => $memberID,
             'memberData' => $memberData,
             'memberName' => $memberName,
+            'membersList' => $membersList,
             'refType' => $refType,
             'refTypeName' => $refTypeName,
             'haveContacts' => $haveContacts,
@@ -583,7 +647,8 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts
             'numbContacts' => $numbContacts,
             'contactID' => $contactID,
             'contactInfo' => $contactInfo,
-            'newContactExists' => $newContactExists,
+            'newContactEmailExists' => $newContactEmailExists,
+            'newContactUsernameExists' => $newContactUsernameExists,
             'misMatchedWpUsers' => $misMatchedWpUsers,
             'usernameChangedToWP' => $usernameChangedToWP,
             'newContactCreated' => $newContactCreated,
index cd2e216..20a353e 100644 (file)
     </ul>
 {/if}            
 
+  {if $newContactEmailExists} 
+        <h4><span class="glm-error">NOTE:</span> The Email address for this contact is already in use. Please check if they already are a contact in this system.</h3>
+  {/if}
+  {if $newContactUsernameExists} 
+        <h4><span class="glm-error">NOTE:</span> The Login Username for this contact is already in use. Please check if they already are a contact in this system.</h3>
+  {/if}
+  {if $misMatchedWpUsers}
+        <h4><span class="glm-error">NOTE:</span> There is already a WordPress user with the requested Email address and a different user with the requested Login Username. Please select a different Login Username.</h3>        
+  {/if}
+        
+
 <!-- Check for invalid contact ID to edit -->
 {if $option == 'edit' && !$contactID}
 
         <h3><span class="glm-error">ERROR:</span> Specified contact not found!</h3>
-        
-  {elseif $newContactExists}
-  
-        <h4><span class="glm-warning">NOTE:</span> The Email address or username for this contact is already in use. Please check if they already are a contact in this system.</h3>        
 
   {elseif $misMatchedWpUsers}
   
             <input type="hidden" name="option" value="addNew">
             <input type="hidden" name="create_time" value="now">
             <input type="hidden" name="ref_type" value="{$refType}">
+          {if $haveMember}
             <input type="hidden" name="ref_dest" value="{$memberData.id}">
+          {/if}
         {else}
             <input type="hidden" name="option" value="submit">
             <input type="hidden" name="contact" value="{$contactInfo.fieldData.id}">
                     
             <table class="glm-admin-table glm-shrink">
                 <tr>
+        {if $haveMember}
                     <th>Contact For:</th>
-                    <td>{if $option=='create'}{$refTypeName}{else}{$contactInfo.fieldData.ref_type.name}{/if} - {$memberData.name}</td>
+                    <td>
+                        {$memberData.name}
+        {else}
+                    <th class="glm-required">Contact For:</th>
+                    <td {if $contactInfo.fieldFail.ref_dest}class="glm-form-bad-input"{/if}>
+                        <select name="ref_dest">
+                            <option value="">( Select a {$terms.term_member_cap} )</option>
+            {foreach $membersList as $m}
+                            <option value="{$m.id}"{if isset($m.selected)} selected="selected"{/if}>{$m.name}</option>
+            {/foreach}
+                        </select>
+        {/if}
+                        {if $contactInfo.fieldFail.ref_dest}<p>A {$terms.term_member_cap} was not selected.</p>{/if}
+                    </td>
                 </tr>
                 <tr>
                     <th>Active:</th>
                 <tr>
                     <th {if $contactInfo.fieldRequired.contact_role}class="glm-required"{/if}>Permissions:</th>
                     <td {if $contactInfo.fieldFail.contact_role}class="glm-form-bad-input"{/if}>
-                        <select name="contact_role">
+                        <select id="contactRoleSelect" name="contact_role">
             {foreach from=$contactInfo.fieldData.contact_role.list item=v}
                             <option value="{$v.value}"{if $v.default} selected="selected"{/if}>
                                 {$v.name}
                 </tr>
                 <tr>
             {if $option == 'create'}                
-                    <th {if $contactInfo.fieldRequired.email}class="glm-required"{/if}>Email Address:</th>
+                    <th id="emailRequiredForLoginContacts" class="glm-required">Email Address:</th>
                     <td {if $contactInfo.fieldFail.email}class="glm-form-bad-input"{/if}>
                         <input type="text" name="email" value="{$contactInfo.fieldData.email}" class="glm-form-text-input-medium" placeholder="(ex: name@domain.com)">
-                        <br><span class="glm-notice">NOTE:</span> This field is required for users who will have login privileges.
+                        <br><span class="glm-notice">NOTE:</span> This field is only required for users who will have login privileges.
                         {if $contactInfo.fieldFail.email}<p>{$contactInfo.fieldFail.email}</p>{/if}
                     </td>
             {else}
                 window.location.replace("{$thisUrl}?page=glm-members-admin-menu-contacts&glm_action=index&option=delete&contact={$contactID}");
 {/if}
             });
-
+            
+            // Set required status for E-Mail field based on permission settings
+            $('#contactRoleSelect').on('change', function() {
+                checkContactRoleSelect();
+            });
+            function checkContactRoleSelect() {
+                var role = $('#contactRoleSelect').val();
+                if (role < 40) {
+                    $('#emailRequiredForLoginContacts').addClass('glm-required');
+                } else {
+                    $('#emailRequiredForLoginContacts').removeClass('glm-required');
+                }
+            }
+            checkContactRoleSelect();
+            
             // Set masking for phone number fields - see http://digitalbush.com/projects/masked-input-plugin/
             $.mask.definitions['e'] = "[A-Za-z0-9: ]";
             $(".glm-phone-input").mask("999-999-9999? eeeeeeeeeee");
index 2094455..60eb773 100644 (file)
 
              var availableTags = [
     {foreach $namesList as $e}
-                { label: "{$e.fname|replace:'"':"'"} {$e.lname|replace:'"':"'"} {$e.ref_dest_name|replace:'"':"'"}", value: "{$e.fname|replace:'"':"'"} {$e.lname|replace:'"':"'"}, {$e.ref_dest_name|replace:'"':"'"}", id: '{$e.id}', member: '{$e.ref_dest}' },
+                { label: "{$e.fname|replace:'"':"'"} {$e.lname|replace:'"':"'"} {$e.ref_dest_name|replace:'"':"'"} ( username: {$e.username})", value: "{$e.fname|replace:'"':"'"} {$e.lname|replace:'"':"'"}, {$e.ref_dest_name|replace:'"':"'"}, {$e.username}", id: '{$e.id}', member: '{$e.ref_dest}' },
     {/foreach}
              ];