I wrote this tid-bit of code as an attempt to grab the data from a timesheet form I created with contact form 7, save that data into a csv formatted the way I want, and then send that over to email specified in the "Mail" section of the Contact Form. Unfortunately, I've ran into 2 problems that I can't seem to wrap my head around.
Can't seem to get save the CSV document into the directory I defined. Or save at all for that matter. Here's my code:
<?php
add_action( 'wpcf7_before_send_mail', 'add_form_as_attachment', 10, 1 );
function add_form_as_attachment(&$WPCF7_ContactForm) {
$current_user = wp_get_current_user();
$pay_rate = get_user_meta( $current_user->ID, 'pay-rate', true );
$list = array (
array( 'Employee Name:', $current_user->display_name),
array( 'Selected Week:', $pay_rate),
array( 'Weekday', 'Job Number', 'Labor Desc', 'Labor Code', 'Total Hours', 'Break Hours'),
array(
'Sunday',
$WPCF7_ContactForm->posted_data['job-number-sun'],
$WPCF7_ContactForm->posted_data['labor-desc-sun'],
$WPCF7_ContactForm->posted_data['labor-code-sun'],
$WPCF7_ContactForm->posted_data['total-hours-sun'],
$WPCF7_ContactForm->posted_data['break-hours-sun']
),array(
'Monday',
$WPCF7_ContactForm->posted_data['job-number-mon'],
$WPCF7_ContactForm->posted_data['labor-desc-mon'],
$WPCF7_ContactForm->posted_data['labor-code-mon'],
$WPCF7_ContactForm->posted_data['total-hours-mon'],
$WPCF7_ContactForm->posted_data['break-hours-mon']
),array(
'Tuesday',
$WPCF7_ContactForm->posted_data['job-number-tue'],
$WPCF7_ContactForm->posted_data['labor-desc-tue'],
$WPCF7_ContactForm->posted_data['labor-code-tue'],
$WPCF7_ContactForm->posted_data['total-hours-tue'],
$WPCF7_ContactForm->posted_data['break-hours-tue']
),array(
'Wednesday',
$WPCF7_ContactForm->posted_data['job-number-wed'],
$WPCF7_ContactForm->posted_data['labor-desc-wed'],
$WPCF7_ContactForm->posted_data['labor-code-wed'],
$WPCF7_ContactForm->posted_data['total-hours-wed'],
$WPCF7_ContactForm->posted_data['break-hours-wed']
),array(
'Thursday',
$WPCF7_ContactForm->posted_data['job-number-thu'],
$WPCF7_ContactForm->posted_data['labor-desc-thu'],
$WPCF7_ContactForm->posted_data['labor-code-thu'],
$WPCF7_ContactForm->posted_data['total-hours-thu'],
$WPCF7_ContactForm->posted_data['break-hours-thu']
),array(
'Friday',
$WPCF7_ContactForm->posted_data['job-number-fri'],
$WPCF7_ContactForm->posted_data['labor-desc-fri'],
$WPCF7_ContactForm->posted_data['labor-code-fri'],
$WPCF7_ContactForm->posted_data['total-hours-fri'],
$WPCF7_ContactForm->posted_data['break-hours-fri']
),array(
'Saturday',
$WPCF7_ContactForm->posted_data['job-number-sat'],
$WPCF7_ContactForm->posted_data['labor-desc-sat'],
$WPCF7_ContactForm->posted_data['labor-code-sat'],
$WPCF7_ContactForm->posted_data['total-hours-sat'],
$WPCF7_ContactForm->posted_data['break-hours-sat']
),array(
'Travel Mileage (Miles):',
$WPCF7_ContactForm->posted_data['mileage'],
'Travel Mileage (MPG):',
$WPCF7_ContactForm->posted_data['mpg']
),array(
'Remarks:',
$WPCF7_ContactForm->posted_data['remarks']
)
);
$debug_echo = 'Target Directory: ' . $target_directory . '\n\n';
$debug_echo .= 'WPCF7 Upload Directory: ' . wpcf7_upload_tmp_dir() . '\n\n';
$debug_echo .= 'Real Path: ' . realpath(dirname(__FILE__)) . '\n\n';
$debug_echo .= 'Target Directory Permissions: ' . substr(sprintf('%o', fileperms( $target_directory )), -4);
$target_directory = '/nas/content/live/lovettmechanic/wp-content/uploads/timesheets/timesheet.csv';
$fp = fopen( $target_directory , 'w');
debug_to_console( $debug_echo );
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}
Any help will be definitely appreciated. Been banging my head against the wall trying to figure this one out for a while.
==EDIT==
I edited the question to show that perhaps the function isn't firing in the first place. Or at least the echo
command in the debug_to_console()
function isn't. Then again it must be firing, because I'll get a http 500 error if there's a syntax error. But the debug_to_console()
function isn't behaving as expected. I think that may help figure out the issue.