<?php
/* $Id: index.php,v 1.67 2003/11/13 12:23:51 k-fish Exp $ */

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

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

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

// =================================================
//	              Eintrag createn              
// =================================================
if (isset($_POST["create"])){
    $id = mgw_genID('mgw__seq_partprog');

    $name = $conn->QMagic($_POST["name"]);
    $company = $conn->QMagic($_POST["company"]);
    $account = $conn->QMagic($_POST["account"]);
    $pw = $conn->QMagic($_POST["pw"]);
    $email = $conn->QMagic($_POST["email"]);
    $url = $conn->QMagic($_POST["url"]);
    $description = $conn->QMagic($_POST["description"]);
    $public = (int) $_POST['public'];

    $sql="INSERT INTO mgw_partprog (id, name, company, account, pw, url, description, lu_user, lu_date, email, ispublic, owner)	VALUES ($id, $name, $company, $account, $pw, $url, $description, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).", $email, $public, ".$_SESSION["MGW"]->userid.")";

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

// =================================================
//	              Eintrag ändern              
// =================================================
	 
if (isset($_POST["edit"])){
    $id = (int)$_POST["id"];
    $name = $conn->QMagic($_POST["name"]);
    $company = $conn->QMagic($_POST["company"]);
    $account = $conn->QMagic($_POST["account"]);
    $pw = $conn->QMagic($_POST["pw"]);
    $email = $conn->QMagic($_POST["email"]);
    $url = $conn->QMagic($_POST["url"]);
    $description = $conn->QMagic($_POST["description"]);
    $public = (int) $_POST['public'];

    $sql="UPDATE mgw_partprog SET name=$name, company=$company, account=$account, pw=$pw, email=$email, url=$url, description=$description, lu_user=".$_SESSION["MGW"]->userid.", lu_date=".$conn->DBTimeStamp(time()).", ispublic=$public WHERE id=$id";
    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}
   
// =================================================
//	              PARTPROG delete              
// =================================================
if (isset($_GET["action"]) && $_GET["action"] == "delete"){
    // show confirmation screen if it wasn't there so far
    $sql = "SELECT * FROM mgw_partprog WHERE id=".(int)$_GET["id"];

    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();
		
    $smarty->assign("menuid", "7");
    $smarty->assign("parms", str_replace("delete","list",$_SERVER["QUERY_STRING"]));
    $smarty->assign("deleteitem", Lang::getLanguageString("deleteitem"));
    $smarty->assign("deleteheader", Lang::getLanguageString("deleteheader"));
    $smarty->assign("deletedetail1", $row["name"]);
    $smarty->assign("deletedetail2", $row["company"]);
    $smarty->assign("deletedetail3", $row["account"]);
    $smarty->assign("suclass","submit");

    $smarty->display($appconf["gentemplates"] ."/delete_item.tpl");
    exit();
}
elseif(isset($_POST["confirmation_yes"])){
    // delete only when user said so
    $sql = "DELETE FROM mgw_partprog WHERE  id=".(int)$_GET["id"];
    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}

// =================================================
//	              New Item Formular
// =================================================
if (IsSet($_GET["action"]) && $_GET["action"] == "newform") {
    $smarty->assign("suname","create");
    $smarty->assign("suclass","submit");
    $smarty->assign("mode","new");
    $smarty->assign("pubcheckedno", " checked");
		
    $smarty->display('partprog_edit.tpl');
    exit();
}
 
// =================================================
//	              Detail Ansicht
// =================================================
if (IsSet($_GET["action"]) && $_GET["action"] == "details") {
    $sql = "SELECT * FROM mgw_partprog WHERE id=".(int)$_GET["id"]." AND (ispublic=1 OR owner=".$_SESSION['MGW']->userid.")";
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();

    $smarty->assign("id", $id);
    $smarty->assign("td", $row);
    $smarty->assign("name", Lang::getLanguageString("name"));
    $smarty->assign("company", Lang::getLanguageString("company"));
    $smarty->assign("account", Lang::getLanguageString("account"));
    $smarty->assign("password", Lang::getLanguageString("password"));
    $smarty->assign("url", Lang::getLanguageString("url"));
    $smarty->assign("email", Lang::getLanguageString("email"));
    $smarty->assign("description", Lang::getLanguageString("desc"));
    $smarty->assign("contentheader", Lang::getLanguageString("detailstext").$row["name"]);
    $smarty->assign("public", Lang::getLanguageString("public"));
    if($row["ispublic"]==1) $smarty->assign("ispublic", Lang::getLanguageString("yes"));	
    else $smarty->assign("ispublic", Lang::getLanguageString("no"));	

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


// =================================================
//	              Edit Item Formular
// =================================================
if(isset($_GET["action"]) && $_GET["action"] == "editform") {
    $sql = "SELECT * FROM mgw_partprog WHERE id=".(int)$_GET['id'];
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();

    if($row["ispublic"]==1) $smarty->assign("pubcheckedyes", " checked");
    else $smarty->assign("pubcheckedno", " checked");

    $smarty->assign("id", (int)$_GET['id']);
    $smarty->assign("td", $row);
    $smarty->assign("myurl", $row["url"]);
    $smarty->assign("suname","edit");
    $smarty->assign("suclass","submit");
    $smarty->assign("mode","edit");

    $smarty->assign("editpp", Lang::getParmLang($langArr["editpp"],array($row["name"])));

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


// =================================================
//	              List View
// =================================================

// if user wants icons, assign icons if not -> not
$icons = getIconStrings();
$space = $icons["space"];
$smarty->assign("prevsym", $icons["prev"]);
$smarty->assign("nextsym", $icons["next"]);

if( isset($_GET["action"]) && $_GET["action"] == "alphab") {
    $searchterm = $conn->QMagic($_GET['list']."%");
    $sql = "SELECT * FROM mgw_partprog WHERE name LIKE ".$searchterm." ORDER BY name";
} elseif(isset($_POST["action"]) && $_POST["action"] == "qsearch") {
    $searchterm = $conn->QMagic("%".$_POST["searchterm"]."%");
    $sql = "SELECT * FROM mgw_partprog WHERE name LIKE ".$searchterm;
} else {
    $sql = "SELECT * FROM mgw_partprog WHERE ispublic=1 OR (ispublic=0 AND owner=".$_SESSION['MGW']->userid.") ORDER BY name";
}
  		
// ========== PAGING STUFF 1 ================
if (isset($_GET['npage'])){
    $_SESSION["curr_page"] = $_GET['npage'];
} else {
    $_SESSION["curr_page"] = 1; // at first page
}
// ============================================

if(!$res = $conn->PageExecute($sql, $_SESSION['MGW']->settings["contacts_per_page"], $_SESSION['curr_page'])) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

// ========== PAGING STUFF 2 ================
// if NOT on first page
if(!$res->AtFirstPage()) {
    $smarty->assign("_prev", true); 
    $smarty->assign("ppage", $res->AbsolutePage() - 1);
}

// if NOT on last page
if(!$res->AtLastPage()) {
    $smarty->assign("_next", true); 
    $smarty->assign("npage", $res->AbsolutePage() + 1);
}
// ============================================

while($row = $res->FetchRow()){
    // create action urls
    $myid = $row["id"];
    $detlink = "<a href=\"index.php?action=details&id=".$myid."&".SID."\" class=\"edit\">".$icons["details"]."</a>";
    $editlink = "<a href=\"index.php?action=editform&id=".$myid."&".SID."\" class=\"edit\">".$icons["edit"]."</a>";
    $dellink = "<a href=\"index.php?action=delete&id=".$myid."&".SID."\" class=\"delete\">".$icons["delete"]."</a>";

    if($row["ispublic"]==1) $private = "&nbsp;";
    else $private = $icons["private"];

    $smarty->append("priv",$private);
    $smarty->append("urls", $detlink . $space . $editlink . $space . $dellink);
    $smarty->append("urlto", "<a href=\"".ROOTURL."norefer.php?url=" . rawurlencode($row["url"]) . "\" target=\"new\">" . $row["url"] . "</a>");
    $smarty->append("td",$row);
}	

$smarty->display('partprog_list.tpl');
?>
