<?php
/* $Id: index.php,v 1.78 2003/12/03 08:54:20 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.78 $", __FILE__);


// =================================================
//	              Toogle news to 'read'/'unread'
// =================================================
	 
if (isset($_GET['action']) && $_GET['action']=="toggle"){
toggle_read($_GET['id']);
if (isset($_GET['backurl'])) redirect($_GET['backurl']);
}

// =================================================
//	              Creating new news              
// =================================================
	 
if (isset($_POST['create'])){
    queryRights2('root_modules_news_write');

    $id = mgw_genID('mgw__seq_news');
	 	
    $qheadline = ($_POST['headline']=="") ? "'[no headline]'" : $conn->QMagic($_POST['headline']);
    $qteaser = $conn->QMagic($_POST['teaser']);
    $qcontent = $conn->QMagic($_POST['content']);
	
	//archive mode & expiration
	if ( $_POST['archive_mode']!=0 || (!(isset($_POST['expire_year']) && $_POST['expire_year']!="") && 
		!(isset($_POST['expire_month']) && $_POST['expire_month']!="") && 
		!(isset($_POST['expire_day']) && $_POST['expire_day']!=""))
		) $qexpire = $conn->DBTimeStamp(mktime(0,0 ,0 ,01 ,01 ,2000 ));
	else {
		$exp_year = (isset($_POST['expire_year']) && $_POST['expire_year']!="") ? $_POST['expire_year'] : date("Y",time());
		$exp_month = (isset($_POST['expire_month']) && $_POST['expire_month']!="") ? $_POST['expire_month'] : date("m",time());
		$exp_day = (isset($_POST['expire_day']) && $_POST['expire_day']!="") ? $_POST['expire_day'] : date("d",time());
		
		$qexpire = $conn->DBTimeStamp(mktime(0,0 ,0 ,$exp_month ,$exp_day ,$exp_year ));
	}
	
	$active = $_POST['archive_mode']==2 ? '0' : '1';
	
    $channelid = (int) $_POST['channelid'];
    $projectid = (int) $_POST['projectid'];
    $ispublic = (int) $_POST['ispublic'];

    $sql= "INSERT INTO mgw_news (id, headline, teaser, content, channelid, projectid, ispublic, owner, expire_date, isactive, lu_user, lu_date) VALUES ($id, $qheadline, $qteaser, $qcontent, $channelid, $projectid, $ispublic, ".$_SESSION["MGW"]->userid.", $qexpire, $active, ".$_SESSION["MGW"]->userid.",". $conn->DBTimeStamp(time()). ")";
    if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}

// =================================================
//	              Editting news              
// =================================================
	 
if (isset($_POST['edit']))   {
    query_modify_right($_POST['id']);

    $qheadline = ($_POST['headline']=="") ? "'[no headline]'" : $conn->QMagic($_POST['headline']);
    $qteaser = $conn->QMagic($_POST['teaser']);
    $qcontent = $conn->QMagic($_POST['content']);

	$qold_headline = ($_POST['old_headline']=="") ? "'[no headline]'" : $conn->QMagic($_POST['old_headline']);
	$qold_teaser = $conn->QMagic($_POST['old_teaser']);
	$qold_content = $conn->QMagic($_POST['old_content']);
	
	//update lu_date + set 'not_read', only if headline/teaser/content changed :
	if ($qheadline!=$qold_headline || $qteaser!=$qold_teaser || $qcontent!=$qold_content) {
		//set not_read for all (=delete records in table)
		$sql_not_read = "DELETE FROM mgw_news_consult WHERE newsid='".$_POST['id']."'";
		if(!$conn->Execute($sql_not_read)) die(showSQLerror($sql_not_read, $conn->ErrorMsg(), __LINE__, __FILE__));
		
		$sql_lu_date = " lu_date=". $conn->DBTimeStamp(time())." ";
	}
	else $sql_lu_date = " lu_date=lu_date ";
	
	//archive mode & expiration:
	if ( $_POST['archive_mode']!=0 || (!(isset($_POST['expire_year']) && $_POST['expire_year']!="") && 
		!(isset($_POST['expire_month']) && $_POST['expire_month']!="") && 
		!(isset($_POST['expire_day']) && $_POST['expire_day']!=""))
		) $qexpire = $conn->DBTimeStamp(mktime(0,0 ,0 ,01 ,01 ,2000 ));
	else {
		$exp_year = (isset($_POST['expire_year']) && $_POST['expire_year']!="") ? $_POST['expire_year'] : date("Y",time());
		$exp_month = (isset($_POST['expire_month']) && $_POST['expire_month']!="") ? $_POST['expire_month'] : date("m",time());
		$exp_day = (isset($_POST['expire_day']) && $_POST['expire_day']!="") ? $_POST['expire_day'] : date("d",time());
		
		$qexpire = $conn->DBTimeStamp(mktime(0,0 ,0 ,$exp_month ,$exp_day ,$exp_year ));
	}
	
	if ($_POST['archive_mode']==2) {
	   $active = '0' ;
	   $qexpire = $conn->DBTimeStamp(time());
	   $_GET['action'] = "list_archive";
	}
	else $active = '1';             
	
	
    $channelid = (int) $_POST['channelid'];
    $projectid = (int) $_POST['projectid'];
    $ispublic = (int) $_POST['ispublic'];
	 	
	$sql= "UPDATE mgw_news SET headline=$qheadline, teaser=$qteaser, content=$qcontent, channelid=$channelid, projectid=$projectid, ispublic=$ispublic, owner=".$_SESSION["MGW"]->userid.", expire_date = $qexpire, isactive = $active, lu_user=".$_SESSION["MGW"]->userid.", ".$sql_lu_date." WHERE id=".(int) $_POST['id'];
    if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}
   
// =================================================
//	              News delete              
// =================================================

if ((isset($_POST['action']) && $_POST['action'] == "delete") || (isset($_GET['action']) && $_GET['action'] == "delete")){

    $id = isset($_GET['id']) ? $_GET['id'] : (isset($_POST['itemid']) ? $_POST['itemid'] : "-1");
    query_delete_right($id);
	
    // show confirmation screen if it wasnt there so far
    if(!isset($_POST['confirmation_no']) && !isset($_POST['confirmation_yes'])) {
	$sql = "SELECT headline, teaser, content FROM mgw_news WHERE id=".(int) $_GET['id'];
	
	if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row = $res->FetchRow();
		
	$smarty->assign("menuid", "1");
	$smarty->assign("itemid", (int) $_GET['id']);
	$smarty->assign("parms", $_SERVER["QUERY_STRING"]);
	$smarty->assign("reallydeleteitem", Lang::getLanguageString("reallydelnews"));
	$smarty->assign("deleteheader", Lang::getLanguageString("deletenewsh"));
	$smarty->assign("deletedetail1", $row["headline"]);
	$smarty->assign("deletedetail2", $row["teaser"]);
	$smarty->assign("deletedetail3", $row["content"]);
	
	$smarty->display($appconf["gentemplates"] ."/delete_item.tpl");
	exit();
    }
	
    // delete only when user said so
    if(isset($_POST['confirmation_yes'])) {
	$sql = "DELETE FROM mgw_news WHERE id=".(int) $_POST['itemid'];
	if(!$conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    }
	if (isset($_POST['action'])) unset($_POST['action']);
	if (isset($_GET['action'])) unset($_GET['action']);
}
	 
// =================================================
//	              New Item Formular
// =================================================
if (isset($_GET['action']) && $_GET['action'] == "newform") {
    queryRights2('root_modules_news_write');

    include(INCLUDEPATH . "mSelectBox.class.php");
		
    $smarty->assign("suname","create");
    $smarty->assign("suvalue",Lang::getLanguageString("create"));
    $smarty->assign("suclass","submit");

    $smarty->assign("contentheader", "Neue News erstellen");

    $sb6 = new mSelectBox("projectid");
    $sb6->setContentType("sql");
    $sb6->setSQLvalue("projectid");
    $sb6->setSQLcontent("title");
	$sql = "SELECT project_id FROM mgw_projects";
	if($conn->Execute($sql)) {
	      $sb6->addContent("select project_id as projectid, title from mgw_projects order by title");
    }
	else if($conn->MetaError() != DB_ERROR_NOSUCHTABLE) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $sb6->addOption("0",Lang::getLanguageString("noprojectmapping"), "pre");
    $smarty->assign("projects", $sb6->draw());

    $sb7 = new mSelectBox("channelid");
    $sb7->setContentType("sql");
    $sb7->setSQLvalue("chid");
    $sb7->setSQLcontent("channelname");
    $sb7->addContent("select id as chid, channelname from mgw_news_channels order by channelname");
    $smarty->assign("channels", $sb7->draw());
		
    $smarty->assign("pubcheckedyes"," checked");
    $smarty->assign("submenu", "3");
    $smarty->display('news_edit.tpl');
    exit();
}
 
// =================================================
//	    Show news details
// =================================================
if (isset($_GET['action']) && $_GET['action'] == "details") {
		
	// toggle read to on :
	toggle_read($_GET['id'],"on");
	
    $sql = "SELECT t1.*, t2.channelname FROM mgw_news t1, mgw_news_channels t2 WHERE t1.channelid=t2.id AND t1.id = ".(int) $_GET['id'];
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();

    $smarty->assign("td",$row);
    $smarty->assign("textformatted",nl2br($row["content"]));
	
	//expiration
    $exp_ts = $conn->UnixTimeStamp($row["expire_date"]) != mktime(0,0 ,0 ,01 ,01 ,2000 ) ? $conn->UnixTimeStamp($row["expire_date"]) : 0;
    $smarty->assign("expire_day", ($exp_ts!=0 ? date("d",$exp_ts) : ""));
    $smarty->assign("expire_month", ($exp_ts!=0 ? date("m",$exp_ts) : ""));
    $smarty->assign("expire_year", ($exp_ts!=0 ? date("Y",$exp_ts) : ""));
	//archive mode
    $smarty->assign("archive_mod_0", ($exp_ts!=0 && (int)$row["isactive"]==1) ? "1" : "");
    $smarty->assign("archive_mod_1", ($exp_ts==0 && (int)$row["isactive"]==1) ? "1" : "");
    $smarty->assign("archive_mod_2", (int)$row["isactive"]==0 ? "1" : "");
	
    //author full name : 
	$sql_a = "SELECT ".$conn->Concat("firstname","' '","lastname")." as authorname FROM mgw_news n, mgw_users u WHERE n.id = ".(int) $_GET['id']." AND u.id=n.owner";
	if(!$res_a = $conn->Execute($sql_a)) die(showSQLerror($sql_a, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row_a = $res_a->FetchRow();
	$smarty->assign("authorname",$row_a["authorname"]);
	
    $icons = getIconStrings();
    $smarty->assign("edit_icon", query_modify_right_bool($_GET['id']) ? $icons["edit"] : "");
    $smarty->assign("del_icon", query_delete_right_bool($_GET['id']) ? $icons["delete"] : "");
    $smarty->assign("icons", $icons);
   
    if (isset($_GET['backurl'])) $smarty->assign("backurl",$_GET['backurl']);
    
    $smarty->display('news_details.tpl');
    exit();
}


// =================================================
//	              Edit Item Formular
// =================================================
if (isset($_GET['action']) && $_GET['action'] == "editform") {
	query_modify_right($_GET['id']);
	
    include(INCLUDEPATH . "mSelectBox.class.php");
    $sql = "SELECT * FROM mgw_news WHERE id = ".(int) $_GET['id'];

    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();
                
    $smarty->assign("suname","edit");
    $smarty->assign("suvalue",Lang::getLanguageString("save"));
    $smarty->assign("suclass","submit");
    $smarty->assign("td", $row);

	//expiration
	$exp_ts = $conn->UnixTimeStamp($row["expire_date"]) != mktime(0,0 ,0 ,01 ,01 ,2000 ) ? $conn->UnixTimeStamp($row["expire_date"]) : 0;
	$smarty->assign("expire_day", ($exp_ts!=0 ? date("d",$exp_ts) : ""));
	$smarty->assign("expire_month", ($exp_ts!=0 ? date("m",$exp_ts) : ""));
	$smarty->assign("expire_year", ($exp_ts!=0 ? date("Y",$exp_ts) : ""));
	//archive mode
	$smarty->assign("archive_mod_0", ($exp_ts!=0 && (int)$row["isactive"]==1) ? "1" : "");
	$smarty->assign("archive_mod_1", ($exp_ts==0 && (int)$row["isactive"]==1) ? "1" : "");
	$smarty->assign("archive_mod_2", (int)$row["isactive"]==0 ? "1" : "");
	
    $sb6 = new mSelectBox("projectid");
    $sb6->setContentType("sql");
    $sb6->setSQLvalue("projectid");
    $sb6->setSQLcontent("title");
	$sql = "SELECT project_id FROM mgw_projects";
    if($conn->Execute($sql)) {
	      $sb6->addContent("select project_id as projectid, title from mgw_projects order by title");
    }
	else if($conn->MetaError() != DB_ERROR_NOSUCHTABLE) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $sb6->addOption("0",Lang::getLanguageString("noprojectmapping"), "pre");
    $smarty->assign("projects", $sb6->draw($row["projectid"]));

    $sb7 = new mSelectBox("channelid");
    $sb7->setContentType("sql");
    $sb7->setSQLvalue("chid");
    $sb7->setSQLcontent("channelname");
    $sb7->addContent("select id as chid, channelname from mgw_news_channels order by channelname");
    $smarty->assign("channels", $sb7->draw($row["channelid"]));
				
    if($row["ispublic"]==1) $smarty->assign("pubcheckedyes"," checked");
    else $smarty->assign("pubcheckedno"," checked");
    $smarty->assign("mode", "edit");

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


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

if (!isset($_GET['action']) || (isset($_GET['action']) && $_GET['action']== "list_current" ) ) {

	check_and_autoarchive();

	$sql = "SELECT s1.channelname AS channelname, s1.id AS channelid, COUNT(*) AS anz FROM mgw_news n LEFT JOIN mgw_news_channels s1 ON n.channelid = s1.id WHERE n.isactive = '1' GROUP BY s1.id, s1.channelname ORDER BY channelid, channelname";
	if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

if($res->RecordCount() > 0) {
    $arrayid = 0;
    while($row = $res->FetchRow()){
	$smarty->append('main', $row['channelname']);
	$smarty->append('count', $row['anz']);

	$sql2 = "SELECT * FROM mgw_news WHERE isactive = '1' AND channelid=".(int)$row['channelid'];
	if(!$res2 = $conn->Execute($sql2)) die(showSQLerror($sql2, $conn->ErrorMsg(), __LINE__, __FILE__));
	while($row2 = $res2->FetchRow()){
	    $newsarray[$arrayid][] = $row2['headline'];
	    $idarray[$arrayid][] = $row2['id'];
		//does the news have been read ?:
		$sql_read = "SELECT userid FROM mgw_news_consult WHERE userid='".$_SESSION["MGW"]->userid."' AND newsid='".$row2['id']."'";
		if(!$res_read = $conn->Execute($sql_read)) exit(showSQLerror($sql_read, $conn->ErrorMsg(), __LINE__, __FILE__));
		$row_read = $res_read->FetchRow();
		$not_readarray[$arrayid][] = $row_read["userid"]!=$_SESSION["MGW"]->userid ? "1" : "";
	}
	$arrayid++;
    }
}
else{
    $newsarray = array();
    $idarray = array();
}

$smarty->assign('total', Lang::getLanguageString('total'));
$smarty->assign('news', $newsarray);
$smarty->assign('ids', $idarray);
$smarty->assign('not_read', $not_readarray);
$smarty->assign("backurl",urlencode(ROOTURL."modules/news/index.php?action=list_current&".SID));
$smarty->assign('pagetitle', Lang::getLanguageString("current_news"));
$smarty->assign('submenu', "1");
$smarty->display('news_list.tpl');
exit();
}


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

if (isset($_GET['action']) && $_GET['action']== "list_archive" ) {

$sql = "SELECT s1.channelname AS channelname, s1.id AS channelid, COUNT(*) AS anz FROM mgw_news n LEFT JOIN mgw_news_channels s1 ON n.channelid = s1.id WHERE n.isactive = '0' GROUP BY s1.id, s1.channelname ORDER BY channelid, channelname";

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

if($res->RecordCount() > 0) {
    $arrayid = 0;
    while($row = $res->FetchRow()){
	$smarty->append('main', $row['channelname']);
	$smarty->append('count', $row['anz']);

	$sql2 = "SELECT * FROM mgw_news WHERE isactive = '0' AND channelid=".(int)$row['channelid'];
	if(!$res2 = $conn->Execute($sql2)) die(showSQLerror($sql2, $conn->ErrorMsg(), __LINE__, __FILE__));
	while($row2 = $res2->FetchRow()){
	    $newsarray[$arrayid][] = $row2['headline'];
	    $idarray[$arrayid][] = $row2['id'];
	}
	$arrayid++;
    }	
}

$smarty->assign('total', Lang::getLanguageString('total'));
$smarty->assign('news', $newsarray);
$smarty->assign('ids', $idarray);
$smarty->assign('pagetitle', Lang::getLanguageString("archived_news"));
$smarty->assign('submenu', "2");
$smarty->display('news_list.tpl');

}
?>
