spivurno
2/21/2015 - 3:10 PM

Ounce of Talent // Gravity Forms // Export Entry to CSV and Email on Submission

Ounce of Talent // Gravity Forms // Export Entry to CSV and Email on Submission

<?php
/**
 * Gravity Wiz // Gravity Forms // Export Entry to CSV and Email on Submission
 *
 * This snippet will export the entry to CSV format, email that CSV file to a specified email address, and then delete the entry.
 *
 * @version   1.1
 * @author    David Smith <david@gravitywiz.com>
 * @license   GPL-2.0+
 * @link      http://gravitywiz.com/...
 */
class BT_Email_CSV {

	private static $_current_entry;

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id' => false,
            'email'   => false
        ) );

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        // make sure we're running the required minimum version of Gravity Forms
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
            return;
        }

        // carry on

	    add_action( 'gform_after_submission_' . $this->_args['form_id'], array( $this, 'email_csv' ), 20, 2 );

    }

	public function email_csv( $entry, $form ) {

		$csv_deets = $this->get_csv( $form, $entry );

		wp_mail( $this->_args['email']['to'], $this->_args['email']['subject'], $this->_args['email']['message'], '', array( $csv_deets['path'] ) );

		// delete CSV from file system once email is sent
		unlink( $csv_deets['path'] );

		// delete entry once email is sent
		GFAPI::delete_entry( $entry['id'] );

	}

	public function get_csv( $form, $entry  ) {

		require_once( GFCommon::get_base_path() . '/export.php' );

		self::$_current_entry = $entry;
		add_filter( 'gform_leads_before_export', array( $this, 'filter_export_entries' ) );

		ob_start();

		$_POST['export_field'] = array_keys( $entry );

		GFExport::start_export( $form, 0, $entry['id'] );

		$csv = ob_get_clean();

		self::$_current_entry = null;
		remove_filter( 'gform_leads_before_export', array( $this, 'filter_export_entries' ) );

		//GFFormsModel::get_file_upload_path( $form['id'], sprintf( 'export-%d.csv', $entry['id'] ) );

		return $this->get_file_path( $entry['id'] );
	}

	public function get_file_path( $entry_id ){

		$export_folder = trailingslashit( GFFormsModel::get_upload_root() . 'export' );
		$result = array( 'path' => $export_folder . sanitize_file_name( 'export-' . $entry_id .'.csv' ) );

		return $result;
	}

	public function filter_export_entries( $entries ) {

		if( ! self::$_current_entry ) {
			return $entries;
		}

		foreach( $entries as $entry ) {
			if( $entry['id'] == self::$_current_entry['id'] ) {
				return array( $entry );
			}
		}

		return $entries;
	}

}

# Configuration

new BT_Email_CSV( array(
	'form_id' => 414,
	'email'   => array(
		'to'      => 'david@gravitywiz.com',
		'subject' => 'This is the subject',
		'message' => 'This is the message'
	)
) );