<?php
/* $Id: category.class.php,v 1.11 2003/11/08 18:11:20 emailtotom Exp $ */

class Category{

    function opCreate() {
        global $conn, $smarty, $HTTP_POST_VARS, $appconf;
	queryRights2('root_modules_notes_categories_create');

	    $id = mgw_genID('mgw__seq_notes_categories');
	    $name = $conn->QMagic($HTTP_POST_VARS["name"]);
	    $description = $conn->QMagic($HTTP_POST_VARS["description"]);

	    $sql="INSERT INTO mgw_notes_categories (id, name, description, lu_user, lu_update) VALUES ($id, $name, $description, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
	    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	    return true;
	}

    function opUpdate() {
        global $conn, $smarty, $HTTP_POST_VARS, $appconf;
	queryRights2('root_modules_notes_categories_modify');

	    $id = (int) $HTTP_POST_VARS["id"];
	    $name = $conn->QMagic($HTTP_POST_VARS["name"]);
	    $description = $conn->QMagic($HTTP_POST_VARS["description"]);

	    $sql="UPDATE mgw_notes_categories SET name=$name, description=$description, lu_user=".$_SESSION["MGW"]->userid.", lu_update=".$conn->DBTimeStamp(time())." WHERE id=".$id;

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

	    return true;
	}

    function opDelete($id){
        global $conn;
	queryRights2('root_modules_notes_categories_delete');

	$sql = "DELETE FROM mgw_notes_categories WHERE id=".(int)$id;
	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$sql = "UPDATE mgw_notes SET catid=0 WHERE catid=".(int)$id;
	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

        return true;
    }

    function viewDelete($id) {
        global $conn,$smarty,$appconf, $HTTP_SERVER_VARS;
	queryRights2('root_modules_notes_categories_delete');

	    $sql = "SELECT * FROM mgw_notes_categories WHERE id=".(int)$id;
	    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    $row = $res->FetchRow();

	    $smarty->assign("menuid", "3");
	    $smarty->assign("itemid", $id);
	    $smarty->assign("parms", str_replace("delete","list",$HTTP_SERVER_VARS["QUERY_STRING"]));
	    $smarty->assign("deleteitem", Lang::getLanguageString("deleteitem"));
	    $smarty->assign("reallydeleteitem", Lang::getLanguageString("reallydeletecategory"));
	    $smarty->assign("deleteheader", Lang::getLanguageString("deletecategory"));
	    $smarty->assign("deletedetail1", $row["name"]);
	    $smarty->assign("deletedetail2", $row["description"]);
	    $smarty->assign("deletedetail3", "");
	    $smarty->assign("suclass","submit");
	    $smarty->assign("js", "");
	    $smarty->display($appconf["gentemplates"] ."/delete_item.tpl");

	    return true;
	}

    function viewNewForm() {
        global $smarty, $appconf;
	queryRights2('root_modules_notes_categories_create');

	    $smarty->assign("contentheader",Lang::getLanguageString("newcategory"));
	    $smarty->assign("suname","create");
	    $smarty->assign("suclass","submit");
	    $smarty->assign("mode","new");
	    $smarty->assign("js", "");
	    $smarty->display('notes_categories_edit.tpl');

	    return true;
	}


    function viewEditForm($id) {
        global $smarty, $conn, $appconf;
	queryRights2('root_modules_notes_categories_modify');

	    $sql = "SELECT * FROM mgw_notes_categories WHERE id=".(int)$id;
	    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    $row = $res->FetchRow();

	    $smarty->assign("id", $id);
	    $smarty->assign("td", $row);
	    $smarty->assign("suname","edit");
	    $smarty->assign("suclass","submit");
	    $smarty->assign("contentheader", Lang::getParmLang(Lang::getLanguageString("editcategory"),array($row["name"])));
	    $smarty->assign("mode","edit");
	    $smarty->assign("js", "");

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

	    return true;
	}

    function viewList() {
        global $conn, $smarty;
	queryRights2('root_modules_notes_categories_list');

	$icons = getIconStrings();
	$smarty->assign("prevsym", $icons["prev"]);
	$smarty->assign("nextsym", $icons["next"]);

	$sql = "SELECT id,name,description FROM mgw_notes_categories ORDER BY name";

	// paging part 1
	if (isset($HTTP_GET_VARS['npage'])) $curr_page = $HTTP_GET_VARS['npage'];
	else $curr_page = 1; // at first page
	// pagin part 1

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

	// paging part 2
	// if NOT on first page
	if(!$res->AtFirstPage()) {
	    $smarty->assign("_prev", true);
	    $smarty->assign("npage", $res->AbsolutePage() - 1);
	}

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

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

	while($row = $res->FetchRow()){
	    $myid = $row["id"];
	    $smarty->append("td",$row);

	    // create action urls
	    $editlink = (queryRights2_bool('root_modules_notes_categories_modify')) ?
		"<a href=\"index.php?mod=notes&amp;obj=category&amp;view=editform&amp;id=$myid&amp;".SID."\" CLASS=\"edit\">".$icons["edit"]."</a>" :
		"";
	    $dellink = (queryRights2_bool('root_modules_notes_categories_delete')) ?
		"<a href=\"index.php?mod=notes&amp;obj=category&amp;view=delete&amp;id=$myid&amp;".SID."\" CLASS=\"delete\">".$icons["delete"]."</a>" :
		"";
	    $urls[] = $editlink . $icons["space"] . $dellink;
	}

	$smarty->assign('urls', (isset($urls) ? $urls : ""));
	$smarty->assign("contentheader", Lang::getLanguageString("categories"));
	$smarty->assign('writeperm',	queryRights2_bool('root_modules_notes_categories_create'));
	$smarty->assign('js', "");
	$smarty->display('notes_categories_list.tpl');

	return true;
    }
}
?>