Setup city select and add new city for billing.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 3 Aug 2018 14:48:15 +0000 (10:48 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 3 Aug 2018 14:48:15 +0000 (10:48 -0400)
This is for the contact edit page.
Will add the new city if it is not in the database already.

classes/billingSupport.php
views/admin/billing/contact.html

index a0fb635..6321361 100644 (file)
@@ -1647,6 +1647,24 @@ class GlmBillingSupport
                 '%s', // zip
             );
         } else if ( $use_billing ) {
+            // Check to make sure the billing city is added to the city table
+            $city_name = filter_var( $_REQUEST['billing_city'] );
+            $city_id = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+                      WHERE name = %s",
+                    $city_name
+                )
+            );
+            if ( !$city_id ) {
+                // Add new city
+                $this->wpdb->insert(
+                    GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
+                    array( 'name' => $city_name ),
+                    array( '%s' )
+                );
+            }
             $billing_updated = array(
                 'email'            => filter_var( $_REQUEST['billing_email'] ),
                 'billing_company'  => filter_var( $_REQUEST['billing_company'] ),
index 3ea9058..466890d 100644 (file)
         <tr>
             <th class="{if $account.fieldRequired.billing_city} glm-required{/if}">Billing City</th>
             <td class="{if $account.fieldFail.billing_city} glm-form-bad-input{/if}" data-tabid="glm-billing-city">
-                <select name="billing_city"{if $account.fieldRequired.billing_city} required{/if}>
+                <select id="billingCity" name="billing_city"{if $account.fieldRequired.billing_city} required{/if}>
                     <option value=""></option>
                 {foreach $cities as $city}
                     <option value="{$city.name}"{if $city.name == $account.fieldData.billing_city} selected{/if}>{$city.name}</option>
                 {/foreach}
                 </select>
+                <!--  Add new city dialog -->
+                <div id="newBillingCityButton" class="newCityButton button button-secondary">Add a new City</div>
+                <div id="newBillingCityDialog" class="glm-dialog-box" title="Enter a New City">
+                    <table class="glm-admin-table">
+                        <tr>
+                            <th class="glm-required">City Name</th>
+                            <td id="newBillingCityNameTD" class="newCityNameTD">
+                                <input id="newBillingCityName" type="text" name="newBillingCityName" class="newCityName glm-form-text-input">
+                                <div id="newBillingCityNameRequired" class="newCityNameRequired"></div>
+                            </td>
+                        </tr>
+                    </table>
+                    <p><span class="glm-required">*</span> Required</p>
+                    <a id="newBillingCityCancel" class="newCityCancel button button-primary glm-right">Cancel</a>
+                    <input id="newBillingCitySubmit" type="submit" value="Add new City" class="newCitySubmit">
+                </div>
+                <!-- City Selection -->
+                <input id="billingCityName" type="hidden" name="newBillingCityName" value=""><!-- this field is only used if adding a new city to pass the new name -->
+
             {if $account.fieldFail.billing_city}<p>{$account.fieldFail.billing_city}</p>{/if}<br>
+
             </td>
         </tr>
 
@@ -249,7 +269,8 @@ jQuery(document).ready(function($){
         $('input[name=billing_country]').prop('disabled', true);
         $('input[name=billing_phone]').prop('disabled', true);
         $('input[name=billing_fax]').prop('disabled', true);
-        console.log( 'billing form disabled' );
+        // Disable click event for add city
+        $('#newBillingCityButton').off('click');
     }
     // Enable the billing form
     function enableBillingForm()
@@ -267,6 +288,100 @@ jQuery(document).ready(function($){
         $('input[name=billing_country]').prop('disabled', false);
         $('input[name=billing_phone]').prop('disabled', false);
         $('input[name=billing_fax]').prop('disabled', false);
+        // Add a new city button action - pop-up dialog
+        $('#newBillingCityButton').click( function() {
+            $("#newBillingCityDialog").dialog("open");
+        });
+
     }
+
+
+    /*
+     * New Billing City Dialog
+     */
+
+    // Setup dialog box for adding a new city
+    $("#newBillingCityDialog").dialog({
+        autoOpen: false,
+        minWidth: 400,
+        dialogClass: "glm-dialog-no-close"
+    });
+    $('#newBillingCityCancel').click( function() {
+        // Clear new city name from form
+        $('#newBillingCityName').val('');
+        $('#newBillingCityNameTD').removeClass('glm-form-bad-input');
+        $('#newBillingCityNameRequired').text('');
+
+        $("#newBillingCityDialog").dialog("close");
+    });
+
+    // Submit new city
+    var newBillingCityAdded = false;
+    $('#newBillingCitySubmit').click( function() {
+
+        // Get new city name
+        var newBillingCityName = $('#newBillingCityName').val();
+        newBillingCityName = newBillingCityName.replace(/"/g, '');
+
+        // If no name is supplied, notify used it's required
+        if (newBillingCityName == '') {
+            $('#newBillingCityNameTD').addClass('glm-form-bad-input');
+            $('#newBillingCityNameRequired').text('A city name is required!');
+            return false;
+        }
+
+        // If the city name is already in the list then don't add it!
+        var nameFoundInBillingCity = false;
+        $('#billingCity option').each(function(){
+            console.log( 'newBillingCityName', newBillingCityName );
+            console.log( 'this.value', this.value );
+            console.log( 'matches: ', newBillingCityName == this.value );
+            if ( newBillingCityName == this.value ) {
+                // found the city in list already
+                // Set the select to that value
+                $('#billingCity').val(newBillingCityName);
+
+                // Reset add city form and close
+                $('#newBillingCityName').val('');
+                $('#newBillingCityNameTD').addClass('glm-form-bad-input');
+                $('#newBillingCityNameRequired').text('Already added');
+                nameFoundInBillingCity = true;
+                return false;
+            }
+        });
+
+        if ( nameFoundInBillingCity ) {
+            return false;
+        }
+
+
+        // Add new city name to the hidden field that will pass the new name to PHP.
+        $('#billingCityName').val(newBillingCityName);
+
+        // Add new city name to picklist and for storing - Only one permitted per submission
+        if (newBillingCityAdded) {
+
+            // New city already added, so just update the name and select that one
+            $('#billingCity option[value=' + newBillingCityAdded + ']').val(newBillingCityName);
+            $('#billingCity option:selected').text(newBillingCityName);
+
+        } else {
+
+            // Add the new city name to the city picklist
+            $('#billingCity').append('<option value="' + newBillingCityName + '">' + newBillingCityName + '</option>');
+            $('#billingCity').val(newBillingCityName);
+            $('#newBillingCityNameTD').append('<input type="hidden" name="newBillingCity" value="' + newBillingCityName + '">');
+            newBillingCityAdded = newBillingCityName;
+
+        }
+
+        // Clear new city name from form
+        $('#newBillingCityName').val('');
+        $('#newBillingCityNameTD').removeClass('glm-form-bad-input');
+        $('#newBillingCityNameRequired').text('');
+
+        $('#newBillingCityDialog').dialog('close');
+
+    });
 });
 </script>