<?php
  // These can be configured to whatever you need
  $EMAIL_TO = "youremailaddress@yourdomain.org";
  $EMAIL_FROM_NAME = "Your Church Name";
  $EMAIL_FROM_ADDRESS = "youremailaddress@yourdomain.org";
  $EMAIL_SUBJECT = "New Stewardship Pledge";
  $EMAIL_MESSAGE = "Attached is the pledge information.";
  $SUCCESS_REDIRECT = "2016success.html";
  $ERROR_REDIRECT = "2016error.html";

  // Map input form name to CSV column name
  $CSV_FIELDS = array(
    "id" => "famIDEnv",
    "recurract" => "recurract",
    "terms" => "terms",
    "begin" => "startDate",
    "end" => "endDate",
    "rate" => "rate",
    "total" => "pledgeTotal",
    "Name" => "famName",
    "Address" => "famAddress",
    "City" => "famCity",
    "State" => "famState",
    "Zip" => "famZip",
    "Phone" => "famPhone",
    "Email" => "famEmail",
    "Took A Step" => "tookAStep"
  );

  $TERM_TO_NUMBER = array(
    "weekly" => 52,
    "biweekly" => 27,
    "monthly" => 12,
    "bimonthly" => 6,
    "semimonthly" => 24,
    "quarterly" => 4,
    "semiannually" => 2,
    "annually" => 1,
    "special" => 1
  );

  // Sanitize user input
  function sanitize($value) {
    return addslashes(trim($value));
  }
  array_filter($_POST, sanitize);

  // Extract POSTed form data
  foreach ($CSV_FIELDS as $header => $input) {
    $formValue = $_POST[$input];

    if ($header === "Took A Step") {
      if ($formValue === "on") {
        $formValue = "TRUE";
      } else {
        $formValue = "FALSE";
      }
    }

    if ($header === "total") {
      $pledgeRate = $_POST[$CSV_FIELDS["rate"]];
      $pledgeTerms = $_POST[$CSV_FIELDS["terms"]];
      $pledgeTerms = $TERM_TO_NUMBER[$pledgeTerms];
      $pledgeRate = floatval($pledgeRate);
      $pledgeTerms = intval($pledgeTerms);
      $formValue = ($pledgeRate * $pledgeTerms) . '';
    }

    $userCsvData[$header] = $formValue;
  }

  // Build encoded CSV string for mail attachment
  $headers = implode(",", array_keys($userCsvData));
  $data = implode(",", array_values($userCsvData));
  $encodedCsv = chunk_split(base64_encode($headers . "\r\n" . $data));

  // Build Mail headers
  $uid = md5(uniqid(time()));
  $fileName = "submission.csv";
  $header = "From: " . $EMAIL_FROM_NAME . " <" . $EMAIL_FROM_ADDRESS . ">\r\n";
  $header .= "Reply-To: " . $EMAIL_FROM_ADDRESS . "\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
  $header .= "This is a multi-part message in MIME format.\r\n";
  $header .= "--" . $uid . "\r\n";
  $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
  $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  $header .= $EMAIL_MESSAGE . "\r\n\r\n";
  $header .= "--" . $uid . "\r\n";
  $header .= "Content-Type: text/csv; name=\"" . $fileName . "\"\r\n";
  $header .= "Content-Transfer-Encoding: base64\r\n";
  $header .= "Content-Disposition: attachment; filename=\"" . $fileName . "\"\r\n\r\n";
  $header .= $encodedCsv . "\r\n\r\n";
  $header .= "--" . $uid . "--";

  // Send email and perform redirect
  $mailStatus = mail($EMAIL_TO, $EMAIL_SUBJECT, $EMAIL_MESSAGE, $header);
  if ($mailStatus) {
    header('Location: ' . $SUCCESS_REDIRECT);
  } else {
    header('Location: ' . $ERROR_REDIRECT);
  }
  die();
?>
