<?php
/* $Id: outboard.class.php,v 1.17 2003/11/21 16:21:49 k-fish Exp $ */

class OutBoard{
    var $status = array();

    function OutBoard(){
	require("inc/status.inc.php");
    }

    // ===========================
    // Outboard - > Sign In
    // ===========================
    function signin($id) {
	global $smarty, $conn;
	$sql= "DELETE FROM mgw_outboard WHERE id_username=".(int)$id;

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

    // ===========================
    // Outboard - > Sign Out
    // ===========================
    function opSignout(){
	global $smarty, $conn, $_POST;

	$out = $conn->DBTimeStamp((int)$_POST['StartYear']."-".(int)$_POST['StartMonth']."-".(int)$_POST['StartDay']." ".date("H:i:s",(int)$_POST['time_in']));
	$in  = $conn->DBTimeStamp((int)$_POST['EndYear']."-".(int)$_POST['EndMonth']."-".(int)$_POST['EndDay']." ".date("H:i:s",(int)$_POST['time_out']));
	$lu_update = $conn->DBTimeStamp(time());

	$id_username = (int) $_POST['id_username'];
	$autoin = (int) $_POST['autoin'];
	$status = (int) $_POST['status'];			
	$location = $conn->QMagic($_POST['location']);
	$notes = $conn->QMagic($_POST['notes']);
    
	$sql= "DELETE FROM mgw_outboard WHERE id_username=".(int)$id_username;
	if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	$sql = "INSERT INTO mgw_outboard (id_username, tsout, tsin, location, notes, autoin, statusid, lu_user, lu_update) VALUES($id_username, $out, $in, $location, $notes, '$autoin', '$status', '".$_SESSION["MGW"]->userid."', $lu_update)";

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

	return true;
    }


    function viewDetails($id) {
	global $conn, $smarty;

	$actual_date_arr = getdate(mktime());

	$concat = $conn->Concat("u.lastname","', '","u.firstname");
	$sql= "SELECT o.id_username, $concat AS fullname, o.notes as notes, o.tsin, o.tsout, o.location AS location, o.autoin AS autoin, o.statusid AS statusid FROM mgw_outboard o LEFT JOIN mgw_users u ON u.id = o.id_username WHERE id_username=".(int)$id;
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	$row = $res->FetchRow();
	$row["status"] = $this->status[$row['statusid']];
	$row["personsfull"] = htmlspecialchars($row["fullname"]);
	$row["location"] = htmlspecialchars($row["location"]);
	$row["notes"] = nl2br(htmlspecialchars($row["notes"]));

	$row["tsin"]  = $conn->UnixTimeStamp($row["tsin"]);
	$row["tsout"] = $conn->UnixTimeStamp($row["tsout"]);

	$smarty->assign("rows", $row);

	$smarty->display("outboard_details.tpl");
	exit();
    }

    // ===========================
    // Outboard - > Form Controller
    // ===========================
    function viewEditForm($id) {
	global $smarty, $conn;

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

	$concat = $conn->Concat("u.lastname","', '","u.firstname");
	$sql= "SELECT o.id_username, $concat AS fullname, o.notes as notes, o.tsin, o.tsout, o.location AS location, o.autoin AS autoin, o.statusid AS statusid FROM mgw_outboard o LEFT JOIN mgw_users u ON u.id = o.id_username WHERE id_username=".(int)$id;
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row = $res->FetchRow();

	// User list box
	$smarty->assign("userfull", $row["fullname"]);
	
	// Status list box
	$sl = new mSelectBox("status");
	$sl->setContentType("arr");
	$sl->addContent($this->status);
	$smarty->assign("statuslist", $sl->draw($row["statusid"]));

	// Set new values for time-handling
	$timemask = $_SESSION["MGW"]->settings["timefmt"];
	$interval_sec = 30 * 60; // $_SESSION["MGW"]->settings["calendar_interval"] * 60;
	$actual_date_arr = getdate();
	$app_start_time =  mktime(0,0,0, $actual_date_arr["mon"], $actual_date_arr["mday"], $actual_date_arr["year"]);
	$app_end_time   =  mktime(0,0,0, $actual_date_arr["mon"], $actual_date_arr["mday"], $actual_date_arr["year"]) + 23.5 * 3600;

	$row["tsout"] = $conn->Unixtimestamp($row["tsout"]);
	$row["tsin"] = $conn->Unixtimestamp($row["tsin"]);
	
	for( $t=$app_start_time; $t < $app_end_time; $t+=$interval_sec) {
	    $ssel = (date("Hi",$t) == date("Hi",$row["tsout"])) ? " selected" : "";
	    $esel = (date("Hi",$t) == date("Hi",$row["tsin"])) ? " selected" : "";

	    $appointment_start_list = array("timestamp" 	=> $t,
					    "time_label"  	=> date($timemask, $t),
					    "sel"   		=> $ssel);
	    $appointment_end_list =  array("timestamp" 		=> $t,
					   "time_label"  	=> date($timemask, $t),
					   "sel"   		=> $esel);

	    $smarty->append("appointment_start_list", $appointment_start_list);
	    $smarty->append("appointment_end_list", $appointment_end_list);
	}
    // fixing wrong quoting for input-form - emailtotom
	$smarty->assign("td", array("id_username" => $row["id_username"], "location" =>htmlspecialchars($row["location"]), "notes" =>$row["notes"]));
	$smarty->assign('sts', $row['tsout']);
	$smarty->assign('ets', $row['tsin']);
	$smarty->assign("mode", "edit");
	$smarty->assign("suvalue", Lang::getLanguageString("change"));
	$smarty->assign("autoin",$row["autoin"]);
	$smarty->display('outboard_edit.tpl');

	exit();
    }

    // ===========================
    // Outboard - > Form Controller
    // ===========================
    function viewSignout() {
	global $smarty, $conn;

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

	// User list box
	$ul = new mSelectBox("id_username");
	$ul->setContentType("sql");
	$ul->setSQLValue("id");
	$ul->setSQLContent("name");
	$connstr = $conn->Concat("lastname","', '","firstname");
	$ul->addContent("SELECT id, $connstr AS name FROM mgw_users ORDER BY lastname");
	$smarty->assign("userlist", $ul->draw($_SESSION["MGW"]->userid));
	
	// Status list box
	$sl = new mSelectBox("status");
	$sl->setContentType("arr");
	$sl->addContent($this->status);
	$smarty->assign("statuslist", $sl->draw());

	// Set new values for time-handling
	$timemask = $_SESSION["MGW"]->settings["timefmt"];
	$interval_sec = 30 * 60; // $_SESSION["MGW"]->settings["calendar_interval"] * 60;
	$actual_date_arr = getdate();
	$app_start_time =  mktime(0,0,0, $actual_date_arr["mon"], $actual_date_arr["mday"], $actual_date_arr["year"]);
	$app_end_time   =  mktime(0,0,0, $actual_date_arr["mon"], $actual_date_arr["mday"], $actual_date_arr["year"]) + 23.5 * 3600;

	for( $t=$app_start_time; $t < $app_end_time; $t+=$interval_sec) {
	    $appointment_start_list = array("timestamp" 	=> $t,
					    "time_label"  	=> date($timemask, $t),
					    "sel"   		=> "");
	    $appointment_end_list =  array("timestamp" 		=> $t,
					   "time_label"  	=> date($timemask, $t),
					   "sel"   		=> "");

	    $smarty->append("appointment_start_list", $appointment_start_list);
	    $smarty->append("appointment_end_list", $appointment_end_list);
	}

	$smarty->assign("mode", "signout");
	$smarty->assign("suvalue", Lang::getLanguageString("signout"));

	$smarty->display('outboard_edit.tpl');

	exit();
    }

    // =================================================
    //	              Clean-Up                          
    // this lovely POS scans the db, and removes anyone 
    // that has been asked to be removed auto. Also, it 
    // checks for dupe-entries, and removes all but the 
    // latest. This runs whenever a user views the out- 
    // board. Most efficient way I could think of to do 
    // it.                                              
    // =================================================
    function cleanOutboard(){
	global $res, $conn;
	$sql= "SELECT * FROM mgw_outboard WHERE autoin=1";
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	while($row = $res->FetchRow()){
	    if (time() > $row["tsin"]){
		echo "<!-- cleaning past entry with id: ".$row["id_username"]." -->";
		$sql= "DELETE FROM mgw_outboard WHERE id_username=".(int)$row["id_username"];
		$conn->Execute($sql) or exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    }
	}
    }

    // ===========================
    // Outboard - > List View
    // ===========================
    function viewList() {
	global $conn, $smarty;

	$this->cleanOutboard();

	$icons = getIconStrings();

	$concat = $conn->Concat("u.lastname","', '","u.firstname");

	$sql= "SELECT o.id_username, $concat AS fullname, o.tsin, o.tsout, o.location AS location, o.autoin AS autoin, o.statusid AS statusid FROM mgw_outboard o LEFT JOIN mgw_users u ON u.id = o.id_username ORDER BY u.lastname";
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	$i=0;
	$rows = array();
	while($row = $res->FetchRow()){
	    $rows[$i]["datesin"] = $conn->UnixTimeStamp($row["tsin"]);
	    $rows[$i]["datesout"] = $conn->UnixTimeStamp($row["tsout"]);

	    $rows[$i]["statusforuser"] = $this->status[$row['statusid']];
	    $rows[$i]["personsfull"] = htmlspecialchars($row["fullname"]);
	    $rows[$i]["location"] = htmlspecialchars($row["location"]);

	    $detlink = "<a href=\"index.php?mod=outboard&view=details&amp;id=".$row["id_username"]."&amp;".SID."\" class=\"edit\">".$icons["details"]."</a>";
	    $editlink = "<a href=\"index.php?mod=outboard&view=edit&amp;id=".$row["id_username"]."&amp;".SID."\" class=\"edit\">".$icons["edit"]."</a>";
	    $dellink = "<a href=\"index.php?mod=outboard&amp;view=signin&amp;id=".$row["id_username"]."&amp;".SID."\" class=\"delete\">".$icons["delete"]."</a>";
	    $rows[$i]["actions"] = $detlink . $icons["space"] . $editlink . $icons["space"] . $dellink;

	    $i++;
	}
	$smarty->assign("rows", $rows);

	$smarty->display("outboard_list.tpl");
	exit();
    }

}
?>