<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<div class="athenatitleshadow">Email Blast<div class="athenatitle">Email Blast</div></div>
<br />
<?php
  require_once 'dd.php';
  require_once 'email_blast.php';
  require_once 'config.php';
  require_once 'server_functions.php';

  globalize_request_keys('continue', 'template_id', 'subject', 
    'status', 'message');

  $eb = new Email_Blast ();
  $config = new Config ();

  $tickets = $eb->Tickets();
  if (is_array($tickets) and !count($tickets)) {
    print "Nothing marked to be email blasted.";
    return;
  } elseif (!$tickets) {
    die('<pre>blaster.php: ' . $eb->error . '</pre>');
  }

  if ($continue == 'View Tickets') {
    foreach ($tickets as $tid) {
      print "<a href=\"../ticket/edit_ticket?ticket_id=$tid\">$tid</a><br />\n";
    }
    print '<input type="submit" name="continue" value="Continue" />'."\n";

  } elseif ( in_array($continue, array('Yes', 'Continue', 'Update')) or
            (in_array($continue, array('Finish', 'Review')) and !$subject)) {

    if ($template_id) {
      require_once 'template.php';
      $template = new Template ();
      $template->Get_Info ($template_id);
      $message = $template->Get ('template_text');
    } else {
      $message = '';
    }

    $message .= "\n\nSincerely,\n\n";
    $message .= $config->Get ('Email Sig')."\n\n";
    $message .= $config->Get ('extended_email_sig')."\n\n";

  if (!$subject) print '<font color="red">Missing subject</font>'."\n";
?>

<table border="0">
<tr>
  <td>Template:</td>
  <td><select name="template_id"><option value=""></option><?php echo $dd->templates (); ?></select></td>
</tr>
<tr>
  <td>Subject:</td>
  <td><input type="text" name="subject" value="<?php echo $subject; ?>" /></td>
</tr>
<tr>
  <td>Change status to:</td>
  <td><select name="status"><?php echo $dd->status ($status); ?></select></td>
</tr>
</table>

<br />
Dear User's Name Here,<br />
<br />
<textarea name="message" cols="70" rows="20" wrap="soft"><?php echo $message; ?></textarea><br />
<input type="submit" name="continue" value="Update" />
<input type="submit" name="continue" value="Review" />

<?php

  } elseif ($continue == 'Review') {
    print "<pre>Status: $status

Subject: $subject
$message</pre>";
    print '<input type="hidden" name="status" value="'."$status\" />\n";
    print '<input type="hidden" name="message" value="'.htmlentities($message)."\" />\n";
    print '<input type="hidden" name="subject" value="'.htmlentities($subject)."\" />\n";
    print '<input type="submit" name="continue" value="Finish" />'."\n";

  } elseif ($continue == 'Finish') {
    foreach ($eb->Send_Emails ($subject, $message, $status) as $e) {
      print "$e<br />\n";
    }

    print "Hopefully there were no errors, in which case everything went fine.";

  } elseif (!$continue) {

    $rec_count = $eb->Records_to_Blast ();

    if (!$rec_count) {
      print "<font class=\"errors\">There are no tickets to blast at this time.</font>\n";
      return;
    }
?>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
  <td class="textsm" align="center">

    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td><img alt="" src="../../img/elipsefill-top-corn-left.gif" width="16" height="15" /></td>
      <td background="../../img/elipsefill-vert-line-top.gif"><img alt="" src="../../img/cldot.gif" width="1" height="15" /></td>
      <td><img alt="" src="../../img/elipsefill-top-corn-right.gif" width="15" height="15" /></td>
    </tr>
    <tr>
      <td background="../../img/elipsefill-vert-line-left.gif"><img alt="" src="../../img/cldot.gif" width="16" height="1" /></td>
      <td bgcolor="#E6F0CE" class="textsm" align="center" nowrap="nowrap" width="100%">
        <table border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td align="center" class="textsm"><?php echo $eb->Records_to_Blast (); ?> tickets will be included, do you wish to continue?<br />
            <br />
            <input type="submit" value="Yes" name="continue" class="textsm" /><br />
            <br />
            <input type="submit" value="View Tickets" name="continue" class="textsm" /></td>
        </tr>
        </table>
      </td>
      <td background="../../img/elipsefill-vert-line-right.gif"><img alt="" src="../../img/cldot.gif" width="15" height="1" /></td>
    </tr>
    <tr>
      <td><img alt="" src="../../img/elipsefill-btm-corn-left.gif" width="16" height="15" /></td>
      <td background="../../img/elipsefill-vert-line-btm.gif"><img alt="" src="../../img/cldot.gif" width="1" height="15" /></td>
      <td><img alt="" src="../../img/elipsefill-btm-corn-right.gif" width="15" height="15" /></td>
    </tr>
    </table>
  </td>
</tr>
</table>
<?php } ?>
</form>
