WIP for the search form.
authorSteve Sutton <ssutton@gmail.com>
Sun, 10 Jul 2016 11:57:13 +0000 (07:57 -0400)
committerSteve Sutton <ssutton@gmail.com>
Sun, 10 Jul 2016 11:57:13 +0000 (07:57 -0400)
I have the search form searching for company and contact.
Need to work on the Contact search to better match one of the words.
Need to also include the interests into the search.
I have the form holding the searched parameters.

models/admin/leads/index.php
setup/frontHooks.php
views/admin/leads/index.html

index fb6847d..8ccd66d 100644 (file)
@@ -83,13 +83,48 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
     public function modelAction($actionData = false)
     {
+        $search = filter_var( $_REQUEST['search'], FILTER_VALIDATE_BOOLEAN);
 
         if ( isset( $search ) && $search ) {
-            $leads = $this->getList();
+            $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
+                    )
+                )
+            );
+
+            // build the $where part
+            $where_parts = array();
+            if ( $search_params['company'] ) {
+                $where_parts[] = "org = '" . esc_sql( $search_params['company'] ) . "'";
+            }
+            if ( $search_params['contact'] ) {
+                $where_parts[] = "SOUNDEX(CONCAT_WS(' ', fname, lname)) = SOUNDEX( '" . esc_sql( $search_params['contact'] ) . "')";
+            }
+            $where = implode( ' AND ', $where_parts );
+            echo '<pre>$where: ' . print_r( $where, true ) . '</pre>';
+            $order = "T.lname, T.fname";
+            $leads = $this->getList( $where, $order );
         } else {
             $leads = '';
         }
-        $leads = $this->getList();
 
         $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config );
         $groups    = $groupData->getList();
@@ -98,17 +133,28 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
         if ( $groups ) {
             foreach ( $groups as $group ) {
-                $intData   = new GlmDataInterests( $this->wpdb, $this->config );
-                $grouped_interests[$group['title']] = $intData->getList(
-                    "group_id = " . $group['id'],
-                    'title'
-                );
+                $intData     = new GlmDataInterests( $this->wpdb, $this->config );
+                $group_parts = explode( '_', $group['title'] );
+                $group_parts = array_map( function($item){ return ucfirst( $item ); }, $group_parts );
+                $group_title = implode( ' ', $group_parts );
+                $grouped_interests[$group_title] = $intData->getList( "group_id = " . $group['id'], 'title' );
+            }
+            if ( $grouped_interests ) {
+                foreach ( $grouped_interests as $group_name => &$interests ) {
+                    foreach ( $interests as $key => &$interest ) {
+                        $grouped_interests[$group_name][$key]['selected']
+                            = ( $search_params['interests'][$key] ) ? 1 : 0;
+                    }
+                }
             }
         }
-        echo '<pre>$grouped_interests: ' . print_r($grouped_interests, true) . '</pre>';
+
         // Compile template data
         $templateData = array(
-            'leads' => $leads,
+            'search_params'     => $search_params,
+            'request'           => print_r( $_REQUEST, true ),
+            'leads'             => $leads,
+            'grouped_interests' => $grouped_interests,
         );
 
         // Return status, any suggested view, and any data to controller
index 3f73913..7cbecd2 100644 (file)
@@ -172,6 +172,7 @@ add_action( 'gform_after_submission', function( $entry, $form ){
     if ( !$lead_id ) {
         return;
     }
+    echo '<pre>$glm_leads_entry: ' . print_r( $glm_leads_entry, true ) . '</pre>';
     // create the lead entry
     $lead_entry = $this->wpdb->insert(
         GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_entry', // table
@@ -225,17 +226,12 @@ add_action( 'gform_after_submission', function( $entry, $form ){
             $ints[$interest['id']] = $interest['title'];
         }
     }
-    //echo '<pre>$interests: ' . print_r($interests, true) . '</pre>';
-    //echo '<pre>$ints: ' . print_r($ints, true) . '</pre>';
-    //echo '<pre>$leads_fields: ' . print_r($leads_fields, true) . '</pre>';
     // create the lead to interest entries
     // using $leads_fields
     // first delete any they current have
-    //echo '<pre>$leads_fields: ' . print_r($leads_fields, true) . '</pre>';
     if ( is_array( $leads_fields) && !empty( $leads_fields ) ) {
         foreach ( $leads_fields as $interest_val ) {
             $int_key = array_search( $interest_val, $ints );
-            //echo '<pre>$int_key: ' . print_r($int_key, true) . '</pre>';
             if ( $int_key ) {
                 $this->wpdb->insert(
                     GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests', // table
index c2148df..f2e5d42 100644 (file)
@@ -1,11 +1,41 @@
 {include file='admin/leads/header.html'}
 
-<form>
-    <label>Company</label><br>
-    <input type="text" name="company"><br>
-    <label>Contact</label><br>
-    <input type="text" name="contact"><br>
-    <input type="submit" value="Search">
+<form action="{$thisUrl}?page={$thisPage}" method="post">
+    <input type="hidden" name="search" value="1" />
+    <table style="width: 500px;">
+        <tr>
+            <td><label>Company</label></td>
+            <td><input type="text" 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>
+        </tr>
+        <tr>
+            <th colspan="2">Date Range</th>
+        </tr>
+        <tr>
+            <td>From</td>
+            <td><input type="text" name="from_date" value="{$search_params.from_date}"></td>
+        </tr>
+        <tr>
+            <td>To</td>
+            <td><input type="text" name="to_date" value="{$search_params.to_date}"></td>
+        </tr>
+        {foreach $grouped_interests as $group_name => $group}
+            <tr>
+                <td>{$group_name}</td>
+                <td>
+                    {foreach $group as $interest}
+                    <label><input type="checkbox" name="interests[{$interest.id}]" {if $interest.selected} checked{/if}>{$interest.title}</label>
+                {/foreach}
+                </td>
+            </tr>
+        {/foreach}
+        <tr>
+            <td colspan="2"><input type="submit" value="Search"></td>
+        </tr>
+    </table>
 </form>
 
 {if $leads}
@@ -24,4 +54,5 @@
        {/foreach}
     </table>
 {/if}
+
 {include file='admin/footer.html'}