<?php
/* $Id: workplacemanager.php,v 1.1 2003/11/18 19:41:40 k-fish Exp $ */

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

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

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

// =================================================
//	              create entry
// =================================================
if (isset($_POST["op"]) && $_POST["op"] == "create"){
    
    $_POST["worklocname"] = trim($_POST["worklocname"]);

    // Worklocationname mustn't be ""
    if ($_POST["worklocname"] != "") {

      $gname = $conn->QMagic($_POST["worklocname"]);

      $sql = "SELECT name FROM mgw_workplace WHERE name = ". $gname;
      if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

   	  if($res->RecordCount() == 0) {
      
          // Create Worklocation
    $id = $conn->GenID('mgw__seq_workloc', 60005);
    $gname = $conn->QMagic($_POST["worklocname"]);
    $gdesc = $conn->QMagic($_POST["description"]);

    $sql="INSERT INTO mgw_workplace (id, name, description, lu_user, lu_date) VALUES ($id, $gname, $gdesc, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";

    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
      } else {
          // Worklocation name already exists, display form again with description
          $smarty->assign("suname","create");
          $smarty->assign("suclass","submit");
          $smarty->assign("mode","new");
          $smarty->assign("td", array("description"=>$_POST["description"]));
          $smarty->assign("exception",Lang::getLanguageString("err_duplicateworkloc"));
            
          $smarty->display('workplacemanager_edit.tpl');
          exit;
      }

    } else {
      // worklocname is "", display form again with description
      $smarty->assign("suname","create");
      $smarty->assign("suclass","submit");
      $smarty->assign("mode","new");
      $smarty->assign("td", array("description"=>$_POST["description"]));
      $smarty->assign("exception",Lang::getLanguageString("err_noworklocname"));
            
      $smarty->display('workplacemanager_edit.tpl');
      exit;
    }
}

// =================================================
//	              edit entry
// =================================================

if (isset($_POST["op"]) && $_POST["op"] == "edit"){
    $id = (int) $_POST["id"];
    $gname = $conn->QMagic($_POST["worklocname"]);
    $gdesc = $conn->QMagic($_POST["description"]);
	 	
    $sql="update mgw_workplace set name=$gname, description=$gdesc, lu_user=".$_SESSION["MGW"]->userid.", lu_date=". $conn->DBTimeStamp(time()). " WHERE id=$id";
    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}

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

// =================================================
//	              New Item Formular
// =================================================
if (isset($_GET["view"]) && $_GET["view"] == "newform") {
    $smarty->assign("suname","create");
    $smarty->assign("suclass","submit");
    $smarty->assign("mode","new");

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

// ================================================
//			Edit worklocation form
// ================================================
if (isset($_GET["view"]) && $_GET["view"] == "editform") {
    $id = (int) $_GET["id"];

    $sql = "SELECT * FROM mgw_workplace WHERE id = $id";
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();
	
    $smarty->assign("suname","edit");
    $smarty->assign("suclass","submit");
    $smarty->assign("mode","edit");
    $smarty->assign("td", $row);
    $smarty->assign("editworkloc", Lang::getParmLang(Lang::getLanguageString("editworkloc"), array($row["name"])));
	
    $smarty->display('workplacemanager_edit.tpl');
    exit();
}


// =================================================
//	              List View
// =================================================
$icons = getIconStrings();

$sql = "SELECT a.id, a.name, a.description, a.lu_date, b.username AS lu_user from mgw_workplace a LEFT JOIN mgw_users b ON a.lu_user = b.id";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
while($row = $res->FetchRow()){
    // create Action URLs
    $editlink = "<a href=\"workplacemanager.php?mod=admin&amp;obj=workloc&amp;view=editform&amp;id=". $row["id"] ."&amp;".SID."\" class=\"edit\">". $icons["edit"] . "</a>";
    $dellink = "<a href=\"workplacemanager.php?mod=admin&amp;obj=workloc&amp;view=delete&amp;id=". $row["id"] ."&amp;".SID."\" onclick=\"return confirm('".Lang::getLanguageString('reallydelworkplace')."')\" class=\"delete\">". $icons["delete"] . "</a>";

    $row['lu_date'] = $conn->UnixTimestamp($row['lu_date']);
    $row['action'] = $editlink . $icons["space"] . $dellink;
    $smarty->append("td",$row);
}	
		
$smarty->display("workplacemanager_list.tpl");
?>
