Files
HotLunches/email-logs.php
T
2026-10-19 15:22:52 -07:00

428 lines
15 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Ensure that only admin users have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Volunteer Administrator','Communications Administrator','Financial','Financial Team');
if (checkPermission($AllowedUserTypes) != 1) {
echo("<br>\n");
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
include "form.messages.php";
return;
}
// Set log permissions
$permissions = '';
if ($_SESSION['UserInfo']['type'] == 'staff') {
$permissions = "and (group_num = '0' or group_num = {$_SESSION['UserInfo']['group_num']})";
}
// startLog("email-logs.php");
if (array_key_exists('batch_num', $_GET)) $batch_num = intval($_GET['batch_num']);
if (isset($batch_num)) writeLog("batch_num: $batch_num");
// If batch_num set make sure user is permitted to view log details
if (isset($batch_num) && $batch_num > 0) {
$query = "select * from email_header " .
"where school_num = $school_num " .
"and record_num = $batch_num {$permissions}";
$results = db_query($query);
if (db_num_rows($results) != 1) {
echo("<p class='error'>Error: Record Not Found</p>\n");
exit();
}
}
// Cancel a batch is requested
if (isset($batch_num) && isset($naction)) {
switch ($naction) {
// Cancel a job
case 'cancel':
$query = "update email_header set status = 'cancelled' where record_num = $batch_num {$permissions}";
db_query($query);
$batch_num = 0;
$naction = 'listmine';
echo("<p class='success'>" . getLanguageText('TEXT-THE-EMAIL-BROADCAST-HAS-BEEN-CANCELLED') . "</p>\n");
break;
// Flag a job entry as deleted
case 'delete':
$query = "update email_header set status = 'deleted' where record_num = $batch_num {$permissions}";
db_query($query);
$batch_num = 0;
$naction = 'listmine';
echo("<p class='success'>" . getLanguageText('TEXT-THE-LOG-ENTRY-HAS-BEEN-DELETED') . "</p>\n");
break;
// Undelete a job
case 'undelete':
$query = "select * from email_header " .
"where school_num = $school_num " .
"and record_num = $batch_num {$permissions}";
$results = db_query($query);
$row = db_fetch_array($results);
if ($row['processed_records'] == $row['matching_records']) $new_status = 'completed';
else $new_status = 'cancelled';
$query = "update email_header set status = '$new_status' where record_num = $batch_num {$permissions}";
db_query($query);
$batch_num = 0;
$naction = 'listmine';
echo("<p class='success'>" . getLanguageText('TEXT-THE-LOG-ENTRY-HAS-BEEN-RESTORED') . "</p>\n");
break;
default:
if ($naction != 'listmine') $naction = "list";
break;
}
}
// If no batch number then list all batches
if (!isset($batch_num) || $batch_num == 0) {
$totalLogs = db_getVal("SELECT count(*) FROM email_header WHERE school_num = $school_num");
//
// Get list of header records
//
if ($_SESSION['UserInfo']['type'] == 'SuperUser') {
$query = "select * from email_header " .
"where school_num = $school_num " .
"and status != 'deleted' " .
"and status != 'cancelled' " .
"order by posted desc limit 0,50";
}
else {
$query = "select * from email_header " .
"where school_num = $school_num " .
"and status != 'deleted' " .
"and status != 'cancelled' " .
"AND category NOT IN ('Hot Lunch Contacts Broadcast','Sales Contacts Broadcast') " .
"order by posted desc limit 0,50";
}
$headers = db_query($query);
$numHeaders = db_num_rows($headers);
if ($numHeaders <= 50) $view_all = 'Y';
?>
<center>
<table width='95%' cellpadding='2' cellspacing='1' border='0'>
<tr>
<td colspan='1' align='left' valign='top'><h3><?php displayLanguageText('TEXT-EMAIL-SEND-LOGS'); ?></h3></td>
<td align='center'><nobr><b><?php echo($numHeaders) ?> of <?php echo($totalLogs) ?> <?php displayLanguageText('TEXT-LOG-ENTRIES'); ?></b></nobr></td>
<td align='right'><nobr><?php echo(date("Y-m-d H:i:s",time())) ?></nobr></td>
<td colspan='2' align='right'>
<table>
<tr>
<td align='left'><a href='/?action=email-logs' class='btn btn--primary btn--xsmall'><?php displayLanguageText('TEXT-REFRESH'); ?></a></td>
</tr>
</table>
</td>
</tr>
<tr class='heading'>
<td align='left'><b><?php displayLanguageText('TEXT-CATEGORY'); ?></b></td>
<td align='left'><b><?php displayLanguageText('TEXT-SUBJECT'); ?></b></td>
<td align='left'><b><?php displayLanguageText('TEXT-COMPLETED'); ?></b></td>
<td align='center'><b><?php displayLanguageText('TEXT-NUMBER'); ?></b></td>
<td>&nbsp;</td>
</tr>
<?php
$count = 0;
while ($row = db_fetch_array($headers)) {
extract($row);
$subject = $row['subject'];
$link = "<a href='/?action=email-logs&batch_num=$record_num'>$matching_records</a>";
if ($status == 'cancelled') $completed = getLanguageText('TEXT-CANCELLED');
if ($status == 'deleted') $completed = getLanguageText('TEXT-DELETED');
// Setup second button if there is one
$btn_details = "<a href='/?action=email-logs&batch_num=$record_num&batch_mode=details' title='View Details'><img src='images/icons/app24.png' border='0'></a>";
if ($processed_records == 0) $btn_details = "";
// Setup third button
$cancel_icon = "<img src='images/icons/deletefile24.png' border='0'>";
$cancel_msg = getLanguageText('TEXT-CONFIRM-CANCEL');
$cancel_delete = "<a href='/?action=email-logs&batch_num=$record_num&naction=cancel' title='Cancel Job' onclick='return confirm(\"$cancel_msg\")'>$cancel_icon</a>";
if ($status == 'completed') {
$delete_icon = "<img src='images/icons/delete24.png' border='0'>";
$delete_msg = getLanguageText('TEXT-CONFIRM-DELETE');
$cancel_delete = "<a href='/?action=email-logs&batch_num=$record_num&naction=delete' onClick='return validateDeleteLog();' title='delete' onclick='return confirm(\"$delete_msg\")'>$delete_icon</a>";
}
if ($status == 'deleted' || $status == 'cancelled') {
$undelete_icon = "<img src='images/icons/gotoapp24.png' border='0'>";
$cancel_delete = "<a href='/?action=email-logs&batch_num=$record_num&naction=undelete' title='Undelete Job'>$undelete_icon</a>";
}
$bgclass = "data-even";
if ($count %2) $bgclass = "data-odd";
$count++;
?>
<tr class='<?php echo($bgclass) ?>'>
<td align='left' valign='top'><?php echo($category) ?></td>
<td align='left' valign='top'><?php echo($subject) ?></td>
<td align='left' valign='top'><?php echo($completed) ?></td>
<td align='center' valign='top'><?php echo($link) ?></td>
<td align='left'>
<table cellpadding='0' cellspacing='2' border='0'>
<tr>
<td align='left'><a href='/?action=email-logs&batch_num=<?php echo($record_num) ?>' title='View Log Entry'><img src='images/icons/infoabout24.png' border='0'></a></td>
<?php if (strlen($btn_details) > 0) { ?>
<td align='left'><?php echo($btn_details) ?></td>
<?php } ?>
<td align='left'><?php echo($cancel_delete) ?></td>
</tr>
</table>
</td>
</tr>
<?php
}
?>
</table>
<?php if (!isset($view_all) || $view_all != 'Y') { ?>
<table cellpadding='0' cellspacing='5' border='0'>
<tr>
<td align='center' class='btn btn--primary btn--xsmall'><a href='/?action=email-logs&view_all=Y'><?php displayLanguageText('TEXT-VIEW-ALL-ENTRIES'); ?></a></td>
</tr>
</table>
<?php } ?>
<br>
</center>
<?php
}
if (isset($batch_num)) writeLog("batch_num: $batch_num");
if (isset($naction)) writeLog("naction: $naction");
else writeLog("naction is not set");
// If there is a batch number display batch details
if (isset($batch_num) && $batch_num > 0 && $naction = 'list') {
//
// Get batch details
//
$query = "select * from email_header where school_num = $school_num and record_num = '$batch_num' {$permissions}";
$results = db_query($query);
if (db_num_rows($results)) {
$header = db_fetch_array($results);
if ($header['status'] == 'cancelled') $header['completed'] = 'Cancelled';
$processed = $header['processed_records'];
$subject = $header['subject'];
$reply_address = $header['reply_address'];
// Set up link to message text
if (!isset($batch_mode) || $batch_mode == 'details') {
$subject = "<a href='/?action=email-logs&batch_num=$batch_num&batch_mode=message'>$subject</a>";
}
// Set up link to log details
$details = "";
if (!isset($batch_mode) || $batch_mode == 'message') {
if ($processed > 0) {
$details = "<a href='/?action=email-logs&batch_num=$batch_num&batch_mode=details' class='btn btn--primary>'>View Log Details</a>";
}
}
// Create links to delete and cancel this job
if (!isset($batch_mode)) {
// Setup delete job link
if ($header['status'] == 'completed' || $header['status'] == 'cancelled') {
$delete = "<a href='/?action=email-logs&batch_num=$batch_num&naction=delete' class='btn btn--primary>' onClick='return validateDeleteLog();'>Delete This Log Entry</a>";
}
// Setup cancel job link
if ($processed == 0 && $header['status'] == 'open') {
$cancel = "<a href='/?action=email-logs&batch_num=$batch_num&naction=cancel'>Cancel This Job</a>";
}
}
}
?>
<center>
<table width='95%' cellpadding='2' cellspacing='1' border='0'>
<tr>
<td align='left' colspan='2'><h3><?php displayLanguageText('TEXT-EMAIL-LOG'); ?> - <?php displayLanguageText('TEXT-BATCH-NUMBER'); ?>: <?php echo($batch_num) ?></h3></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-CATEGORY'); ?>:</b></td>
<td align='left'><?php echo($header['category']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-DESCRIPTION'); ?>:</b></td>
<td align='left'><?php echo($header['description']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-REPLY-EMAIL-ADDRESS'); ?>:</b></td>
<td align='left'><?php echo($reply_address); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-EMAIL-SUBJECT'); ?>:</b></td>
<td align='left'><?php echo($subject); ?></td>
</tr>
<!--
<tr>
<td align='left' valign='top'><b><?php displayLanguageText('TEXT-ATTACHMENTS'); ?>:</b></td>
<td align='left'>
<?php
$attachList = explode(',',$header['attachments']);
for ($i = 0; $i < count($attachList); $i++) {
echo("- ".preg_replace("#.*/#","",$attachList[$i])."<br>\n");
}
?>
</td>
</tr>
-->
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-TEMPLATE'); ?>:</b></td>
<td align='left'><?php echo($header['email_template']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-POSTED-TIME'); ?>:</b></td>
<td align='left'><?php echo($header['posted']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-START-TIME'); ?>:</b></td>
<td align='left'><?php echo($header['send_after']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-COMPLETION-TIME'); ?>:</b></td>
<td align='left'><?php echo($header['completed']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-NUMBER-OF-RECORDS'); ?>:</b></td>
<td align='left'><?php echo($header['matching_records']); ?></td>
</tr>
<tr>
<td align='left'><nobr><b><?php displayLanguageText('TEXT-PROCESSED'); ?>:</b></nobr></td>
<td align='left'><?php echo($header['processed_records']); ?></td>
</tr>
<?php
echo("<!-- " . $header['query'] . " -->\n\n");
// Display the full batch header
if (!isset($batch_mode)) {
?>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-EMAILS-SENT'); ?>:</b></td>
<td align='left'><?php echo($header['sent_successfully']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-SKIPPED'); ?>:</b></td>
<td align='left'><?php echo($header['skipped']); ?></td>
</tr>
<tr>
<td align='left'><b><?php displayLanguageText('TEXT-ERRORS'); ?>:</b></td>
<td align='left'><?php echo($header['errors']); ?></td>
</tr>
<?php
}
?>
</table>
<?php
if (isset($batch_mode) && $batch_mode == "details") {
?>
<table width='95%' cellpadding='2' cellspacing='1' border='0'>
<tr>
<td align='left' colspan='3'>&nbsp;</td>
</tr>
<tr class="heading">
<td align='left'><b>TEXT-EMAIL-ADDRESS</b></td>
<td align='left'><b><?php displayLanguageText('TEXT-PROCESSED'); ?></b></td>
<td align='left'><b><?php displayLanguageText('TEXT-STATUS'); ?></b></td>
</tr>
<?php
$query = "select * from email_send_log where school_num = $school_num and email_log_id = $batch_num order by email asc";
$results = db_query($query);
$count = 0;
while ($row = db_fetch_array($results)) {
extract($row);
$bgclass = "data-even";
if ($count %2) $bgclass = "data-odd";
$count++;
$prefix = "";
$postfix = "";
if ($status == "Error") {
$prefix = "<font color='red'><b>";
$postfix = "</b></font>";
}
?>
<tr class="<?php echo($bgclass); ?>">
<td align='left'><?php echo("$prefix$email$postfix"); ?></td>
<td align='left'><?php echo("$prefix$processed$postfix"); ?></td>
<td align='left'><?php echo("$prefix$status$postfix"); ?></td>
</tr>
<?php
}
?>
</table>
<!-- <?php echo($header['query']) ?> -->
</center>
<?php
}
if (isset($batch_mode) && $batch_mode == "message") {
echo("<h3>Message Content</h3>\n");
?>
<table width='95%' cellpadding='5' cellspacing='1' border='0' bgcolor='#eeeeee'>
<tr>
<td align='center'>
<table width='100%' cellpadding='10' cellspacing='1' border='0' bgcolor='#eeeeee'>
<tr>
<td align='left'><?php echo($header['message_text']) ?></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
}
?>
<table cellpadding='0' cellspacing='5' border='0'>
<tr>
<?php if (isset($cancel)) { ?>
<td align='center'>&nbsp;&nbsp;<?php echo($cancel); ?>&nbsp;&nbsp;</td>
<?php } ?>
<?php if (isset($details)) { ?>
<td align='center'>&nbsp;&nbsp;<?php echo($details); ?>&nbsp;&nbsp;</td>
<?php } ?>
<?php if (isset($delete)) { ?>
<td align='center'>&nbsp;&nbsp;<?php echo($delete); ?>&nbsp;&nbsp;</td>
<?php } ?>
</tr>
</table>
<script language="JavaScript">
function validateDeleteLog() {
return confirm('You are about to permanently DELETE this log entry.\nAre you sure you want to continue?');
}
</script>