<?php
/* $Id: channels.php,v 1.43 2003/11/27 14:27:44 leyeti Exp $ */

$myEnv["module"] = "news";
$myEnv["output"] = "html";

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

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

// =================================================
//	              Channel creation
// =================================================
	 
if(isset($_POST['create'])){
    queryRights2('root_modules_news_write');

    $channelname = ($_POST['channelname']=="") ? "'[new channel]'" : $conn->QMagic($_POST["channelname"]);
    $qdesc = $conn->QMagic($_POST['description']);
    $id = (int) $_POST['id'];
    $ispublic = (int) $_POST['ispublic'];

    //check if channel name exist
    $sql = "SELECT id FROM mgw_news_channels WHERE channelname=".$channelname;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

    if($res->RecordCount()==0) {
	// get the sequence number
	$id = mgw_genID('mgw__seq_news_channels', 3);
	$sql="INSERT INTO mgw_news_channels (id, channelname, description, ispublic, owner, lu_user, lu_date) VALUES ($id, $channelname, $qdesc, '$ispublic', ".$_SESSION['MGW']->userid.", ".$_SESSION['MGW']->userid.", ". $conn->DBTimeStamp(time()). ")";

	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    }
    else {
	$smarty->assign("mess",Lang::getLanguageString("channelexists"));
	$smarty->assign("showmess",true);
	$smarty->assign("td", Array("channelname"=>htmlentities($_POST['channelname']), "description"=>htmlentities($_POST['description'])));
	if($ispublic=="1") {
	    $smarty->assign("pubcheckedyes"," checked");
	    $smarty->assign("pubcheckedno","");
	}
	else {
	    $smarty->assign("pubcheckedyes","");
	    $smarty->assign("pubcheckedno"," checked");
	}
	$smarty->assign("suname","create");
	$smarty->assign("suclass","submit");
	$smarty->assign("mode","new");
	$smarty->display('news_channel_edit.tpl');
	exit;
    }
}

// =================================================
//	              Channel modification
// =================================================
	 
if(isset($_POST['edit'])){
    queryRights2('root_modules_news_modify');
	
    $id = (int) $_POST['id'];
    $channelname = ($_POST['channelname']=="") ? "[new channel]" : $conn->QMagic($_POST["channelname"]);
    $qdesc = $conn->QMagic($_POST['description']);
    $ispublic = (int) $_POST['ispublic'];

    //check if channel name exist
    $sql = "SELECT id FROM mgw_news_channels WHERE channelname=".$channelname." AND id != ".$id;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

    if($res->RecordCount()==0) {
	$sql="UPDATE mgw_news_channels SET channelname=$channelname, description=$qdesc, ispublic='$ispublic', owner=".$_SESSION['MGW']->userid.", lu_user=".$_SESSION['MGW']->userid.", lu_date=". $conn->DBTimeStamp(time()). " WHERE id=".$id;
	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    }
    else {
	$smarty->assign("mess",Lang::getLanguageString("channelexists"));
	$smarty->assign("showmess",true);
	$smarty->assign("td", Array("id"=>$id, "channelname"=>htmlentities($_POST['channelname']), "description"=>htmlentities($_POST['description'])));
	if($ispublic=="1") {
	    $smarty->assign("pubcheckedyes"," checked");
	    $smarty->assign("pubcheckedno","");
	}
	else {
	    $smarty->assign("pubcheckedyes","");
	    $smarty->assign("pubcheckedno"," checked");
	}
	$smarty->assign("suname","edit");
	$smarty->assign("suclass","submit");
	$smarty->assign("mode","edit");
	$smarty->assign("editchannel", Lang::getParmLang(Lang::getLanguageString("editchannel"), htmlentities($_POST['channelname'])));
	$smarty->assign("list",$_POST['list']);
	$smarty->assign("searchterm",$_POST['searchterm']);
	$smarty->assign("npage",$_POST['npage']);
	$smarty->display('news_channel_edit.tpl');
	exit();
    }
}
   
// =================================================
//	              Channel delete              
// =================================================
if ($_GET['action'] == "delete" || $_POST['action'] == "delete"){
    queryRights2('root_modules_news_delete');

    // show confirmation screen if it wasnt there so far
    if(!isset($_POST['confirmation_no']) && !isset($_POST['confirmation_yes'])) {
	$id = (int) $_GET["id"];

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

	$smarty->assign("menuid", "1");
	$smarty->assign("itemid", $id);
	$smarty->assign("parms", $_SERVER["QUERY_STRING"]);
	$smarty->assign("reallydeleteitem", Lang::getLanguageString("reallydelchannel"));
	$smarty->assign("deleteheader", Lang::getLanguageString("deletechannelh"));
	$smarty->assign("deletedetail1", Lang::getLanguageString("channeltext"). ": &nbsp;". htmlentities($row["channelname"]));
	$smarty->assign("deletedetail2", Lang::getLanguageString("channeldeletenote"));

	$smarty->display($appconf["gentemplates"] ."/delete_item.tpl");
	exit();
    }

    // delete only when user said so
    if(isset($_POST['confirmation_yes'])) { 
	$id = (int) $_POST["itemid"];

	// first delete all news associated with this channel
	$sql = "DELETE FROM mgw_news WHERE channelid = ".$id;
	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	// now lets delete the channel itself		
	$sql = "DELETE FROM mgw_news_channels WHERE id = ".$id;
	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    }
}

// =================================================
//	              New Item Formular
// =================================================
if($_GET['action'] == "newform") {
    $smarty->assign("suname","create");
    $smarty->assign("suclass","submit");
    $smarty->assign("pubcheckedyes"," checked");
    $smarty->assign("pubcheckedno","");
    $smarty->assign("mode","new");

	//submenu :
	$numchan = $conn->GetRow('SELECT COUNT(id) AS numchan FROM mgw_news_channels');
	if($numchan['numchan'] > 0) $smarty->assign("submenu", "4");
	else $smarty->assign("submenu", "3");
	$smarty->assign("numchan",(int)$numchan['numchan'] + 1);
    $smarty->display('news_channel_edit.tpl');
    exit();
}

// =================================================
//	              Channel Details
// =================================================
if ($_GET['action'] == "details") {
    $id = (int) $_GET['id'];
    $sql = "SELECT * from mgw_news_channels WHERE id=".$id;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

    $row = $res->FetchRow();
    $smarty->assign("id", $id);
    $smarty->assign("td", $row);
	
    if($row["ispublic"]==1) {
	$smarty->assign("mypublic", Lang::getLanguageString("yes"));
    }
    else {
	$smarty->assign("mypublic", Lang::getLanguageString("no"));
    }
    $smarty->assign("contentheader", Lang::getLanguageString("details"));
    $smarty->display("news_channel_details.tpl");
    exit();
}


// =================================================
//	              Edit Item Formular
// =================================================
if($_GET['action'] == "editform") {
    queryRights2('root_modules_news_modify');

    $id = (int) $_GET['id'];
    $sql = "SELECT * FROM mgw_news_channels WHERE id=".$id;

    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();
    $row['channelname'] = htmlentities($row['channelname']);
    $row['description'] = htmlentities($row['description']);
    $smarty->assign("td", $row);
    $smarty->assign("suname","edit");
    $smarty->assign("suclass","submit");
    $smarty->assign("mode","edit");

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

    $smarty->assign("editchannel", Lang::getParmLang(Lang::getLanguageString("editchannel"), array($row["channelname"])));

    $smarty->assign("list",$_GET['list']);
    $smarty->assign("searchterm",$_GET['searchterm']);
    $smarty->assign("npage",$_GET['npage']);
    $smarty->display('news_channel_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"]);

$searchterm = $conn->QMagic($_GET['list']."%");

if($_GET['action'] == "alphab") {
    $sql = "SELECT * FROM mgw_news_channels WHERE channelname LIKE ".$searchterm." AND (ispublic=1 OR (ispublic=0 and owner=".$_SESSION['MGW']->userid.")) ORDER BY channelname";
}
elseif ($_POST['action'] == "qsearch") {
    $searchterm = $conn->QMagic("%".$_POST['searchterm']."%");
    $sql = "SELECT * FROM mgw_news_channels WHERE channelname LIKE ".$searchterm." AND (ispublic = 1 OR (ispublic = 0 AND owner=".$_SESSION['MGW']->userid.")) ORDER BY channelname";
}
else {
    $sql = "SELECT * FROM mgw_news_channels WHERE ispublic=1 OR (ispublic=0 AND owner=".$_SESSION['MGW']->userid.") ORDER BY channelname";
}
  		
// ========== PAGING STUFF #1 ================
session_register('curr_page');
if (isset($_GET['npage']) || IsSet($_POST['npage'])){
    $curr_page = IsSet($_POST['npage']) ? IntVal($_POST['npage']) : IntVal($_GET['npage']);
}
else {
    $curr_page = 1; // at first page
}
// ============================================
	
if(!$res = $conn->PageExecute($sql, $_SESSION['MGW']->settings["contacts_per_page"], $curr_page)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	
// ========== PAGING STUFF #2 ================
// if NOT on first page
if(!$res->AtFirstPage()) {
    $smarty->assign("prev", true); 
    $smarty->assign("npage", $res->AbsolutePage() - 1);
}
else {
    $smarty->assign("prev", false); 
}
	
// if NOT on last page
if(!$res->AtLastPage()) {
    $smarty->assign("next", true); 
    $smarty->assign("npage", $res->AbsolutePage() + 1);
}
else {
    $smarty->assign("next", false); 
}

// ============================================
if ($searchterm=="'%'") $searchterm = "";
while($row = $res->FetchRow()) {
    $myid = $row["id"];
    $smarty->append("td",Array("channelname" => $row["channelname"], "description" => $row["description"]));

    // create Action URLs
    $detlink = "<a href=\"channels.php?action=details&id=$myid&".SID."\" class=\"edit\">".$icons["details"]."</a>";

    // modify only for user with appropriate rights
    if((queryRights2_bool('root_modules_news_modify') && $row["owner"] == $_SESSION['MGW']->userid) || queryRights2_bool('root_modules_news_modifyothers')) {
	$editlink = "<a href=\"channels.php?action=editform&id=$myid&act=$action&searchterm=$searchterm&list=$list&npage=$npage&".SID."\" class=\"edit\">".$icons["edit"]."</a>";
    }
    else {
	$editlink = "";
    }

    // delete only for user with appropriate rights
    if((queryRights2_bool('root_modules_news_delete') && $row["owner"] == $_SESSION['MGW']->userid) || queryRights2_bool('root_modules_news_deleteothers')) {
	$dellink = "<a href=\"channels.php?action=delete&id=$myid&act=$action&searchterm=$searchterm&list=$list&npage=$npage&".SID."\" class=\"delete\">".$icons["delete"]."</a>";
    }
    else {
	$dellink = "";
    }

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

    $smarty->append('images', $priv);
    $smarty->append('urls', $detlink . $space . $editlink . $space . $dellink);
}		

//$smarty->assign("icon_new","<a href=\"{$phpself}?action=newform&amp;{$SESSION}\" class=\"new\">".$icons["new"]."</a>");
$smarty->assign('suclass', "submit");
$smarty->assign('searchterm', $_POST['searchterm']);
$smarty->assign('list', $_GET['list']);
$smarty->assign("hascreaterights", queryRights2_bool('root_modules_news_write'));
$smarty->assign('at', alphabet_table("channels.php"));
$smarty->assign('action', Lang::getLanguageString("action"));
$smarty->assign('suvalue', Lang::getLanguageString("search"));
$smarty->assign('table_header', Lang::getLanguageString("overview_channels"));

//submenu :
$numchan = $conn->GetRow('SELECT COUNT(id) AS numchan FROM mgw_news_channels');
if($numchan['numchan'] > 0) $smarty->assign("submenu", "5");
else $smarty->assign("submenu", "4");
	
	
$smarty->display("news_channel_list.tpl");
?>
