<?php

/* $Id: news_func.inc.php,v 1.3 2003/12/01 17:17:33 leyeti Exp $ */

//=========================================
// 		Check for delete right on a news
//=========================================
// @return boolean
function query_delete_right_bool($newsid){ //check if current user as delete right
global $conn;
if(queryRights2_bool('root_modules_news_deleteothers')) return true;
else if(queryRights2_bool('root_modules_news_delete')) // user can not delete for others, now can he delete for him ?
	{
		//user can delete only for him : does the news belong to him ?
		$sql = "SELECT owner FROM mgw_news WHERE id = '$newsid'";
		if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
		$row = $res->FetchRow();
		if((int)$row["owner"]==(int)$_SESSION["MGW"]->userid) return true;
		else return false;
	}
	else return false;
}

//=========================================
// 		Check for delete right on a news
//=========================================
// @return : nothing if right is OK, display error message and die if right is wrong
function query_delete_right($newsid){ //check if current user as delete right
global $smarty,$appconf;
if (query_delete_right_bool($newsid)==false) {
	$smarty->assign("errorheader",Lang::getLanguageString("permerror"));
	$smarty->assign("permerror",Lang::getLanguageString("permerrormsg"));
	$smarty->display($appconf["gentemplates"] ."/perm_errors.tpl");
	die();
   }
}

//=========================================
// 		Check for modify right on a news
//=========================================
// @return : boolean
function query_modify_right_bool($newsid){ //check if current user as modify right
global $conn;
if(queryRights2_bool('root_modules_news_modifyothers')) return true;
else if(queryRights2_bool('root_modules_news_modify'))// user can not modify for others, now can he modify for him ? 
	{
		//user can modify only for him : does the news belong to him ?
		$sql = "SELECT owner FROM mgw_news WHERE id = '$newsid'";
		if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
		$row = $res->FetchRow();
		if((int)$row["owner"]==(int)$_SESSION["MGW"]->userid) return true;
		else return false;
	}
	else return false;
}

//=========================================
// 		Check for modify right on a news
//=========================================
// @return : nothing if right is OK, display error message and die if right is wrong
function query_modify_right($newsid){ //check if current user as delete right
global $smarty,$appconf;
if (query_modify_right_bool($newsid)==false) {
	$smarty->assign("errorheader",Lang::getLanguageString("permerror"));
	$smarty->assign("permerror",Lang::getLanguageString("permerrormsg"));
	$smarty->display($appconf["gentemplates"] ."/perm_errors.tpl");
	die();
   }
}



//=========================================================================
// 		Check if some news are expired, and archive them in this case
//=========================================================================
function check_and_autoarchive(){
global $conn;
// if (some news are out of date) AND (auto_archive option is ON) => set not active (put news in archive)
if (isset($_SESSION["MGW"]->settings["news_auto_archive"]) && $_SESSION["MGW"]->settings["news_auto_archive"]==1) {
	$sql = "UPDATE mgw_news set isactive = '0' , expire_date=expire_date WHERE expire_date != ".$conn->DBTimeStamp(mktime(0,0 ,0 ,01 ,01 ,2000 ))." AND expire_date < ".$conn->DBTimeStamp(time());
	if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
 }
}


// ==================================================================================
//   Toogle news to 'read'('!' not shown) / 'unread'('!' shown)
//	@param $newsid : id of the news
//	@param $how : how to set the news : "on"=read, "off"=unread, ""=change the state
// ==================================================================================
function toggle_read($newsid,$how=""){
global $conn;

$sql_read = "SELECT userid FROM mgw_news_consult WHERE userid='".$_SESSION["MGW"]->userid."' AND newsid='$newsid'";
if(!$res_read = $conn->Execute($sql_read)) exit(showSQLerror($sql_read, $conn->ErrorMsg(), __LINE__, __FILE__));

if($res_read->NumRows()>0 && $how!="on") { //toggle to 'unread' => delete the record
	$sql = "DELETE FROM mgw_news_consult WHERE userid='".$_SESSION["MGW"]->userid."' AND newsid='$newsid'";
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	}
else if( $res_read->NumRows()<=0 && $how!="off") { //toggle to 'read' => create the record
	$sql = "INSERT INTO mgw_news_consult (userid, newsid) VALUES('".$_SESSION["MGW"]->userid."', '$newsid')";
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	}
}

?>