From 9276f60101cf4df56112dbad9037d769d1cc118a Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 13 Jul 2016 10:52:10 -0400 Subject: [PATCH] Adding csv file download from Leads. I have the search results output a link for the download of the leads. The download file is a CSV file format. --- models/admin/leads/index.php | 71 ++++++++++++++++++++++++++++++++---- views/admin/leads/edit.html | 2 +- views/admin/leads/index.html | 4 +- 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/models/admin/leads/index.php b/models/admin/leads/index.php index 8dc961d..6a56705 100644 --- a/models/admin/leads/index.php +++ b/models/admin/leads/index.php @@ -94,7 +94,13 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $option = $_REQUEST['option']; } - // Get event ID if supplied + if ( isset( $_REQUEST['glm_action'] ) && $_REQUEST['glm_action'] === 'csvExport' ) { + $search = true; + $option = 'csv'; + } + + + // Get entry ID if supplied if (isset($_REQUEST['entry'])) { // Make sure it's numeric @@ -123,11 +129,11 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $search_id = $result['id']; if ( !isset( $_REQUEST['search'] ) ) { $search_params = unserialize( $result['search'] ); - $search = true; + //$search = true; } } - if ( isset( $search ) && $search ) { + if ( ( isset( $search ) && $search ) || isset( $search_params ) && $search_params ) { // Filter the $_POST variables from the search form. // Verify mm/dd/YYYY date format for the from and to dates. if ( !isset( $search_params ) ) { @@ -230,10 +236,13 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry } else if ( $search_params['to_date'] ) { $where_parts[] = "T.date_submitted <= STR_TO_DATE('{$search_params['to_date']}', '%m/%d/%Y')"; } - $where = implode( ' AND ', $where_parts ); - //echo '
$where: ' . print_r( $where, true ) . '
'; - $order = "T.lname, T.fname"; - $leads = $this->getList( $where, $order ); + if ( isset( $search ) && $search ) { + $where = implode( ' AND ', $where_parts ); + $order = "T.lname, T.fname"; + $leads = $this->getList( $where, $order ); + } else { + $leads = false; + } } else { $leads = ''; $search_params = array( @@ -252,6 +261,54 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $grouped_interests = array(); switch ( $option ) { + case 'csv': + // 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 ) ) { + foreach ( $leads as $entry_id => $lead ) { + $out = array( + 'fname' => $lead['fname'], + 'lname' => $lead['lname'], + 'email' => $lead['lead_id']['name'], + 'org' => $lead['org'], + 'addr1' => $lead['addr1'], + 'addr2' => $lead['addr2'], + 'city' => $lead['city'], + 'state' => $lead['state'], + 'zip' => $lead['zip'], + 'country' => $lead['country'], + 'phone' => $lead['phone'], + 'phone2' => $lead['phone2'], + 'fax' => $lead['fax'], + 'date_submitted' => $lead['date_submitted'], + ); + /* + * 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"; + } + } + + // Add Headers for forcing a file to download. + header('Content-Description: File Transfer'); + header('Content-Type: application/force-download'); + header('Content-Disposition: attachment; filename=members-leads.csv'); + echo $csv_file_output; + exit; + break; + case 'update': if ( $this->config['loggedInUser']['contactUser'] ) { break; diff --git a/views/admin/leads/edit.html b/views/admin/leads/edit.html index e900b77..353f9a4 100644 --- a/views/admin/leads/edit.html +++ b/views/admin/leads/edit.html @@ -70,7 +70,7 @@ - + diff --git a/views/admin/leads/index.html b/views/admin/leads/index.html index fe4eb14..47b87ff 100644 --- a/views/admin/leads/index.html +++ b/views/admin/leads/index.html @@ -33,7 +33,7 @@ {/foreach} - + @@ -41,7 +41,7 @@ {if $leads}

Search Results:

- Download + Download as CSV File
-- 2.17.1