Adding the source and mail_ok to search form
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Jul 2016 13:18:19 +0000 (09:18 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Jul 2016 13:18:19 +0000 (09:18 -0400)
Can now search by the Source (like contact types) and mail_ok field.

classes/data/dataLeadEntry.php
models/admin/leads/index.php
setup/frontHooks.php
views/admin/leads/index.html

index ef34334..ed7afe1 100644 (file)
@@ -115,6 +115,18 @@ class GlmDataLeadEntry extends GlmDataAbstract
                 'use'       => 'a',
             ),
 
+            'source_id' => array(
+                'field'      => 'source_id',
+                'type'       => 'pointer',
+                'p_table'    => GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
+                'p_field'    => 'title',
+                'p_orderby'  => 'title',
+                'p_blank'    => true,
+                'force_list' => true,
+                'required'   => true,
+                'use'        => 'a'
+            ),
+
             'lead_id' => array(
                 'field'      => 'lead_id',
                 'type'       => 'pointer',
index 6a56705..5868108 100644 (file)
@@ -99,7 +99,6 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             $option = 'csv';
         }
 
-
         // Get entry ID if supplied
         if (isset($_REQUEST['entry'])) {
 
@@ -111,10 +110,12 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             }
 
         }
+
         // Check for a search being done.
         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(
@@ -140,6 +141,7 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                 $search_params = filter_var_array(
                     $_POST,
                     array(
+                        'source_id' => FILTER_VALIDATE_INT,
                         'company'   => FILTER_SANITIZE_STRING,
                         'contact'   => FILTER_SANITIZE_STRING,
                         'from_date' => array(
@@ -154,6 +156,7 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                                 'regexp' => '%([0-9]{2})/([0-9]{2})/([0-9]{4})%'
                             )
                         ),
+                        'mail_ok' => FILTER_VALIDATE_BOOLEAN,
                         'interests' => array(
                             'filter' => FILTER_VALIDATE_BOOLEAN,
                             'flags'  => FILTER_FORCE_ARRAY
@@ -188,6 +191,12 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
             // build the $where part
             $where_parts = array();
+            if ( $search_params['mail_ok'] ) {
+                $where_parts[] = "T.lead_id IN (
+                    SELECT id
+                      FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads
+                     WHERE mail_ok = true)";
+            }
             if ( $this->config['loggedInUser']['contactUser'] ) {
                 $where_parts[] = "T.lead_id IN (
                     SELECT id
@@ -196,6 +205,9 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             } else {
                 $user_can_edit_leads = true;
             }
+            if ( $search_params['source_id'] ) {
+                $where_parts[] = "source_id = {$search_params['source_id']}";
+            }
             if ( $search_params['company'] ) {
                 $where_parts[] = "org = '" . esc_sql( $search_params['company'] ) . "'";
             }
@@ -254,10 +266,14 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             );
         }
 
+        // Initialize the interests array
         $interests = array();
+
+        // Fetch the groupData.
         $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config );
         $groups    = $groupData->getList();
 
+        // Initialize the grouped_interests array
         $grouped_interests = array();
 
         switch ( $option ) {
@@ -265,17 +281,10 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             // Initialize.
             $out             = array();
             $csv_file_output = '';
-            // First line should be the header line
-            $out = array(
-                'fname', 'lname', 'email', 'org',
-                'addr1', 'addr2', 'city', 'state', 'zip', 'country',
-                'phone', 'phone2', 'fax',
-                'date_submitted',
-            );
-            $csv_file_output = implode( ',', array_map( function($str){return sprintf( '"%s"', $str );}, $out ) ) . "\n";
 
             // Generate the output for the csv file
             if ( isset( $leads ) && is_array( $leads ) && !empty( $leads ) ) {
+                $lead_counter = 0;
                 foreach ( $leads as $entry_id => $lead ) {
                     $out = array(
                         'fname'          => $lead['fname'],
@@ -292,12 +301,44 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                         'phone2'         => $lead['phone2'],
                         'fax'            => $lead['fax'],
                         'date_submitted' => $lead['date_submitted'],
+                        'source'         => $lead['source_id']['name'],
+                    );
+                    $interest_by_groups = array();
+                    /*
+                     * Setup the interests so they are comma separated into a
+                     * grouped field. Group name will be the field name header.
+                     */
+                    $lead_interests = $this->wpdb->get_results(
+                        $this->wpdb->prepare(
+                            "SELECT li.interest_id, g.title as 'group', i.title
+                               FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests li
+                                    LEFT OUTER JOIN " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "interests i ON (i.id = li.interest_id)
+                                    LEFT OUTER JOIN " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "interest_groups g ON (i.group_id = g.id)
+                              WHERE lead_entry_id = %d",
+                            $lead['id']
+                        ),
+                        ARRAY_A
                     );
+                    if ( $lead_interests ) {
+                        foreach ( $lead_interests as $interest ) {
+                            $interest_by_groups[$interest['group']][] = $interest['title'];
+                        }
+                        if ( $interest_by_groups ) {
+                            foreach ( $interest_by_groups as $group_name => $group_interest ) {
+                                $out[$group_name] = implode( ';', $group_interest );
+                            }
+                        }
+                    }
+                    // First line should be the header line
+                    if ( $lead_counter === 0 ) {
+                        $csv_file_output = implode( ',', array_map( function($str){return sprintf( '"%s"', $str );}, array_keys( $out ) ) ) . "\n";
+                    }
                     /*
                      * remove any double quotes from the values and add double
                      * quotes around all values.
                      */
                     $csv_file_output .= implode( ',', array_map( function($str){return sprintf( '"%s"', str_replace( '"', '', $str ) );}, $out ) ) . "\n";
+                    ++$lead_counter;
                 }
             }
 
@@ -335,7 +376,6 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                     var_dump( $this->wpdb->insert_id );
                 }
             }
-            echo '<pre>$_POST: ' . print_r($_POST, true) . '</pre>';
 
         case 'edit':
             if ( $this->config['loggedInUser']['contactUser'] ) {
@@ -386,15 +426,9 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             $templateData = array(
                 'entry'             => $entry,
                 'lead_interests'    => $lead_interests,
-                'grouped_interests' => $grouped_interests,
             );
             break;
 
-        case 'download':
-            echo '<pre>$_REQUEST: ' . print_r($_REQUEST, true) . '</pre>';
-            exit;
-            break;
-
         default:
             if ( $groups ) {
                 foreach ( $groups as $group ) {
@@ -418,13 +452,29 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             $templateData = array(
                 'user_can_edit_leads' => $user_can_edit_leads,
                 'search_params'       => $search_params,
-                'request'             => print_r( $_REQUEST, true ),
                 'leads'               => $leads,
-                'grouped_interests'   => $grouped_interests,
             );
             break;
         }
 
+        // Fetch the sources
+        $sources = $this->wpdb->get_results(
+            "SELECT * FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources ORDER BY title",
+            ARRAY_A
+        );
+
+        if ( $sources ) {
+            foreach ( $sources as &$source ) {
+                $source['selected'] = ( isset( $search_params['source_id'] ) && $source['id'] == $search_params['source_id'] )
+                    ? 1
+                    : 0;
+            }
+        }
+
+        // Common things to place into the $templateData array
+        $templateData['sources']           = $sources;
+        $templateData['grouped_interests'] = $grouped_interests;
+
         // Return status, any suggested view, and any data to controller
         return array(
                 'status'        => true,
index 6a00168..5a673dc 100644 (file)
@@ -84,7 +84,7 @@ add_filter( 'gform_pre_render', function( $form ) {
         $this->wpdb->insert(
             GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
             array(
-                'title'   => 'Gravity Form - ' . $form['title'],
+                'title'   => $form['title'],
                 'form_id' => $form['id'],
                 'enabled' => $form_enabled
             ),
@@ -113,7 +113,7 @@ add_action( 'gform_after_submission', function( $entry, $form ){
         $this->wpdb->insert(
             GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
             array(
-                'title'   => 'Gravity Form - ' . $form['title'],
+                'title'   => $form['title'],
                 'form_id' => $form['id'],
                 'enabled' => $form_enabled
             ),
index 47b87ff..933b566 100644 (file)
@@ -1,33 +1,58 @@
 {include file='admin/leads/header.html'}
 
+<style>
+    #lead-search-form td {
+        padding: 5px;
+    }
+</style>
+
 <form action="{$thisUrl}?page={$thisPage}" method="post">
     <input type="hidden" name="search" value="1" />
-    <table style="width: 500px;">
+    <table style="width: 500px;" id="lead-search-form">
+        <tr>
+            <td style="text-align: right;"><label for="glm-form-source_id">Source</label></td>
+            <td>
+            {if $sources}
+                <select id="glm-form-source_id" name="source_id">
+                    <option value=""></option>
+                {foreach $sources as $source}
+                    <option value="{$source.id}" {if $source.selected} selected{/if}>{$source.title}</option>
+                {/foreach}
+                </select>
+            {/if}
+            </td>
+        </tr>
         <tr>
-            <td><label>Company</label></td>
-            <td><input type="text" name="company" value="{$search_params.company}"></td>
+            <td style="text-align: right;"><label for="glm-form-company">Company</label></td>
+            <td><input type="text" id="glm-form-company" name="company" value="{$search_params.company}"></td>
         </tr>
         <tr>
-            <td><label>Contact</label></td>
-            <td><input type="text" name="contact" value="{$search_params.contact}"></td>
+            <td style="text-align: right;"><label for="glm-form-contact">Contact</label></td>
+            <td><input type="text" id="glm-form-contact" name="contact" value="{$search_params.contact}"></td>
         </tr>
         <tr>
             <th colspan="2">Date Range</th>
         </tr>
         <tr>
-            <td>From</td>
-            <td><input type="text" id="from_date" name="from_date" value="{$search_params.from_date}"></td>
+            <td style="text-align: right;"><label for="glm-form-from_date">From</label></td>
+            <td><input type="text" id="glm-form-from_date" name="from_date" value="{$search_params.from_date}"></td>
+        </tr>
+        <tr>
+            <td style="text-align: right;"><label for="glm-form-to_date">To</label></td>
+            <td><input type="text" id="glm-form-to_date" name="to_date" value="{$search_params.to_date}"></td>
         </tr>
         <tr>
-            <td>To</td>
-            <td><input type="text" id="to_date" name="to_date" value="{$search_params.to_date}"></td>
+            <td style="text-align: right;"><label>Mail Ok</label></td>
+            <td><label><input type="checkbox" id="glm-form-mail_ok" name="mail_ok" value="1" {if $search_params.mail_ok}checked{/if}> Yes</label></td>
         </tr>
         {foreach $grouped_interests as $group_name => $group}
             <tr>
-                <td>{$group_name}</td>
+                <td style="text-align: right;"><strong>{$group_name}</strong></td>
                 <td>
                     {foreach $group as $interest}
-                    <label><input type="checkbox" name="interests[{$interest.id}]" {if $interest.selected} checked{/if}>{$interest.title}</label>
+                    <label style="display: block;width: 90%;">
+                        <input type="checkbox" name="interests[{$interest.id}]" {if $interest.selected} checked{/if}>{$interest.title}
+                    </label>
                 {/foreach}
                 </td>
             </tr>
@@ -46,6 +71,7 @@
     <table class="glm-admin-table">
         <tr>
             <th>Contact</th>
+            <th>Source</th>
             <th>Company</th>
             <th>Submitted</th>
         </tr>
@@ -56,6 +82,7 @@
                     {$lead.fname} {$lead.lname}
                 {if $user_can_edit_leads}</a>{/if}
             </td>
+            <td> {$lead.source_id.name} </td>
             <td> {$lead.org} </td>
             <td> {$lead.date_submitted} </td>
         </tr>
@@ -65,8 +92,8 @@
 
 <script>
 jQuery(document).ready(function($){
-    $('#from_date').datepicker();
-    $('#to_date').datepicker();
+    $('#glm-form-from_date').datepicker();
+    $('#glm-form-to_date').datepicker();
 });
 </script>