$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
$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 ) ) {
} 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(
$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;