<?php
/* $Id: contactgroup.class.php,v 1.21 2003/12/02 14:45:01 k-fish Exp $ */

class ContactGroup {

    function opCreate(){
	global $conn;

	$id = mgw_genID('mgw__seq_contacts_groups', 4);
	$name = $conn->QMagic(isset($_POST["name"])?$_POST["name"]:"");
	$desc = $conn->QMagic(isset($_POST["desc"])?$_POST["desc"]:"");
	queryRights2('root_modules_contact_group_create');

	$sql="SELECT id FROM mgw_contacts_groups WHERE name_key=$name";
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	if (!$res->FetchRow()) { 
	    $sql="INSERT INTO mgw_contacts_groups (id, name_key, description, lu_user, lu_date) VALUES ($id, $name, $desc, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
	    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    $GLOBALS['log']->message(MGW_NOTICE, "Group created successfully: ".$_POST["name"], __LINE__, __FILE__);
	    $GLOBALS['notify']->message("Group created successfully: ".$_POST["name"], NOTIFY_SCREEN);
	}
	else{
	    $GLOBALS['log']->message(MGW_WARNING, "Skipped group creation, group already present: ".$_POST["name"], __LINE__, __FILE__);
	    $GLOBALS['notify']->message("Skipped group creation, group already present: ".$_POST["name"], NOTIFY_SCREEN);
	}
	return true;
    }

    function opUpdate($id){
	global $conn;
	queryRights2('root_modules_contact_group_modify');

	$name = $conn->QMagic(isset($_POST["name"])?$_POST["name"]:"");
	$desc = $conn->QMagic(isset($_POST["desc"])?$_POST["desc"]:"");

	$sql="UPDATE mgw_contacts_groups SET name_key=$name, description=$desc, lu_user=".$_SESSION["MGW"]->userid.", lu_date=".$conn->DBTimeStamp(time())." WHERE id=".(int)$id;
   	if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	return true;
    }

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

	$sql = "DELETE FROM mgw_contacts_groups WHERE id=".(int)$id;
	if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	$sql="UPDATE mgw_contacts SET groupid=0 WHERE groupid=".(int)$id;
	if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	$sql="UPDATE mgw_companies SET groupid=0 WHERE groupid=".(int)$id;
	if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	return true;
    }

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

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

	$smarty->assign("menuid", "1");
	$smarty->assign("itemid", $id);
	$smarty->assign("parms", str_replace("delete","list",$_SERVER["QUERY_STRING"]));
	$smarty->assign("reallydeleteitem", Lang::getLanguageString("reallydelgroup"));
	$smarty->assign("deleteheader", Lang::getLanguageString("deletegrouph"));
	$smarty->assign("deletedetail1", $row["name_key"]);
	$smarty->assign("deletedetail2", $row["description"]);
	$smarty->assign("suclass", "submit");		
	$smarty->display($appconf["gentemplates"] ."/delete_item.tpl");

	return true;
    }

    function viewNewForm(){
	global $smarty;
	include(INCLUDEPATH . "mSelectBox.class.php");
	queryRights2('root_modules_contact_group_create');

	$smarty->assign("countrysb", createCountrySB("country"));
	$smarty->assign("suname", "create");
	$smarty->assign("suvalue", Lang::getLanguageString("create"));
	$smarty->assign("suclass", "submit");
	$smarty->assign("mode", "new");
	$smarty->assign("contentheader", Lang::getLanguageString("createnewgroup"));

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

	return true;
    }

    function viewEditForm($id){
	global $conn, $smarty;
	include(INCLUDEPATH . "mSelectBox.class.php");
	queryRights2('root_modules_contact_group_modify');

	$sql = "SELECT * FROM mgw_contacts_groups WHERE id=".(int)$id;
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row = $res->FetchRow();
    
	$smarty->assign("td", $row);
	$smarty->assign("id",$id);
	$smarty->assign("suname","edit");
	$smarty->assign("suvalue",Lang::getLanguageString("save"));
	$smarty->assign("suclass","submit");
	$smarty->assign("contentheader", Lang::getLanguageString("editgroup").": ".$row["name_key"]);
		
	$smarty->display('contact_groups_edit.tpl');

	return true;
    }

    function viewDetails($id){
	global $conn, $smarty;
	include(INCLUDEPATH . "mSelectBox.class.php");

	$sql = "SELECT a.*,b.username AS lu_user FROM mgw_contacts_groups a LEFT JOIN mgw_users b ON a.lu_user=b.id WHERE a.id=".(int)$id;
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row = $res->FetchRow();
	$row['description'] = nl2br(htmlspecialchars($row['description']));
	$row['lu_date'] = $conn->UnixDate($row['lu_date']);
	$smarty->assign("td", $row);
	$smarty->display('contact_groups_details.tpl');

	return true;
    }

    function viewList(){
	global $conn, $smarty;
	$icons = getIconStrings();

	$sql = "SELECT a.*, b.username FROM mgw_contacts_groups a LEFT JOIN mgw_users b ON a.lu_user=b.id ORDER BY a.name_key";

	if (isset($_GET['npage']) || (isset($_POST['npage']) and $_POST['npage'])){
	    $curr_page = isset($_POST['npage']) ? $_POST['npage'] : $_GET['npage'];
	} else {
	    $curr_page = 1; // at first page
	}  		
	if(!$res = $conn->PageExecute($sql, $_SESSION["MGW"]->settings["contacts_per_page"], $curr_page)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	// 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()) {
	    $row['lu_date'] = $conn->UnixDate($row['lu_date']);
	    $smarty->append("td",$row);		
		
	    // aktion urls zusammenbauen
	    $detlink = "<a href=\"index.php?mod=contact&amp;obj=group&amp;view=details&amp;id=".$row["id"]."&amp;".SID."\" class=\"edit\">".$icons["details"]."</a>";
	    
	    // modify only for user with appropriate rights
	    if(queryRights2_bool("root_modules_contact_group_modify")) {
		$editlink = "<a href=\"index.php?mod=contact&amp;obj=group&amp;view=editform&amp;id=". $row["id"] ."&amp;".SID."\" class=\"edit\">".$icons["edit"]."</a>";
	    }
	    else {
		$editlink = "";
	    }
	
	    // delete only for user with appropriate rights
	    if(queryRights2_bool("root_modules_contact_group_delete")) {
		$dellink = "<a href=\"index.php?mod=contact&amp;obj=group&amp;view=delete&amp;id=". $row["id"]. "&amp;".SID."\" class=\"delete\">".$icons["delete"]."</a>";
	    }
	    else {
		$dellink = "";
	    }
	
	    $space = "&nbsp;&nbsp;";
	    $urls[] = $detlink . $space . $editlink . $space . $dellink;
	    $smarty->assign('urls', $urls);
	}	
	
	$smarty->assign("createdby",Lang::getLanguageString("createdby"));
	$smarty->assign("created_date", Lang::getLanguageString("created_date"));
	$smarty->assign("hascreaterights", queryRights2_bool('root_modules_contact_group_create'));
	$smarty->assign('at', alphabet_table("contact_comp.php"));
	$smarty->assign('suname', Lang::getLanguageString("search"));
	$smarty->assign('suclass', "submit");

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

	return true;
    }
}
?>