Start working on the ajax for lead downloads.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 12 Jul 2016 20:54:11 +0000 (16:54 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 12 Jul 2016 20:54:11 +0000 (16:54 -0400)
Should be setup much like the other ajax stuff I setup on events.

models/admin/ajax/csvExport.php [new file with mode: 0644]
models/admin/leads/index.php
setup/validActions.php
views/admin/leads/index.html

diff --git a/models/admin/ajax/csvExport.php b/models/admin/ajax/csvExport.php
new file mode 100644 (file)
index 0000000..8ef85d5
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+/**
+ * Gaslight Media Members Leads Database
+ * CSV Output by admin-ajax
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+require_once GLM_MEMBERS_LEADS_PLUGIN_PATH . '/models/admin/leads/index.php';
+/**
+ * Steve Note...
+ *
+ * You can get to this using the following URL.
+ *
+ *  {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=csvExport&mystuff=THIS
+ *
+ * You should be able to do this as POST or GET and should be able to add and read additional parameters.
+ * I added a "mystuff" parameter to the URL above and it does output from the code in the
+ * modelAction() function below.
+ *
+ * To add another model under models/admin/ajax all you need to do is create it and add it to the
+ * setup/validActions.php file.
+ *
+ */
+
+// Load Members data abstract
+// require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php');
+
+/**
+ * This class performs the work of handling images passed to it via
+ * an AJAX call that goes through the WorPress AJAX Handler.
+ *
+ */
+class GlmMembersAdmin_ajax_csvExport extends GlmMembersAdmin_leads_index
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+}
index 9f03991..8dc961d 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
- * Gaslight Media Members Database
- * Admin Member User Profile
+ * Gaslight Media Member Leads Database
+ * Leads search, list, edit page
  *
  * PHP version 5.5
  *
@@ -84,8 +84,10 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
     public function modelAction($actionData = false)
     {
-        $option = false;
+        $option              = false;
         $user_can_edit_leads = false;
+        $wpUser              = $this->config['loggedInUser']['wpUser'];
+        $search_id           = false;
 
         // Get any provided option
         if ( isset( $_REQUEST['option'] ) ) {
@@ -107,33 +109,76 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
         if ( isset( $_REQUEST['search'] ) ) {
             $search = filter_var( $_REQUEST['search'], FILTER_VALIDATE_BOOLEAN);
         }
+        // See if they have a saved search to use.
+        $result = $this->wpdb->get_row(
+            $this->wpdb->prepare(
+                "SELECT *
+                   FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "searches
+                  WHERE user_id = %d",
+                $wpUser['ID']
+            ),
+            ARRAY_A
+        );
+        if ( $result ) {
+            $search_id = $result['id'];
+            if ( !isset( $_REQUEST['search'] ) ) {
+                $search_params = unserialize( $result['search'] );
+                $search = true;
+            }
+        }
 
         if ( isset( $search ) && $search ) {
             // Filter the $_POST variables from the search form.
-            // Verlify mm/dd/YYYY date format for the from and to dates.
-            $search_params = filter_var_array(
-                $_POST,
-                array(
-                    'company'   => FILTER_SANITIZE_STRING,
-                    'contact'   => FILTER_SANITIZE_STRING,
-                    'from_date' => array(
-                        'filter'  => FILTER_VALIDATE_REGEXP,
-                        'options' => array(
-                            'regexp' => '%([0-9]{2})/([0-9]{2})/([0-9]{4})%'
-                        )
-                    ),
-                    'to_date' => array(
-                        'filter'  => FILTER_VALIDATE_REGEXP,
-                        'options' => array(
-                            'regexp' => '%([0-9]{2})/([0-9]{2})/([0-9]{4})%'
+            // Verify mm/dd/YYYY date format for the from and to dates.
+            if ( !isset( $search_params ) ) {
+                $search_params = filter_var_array(
+                    $_POST,
+                    array(
+                        'company'   => FILTER_SANITIZE_STRING,
+                        'contact'   => FILTER_SANITIZE_STRING,
+                        'from_date' => array(
+                            'filter'  => FILTER_VALIDATE_REGEXP,
+                            'options' => array(
+                                'regexp' => '%([0-9]{2})/([0-9]{2})/([0-9]{4})%'
+                            )
+                        ),
+                        'to_date' => array(
+                            'filter'  => FILTER_VALIDATE_REGEXP,
+                            'options' => array(
+                                'regexp' => '%([0-9]{2})/([0-9]{2})/([0-9]{4})%'
+                            )
+                        ),
+                        'interests' => array(
+                            'filter' => FILTER_VALIDATE_BOOLEAN,
+                            'flags'  => FILTER_FORCE_ARRAY
                         )
-                    ),
-                    'interests' => array(
-                        'filter' => FILTER_VALIDATE_BOOLEAN,
-                        'flags'  => FILTER_FORCE_ARRAY
                     )
-                )
-            );
+                );
+            }
+
+            if ( $wpUser['ID'] ) {
+                $search_data = array(
+                    'title'        => '',
+                    'search'       => serialize( $search_params ),
+                    'user_id'      => $wpUser['ID'],
+                    'date_created' => date( 'Y-m-d' )
+                );
+                if ( $search_id ) {
+                    $this->wpdb->update(
+                        GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'searches',
+                        $search_data,
+                        array( 'id' => $search_id ),
+                        array( '%s', '%s', '%d', '%s' ),
+                        array( '%d' )
+                    );
+                } else  {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'searches',
+                        $search_data,
+                        array( '%s', '%s', '%d', '%s' )
+                    );
+                }
+            }
 
             // build the $where part
             $where_parts = array();
@@ -288,6 +333,11 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             );
             break;
 
+        case 'download':
+            echo '<pre>$_REQUEST: ' . print_r($_REQUEST, true) . '</pre>';
+            exit;
+            break;
+
         default:
             if ( $groups ) {
                 foreach ( $groups as $group ) {
index 33b89fb..ccba4e7 100644 (file)
@@ -32,6 +32,9 @@
 
 $glmMembersLeadsAddOnValidActions = array(
     'adminActions' => array(
+        'ajax'  => array(
+            'csvExport' => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
+        ),
         'leads' => array(
             'index' => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
             'edit'  => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
index 98ea4db..fe4eb14 100644 (file)
@@ -39,8 +39,9 @@
 </form>
 
 {if $leads}
+    <h4>Search Results:</h4>
     <form action="{$thisUrl}?page={$thisPage}">
-        <input type="submit" value="Download" />
+        <a href="{$thisUrl}?page={$thisPage}&option=download" class="button">Download</a>
     </form>
     <table class="glm-admin-table">
         <tr>