working on the become a member form
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 24 Apr 2018 18:44:43 +0000 (14:44 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 24 Apr 2018 18:44:43 +0000 (14:44 -0400)
Have it adding member, contact, custom fields, account,member_info.

models/admin/member/billing.php
models/front/billing/becomeMember.php
views/admin/billing/renew.html
views/front/billing/becomeMember.html
views/front/billing/thankyou.html [new file with mode: 0644]

index a6df1a3..726c838 100644 (file)
@@ -222,6 +222,10 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
             require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php';
             $account = $Accounts->editEntry( $accountID );
 
+            // Need to get the accounts
+            $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
+            $accounts = $Accounts->getSimpleAccountList( "T.boss <> true AND T.boss IS NOT NULL AND T.invoice_type != 0", 'ref_name' );
+
             break;
 
         case 'renewMembership':
@@ -468,7 +472,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
             // $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
             // Need to get the accounts
             $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
-            $accounts = $Accounts->getSimpleAccountList( "T.boss <> true AND T.boss IS NOT NULL", 'ref_name' );
+            $accounts = $Accounts->getSimpleAccountList( "T.boss <> true AND T.boss IS NOT NULL AND T.invoice_type != 0", 'ref_name' );
 
             if ( !$account ) {
                 if ( $accountID ) {
index 8456b92..84e4116 100644 (file)
@@ -144,43 +144,261 @@ class GlmMembersFront_billing_becomeMember // extends GlmDataBilling
             }
             // Process the main form
             // 1. Setup the member
-            $memberData = array(
-                'access'      => $access,
-                'member_type' => $memberType,
-                'created'     => date( 'Y-m-d' ),
-                'member_slug' => $memberSlug,
+            $member_fname = filter_var( $_REQUEST['fname'], FILTER_SANITIZE_STRING );
+            $member_lname = filter_var( $_REQUEST['lname'], FILTER_SANITIZE_STRING );
+            $member_name  = $member_lname . ', ' . $member_fname;
+            $invoice_type = filter_var( $_REQUEST['member_renewing'], FILTER_VALIDATE_INT );
+            // Member type is based on the renewing_member field (invoice_types)
+            $invoice_type_data = $BillingSupport->getInvoiceTypeById( $invoice_type );
+            if ( !$invoice_type_data ) {
+                $error = true;
+                $messages[] = '<span style="color:red;">An error occurred! invoice_type_data</span>';
+            }
+            echo '<pre>$invoice_type_data: ' . print_r( $invoice_type_data, true ) . '</pre>';
+            $member_type = $invoice_type_data['member_type'];
+            if ( !$member_type ) {
+                $error = true;
+                $messages[] = '<span style="color:red;">An error occurred! member_type</span>';
+            }
+            echo '<pre>$member_type: ' . print_r( $member_type, true ) . '</pre>';
+            // Start database transaction
+            // $this->wpdb->show_errors();
+            $this->wpdb->query('START TRANSACTION');
+            // $access = $this->config['access_numb']['Full'];
+            $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members',
+                array(
+                    'access'      => 40, // Not Moderated
+                    'member_type' => $member_type,
+                    'created'     => date( 'Y-m-d' ),
+                    'name'        => $member_name,
+                    'member_slug' => sanitize_title( $member_name ),
+                ),
+                array(
+                    '%d',// access
+                    '%d',// member_type
+                    '%s',// created
+                    '%s',// name
+                    '%s',// member_slug
+                )
             );
+            $member_id = $this->wpdb->insert_id;
+            echo '<pre>$member_id: ' . print_r( $member_id, true ) . '</pre>';
+            if ( !$member_id ) {
+                $error = true;
+                $messages[] = '<span style="color:red;">An error occurred! member_id</span>';
+            } else {
+                $member = $this->wpdb->get_row(
+                    $this->wpdb->prepare(
+                        "SELECT *
+                           FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                          WHERE id = %d",
+                        $member_id
+                    )
+                );
+                echo '<pre>$member: ' . print_r( $member, true ) . '</pre>';
+            }
             // 2. Setup the member_info
-            $memberInfoData = array(
-                'member',
-                'member_name',
-                'status',
-                'reference_name',
-                'descr',
-                'short_descr',
-                'addr1',
-                'addr2',
-                'city',
-                'state',
-                'country',
-                'zip',
-                'region',
-                'county',
-                'phone',
-                'toll_free',
-                'url',
-                'email',
-                'mailing_addr1',
-                'mailing_addr2',
-                'mailing_city',
-                'mailing_state',
-                'mailing_zip',
-                'create_time',
+            $billing_addr1    = filter_var( $_REQUEST['billing_addr1'] );
+            $billing_city     = filter_var( $_REQUEST['billing_city'] );
+            $billing_city_id  = $this->getCityId( $billing_city );
+            echo '<pre>$billing_city_id: ' . print_r( $billing_city_id, true ) . '</pre>';
+            $billing_state    = filter_var( $_REQUEST['billing_state'] );
+            $billing_zip      = filter_var( $_REQUEST['billing_zip'] );
+            $phone            = filter_var( $_REQUEST['phone'] );
+            $website          = filter_var( $_REQUEST['website'] );
+            $email_on_website = filter_var( $_REQUEST['email_on_website'] );
+
+            $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
+                array(
+                    'member'         => $member_id,
+                    'member_name'    => $member_name,
+                    'status'         => 10,
+                    'reference_name' => 'new member form',
+                    'addr1'          => $billing_addr1,
+                    'city'           => $billing_city_id,
+                    'state'          => $billing_state,
+                    'zip'            => $billing_zip,
+                    'phone'          => $phone,
+                    'url'            => $website,
+                    'email'          => $email_on_website,
+                    'create_time'    => date( 'Y-m-d' ),
+                ),
+                array(
+                    '%d', // member
+                    '%s', // member_name
+                    '%d', // status
+                    '%s', // reference_name
+                    '%s', // addr1
+                    '%d', // city
+                    '%s', // state
+                    '%s', // zip
+                    '%s', // phone
+                    '%s', // url
+                    '%s', // email
+                    '%s', // create_time
+                )
             );
+            $member_info_id = $this->wpdb->insert_id;
+            if ( !$member_info_id ) {
+                $error = true;
+                $messages[] = '<span style="color:red;">An error occurred! member_info_id</span>';
+            } else {
+                $member_info = $this->wpdb->get_row(
+                    $this->wpdb->prepare(
+                        "SELECT *
+                           FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info
+                          WHERE id = %d",
+                        $member_info_id
+                    )
+                );
+                echo '<pre>$member_info: ' . print_r( $member_info, true ) . '</pre>';
+            }
             // 3. Setup the contact (and wpUser)
+            // Determine the Worpress Role to be used for contact import - Using Entity Manager right now
+            $contactRoleNumb    = $this->config['contact_role_numb']['EntityManager'];
+            $wpRole             = $this->config['contact_role_wordpress'][$contactRoleNumb];
+            $memberContactEmail = filter_var( $_REQUEST['email'], FILTER_VALIDATE_EMAIL );
+            $memberLogin        = filter_var( $_REQUEST['username'] );
+            $this->wpdb->insert(
+                GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts',
+                array(
+                    'active'          => true,
+                    'primary_contact' => true,
+                    'access'          => $this->config['access_numb']['Full'],
+                    'fname'           => $member_fname,
+                    'lname'           => $member_lname,
+                    'contact_type'    => $this->config['contact_type_numb']['Personal'],
+                    'contact_role'    => $contactRoleNumb,
+                    'email'           => $memberContactEmail,
+                    'username'        => $memberLogin,
+                    'notes'           => 'Become Member Form.',
+                    'create_time'     => date('Y-m-d H:i:s', time()),
+                    'ref_type'        => $this->config['ref_type_numb']['Member'],
+                    'ref_dest'        => $member_id
+                ),
+                array(
+                    '%d', // active
+                    '%d', // primary_contact
+                    '%d', // access
+                    '%s', // fname
+                    '%s', // lname
+                    '%d', // contact_type
+                    '%d', // contact_role
+                    '%s', // email
+                    '%s', // username
+                    '%s', // notes
+                    '%s', // create_time
+                    '%d', // ref_type
+                    '%d', // ref_dest
+                )
+            );
+            $newContactID = $this->wpdb->insert_id;
+            if ( $newContactID ) {
+                $new_contact = $this->wpdb->get_row(
+                    $this->wpdb->prepare(
+                        "SELECT *
+                           FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+                          WHERE id = %d",
+                        $newContactID
+                    )
+                );
+                echo '<pre>$new_contact: ' . print_r( $new_contact, true ) . '</pre>';
+            } else {
+                // $this->wpdb->print_error();
+                $error = true;
+                $messages[] = '<span style="color:red;">An error occurred! newContactID</span>';
+            }
+            $memberPasswd = filter_var( $_REQUEST['password'] );
+            $wpUserID = wp_insert_user(
+                array(
+                    'user_email' => $memberContactEmail,
+                    'user_login' => $memberLogin,
+                    'user_pass'  => $memberPasswd,
+                    'first_name' => $member_fname,
+                    'last_name'  => $member_lname,
+                    'role'       => $wpRole
+                )
+            );
+            if (is_int($wpUserID) && $wpUserID > 0) {
+                // Store the contact ID and active status into user meta data.
+                update_user_meta($wpUserID, 'glmMembersContactID', $newContactID);
+                update_user_meta($wpUserID, 'glmMembersContactActive', true);
+                echo '<pre>$wpUserID: ' . print_r( $wpUserID, true ) . '</pre>';
+            } else {
+                $error = true;
+                $messages[] = '<span style="color:red;">An error occurred! wpUserID</span>';
+            }
             // 4. Setup the custom fields
-
             // Process any custom fields
+            if ( isset( $_REQUEST['cf'] ) && !empty( $_REQUEST['cf'] ) ) {
+                foreach ( $_REQUEST['cf'] as $customFieldKey => $customFieldValue ) {
+                    $customFieldKey   = filter_var( $customFieldKey, FILTER_VALIDATE_INT );
+                    $customFieldValue = filter_var( $customFieldValue );
+                    if ( $customFieldKey && $customFieldValue ) {
+                        $this->wpdb->insert(
+                            GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_data',
+                            array(
+                                'entity_id'  => $newContactID,
+                                'field_id'   => $customFieldKey,
+                                'field_data' => $customFieldValue
+                            ),
+                            array(
+                                '%d',
+                                '%d',
+                                '%s'
+                            )
+                        );
+                        $customFieldId = $this->wpdb->insert_id;
+                        if ( $customFieldId ) {
+                            echo '<pre>$customFieldId: ' . print_r( $customFieldId, true ) . '</pre>';
+                        } else {
+                            $error = true;
+                            $messages[] = '<span style="color:red;">An error occurred! customFieldId</span>';
+                        }
+                    }
+                }
+            }
+            // Setup billing account
+            $this->wpdb->insert(
+                GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+                array(
+                    'ref_dest'         => $member_id,
+                    'ref_name'         => $member_name,
+                    'anniversary_date' => date( 'Y-m-d' ),
+                    'boss'             => false,
+                    'invoice_type'     => $invoice_type,
+                    'billing_fname'    => $billing_fname,
+                    'billing_lname'    => $billing_lname,
+                    'billing_addr1'    => $billing_addr1,
+                    'billing_city'     => $billing_city,
+                    'billing_state'    => $billing_state,
+                    'billing_phone'    => $billing_phone,
+                    'email'            => $memberContactEmail,
+                ),
+                array(
+                    '%d', // ref_dest
+                    '%s', // ref_name
+                    '%s', // anniversary_date
+                    '%d', // boss
+                    '%d', // invoice_type
+                    '%s', // billing_fname
+                    '%s', // billing_lname
+                    '%s', // billing_addr1
+                    '%s', // billing_city
+                    '%s', // billing_state
+                    '%s', // billing_phone
+                    '%s', // email
+                )
+            );
+            // Save or rollback
+            if ( $error ) {
+                $this->wpdb->query('ROLLBACK');
+            } else {
+                $this->wpdb->query('COMMIT');
+                $view = 'thankyou';
+            }
             break;
 
         default:
@@ -217,5 +435,27 @@ class GlmMembersFront_billing_becomeMember // extends GlmDataBilling
 
     }
 
+    public function getCityId( $city_name )
+    {
+        // First try to get city id
+        $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 ) {
+            return $city_id;
+        }
+        $this->wpdb->insert(
+            GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
+            array( 'name' => $city_name ),
+            array( '%s' )
+        );
+        return $this->wpdb->insert_id;
+    }
+
 
 }
index 8b978a7..5ab1fec 100644 (file)
@@ -76,6 +76,7 @@
                     <strong>Associated Members/Employees</strong>
                 </div>
                 <div class="glm-billing-input">
+                    <input id="glm-billing-member-list" placeholder="Lookup Member">
                     <table>
                         {foreach $employees as $employee}
                             <tr>
 <script>
 jQuery(document).ready(function($){
 
+    var accounts = [ {foreach $accounts as $m} { label: "{$m.ref_name|unescape:'html'|replace:'"':''}", value: "{$m.ref_name|unescape:'html'|replace:'"':''}", id: '{$m.id}' }, {/foreach} ];
+
+    // Setup autocomplete for both inputs
+    $('#glm-billing-member-list').autocomplete({
+        source: accounts,
+        select: function( event, ui ){
+            addEmployee( ui.item );
+            $('#glm-billing-member-list').val( '' );
+            return false;
+        },
+        change: function( event, ui) {
+            if( ui.item == null ) {
+                $('#glm-billing-member-list').val( '' );
+                return false;
+            }
+        },
+    });
+
+    function addEmployee( employee ) {
+        var html = '<div class="glm-billing-employee">';
+        html += '<input type="hidden" name="employees[' + employee.id + ']" value="1">';
+        html += employee.label;
+        html += '<span class="glm-billing-employee-del dashicons dashicons-trash glm-right"></span>';
+        html += '</div>';
+        $('#glm-billing-employees').append(html);
+    }
+
     $('#billing-invoice-select').change(function(){
         // Get the data-amount and set the amount being paid.
         $('#billing-amount').val( $(this).find('option:selected').data( 'amount') );
index 85fc4b9..f1b4cfe 100644 (file)
@@ -1,5 +1,5 @@
 {* This template is for MiGCSA Become a Member Form *}
-<h3>Membership Renewal</h3>
+<h3>Become a Member</h3>
 <div id="billing-payment-form" class="glm-billing-form">
 
     {if $paymentSuccess}<span class="glm-notice glm-flash-updated">Payment Completed</span>{/if}
diff --git a/views/front/billing/thankyou.html b/views/front/billing/thankyou.html
new file mode 100644 (file)
index 0000000..ca2a270
--- /dev/null
@@ -0,0 +1,3 @@
+{* Thank you page template *}
+<h3>Thank You</h3>
+<p>Welcome to MiGCSA. As a member ... </p>