<?php
/* $Id: todo.class.php,v 1.42 2006/04/27 11:54:07 k-fish Exp $ */

class Todo{
    // =================================================
    // create entry
    // =================================================
    function opCreate(){
	global $conn;

	$id = mgw_genID('mgw__seq_todo');
	$delegade = (int) $_POST["delegade"];
	$etype = (int) $_POST["endtype"];
	$stype = (int) $_POST["starttype"];
	$priority = (int) $_POST["priority"];
	$projectid = (isset($_POST["projectid"])) ? (int) $_POST["projectid"] : 0;
	$status = (int) $_POST["status"];
	$ts = $_POST["StartYear"]."-".$_POST["StartMonth"]."-".$_POST["StartDay"]." ".$_POST["StartHour"].":".$_POST["StartMinute"].":00";
	$sdate=$conn->DBTimeStamp($ts);
	$ts = $_POST["EndYear"]."-".$_POST["EndMonth"]."-".$_POST["EndDay"]." ".$_POST["EndHour"].":".$_POST["EndMinute"].":00";
	$edate=$conn->DBTimeStamp($ts);
	$qheader = ($_POST['header'] == "") ? "'[".Lang::getLanguageString("nosubject")."]'" : $conn->QMagic($_POST['header']);
	$qdesc = $conn->QMagic($_POST['description']);

	// check if delegaded
	if($delegade>0) {
	    $curruserid = $delegade;
	    $fromuserid = $_SESSION["MGW"]->userid;

	    $msg = Lang::getLanguageString('newdelegationnotification')."\n".$_POST['header'];
	    if(isset($_POST['todo_notification_screen']))
		$GLOBALS['notify']->message($msg, NOTIFY_SCREEN, 'u:'.$delegade);
	    if(isset($_POST['todo_notification_mim']))
		$GLOBALS['notify']->message($msg, NOTIFY_MIM, 'u:'.$delegade);
	    if(isset($_POST['todo_notification_mail']))
		$GLOBALS['notify']->message($msg, NOTIFY_MAIL, 'u:'.$delegade);
	}
	else {
	    $curruserid = $_SESSION["MGW"]->userid;
	    $fromuserid = 0;
	}

	$sql="INSERT INTO mgw_todo (id, userid, header, description, startdate, starttype, enddate, endtype, priority, status, fromuserid, owner, lu_user, lu_date, projectid) VALUES ('$id', '$curruserid', $qheader, $qdesc, $sdate, '$stype', $edate, '$etype', '$priority', '$status', '$fromuserid', '". $_SESSION["MGW"]->userid ."', '". $_SESSION["MGW"]->userid ."', ". $conn->DBTimeStamp(time()). ", '$projectid')";

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

    // =================================================
    // update entry
    // =================================================
    function opUpdate(){
	global $conn;

	$id = (int) $_POST["id"];
	$qheader = $conn->QMagic($_POST['header']);
	$qdesc = $conn->QMagic($_POST['description']);
	$ts = $_POST["StartYear"]."-".$_POST["StartMonth"]."-".$_POST["StartDay"]." ".$_POST["StartHour"].":".$_POST["StartMinute"].":00";
	$sdate=$conn->DBTimeStamp($ts);
	$stype = (int) $_POST["starttype"];
	$ts = $_POST["EndYear"]."-".$_POST["EndMonth"]."-".$_POST["EndDay"]." ".$_POST["EndHour"].":".$_POST["EndMinute"].":00";
	$edate=$conn->DBTimeStamp($ts);
	$etype = (int) $_POST["endtype"];
	$priority = (int) $_POST["priority"];
	$status = (int) $_POST["status"];
	$fromuserid = (int) $_POST["fromuserid"];
	$projectid = (isset($_POST["projectid"])) ? (int) $_POST["projectid"] : 0;
	$delegade = (int) $_POST["delegade"];
	if($delegade>0) {
	    $curruserid = $delegade;
	    $fromuserid = $_SESSION["MGW"]->userid;
	}
	else {
	    $curruserid = $_SESSION["MGW"]->userid;
	    $fromuserid = ($fromuserid == $_SESSION["MGW"]->userid) ? 0 : $fromuserid;
	}

	$sql="UPDATE mgw_todo SET userid='$curruserid', header=$qheader, description=$qdesc, startdate=$sdate, starttype='$stype', enddate=$edate, endtype='$etype', priority='$priority', status='$status', fromuserid='$fromuserid', lu_user='".$_SESSION["MGW"]->userid."', lu_date=". $conn->DBTimeStamp(time()). ", projectid='$projectid' WHERE id='$id'";

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

    function viewNewForm(){
	global $conn, $smarty;
	include(INCLUDEPATH . "mSelectBox.class.php");
	queryRights2('root_modules_todo_create');

	$sb4 = new mSelectBox("status");
	$sb4->setContentType("arr");
	$sb4->addContent(array("0%", "25%", "50%", "75%", "100%", Lang::getLanguageString("pending"), Lang::getLanguageString("aborted")));
	$smarty->assign("status", $sb4->draw());

	$sb5 = new mSelectBox("delegade");
	$sb5->setContentType("sql");
	$sb5->setSQLvalue("id");
	$sb5->setSQLcontent("name");
	$concat=$conn->Concat("lastname", "', '", "firstname");
	$sb5->addContent("SELECT *, $concat AS name FROM mgw_users WHERE id <> ".$_SESSION["MGW"]->userid." AND NOT(level=0) ORDER BY lastname");
	$sb5->addOption("0",Lang::getLanguageString("nodelegation"), "pre");
	$smarty->assign("delegade", $sb5->draw());

	if(isModuleInstalled('projects')){
	  $sb6 = new mSelectBox("projectid");
	  $sb6->setContentType("sql");
	  $sb6->setSQLvalue("project_id");
	  $sb6->setSQLcontent("title");
	  $sb6->addContent("SELECT project_id, title FROM mgw_projects ORDER BY title");
	  $sb6->addOption("0",Lang::getLanguageString("noprojectmapping"), "pre");
	  $smarty->assign("projects", $sb6->draw());
	}

	$sb7 = new mSelectBox("priority",3);
	$sb7->setContentType("arr");
	$sb7->addContent(array(1=>Lang::getLanguageString("priority_1"), Lang::getLanguageString("priority_2"), Lang::getLanguageString("priority_3"), Lang::getLanguageString("priority_4"), Lang::getLanguageString("priority_5")));
	$smarty->assign("prty", $sb7->draw());

	if(isset($_SESSION['MGW']->settings['todo_notification']) && is_array($_SESSION['MGW']->settings['todo_notification'])){
	  if(in_array(NOTIFY_SCREEN,$_SESSION['MGW']->settings['todo_notification']))
	    $smarty->assign('delscreen', 'checked ');
	  if(in_array(NOTIFY_MIM,$_SESSION['MGW']->settings['todo_notification']))
	    $smarty->assign('delmim', 'checked ');
	  if(in_array(NOTIFY_MAIL,$_SESSION['MGW']->settings['todo_notification']))
	    $smarty->assign('delmail', 'checked ');
	}

	$smarty->assign("mode", "new");
	$smarty->assign("suclass","submit");
	$smarty->assign("sselecttype3", "checked");
	$smarty->assign("eselecttype2", "checked");
	$smarty->assign("pubcheckedyes"," checked");
	$smarty->assign("sts", time());
	$smarty->assign("ets", time());
	$smarty->assign('viewmode',$_SESSION["todo"]["viewmode"]);
    
	$smarty->display('todo_edit.tpl');
    }

    function viewEditForm(){
	global $conn, $smarty;

	require_once(INCLUDEPATH . "mSelectBox.class.php");

	$id = (int) $_GET["id"];
		
	$sql = "SELECT a.*, b.firstname, b.lastname, c.id AS id2, c.firstname AS name2, c.lastname AS lastname2, a.enddate FROM mgw_todo a LEFT JOIN mgw_users b ON a.fromuserid=b.id LEFT JOIN mgw_users c ON a.userid=c.id WHERE a.id='$id'";
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row = $res->FetchRow();   

	if($row["status"]>=0 && $row["status"]<=6)
	    $selector = $row["status"];
	else
	    $selector = 0;		
	$sb4 = new mSelectBox("status");
	$sb4->setContentType("arr");
	$sb4->addContent(array("0%", "25%", "50%", "75%", "100%", Lang::getLanguageString("pending"), Lang::getLanguageString("aborted")));
	$smarty->assign("status", $sb4->draw($selector));
    
	// item has been delegated, just show by whom
	if(($row["fromuserid"] > 0) && ($row["userid"] == $_SESSION["MGW"]->userid)){
	    $todotype = "delegatedfrom";
	    $smarty->assign("delegade", $row["lastname"].", ".$row["firstname"]);
	}
	// own or delegated by the user, show edit form
	elseif($row["fromuserid"] == $_SESSION["MGW"]->userid || $row["fromuserid"] == 0){
	    $todotype = "delegatedto";
	    $sb5 = new mSelectBox("delegade");
	    $sb5->setContentType("sql");
	    $sb5->setSQLvalue("id");
	    $sb5->setSQLcontent("name");
	    $concat=$conn->Concat("lastname", "', '", "firstname");
	    $sb5->addContent("SELECT *, $concat AS name FROM mgw_users WHERE id <> '".$_SESSION["MGW"]->userid."' AND NOT(level=0) ORDER BY lastname");
	    $sb5->addOption("0",Lang::getLanguageString("nodelegation"), "pre");
	    if($row["fromuserid"] == 0) $smarty->assign("delegade", $sb5->draw());
	    else $smarty->assign("delegade", $sb5->draw($row["id2"]));
	}
	$smarty->assign("delegadeid", $row["fromuserid"]);

	if(isModuleInstalled('projects')){
	  $sb6 = new mSelectBox("projectid", $row["projectid"]);
	  $sb6->setContentType("sql");
	  $sb6->setSQLvalue("project_id");
	  $sb6->setSQLcontent("title");
	  $sb6->addContent("SELECT project_id, title FROM mgw_projects ORDER BY title");
	  $sb6->addOption("0",Lang::getLanguageString("noprojectmapping"), "pre");
	  $smarty->assign("projects", $sb6->draw($row["projectid"]));
	}

	$sb7 = new mSelectBox("priority",$row["priority"]);
	$sb7->setContentType("arr");
	$sb7->addContent(array(1=>Lang::getLanguageString("priority_1"), Lang::getLanguageString("priority_2"), Lang::getLanguageString("priority_3"), Lang::getLanguageString("priority_4"), Lang::getLanguageString("priority_5")));
	$smarty->assign("prty", $sb7->draw());

	$smarty->assign("header", @htmlentities($row["header"],ENT_QUOTES,$_SESSION['MGW']->charset));
	$smarty->assign("description", @htmlentities($row["description"],ENT_QUOTES,$_SESSION['MGW']->charset));
	$smarty->assign("mode", "edit");
	$smarty->assign("pubcheckedyes"," checked");
	$smarty->assign("id",$id);
	$smarty->assign("sts",$conn->UnixTimestamp($row['startdate']));
	$smarty->assign("ets",$conn->UnixTimestamp($row['enddate']));
	$smarty->assign("sselecttype".$row["starttype"], "checked");
	$smarty->assign("eselecttype".$row["endtype"], "checked");
	$smarty->assign("suname","edit");
	$smarty->assign("suclass","submit");
	$smarty->assign("todotype", $todotype);
	$smarty->assign('viewmode',$_SESSION["todo"]["viewmode"]);

	if(isset($_SESSION['MGW']->settings['todo_notification'])){
	    if(in_array(NOTIFY_SCREEN,$_SESSION['MGW']->settings['todo_notification']))
		$smarty->assign('delscreen', 'checked ');
	    if(in_array(NOTIFY_MIM,$_SESSION['MGW']->settings['todo_notification']))
		$smarty->assign('delmim', 'checked ');
	    if(in_array(NOTIFY_MAIL,$_SESSION['MGW']->settings['todo_notification']))
		$smarty->assign('delmail', 'checked ');
	}

	$smarty->display('todo_edit.tpl');
    }

    function viewDetails(){
	global $conn, $appconf, $smarty;
	$id = (int) $_GET["id"];

	if(isModuleInstalled('projects')){
	  $sql = "SELECT a.*, b.title AS projectname FROM mgw_todo a LEFT JOIN mgw_projects b ON a.projectid = b.project_id WHERE a.id=$id";
	}
	else{
	  $sql = "SELECT *, '' AS projectname FROM mgw_todo WHERE id=$id";
	}

	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
        
	$row = $res->FetchRow();   
		
	if($row["projectname"]=="") $row["projectname"] = Lang::getLanguageString("noprojectmapping");
	else $row["projectname"] = "<a href=\"../projects/index.php?action=details&amp;id=".$row["projectid"]."&amp;".SID."\">".$row["projectname"]."</a>";

	$row["priority"] = Lang::getLanguageString("priority_".$row["priority"]);
	if($_SESSION["MGW"]->settings["iconmode"]==1) {
	    switch($row["status"]){
	    case 0:
	    case 1:
	    case 2:
	    case 3:
	    case 4:
		$row["status"] = "<img src=\"". $appconf["imgpathgeneral"]. "/status".$row["status"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" alt=\"".($row["status"]*25)."%\" title=\"".($row["status"]*25)."%\">";
		break;
	    case 5:
		$row["status"] = "<img src=\"". $appconf["imgpathgeneral"]. "/status".$row["status"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" align=\"middle\" alt=\"".Lang::getLanguageString("pending")."\" title=\"".Lang::getLanguageString("pending")."\">&nbsp;".Lang::getLanguageString("pending");
		break;
	    case 6:
		$row["status"] = "<img src=\"". $appconf["imgpathgeneral"]. "/delete_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" align=\"middle\" alt=\"".Lang::getLanguageString("aborted")."\" title=\"".Lang::getLanguageString("aborted")."\">&nbsp;".Lang::getLanguageString("aborted");
	    }
	}
	else{
	    switch($row["status"]){
	    case 0:
	    case 1:
	    case 2:
	    case 3:
	    case 4:
		$row["status"] = ($row["status"]*25)."%";
		break;
	    case 5:
		$row["status"] = Lang::getLanguageString("pending");
		break;
	    case 6:
		$row["status"] = Lang::getLanguageString("aborted");
	    }
	}

	$row["description"] = nl2br(htmlspecialchars($row["description"]));
	$row["startdate"] = $conn->UnixTimeStamp($row["startdate"]);
	$row["enddate"] = $conn->UnixTimeStamp($row["enddate"]);
	$row["lu_date"] = $conn->UnixTimeStamp($row["lu_date"]);

	$smarty->assign("id", $id);
	$smarty->assign("td", $row);
	$smarty->assign("contentheader", Lang::getLanguageString("detailview"));
	$smarty->assign("ty", $_GET["ty"]);
	$smarty->assign("suclass", "submit");
	$smarty->assign('viewmode',$_SESSION["todo"]["viewmode"]);
    
	$smarty->display('todo_details.tpl');
    }

    function viewList(){
	global $conn, $appconf, $smarty;
	$icons = getIconStrings();
	$sqladd="";

	if(isset($_GET["view"]) && $_GET["view"]=="archive"){
	    $_SESSION["todo"]["viewmode"] = "archive";
	    $_SESSION["todo"]["searchterm"] = "";
	}
	elseif(isset($_GET["view"]) && $_GET["view"]=="list"){
	    $_SESSION["todo"]["viewmode"] = "list";
	    $_SESSION["todo"]["searchterm"] = "";
	}
	elseif(!isset($_SESSION["todo"])){
	    $_SESSION["todo"]["viewmode"] = "list";
	    $_SESSION["todo"]["searchterm"] = "";
	}

	if(isset($_GET["sort"])){
	    if($_GET["sort"] == $_SESSION["todo"]["prevorderby"]){
		$_SESSION["todo"]["sort_order"] = ($_SESSION["todo"]["sort_order"] == "DESC") ? "ASC" : "DESC";
	    }
	    else{
		$_SESSION["todo"]["sort_order"] = $_SESSION["MGW"]->settings["todo_order_dir"];
	    }
	    $_SESSION["todo"]["prevorderby"] = $conn->QMagic($_GET["sort"]);

	    switch($_GET["sort"]){
	    case "enddate":
		$_SESSION["todo"]["sort_by"] = "enddate";
		break;
	    case "subject":
		$_SESSION["todo"]["sort_by"] = "header";
		break;
	    case "priority":
		$_SESSION["todo"]["sort_by"] = "priority";
		break;
	    case "project":
		$_SESSION["todo"]["sort_by"] = "projectid";
		break;
	    case "status":
		$_SESSION["todo"]["sort_by"] = "status";
		break;
	    default:
		$_SESSION["todo"]["sort_by"] = $_SESSION["MGW"]->settings["todo_orderby"];
	    }
	}
	elseif(!isset($_SESSION["todo"]["sort_by"]) || $_SESSION["todo"]["sort_by"] == ""){
	    $_SESSION["todo"]["sort_by"] = (isset($_SESSION["MGW"]->settings["todo_orderby"])) ? $_SESSION["MGW"]->settings["todo_orderby"] : '';
	    $_SESSION["todo"]["sort_order"] = (isset($_SESSION["MGW"]->settings["todo_order_dir"])) ? $_SESSION["MGW"]->settings["todo_order_dir"] : '';
	}

	if(isset($_POST["searchterm"]))
	    $_SESSION["todo"]["searchterm"] = $_POST["searchterm"];
	else
	    $_SESSION["todo"]["searchterm"] = '';

	if($_SESSION["todo"]["searchterm"] != ""){
	    $qsearchterm = $conn->QMagic('%'.$_SESSION["todo"]["searchterm"].'%');
	    $sqladd = " AND (a.header LIKE $qsearchterm OR a.description LIKE $qsearchterm)";
	}

	if(isset($_SESSION["todo"]["sort_by"]) && $_SESSION["todo"]["sort_by"]!="") {
	    $sqladd .= " ORDER BY ".$_SESSION["todo"]["sort_by"];
	    if(isset($_SESSION["todo"]["sort_order"]) && $_SESSION["todo"]["sort_order"]!="") {
		$sqladd .= " ".$_SESSION["todo"]["sort_order"];
	    }
	}

	// ------------------------------
	// OWN todo's
	// ------------------------------
	if(isset($_SESSION["MGW"]->settings["todo_merged"]) && $_SESSION["MGW"]->settings["todo_merged"]==1) {
	    $id_filter = "a.userid=".$_SESSION["MGW"]->userid;
	} else {
	    $id_filter = "a.userid=".$_SESSION["MGW"]->userid." and a.fromuserid=0";
	}

	if(isset($_SESSION["todo"]["viewmode"]) && $_SESSION["todo"]["viewmode"]=="archive"){
	  if(isModuleInstalled('projects')){
	    $sql = "SELECT a.*, c.title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.owner = b.id LEFT OUTER JOIN mgw_projects c ON c.project_id = a.projectid WHERE ".$id_filter." AND a.status IN(4,6)".$sqladd;
	  }
	  else{
	    $sql = "SELECT a.*, '' AS title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.owner = b.id WHERE ".$id_filter." AND a.status IN(4,6)".$sqladd;
	  }
	    $smarty->assign('mode', "archive");
	    $smarty->assign('title1', Lang::getLanguageString("overviewowntodos_a"));
	    $smarty->assign('title2', Lang::getLanguageString("overviewdelegatedtome_a"));
	    $smarty->assign('title3', Lang::getLanguageString("overviewdelegated_a"));
	} else {		
	  if(isModuleInstalled('projects')){
	    $sql = "SELECT a.*, c.title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.owner = b.id LEFT OUTER JOIN mgw_projects c ON c.project_id = a.projectid WHERE ".$id_filter." AND a.status<>4 AND a.status<>6".$sqladd;
	  }
	  else{
	    $sql = "SELECT a.*, '' AS title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.owner = b.id WHERE ".$id_filter." AND a.status<>4 AND a.status<>6".$sqladd;
	  }
	  $smarty->assign('title1', Lang::getLanguageString("overviewowntodos"));
	  $smarty->assign('title2', Lang::getLanguageString("overviewdelegatedtome"));
	  $smarty->assign('title3', Lang::getLanguageString("overviewdelegated"));
	}
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	$projects = array();
	$urls = array();
	$mysta = array();
	while($row = $res->FetchRow()){
	    $myid = $row["id"];

	    if((isset($_SESSION["MGW"]->settings["todo_merged"]) && $_SESSION["MGW"]->settings["todo_merged"]==1) && $row["owner"]!=$_SESSION["MGW"]->userid) {
		$row["header"] = $row["header"]."<br /><strong>". Lang::getLanguageString("delegadefrom") . ": ". $row["lastname"]. ", ". $row["firstname"]. "</strong>";
	    } 
	
	    if($row['title'] != '')
	      $projects[] = "<a href=\"../projects/index.php?action=details&amp;id=".$row["projectid"]."&amp;".SID."\">".htmlentities($row["title"])."</a>";
	    else
	      $projects[] = "";


	    if($conn->UnixTimeStamp($row['startdate']) > time()){
		if($_SESSION["MGW"]->settings["iconmode"]==1) {
		    $row["priority"] = "<img src=\"". $appconf["imgpathgeneral"]. "/status5_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("unstarted")."\" title=\"".Lang::getLanguageString("unstarted")."\">";
		}
		else $row["priority"] = Lang::getLanguageString("unstarted");
	    }
	    else{
		if($_SESSION["MGW"]->settings["iconmode"]==1) {
		    $row["priority"] = "<img src=\"". $appconf["imgpathgeneral"]. "/prio".$row["priority"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("priority_".$row["priority"])."\" title=\"".Lang::getLanguageString("priority_".$row["priority"])."\">";
		}
		else $row["priority"] = Lang::getLanguageString("priority_".$row["priority"]);
	    }
	    $row["startdate"] = $conn->UnixTimeStamp($row["startdate"]);
	
	    switch($row["endtype"]){
	    case 3:
		$smarty->append("mydate", "");
		break;
	    default:
		$smarty->append("mydate", $conn->UnixTimeStamp($row["enddate"]));
	    }
	    $row['header'] = '<a href="index.php?mod=todo&amp;obj=item&amp;view=details&amp;id='.$myid.'&amp;ty=own&amp;'.SID.'">'.$row["header"]."</a>";
	    $smarty->append("td",$row);
    
	    if(strlen($row["description"])>40)
		$cutteddesc = htmlspecialchars(substr($row["description"],0,40) . "...");
	    else
		$cutteddesc = htmlspecialchars($row["description"]);
    
	    $smarty->append("cutteddesc", $cutteddesc);
	
	    if($_SESSION["MGW"]->settings["iconmode"]==1) {
		switch($row["status"]){
		case 5:
		    $mysta[] = "<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:
		    $mysta[] = "<img src=\"". $appconf["imgpathgeneral"]. "/delete_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("aborted")."\" title=\"".Lang::getLanguageString("aborted")."\">";
		    break;
		default:
		    $mysta[] = "<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:
		    $mysta[] = Lang::getLanguageString("pending");
		    break;
		case 6:
		    $mysta[] = Lang::getLanguageString("aborted");
		    break;
		default:
		    $mysta[] = ($row["status"]*25)."%";
		}
	    }
    
	$detlink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=details&amp;id=$myid&amp;ty=own&amp;".SID."\" CLASS=\"edit\">".$icons["details"]."</a>";
	$editlink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=editform&amp;id=$myid&amp;".SID."\" CLASS=\"edit\">".$icons["edit"]."</a>";
	$dellink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=delete&amp;id=$myid&amp;".SID."\" CLASS=\"delete\">".$icons["delete"]."</a>";
	    $space = $icons["space"];
	    $urls[] = "<nobr>" . $detlink . $space . $editlink  . $space . $dellink . "</nobr>";
	}	

	$smarty->assign('projects', $projects);
	$smarty->assign('urls', $urls);
	$smarty->assign('status', $mysta);

	// ------------------------------
	// delegaded from (ToDo's i got)
	// ------------------------------
	if(isset($_SESSION["MGW"]->settings["todo_merged"]) && $_SESSION["MGW"]->settings["todo_merged"]!=1) {
	  if(isset($_SESSION["todo"]["viewmode"]) && $_SESSION["todo"]["viewmode"]=="archive"){
	    if(isModuleInstalled('projects')){
	      $sql = "SELECT a.*, c.title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.fromuserid = b.id LEFT OUTER JOIN mgw_projects c ON c.project_id = a.projectid WHERE a.userid=".$_SESSION["MGW"]->userid." AND a.fromuserid>0 AND a.status IN(4,6)".$sqladd;
	    }
	    else{
	      $sql = "SELECT a.*, '' AS title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.fromuserid = b.id WHERE a.userid=".$_SESSION["MGW"]->userid." AND a.fromuserid>0 AND a.status IN(4,6)".$sqladd;
	    }
	}
	else {		
	  if(isModuleInstalled('projects')){
	    $sql = "SELECT a.*, c.title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.fromuserid = b.id LEFT OUTER JOIN mgw_projects c ON c.project_id = a.projectid WHERE a.userid=".$_SESSION["MGW"]->userid." AND a.fromuserid>0 AND a.status<>4 AND a.status<>6".$sqladd;
	  }
	  else{
	    $sql = "SELECT a.*, '' AS title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.fromuserid = b.id WHERE a.userid=".$_SESSION["MGW"]->userid." AND a.fromuserid>0 AND a.status<>4 AND a.status<>6".$sqladd;
	  }
	}
	
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	
	$projects2 = array();
	$urls2 = array();
	$mysta2 = array();
	while($row = $res->FetchRow()){
		$myid = $row["id"];

		if(strlen($row["description"])>40)
		    $cutteddesc = htmlspecialchars(substr($row["description"],0,40) . "...");
		else
		    $cutteddesc = htmlspecialchars($row["description"]);
	    
		$smarty->append("cutteddesc2", $cutteddesc);

		if($row['title'] != '')
		  $projects2[] = "<a href=\"../projects/index.php?action=details&amp;id=".$row["projectid"]."&amp;".SID."\">".htmlentities($row["title"])."</a>";
		else
		  $projects2[] = '';

		if($conn->UnixTimeStamp($row['startdate']) > time()){
		    if($_SESSION["MGW"]->settings["iconmode"]==1) {
			$row["priority"] = "<img src=\"". $appconf["imgpathgeneral"]. "/status5_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("unstarted")."\" title=\"".Lang::getLanguageString("unstarted")."\">";
		    }
		    else $row["priority"] = Lang::getLanguageString("unstarted");
		}
		else{
		    if($_SESSION["MGW"]->settings["iconmode"]==1) {
			$row["priority"] = "<img src=\"". $appconf["imgpathgeneral"]. "/prio".$row["priority"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("priority_".$row["priority"])."\" title=\"".Lang::getLanguageString("priority_".$row["priority"])."\">";
		    }
		    else $row["priority"] = Lang::getLanguageString("priority_".$row["priority"]);
		}

		switch($row["endtype"]){
		case 3:
		    $smarty->append("mydate2", "");
		    break;
		default:
		    $smarty->append("mydate2", $conn->UnixTimeStamp($row["enddate"]));
		}

		$row["startdate"] = $conn->UnixTimeStamp($row["startdate"]);
		$row['header'] = '<a href="index.php?mod=todo&amp;obj=item&amp;view=details&amp;id='.$myid.'&amp;ty=dfr&amp;'.SID.'">'.$row["header"]."</a><br /><strong>". Lang::getLanguageString("delegadefrom") . ": ". $row["lastname"]. ", ". $row["firstname"]. "</strong>";
		$smarty->append("td2",$row);
	    
		if($_SESSION["MGW"]->settings["iconmode"]==1) {
		    switch($row["status"]){
		    case 5:
			$mysta2[] = "<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:
			$mysta2[] = "<img src=\"". $appconf["imgpathgeneral"]. "/delete_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("aborted")."\" title=\"".Lang::getLanguageString("aborted")."\">";
			break;
		    default:
			$mysta2[] = "<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:
			$mysta2[] = Lang::getLanguageString("pending");
			break;
		    case 6:
			$mysta2[] = Lang::getLanguageString("aborted");
			break;
		    default:
			$mysta2[] = ($row["status"]*25)."%";
		    }
		}

		// build action urls
		$detlink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=details&amp;id=$myid&amp;ty=dfr&amp;".SID."\" CLASS=\"edit\">".$icons["details"]."</a>";
		$editlink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=editform&amp;id=$myid&amp;".SID."\" CLASS=\"edit\">".$icons["edit"]."</a>";
		$dellink = "";
		$space = $icons["space"];
		$urls2[] = "<nobr>" . $detlink . $space . $editlink  . $space . $dellink . "</nobr>";
	    }	
    
	    $smarty->assign('projects2', $projects2);
	    $smarty->assign('status2', $mysta2);
	    $smarty->assign('urls2', $urls2);
	}
	// -------------
	// delegaded to 
	// -------------
	if(isset($_SESSION["todo"]["viewmode"]) && $_SESSION["todo"]["viewmode"]=="archive"){
	  if(isModuleInstalled('projects')){
	    $sql = "SELECT a.*, c.title , b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.userid = b.id LEFT OUTER JOIN mgw_projects c ON c.project_id = a.projectid WHERE a.fromuserid=".$_SESSION["MGW"]->userid." AND a.status IN(4,6)".$sqladd;
	  }
	  else{
	    $sql = "SELECT a.*, '' AS title , b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.userid = b.id WHERE a.fromuserid=".$_SESSION["MGW"]->userid." AND a.status IN(4,6)".$sqladd;
	  }
	}
	else {
	  if(isModuleInstalled('projects')){
	    $sql = "SELECT a.*, c.title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.userid = b.id LEFT OUTER JOIN mgw_projects c ON c.project_id = a.projectid WHERE a.fromuserid=".$_SESSION["MGW"]->userid." AND a.status<>4 AND a.status<>6".$sqladd;
	  }
	  else{
	    $sql = "SELECT a.*, '' AS title, b.lastname, b.firstname FROM mgw_todo a LEFT JOIN mgw_users b ON a.userid = b.id WHERE a.fromuserid=".$_SESSION["MGW"]->userid." AND a.status<>4 AND a.status<>6".$sqladd;
	  }
	}

	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    
	$projects3 = array();
	$urls3 = array();
	$mysta3 = array();
	while ($row = $res->FetchRow()){
	    $myid = $row["id"];

	    if(strlen($row["description"])>40)
		$cutteddesc = htmlspecialchars(substr($row["description"],0,40) . "...");
	    else
		$cutteddesc = htmlspecialchars($row["description"]);
    
	    $smarty->append("cutteddesc3", $cutteddesc);

	    if($row['title'] != '')
	      $projects3[] = "<a href=\"../projects/index.php?action=details&amp;id=".$row["projectid"]."&amp;".SID."\">".htmlentities($row["title"])."</a>";
	    else
	      $projects3[] = '';
    
	    if($conn->UnixTimeStamp($row['startdate']) > time()){
		if($_SESSION["MGW"]->settings["iconmode"]==1) {
		    $row["priority"] = "<img src=\"". $appconf["imgpathgeneral"]. "/status5_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("unstarted")."\" title=\"".Lang::getLanguageString("unstarted")."\">";
		}
		else $row["priority"] = Lang::getLanguageString("unstarted");
	    }
	    else{
		if($_SESSION["MGW"]->settings["iconmode"]==1) {
		    $row["priority"] = "<img src=\"". $appconf["imgpathgeneral"]. "/prio".$row["priority"]."_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("priority_".$row["priority"])."\" title=\"".Lang::getLanguageString("priority_".$row["priority"])."\">";
		}
		else $row["priority"] = Lang::getLanguageString("priority_".$row["priority"]);
	    }

	    $row["startdate"] = $conn->UnixTimestamp($row["startdate"]);

	    switch($row["endtype"]){
	    case 3:
		$smarty->append("mydate3", "");
		break;
	    default:
		$smarty->append("mydate3", $conn->UnixTimestamp($row["enddate"]));
	    }
	    $row['deturl'] = "index.php?mod=todo&amp;obj=item&amp;view=details&amp;id=$myid&amp;ty=dfr&amp;".SID;
	    $row['header'] = '<a href="index.php?mod=todo&amp;obj=item&amp;view=details&amp;id='.$myid.'&amp;ty=dto&amp;'.SID.'">'.$row["header"]."</a><br /><strong>". Lang::getLanguageString("delegadedto") . ": ". $row["lastname"]. ", ". $row["firstname"]. "</strong>";	
	    $smarty->append("td3",$row);
    
	    if($_SESSION["MGW"]->settings["iconmode"]==1) {
		switch($row["status"]){
		case 5:
		    $mysta3[] = "<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:
		    $mysta3[] = "<img src=\"". $appconf["imgpathgeneral"]. "/delete_". $_SESSION["MGW"]->settings["iconid"]. ".gif\" border=\"0\" alt=\"".Lang::getLanguageString("aborted")."\" title=\"".Lang::getLanguageString("aborted")."\">";
		    break;
		default:
		    $mysta3[] = "<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:
		    $mysta3[] = Lang::getLanguageString("pending");
		    break;
		case 6:
		    $mysta3[] = Lang::getLanguageString("aborted");
		    break;
		default:
		    $mysta3[] = ($row["status"]*25)."%";
		}
	    }

	    // build action urls
	    $detlink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=details&amp;id=$myid&amp;ty=dto&amp;".SID."\" CLASS=\"edit\">".$icons["details"]."</a>";
	    $editlink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=editform&amp;id=$myid&amp;".SID."\" CLASS=\"edit\">".$icons["edit"]."</a>";
	    $dellink = "<a href=\"index.php?mod=todo&amp;obj=item&amp;view=delete&amp;id=$myid&amp;".SID."\" CLASS=\"delete\">".$icons["delete"]."</a>";
	    $space = $icons["space"];
	    $urls3[] = "<nobr>" . $detlink . $space . $editlink  . $space . $dellink . "</nobr>";
	}	

	$smarty->assign('merged', (isset($_SESSION["MGW"]->settings["todo_merged"])) ? $_SESSION["MGW"]->settings["todo_merged"] : '');
	$smarty->assign('viewmode',$_SESSION["todo"]["viewmode"]);
	$smarty->assign('searchterm',$_SESSION["todo"]["searchterm"]);
	$smarty->assign('projects3', $projects3);
	$smarty->assign('status3', $mysta3);
	$smarty->assign('urls3', $urls3);

	$smarty->assign('createperm', queryRights2_bool('root_modules_todo_create'));
	$smarty->display('todo_list.tpl');
    }
}
?>