Adding csv file download from Leads.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 13 Jul 2016 14:52:10 +0000 (10:52 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 13 Jul 2016 14:52:10 +0000 (10:52 -0400)
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
views/admin/leads/edit.html
views/admin/leads/index.html

index 8dc961d..6a56705 100644 (file)
@@ -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 '<pre>$where: ' . print_r( $where, true ) . '</pre>';
-            $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;
index e900b77..353f9a4 100644 (file)
@@ -70,7 +70,7 @@
             </td>
         </tr>
         <tr>
-            <td colspan="2"><input type="submit" value="Update Lead"></td>
+            <td colspan="2"><input class="button" type="submit" value="Update Lead"></td>
         </tr>
     </table>
 </form>
index fe4eb14..47b87ff 100644 (file)
@@ -33,7 +33,7 @@
             </tr>
         {/foreach}
         <tr>
-            <td colspan="2"><input type="submit" value="Search"></td>
+            <td colspan="2"><input class="button" type="submit" value="Search"></td>
         </tr>
     </table>
 </form>
@@ -41,7 +41,7 @@
 {if $leads}
     <h4>Search Results:</h4>
     <form action="{$thisUrl}?page={$thisPage}">
-        <a href="{$thisUrl}?page={$thisPage}&option=download" class="button">Download</a>
+        <a href="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=csvExport" class="button">Download as CSV File</a>
     </form>
     <table class="glm-admin-table">
         <tr>