<?php
/* $Id: overview.inc.php,v 1.8 2003/11/21 16:48:32 leyeti Exp $ */

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.8 $", __FILE__);

// ===============================================================
// get list of todo items (for use in overview module)
// ===============================================================
function gettodo() {
    global $appconf, $myEnv, $smarty, $conn;

    $own_order="";
    if(isset($_SESSION["MGW"]->settings["todo_overview_unstarted"]) && !$_SESSION["MGW"]->settings["todo_overview_unstarted"]) {
	$own_order .= " AND startdate < ".$conn->DBTimeStamp(time())." ";
    }
    if(isset($_SESSION["MGW"]->settings["todo_orderby"]) && $_SESSION["MGW"]->settings["todo_orderby"]!="") {
	$own_order .= " ORDER BY ".$_SESSION["MGW"]->settings["todo_orderby"];
	if(isset($_SESSION["MGW"]->settings["todo_order_dir"]) && $_SESSION["MGW"]->settings["todo_order_dir"]!="") {
	    $own_order .= " ".$_SESSION["MGW"]->settings["todo_order_dir"];
	}
    }

    $sql = "SELECT a.*, b.firstname AS tofirst, b.lastname AS tolast, b.email AS tomail, c.firstname AS fromfirst, c.lastname AS fromlast, c.email AS frommail FROM mgw_todo a LEFT JOIN mgw_users b ON a.userid=b.id LEFT JOIN mgw_users c ON a.fromuserid=c.id WHERE (a.userid=".$_SESSION["MGW"]->userid." OR a.fromuserid=".$_SESSION["MGW"]->userid.") AND a.status<>4 AND a.status<>6".$own_order;

    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	
    while($row = $res->FetchRow()){
	switch($row["endtype"]){
	case 3:
	    $mydate = "";
	    break;
	default:
	    $mydate = $conn->UnixTimestamp($row["enddate"]);
	}

	if($row["fromuserid"] == $_SESSION["MGW"]->userid){
	    $row["fromto"] = Lang::getLanguageString("to").": ".$row["tolast"].", ".$row["tofirst"];
	    $row["email"] = $row["tomail"];
	    $ty="ty=dto";
	}
	else if($row["userid"] != $row["owner"]){
	    $row["fromto"] = Lang::getLanguageString("from").": ".$row["fromlast"].", ".$row["fromfirst"];
	    $row["email"] = $row["frommail"];
	    $ty="ty=dfr";
	}
	else {
	    $row["fromto"] = "";
	    $row["email"] = "";
	    $ty="ty=own";
	}

	if($_SESSION["MGW"]->settings["iconmode"]==1) {
	    switch($row["status"]){
	    case 5:
		$value = "<img src=\"". $appconf["imgpathgeneral"]. "/status".$row["status"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("pending")."\" title=\"".Lang::getLanguageString("pending")."\">";
		break;
	    case 6:
		$value = "<img src=\"". $appconf["imgpathgeneral"]. "/delete_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("aborted")."\" title=\"".Lang::getLanguageString("aborted")."\">";
		break;
	    default:
		$value = "<img src=\"". $appconf["imgpathgeneral"]. "/status".$row["status"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".($row["status"]*25)."%\" title=\"".($row["status"]*25)."%\">";
	    }
	}
	else{
	    switch($row["status"]){
	    case 5:
		$value = Lang::getLanguageString("pending");
		break;
	    default:
		$value = ($row["status"]*25)."%";
	    }
	}
	$overtime = ($res->UnixTimeStamp($row["enddate"])<time()) ? 1 : 0;

	$todo_list = array("subject" => "<a href=\"../todo/index.php?mod=todo&amp;obj=item&amp;view=details&amp;id=".$row["id"]."&amp;".$ty."&amp;".SID."\">".$row["header"]."</a>", "fromto" => $row["fromto"], "enddate" => $mydate, "status" => $value, "overtime" => $overtime, "email" => $row["email"]);
	$smarty->append("tm", $todo_list);
    }
    return $smarty->fetch("file:".ROOTPATH."/modules/todo/templates/default/".$myEnv["output"]."/todo_overview.tpl");
}
?>
