From: Steve Sutton Date: Thu, 14 Jul 2016 13:18:19 +0000 (-0400) Subject: Adding the source and mail_ok to search form X-Git-Tag: v1.0.0^2~23 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=30fb867ae7b3ef605d03d112ea6e48a76123099e;p=WP-Plugins%2Fglm-member-db-leads.git Adding the source and mail_ok to search form Can now search by the Source (like contact types) and mail_ok field. --- diff --git a/classes/data/dataLeadEntry.php b/classes/data/dataLeadEntry.php index ef34334..ed7afe1 100644 --- a/classes/data/dataLeadEntry.php +++ b/classes/data/dataLeadEntry.php @@ -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', diff --git a/models/admin/leads/index.php b/models/admin/leads/index.php index 6a56705..5868108 100644 --- a/models/admin/leads/index.php +++ b/models/admin/leads/index.php @@ -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 '
$_POST: ' . print_r($_POST, true) . '
'; 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 '
$_REQUEST: ' . print_r($_REQUEST, true) . '
'; - 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, diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 6a00168..5a673dc 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -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 ), diff --git a/views/admin/leads/index.html b/views/admin/leads/index.html index 47b87ff..933b566 100644 --- a/views/admin/leads/index.html +++ b/views/admin/leads/index.html @@ -1,33 +1,58 @@ {include file='admin/leads/header.html'} + +
- +
+ + + + - - + + - - + + - - + + + + + + - - + + {foreach $grouped_interests as $group_name => $group} - + @@ -46,6 +71,7 @@
+ {if $sources} + + {/if} +
Date Range
From
To
{$group_name}{$group_name} {foreach $group as $interest} - + {/foreach}
+ @@ -56,6 +82,7 @@ {$lead.fname} {$lead.lname} {if $user_can_edit_leads}{/if} + @@ -65,8 +92,8 @@
ContactSource Company Submitted
{$lead.source_id.name} {$lead.org} {$lead.date_submitted}