Update for csv export
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 31 Aug 2018 20:11:03 +0000 (16:11 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 31 Aug 2018 20:11:03 +0000 (16:11 -0400)
separate the contacts into their own fields.

models/admin/ajax/leadCsvExport.php

index 8eff7ee..77da2c6 100644 (file)
@@ -80,9 +80,11 @@ class GlmMembersAdmin_ajax_leadCsvExport extends GlmDataTravelLeads
     public function modelAction ($actionData = false)
     {
 
-        $where   = ' true ';
-        $list    = false;
-        $lead_id = false;
+        $where        = ' true ';
+        $list         = false;
+        $lead_id      = false;
+        $lead_results = array();
+        $max_contacts = 0;
 
         // Every Time a user request a Download
         // They need to have a logged in contact.
@@ -200,14 +202,15 @@ class GlmMembersAdmin_ajax_leadCsvExport extends GlmDataTravelLeads
         $out = array();
 
         if ( $haveLeads ) {
-            foreach ( $list as $lead ) {
+            // First have to go through all leads and grab their contacts
+            foreach ( $list as $key => $lead ) {
                 // Extra data
                 $notes       = $this->formatNotes( $lead['notes'] );
-                $contacts    = $this->formatContacts( $lead['contacts'] );
+                // $contacts    = $this->formatContacts( $lead['contacts'] );
                 $referred_by = $this->formatReferredBy( $lead['refs'] );
 
                 // $out array with data for this lead
-                $out = array(
+                $lead_results[$key] = array(
                     'updated'     => $lead['updated']['datetime'],
                     'fname'       => $lead['fname'],
                     'lname'       => $lead['lname'],
@@ -222,23 +225,66 @@ class GlmMembersAdmin_ajax_leadCsvExport extends GlmDataTravelLeads
                     'email'       => $lead['email'],
                     'website'     => $lead['website'],
                     'notes'       => $notes,
-                    'contacts'    => $contacts,
                     'referred_by' => $referred_by,
                 );
+                if ( $lead['contacts'] ) {
+                    $contact_number = 1;
+                    foreach ( $lead['contacts'] as $contact ) {
+                        $lead_results[$key]['contact_' . $contact_number . '_title'] = $contact['title'];
+                        $lead_results[$key]['contact_' . $contact_number . '_name']  = $contact['name'];
+                        $lead_results[$key]['contact_' . $contact_number . '_email'] = $contact['email'];
+                        $lead_results[$key]['contact_' . $contact_number . '_phone'] = $contact['phone'];
+                        if ( $contact_number > $max_contacts ) {
+                            $max_contacts = $contact_number;
+                        }
+                        $contact_number++;
+                    }
+                    // echo '<pre>$contact_number: ' . print_r( $contact_number, true ) . '</pre>';
+                }
+            }
+            // echo '<pre>$max_contacts: ' . print_r( $max_contacts, true ) . '</pre>';
+            // echo '<pre>$lead_results: ' . print_r( $lead_results, true ) . '</pre>';
+            // exit;
+            // Now we can go through the lead result array and write it out to csv file.
+            foreach ( $lead_results as $out ) {
+                $file_data = $out;
+                if ( $max_contacts ) {
+                    for ( $i = 1; $i <= $max_contacts; ++$i ) {
+                        unset( $file_data['contact_' . $i . '_title'] );
+                        unset( $file_data['contact_' . $i . '_name'] );
+                        unset( $file_data['contact_' . $i . '_email'] );
+                        unset( $file_data['contact_' . $i . '_phone'] );
+                    }
+                }
                 // 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";
+                    $csv_file_output = implode( ',', array_map( function($str){return sprintf( '"%s"', $str );}, array_keys( $file_data ) ) );
+                    if ( $max_contacts ) {
+                        for ( $i = 1; $i <= $max_contacts; ++$i ) {
+                            $csv_file_output .= ',"contact_'.$i.'_title","contact_'.$i.'_name","contact_'.$i.'_email","contact_'.$i.'_phone"';
+                        }
+                    }
+                    $csv_file_output .= "\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";
+                $csv_file_output .= implode( ',', array_map( function($str){return sprintf( '"%s"', str_replace( '"', '', $str ) );}, $file_data ) );
+                if ( $max_contacts ) {
+                    for ( $i = 1; $i <= $max_contacts; ++$i ) {
+                        $csv_file_output .= ',"' . ( $out['contact_'.$i.'_title'] ? addslashes( $out['contact_'.$i.'_title'] ): '' ) . '"';
+                        $csv_file_output .= ',"' . ( $out['contact_'.$i.'_name'] ? addslashes( $out['contact_'.$i.'_name'] ): '' ) . '"';
+                        $csv_file_output .= ',"' . ( $out['contact_'.$i.'_email'] ? addslashes( $out['contact_'.$i.'_email'] ): '' ) . '"';
+                        $csv_file_output .= ',"' . ( $out['contact_'.$i.'_phone'] ? addslashes( $out['contact_'.$i.'_phone'] ): '' ) . '"';
+                    }
+                }
+                $csv_file_output .= "\n";
                 $lead_counter++;
             }
-
         }
 
+
         // Compile template data
         // $templateData = array(
         //     'haveLeads' => $haveLeads,