<?php
/* This script runs from cron and determines:
  1. Users who have registered but not opsted a listing (for 7 days and 14 days)
  2. Users who have recieved messages from the contact form (3 days ago, 14 days ago)
  3. Public who have sent message through the contact form (3 days ago, 14 days ago)
  
  It then sends a follow-up email from the email template list specific to
  that person.
  
  (A modified version of Robins updatelistings.php)
  
*/

//
  $placeholders = array(
        'from'            => $GLOBALS['SETTINGS']['adminEmail'],
        'to'              => $,
        'buyer_name'      => $buyerName,
        'buyer_email'     => $buyerEmail,
        'buyer_phone'     => $buyerPhone,
        'seller_name'     => $sellerName,
        'subject'         => htmlspecialchars($_REQUEST['subject']),
        'listing_id'      => $_REQUEST['listing_id'],
        'listing_item'    => $listingsRecord['product']
      );

//need to set an option in the contact_seller table to disable follow-up email if unneccessary
  function emailUsers($template_id, $placeholders) {
    // email seller
    $errors = sendMessage(contact_emailTemplate(array(
      'template_id'      => $template_id,
      'placeholders'     => $placeholders,
      'from'             => $placeholders['from'],
      'to'               => $placeholders['to'],
      'subject'          => $placeholders['subject']        
    )));

    if ($errors) {
      echo($errors);
    }

  }

  require_once "lib/viewer_functions.php";
  
  $today                = date("Y-m-d");
  $threedaysago         = date("Y-m-d", strtotime('-3 days'));
  $sevendaysago         = date("Y-m-d", strtotime('-1 week'));
  $fourteendaysago      = date("Y-m-d", strtotime('-2 weeks'));
  
  $out = array();

  $listingsTable = $GLOBALS["TABLE_PREFIX"] . "listings";
  $accountsTable = $GLOBALS["TABLE_PREFIX"] . "accounts";
  $contactTable  = $GLOBALS["TABLE_PREFIX"] . "contact_seller";

  //no listings posted for 7 days
  $query = " SELECT $listingsTable.createdByUserNum,
                    $listingsTable.product, 
                    $accountsTable.first_name,
                    $accountsTable.email,
                    $accountsTable.createdDate,
                    $accountsTable.num 
             FROM $listingsTable
             RIGHT JOIN $accountsTable ON $listingsTable.createdByUserNum = $accountsTable.num 
             WHERE $accountsTable.createdDate  = '$sevendaysago' 
             ORDER BY $accountsTable.num";

  $result = mysql_query_fetch_all_assoc($query);
  
  $newUserNum = 0;
  $oldUserNum = 0;
  
  foreach ($result as $record) {
    $newUserNum = $record['num']; // set the new user to the current record
    if ( ($newUserNum != $oldUserNum) && ($record['product'] == '') ) { // not someone we've already contacted and there's no listings created
     $placeholders = array (
        'from'            => $GLOBALS['SETTINGS']['adminEmail'],
        'to'              => $record['email'],
        'seller_name'     => $record['first_name'],
        'subject'         => "No postings added yet - 7 days since account opened"
      );
     
      emailUsers('NEW-USER-NO-POSTING-7-DAYS', $placeholders); // send the email to this user
      $oldUserNum = $record['num']; // store the user number we just used so we don't send more than one email to this guy.
    }
  }
  
  //no listings posted for 14 days
  $query = " SELECT $listingsTable.createdByUserNum,
                    $listingsTable.product,  
                    $accountsTable.first_name,
                    $accountsTable.email,
                    $accountsTable.createdDate,
                    $accountsTable.num 
             FROM $listingsTable
             RIGHT JOIN $accountsTable ON $listingsTable.createdByUserNum = $accountsTable.num
             WHERE $accountsTable.createdDate  = '$fourteendaysago' 
             ORDER BY $accountsTable.num";

  $result = mysql_query_fetch_all_assoc($query);
  
  $newUserNum = 0;
  $oldUserNum = 0;
  
  foreach($result as $record) {
    $newUserNum = $record['num']; // set the new user to the current record
    if ( ($newUserNum != $oldUserNum) && ($record['product'] == '') ) { // not someone we've already contacted and there's no listings created
     $placeholders = array (
        'from'            => $GLOBALS['SETTINGS']['adminEmail'],
        'to'              => $record['email'],
        'seller_name'     => $record['first_name'],
        'subject'         => "No postings added yet - 14 days since account opened"
      );
     
      emailUsers('NEW-USER-NO-POSTING-14-DAYS', $placeholders); // send the email to this user
      $oldUserNum = $record['num']; // store the user number we just used so we don't send more than one email to this guy.
    }
  }
  
  //contact form used 3 days ago
  //checks messages, find associated account and listing info
  $query = " SELECT $contactTable.createdByUserNum,
                    $contactTable.message AS buyer_message,
                    $contactTable.name AS buyer_name,
                    $contactTable.email AS buyer_email,
                    $contactTable.phone_number AS buyer_phone,
                    $contactTable.listing_id,
                    $contactTable.num AS message_num,
                    $listingsTable.num AS listing_num,
                    $listingsTable.product,
                    $accountsTable.first_name,
                    $accountsTable.email,
                    $accountsTable.createdDate,
                    $accountsTable.num 
             FROM $contactTable
             INNER JOIN $accountsTable  ON $contactTable.createdByUserNum = $accountsTable.num
             INNER JOIN $listingsTable  ON $contactTable.listing_id   = listing.num 
             WHERE $contactTable.createdDate  = '$threedaysago' AND $contactTable.'contacted' = 'No'  
             ORDER BY $contactTable.createdByUserNum";

  $result = mysql_query_fetch_all_assoc($query);
  
  $newMessageNum = 0;
  $oldMessageNum = 0;
  
  foreach($result as $record) {
    $newMessageNum = $record['message_num']; // set the new user to the current record
    if($newMessageNum != $oldMessageNum) { // have we already sent an email about this message?
     $placeholders = array(
        'from'            => $GLOBALS['SETTINGS']['adminEmail'],
        'to'              => $record['email'],
        'seller_name'     => $record['first_name'],
        'subject'         => "Message recieved 3 days ago",
        'product'         => $record['product']." Listing #".$record['listing_id'],
        'buyer_name'      => $record['buyer_name'],
        'buyer_message'   => $record['message'],
        'buyer_email'     => $record['buyer_email'],
        'buyer_phone'     => $record['buyer_phone']
      );
     
      emailUsers('MESSAGE-RECEIVED-3-DAYS-SELLER', $placeholders); // send the email to the seller
      
      $placeholders['subject'] = "Message sent 3 days ago";
      emailUsers('MESSAGE-RECEIVED-3-DAYS-BUYER', $placeholders); // send the email to the buyer
      
      $oldMessageNum = $record['message_num']; // store the user number we just used so we don't send more than one email to this guy.
    }
  }
  
  //contact form used 14 days ago
  //checks messages, find associated account and listing info
  $query = " SELECT $contactTable.createdByUserNum,
                    $contactTable.message AS buyer_message,
                    $contactTable.name AS buyer_name,
                    $contactTable.email AS buyer_email,
                    $contactTable.phone_number AS buyer_phone,
                    $contactTable.listing_id,
                    $contactTable.num AS message_num,
                    $listingsTable.num AS listing_num,
                    $listingsTable.product,
                    $accountsTable.first_name,
                    $accountsTable.email,
                    $accountsTable.createdDate,
                    $accountsTable.num 
             FROM $contactTable
             INNER JOIN $accountsTable  ON $contactTable.createdByUserNum = $accountsTable.num
             INNER JOIN $listingsTable  ON $contactTable.listing_id   = listing.num 
             WHERE $contactTable.createdDate  = '$fourteendaysago' AND $contactTable.'contacted' = 'No'  
             ORDER BY $contactTable.createdByUserNum";

  $result = mysql_query_fetch_all_assoc($query);
  
  $newMessageNum = 0;
  $oldMessageNum = 0;
  
  foreach($result as $record) {
    $newMessageNum = $record['message_num']; // set the new user to the current record
    if($newMessageNum != $oldMessageNum) { // have we already sent an email about this message?
     $placeholders = array(
        'from'            => $GLOBALS['SETTINGS']['adminEmail'],
        'to'              => $record['email'],
        'seller_name'     => $record['first_name'],
        'subject'         => "Message recieved 14 days ago",
        'product'         => $record['product']." Listing #".$record['listing_id'],
        'buyer_name'      => $record['buyer_name'],
        'buyer_message'   => $record['message'],
        'buyer_email'     => $record['buyer_email'],
        'buyer_phone'     => $record['buyer_phone']
      );
     
      emailUsers('MESSAGE-RECEIVED-14-DAYS-SELLER', $placeholders); // send the email to this seller
      
      $placeholders['subject'] = "Message sent 14 days ago";//change email subject
      emailUsers('MESSAGE-RECEIVED-14-DAYS-BUYER', $placeholders); // send the email to this buyer
      
      $oldMessageNum = $record['message_num']; // store the user number we just used so we don't send more than one email to this guy.
    }
  }

?>