Setting up billing filters for contact billing edits.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 27 Jul 2018 19:13:18 +0000 (15:13 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 27 Jul 2018 19:13:18 +0000 (15:13 -0400)
Billing data is saved with contact edit billing form.

classes/billingSupport.php
models/admin/billing/contact.php [new file with mode: 0644]
setup/adminHooks.php
setup/validActions.php
views/admin/billing/contact.html [new file with mode: 0644]

index beb2483..1b3ca56 100644 (file)
@@ -1541,5 +1541,86 @@ class GlmBillingSupport
         );
     }
 
+    public function saveContactBillingData( $ref_dest )
+    {
+        $required_fields = $errors = array();
+        $billingAccount = new GlmDataAccounts( $this->wpdb, $this->config );
+        $billingFields  = $billingAccount->fields;
+        if ( $billingFields ) {
+            foreach( $billingFields as $field_name => $field ) {
+                if ( $field['required'] ) {
+                    $required_fields[] = $field_name;
+                }
+            }
+        }
+        // echo '<pre>$billingFields: ' . print_r( $billingFields, true ) . '</pre>';
+        // echo '<pre>$required_fields: ' . print_r( $required_fields, true ) . '</pre>';
+        // echo '<pre>$ref_dest: ' . print_r( $ref_dest, true ) . '</pre>';
+        // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+        // Check the given $ref_dest
+        $member_id = filter_var( $ref_dest, FILTER_VALIDATE_INT );
+        if ( $member_id ) {
+            // Get the members account
+            $account = $this->getAccountByRefDest( $member_id );
+            // echo '<pre>$account: ' . print_r( $account, true ) . '</pre>';
+
+            // New Billing Data If using Billing Fields
+            $billing_updated = array(
+                'email'            => filter_var( $_REQUEST['billing_email'] ),
+                'billing_company'  => filter_var( $_REQUEST['billing_company'] ),
+                'billing_position' => filter_var( $_REQUEST['billing_position'] ),
+                'billing_fname'    => filter_var( $_REQUEST['billing_fname'] ),
+                'billing_lname'    => filter_var( $_REQUEST['billing_lname'] ),
+                'billing_addr1'    => filter_var( $_REQUEST['billing_addr1'] ),
+                'billing_addr2'    => filter_var( $_REQUEST['billing_addr2'] ),
+                'billing_city'     => filter_var( $_REQUEST['billing_city'] ),
+                'billing_state'    => filter_var( $_REQUEST['billing_state'] ),
+                'billing_country'  => filter_var( $_REQUEST['billing_country'] ),
+                'billing_zip'      => filter_var( $_REQUEST['billing_zip'] ),
+                'billing_phone'    => filter_var( $_REQUEST['billing_phone'] ),
+                'billing_fax'      => filter_var( $_REQUEST['billing_fax'] ),
+            );
+            $billing_updated_format = array(
+                '%s', // email
+                '%s', // company
+                '%s', // position
+                '%s', // fname
+                '%s', // lname
+                '%s', // addr1
+                '%s', // addr2
+                '%s', // city
+                '%s', // state
+                '%s', // country
+                '%s', // zip
+                '%s', // phone
+                '%s', // fax
+            );
+            // echo '<pre>$billing_updated: ' . print_r( $billing_updated, true ) . '</pre>';
+            // Check for required fields
+            foreach ( $billing_updated as $field_name => $field_value ) {
+                $key = array_search( $field_name, $required_fields );
+                if ( $key !== false && !trim( $field_value ) ) {
+                    $errors[$field_name] = true;
+                }
+            }
+            // If the $errors array is empty then we can store the billing data
+            if ( empty( $errors ) ) {
+                if ( $account ) {
+                    $this->wpdb->update(
+                        GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+                        $billing_updated,
+                        array( 'id' => $account['id'] ),
+                        $billing_updated_format,
+                        array( '%d' )
+                    );
+                }
+            } else {
+                return $errors;
+            }
+        }
+        // echo '<pre>$errors: ' . print_r( $errors, true ) . '</pre>';
+        return true;
+    }
+
 }
 
diff --git a/models/admin/billing/contact.php b/models/admin/billing/contact.php
new file mode 100644 (file)
index 0000000..9eaf5cf
--- /dev/null
@@ -0,0 +1,144 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * Admin Transactions List
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @release  index.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
+ * @link     http://dev.gaslightmedia.com/
+ */
+
+// Load Contacts data class
+require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php';
+require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php';
+
+class GlmMembersAdmin_billing_contact extends GlmDataAccounts
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+    /**
+     * Transactions ID
+     *
+     * @var $account_id
+     * @access public
+     */
+    public $account_id = false;
+
+    /**
+     * Constructor
+     *
+     * This constructor performs the work for this model. This model returns
+     * an array containing the following.
+     *
+     * 'status'
+     *
+     * True if successful and false if there was a fatal failure.
+     *
+     * 'view'
+     *
+     * A suggested view name that the controller should use instead of the
+     * default view for this model or false to indicate that the default view
+     * should be used.
+     *
+     * 'data'
+     *
+     * Data that the model is returning for use in merging with the view to
+     * produce output.
+     *
+     * @wpdb object WordPress database object
+     *
+     * @return array Array containing status, suggested view, and any data
+     */
+    public function __construct ( $wpdb, $config )
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+        /*
+         * Run constructor for the Contacts data class
+         *
+         * Note, the third parameter is a flag that indicates to the Contacts
+         * data class that it should flag a group of fields as 'view_only'.
+         */
+        parent::__construct( false, false, true );
+
+    }
+
+    public function modelAction($actionData = false)
+    {
+
+        $option             = 'list';
+        $this->account_id   = false;
+        $ref_dest           = false;
+
+        // Get any provided option
+        if ( isset( $_REQUEST['option'] ) ) {
+            $option = $_REQUEST['option'];
+        }
+
+        // Get any provided Id
+        if ( isset( $actionData['id'] ) ) {
+            $ref_dest = filter_var( $actionData['id'], FILTER_VALIDATE_INT );
+        }
+
+        if ( $ref_dest ) {
+            $BillingSupport  = new GlmBillingSupport( $this->wpdb, $this->config );
+            $billing_account = $BillingSupport->getAccountByRefDest( $ref_dest );
+            $this->account_id = $billing_account['id'];
+        }
+
+        switch ($option) {
+
+        default:
+
+
+            break;
+
+        }
+
+        if ( $this->account_id ) {
+            // Get the billing data for the form
+            $account = $this->editEntry( $this->account_id );
+        } else {
+            $account = $this->newEntry();
+        }
+
+        $templateData = array(
+            'option'     => $option,
+            'account_id' => $this->account_id,
+            'account'    => $account,
+        );
+
+        // Return status, any suggested view, and any data to controller
+        return array(
+            'status'        => true,
+            'modelRedirect' => false,
+            'view'          => "admin/billing/contact.html",
+            'data'          => $templateData
+        );
+
+    }
+
+}
index 470c0d4..066ac8f 100644 (file)
@@ -96,3 +96,36 @@ add_filter(
         return array_merge( $cron_task, $new_cron );
     }
 );
+
+/**
+ * Filter returning the html for the billing field form (contact)
+ */
+add_filter(
+    'glm-members-billing-contact-form',
+    function( $content, $id ){
+        unset( $_REQUEST['glm_action'] );
+        $content .= $this->controller( 'billing', 'contact', array( 'id' => $id ) );
+        return $content;
+}, 10, 2);
+
+/**
+ * Returns true if plugin is enabled
+ */
+add_filter(
+    'glm-members-billing-enabled',
+    function( $content ){
+       return true;
+});
+
+/**
+ * Save the data for the billing info
+ * Returns Boolean or Array For Error
+ */
+add_filter(
+    'glm-members-billing-contact-save-data',
+    function( $ref_dest ){
+        require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php';
+        $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
+        return $BillingSupport->saveContactBillingData( $ref_dest );
+    }, 10, 1
+);
index e0b1806..0e761bc 100644 (file)
@@ -85,6 +85,7 @@ $glmMembersBillingAddOnValidActions = array(
             'payments' => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
             'accounts' => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
             'logs'     => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
+            'contact'  => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
         ),
         'member' => array(
             'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
diff --git a/views/admin/billing/contact.html b/views/admin/billing/contact.html
new file mode 100644 (file)
index 0000000..5f10978
--- /dev/null
@@ -0,0 +1,116 @@
+<table class="glm-admin-table glm-admin-table-inner">
+    <tbody>
+        <tr>
+            <th class="{if $account.fieldRequired.email} glm-required{/if}">Billing Email</th>
+            <td class="{if $account.fieldFail.email} glm-form-bad-input{/if}" data-tabid="glm-email">
+                <input type="text" name="billing_email" value="{$account.fieldData.email}" class="glm-form-text-input-medium"
+                {if $account.fieldRequired.email} {/if}>
+            {if $account.fieldFail.email}<p>{$account.fieldFail.email}</p>{/if}<br>
+            </td>
+        </tr>
+        <tr>
+            <th class="{if $account.fieldRequired.billing_company} glm-required{/if}">Billing Company</th>
+            <td class="{if $account.fieldFail.billing_company} glm-form-bad-input{/if}" data-tabid="glm-billing-company">
+                <input type="text" name="billing_company" value="{$account.fieldData.billing_company}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_company} required{/if}>
+            {if $account.fieldFail.billing_company}<p>{$account.fieldFail.billing_company}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_position} glm-required{/if}">Billing Position</th>
+            <td class="{if $account.fieldFail.billing_position} glm-form-bad-input{/if}" data-tabid="glm-billing-position">
+                <input type="text" name="billing_position" value="{$account.fieldData.billing_position}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_position} required{/if}>
+            {if $account.fieldFail.billing_position}<p>{$account.fieldFail.billing_position}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_fname} glm-required{/if}">Billing First Name</th>
+            <td class="{if $account.fieldFail.billing_fname} glm-form-bad-input{/if}" data-tabid="glm-billing-fname">
+                <input type="text" name="billing_fname" value="{$account.fieldData.billing_fname}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_fname} required{/if}>
+            {if $account.fieldFail.billing_fname}<p>{$account.fieldFail.billing_fname}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_lname} glm-required{/if}">Billing Last Name</th>
+            <td class="{if $account.fieldFail.billing_lname} glm-form-bad-input{/if}" data-tabid="glm-billing-lname">
+                <input type="text" name="billing_lname" value="{$account.fieldData.billing_lname}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_lname} required{/if}>
+            {if $account.fieldFail.billing_lname}<p>{$account.fieldFail.billing_lname}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_addr1} glm-required{/if}">Billing Address 1</th>
+            <td class="{if $account.fieldFail.billing_addr1} glm-form-bad-input{/if}" data-tabid="glm-billing-addr1">
+                <input type="text" name="billing_addr1" value="{$account.fieldData.billing_addr1}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_addr1} required{/if}>
+            {if $account.fieldFail.billing_addr1}<p>{$account.fieldFail.billing_addr1}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_addr2} glm-required{/if}">Billing Address 2</th>
+            <td class="{if $account.fieldFail.billing_addr2} glm-form-bad-input{/if}" data-tabid="glm-billing-addr2">
+                <input type="text" name="billing_addr2" value="{$account.fieldData.billing_addr2}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_addr2} required{/if}>
+            {if $account.fieldFail.billing_addr2}<p>{$account.fieldFail.billing_addr2}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <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">
+                <input type="text" name="billing_city" value="{$account.fieldData.billing_city}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_city} required{/if}>
+            {if $account.fieldFail.billing_city}<p>{$account.fieldFail.billing_city}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_state} glm-required{/if}">Billing State</th>
+            <td class="{if $account.fieldFail.billing_state} glm-form-bad-input{/if}" data-tabid="glm-billing-state">
+                <select name="billing_state"{if $account.fieldRequired.billing_state} required{/if}>
+                    <option value=""></option>
+                    {foreach $account.fieldData.billing_state.list as $s}
+                        <option value="{$s.value}"{if $account.fieldData.billing_state.value == $s.value} selected="selected"{/if}>
+                            {$s.name}
+                        </option>
+                    {/foreach}
+                </select>
+            {if $account.fieldFail.billing_state}<p>{$account.fieldFail.billing_state}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_zip} glm-required{/if}">Billing Zip</th>
+            <td class="{if $account.fieldFail.billing_zip} glm-form-bad-input{/if}" data-tabid="glm-billing-zip">
+                <input type="text" name="billing_zip" value="{$account.fieldData.billing_zip}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_zip} required{/if}>
+            {if $account.fieldFail.billing_zip}<p>{$account.fieldFail.billing_zip}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_country} glm-required{/if}">Billing Country</th>
+            <td class="{if $account.fieldFail.billing_country} glm-form-bad-input{/if}" data-tabid="glm-billing-country">
+                <input type="text" name="billing_country" value="{$account.fieldData.billing_country}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_country} required{/if}>
+            {if $account.fieldFail.billing_country}<p>{$account.fieldFail.billing_country}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_phone} glm-required{/if}">Billing Phone</th>
+            <td class="{if $account.fieldFail.billing_phone} glm-form-bad-input{/if}" data-tabid="glm-billing-phone">
+                <input type="text" name="billing_phone" value="{$account.fieldData.billing_phone}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_phone} required{/if}>
+            {if $account.fieldFail.billing_phone}<p>{$account.fieldFail.billing_phone}</p>{/if}<br>
+            </td>
+        </tr>
+
+        <tr>
+            <th class="{if $account.fieldRequired.billing_fax} glm-required{/if}">Billing Fax</th>
+            <td class="{if $account.fieldFail.billing_fax} glm-form-bad-input{/if}" data-tabid="glm-billing-fax">
+                <input type="text" name="billing_fax" value="{$account.fieldData.billing_fax}" class="glm-form-text-input-medium"{if $account.fieldRequired.billing_fax} required{/if}>
+            {if $account.fieldFail.billing_fax}<p>{$account.fieldFail.billing_fax}</p>{/if}<br>
+            </td>
+        </tr>
+
+    </tbody>
+</table>
+