<?php 
/* $Id: mim.php,v 1.6 2003/12/11 15:26:58 k-fish Exp $ */

$myEnv['module'] = 'general';
$myEnv['stdsmenu'] = false;
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");

if(isset($_POST['op']) && $_POST['op'] == 'create'){
    $GLOBALS['notify']->message($_POST['message'], NOTIFY_MIM, 'u:'.(int)$_POST['recipient'], $_POST['subject']);
    if(isset($_POST['replyon']) && (int)$_POST['replyon'] > 0){
	$sql = "UPDATE mgw_instmsg SET status='R' WHERE id=".(int)$_POST['replyon'];
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    }
}
elseif(isset($_GET['view']) && $_GET['view'] == 'newform'){
    $rec = (isset($_GET['recipient'])) ? (int)$_GET['recipient'] : '';
    $replyon = (isset($_GET['replyon'])) ? (int)$_GET['replyon'] : null;

    $concat = $conn->Concat("lastname","', '","firstname", "' ('","username","')'");
    $sql = "SELECT $concat AS rec, id FROM mgw_users ORDER BY rec";
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $recsb = $res->GetMenu2('recipient', $rec, false, false, 1);

    if($replyon !== null){
	$sql = "SELECT subject FROM mgw_instmsg WHERE id=$replyon";
	if(!$res = $conn->GetRow($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$sub = (substr($res['subject'],0,4) == 'Re: ') ? $res['subject'] : 'Re: '.$res['subject'];
	$smarty->assign('sub', $sub);
	$smarty->assign('replyon', $replyon);
    }
    $smarty->assign('recsb', $recsb);
    $smarty->display('mim_new.tpl');
    exit();
}
elseif(isset($_GET['view']) && $_GET['view'] == 'details'){
    $concat = $conn->Concat("b.lastname","', '","b.firstname", "' ('","b.username","')'");
    $sql = "SELECT a.*, $concat AS sname FROM mgw_instmsg a LEFT JOIN mgw_users b ON a.sender = b.id WHERE a.id=".(int)$_GET['id'];
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $td = $res->FetchRow();
    $td['message'] = nl2br(htmlspecialchars($td['message'], ENT_QUOTES, $_SESSION['MGW']->charset));
    $td['recieved'] = $conn->UnixTimestamp($td['recieved']);
    $smarty->assign('td', $td);
    $smarty->display('mim_read.tpl');

    $sql = "UPDATE mgw_instmsg SET status='' WHERE id=".(int)$_GET['id'];
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

    exit();
}
else if(isset($_GET['view']) && $_GET['view'] == 'delete'){
    $sql = "DELETE FROM mgw_instmsg WHERE id=".(int) $_GET['id'];
    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}

$icons = getIconStrings();

$concat = $conn->Concat("b.lastname","', '","b.firstname", "' ('","b.username","')'");
$sql = "SELECT a.*, $concat AS sname FROM mgw_instmsg a LEFT JOIN mgw_users b ON a.sender = b.id WHERE a.recipient=".$_SESSION["MGW"]->userid." ORDER BY recieved DESC";

if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

while($row = $res->FetchRow()){
    $row['action'] = "<a href=\"mim.php?mod=mim&amp;obj=message&amp;view=details&amp;id=".$row['id']."&amp;".SID."\" class=\"edit\">".$icons["details"]."</a>" . $icons["space"] . "<a href=\"mim.php?mod=mim&amp;obj=message&amp;view=delete&amp;id=".$row['id']."&amp;".SID."\" class=\"delete\">".$icons["delete"]."</a>";
    $row['recieved'] = $conn->UnixTimestamp($row['recieved']);
    $row['class'] = ($row['status'] == 'N') ? 'newmim' : '';
    $smarty->append("td",$row);
}

$smarty->display('mim_list.tpl');
?>
