<?php
/* $Id: occupationmanager.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["occupname"] = trim($_POST["occupname"]);

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

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

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

   	  if($res->RecordCount() == 0) {
      
          // Create Occupation
    $id = $conn->GenID('mgw__seq_occupations', 70005);
    $gname = $conn->QMagic($_POST["occupname"]);
    $gdesc = $conn->QMagic($_POST["description"]);

    $sql="INSERT INTO mgw_occupations (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 {
          // occupationname 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_duplicateoccup"));
            
          $smarty->display('occupationmanager_edit.tpl');
          exit;
      }

    } else {
      // occupationname 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_nooccupname"));
            
      $smarty->display('occupmanager_edit.tpl');
      exit;
    }
}

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

if (isset($_POST["op"]) && $_POST["op"] == "edit"){
    $id = (int) $_POST["id"];
    $gname = $conn->QMagic($_POST["occupname"]);
    $gdesc = $conn->QMagic($_POST["description"]);
	 	
    $sql="update mgw_occupations 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 occupation
// =================================================
if (isset($_GET["view"]) && $_GET["view"] == "delete"){
    $id = (int) $_GET["id"];
    $sql = "DELETE FROM mgw_occupations 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('occupationmanager_edit.tpl');
    exit;
}

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

    $sql = "SELECT * FROM mgw_occupations 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("editoccup", Lang::getParmLang(Lang::getLanguageString("editoccup"), array($row["name"])));
	
    $smarty->display('occupationmanager_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_occupations a LEFT JOIN mgw_users b ON a.lu_user = b.id";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$urls = array();
while($row = $res->FetchRow()){
    // create Action URLs
    $editlink = "<a href=\"occupationmanager.php?mod=admin&amp;obj=occup&amp;view=editform&amp;id=". $row["id"] ."&amp;".SID."\" class=\"edit\">". $icons["edit"] . "</a>";
    $dellink = "<a href=\"occupationmanager.php?mod=admin&amp;obj=occup&amp;view=delete&amp;id=". $row["id"] ."&amp;".SID."\" onclick=\"return confirm('".Lang::getLanguageString('reallydeloccup')."')\" 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("occupationmanager_list.tpl");
?>
