<?php

						/*************************************\
						
						    Handshakes Professional (3.0)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////
							
						admin_validate_section("tools",2);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									manage_news();
									break;
									
								case 'edit':
									edit_news();
									break;
									
								case 'save':
									save_news();
									break;
									
								case 'delete':
									delete_news();
									break;
									
								case 'deactivate':
									deactivate_news();
									break;
									
								case 'activate':
									activate_news();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_news(){
	
	admin_validate();
	
	$sql_query = "SELECT * FROM news WHERE `status`='1' ORDER BY date DESC";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$news["active"] = array();
	
	foreach($result as $res){
		$entry["ent_id"]     = $res["ent_id"];
		$entry["date"]       = extract_date($res["date"]);
		$entry["title"]      = $res["title"];
		$entry["edit"]       = "index.php?section=news&action=edit&ent_id=".$res["ent_id"];
		$entry["delete"]     = "index.php?section=news&action=delete&ent_id=".$res["ent_id"];
		$entry["deactivate"] = "index.php?section=news&action=deactivate&ent_id=".$res["ent_id"];
		$news["active"][]    = $entry;
	}//foreach
	
	$sql_query = "SELECT * FROM news WHERE `status`='0' ORDER BY date DESC";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$news["inactive"] = array();
	
	foreach($result as $res){
		$entry["ent_id"]    = $res["ent_id"];
		$entry["date"]      = extract_date($res["date"]);
		$entry["title"]     = $res["title"];
		$entry["edit"]      = "index.php?section=news&action=edit&ent_id=".$res["ent_id"];
		$entry["delete"]    = "index.php?section=news&action=delete&ent_id=".$res["ent_id"];
		$entry["activate"]  = "index.php?section=news&action=activate&ent_id=".$res["ent_id"];
		$news["inactive"][] = $entry;
	}//foreach
	
	$data["section"] = "tools";
	$data["subsection"] = 2;
	$data["title_id"] = "39";	$data["news"] = $news;	
	
	$layout[0] = "news_list";
	
	admin_output($layout,$data);
	
}//manage_news

function edit_news(){
	
	admin_validate();
	$ent_id = $GLOBALS["Get"]->type_val("ent_id","integer");
	
	if($ent_id){
	
		$sql_query = "SELECT * FROM news WHERE ent_id=".$ent_id;
		$result = $GLOBALS["DB"]->row($sql_query);
	
		$data["values"]["ent_id"]  = $ent_id;
		$data["values"]["title"]   = $result["title"];
		$data["values"]["message"] = html_entity_decode($result["message"]);
		$data["values"]["status"]  = $result["status"];
		
	}//if
	else{
		$vals = array("title","status");
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
		$data["values"]["message"] = $GLOBALS["GetFromForm"]->field("message","html");
	}//else
	
	$data["ent_id"] = $ent_id;
	
	$data["htmlareas"] = "html_message";
	
	$data["section"] = "tools";
	$data["subsection"] = 2;
	$data["title_id"] = "39";	
	$data["link"] = "index.php?section=news";
	
	$layout[0] = "edit_news";
	
	admin_output($layout,$data);
	
}//edit_news

function save_news(){
	
	admin_validate();
	$vals = array("title","status","ent_id");
	$values = $GLOBALS["Get"]->arr($vals,1);
	$values["message"] = $GLOBALS["GetFromForm"]->field("message","html");
	
	if(empty($values["title"]) || empty($values["message"])){
		$GLOBALS["aerr_id"] = 10;
		edit_news();
		exit;
	}//if
	
	if(!$values["ent_id"]){
		$sql_query = "INSERT INTO news (title,message,date,status)
					  VALUES (':title:',':message:',:time:,':status:')";
		
		$values["time"] = time();
		if(!$values["status"])
			$values["status"] = 0;
			
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
		load_page("index.php?section=news");
	}//if
	else{
		$sql_query = "UPDATE news SET title=':title:',message=':message:',status=':status:'
					  WHERE ent_id=:ent_id:";
		
		if(!$values["status"])
			$values["status"] = 0;
			
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
		load_page("index.php?section=news&action=edit&ent_id=".$values["ent_id"]);
	}//else
	
}//save_news

function delete_news(){
	
	admin_validate();
	$ent_id = $GLOBALS["Get"]->val("ent_id");
	
	if(is_array($ent_id) && count($ent_id)){
		$sql_query = "DELETE FROM news WHERE ent_id IN (".implode(",",$ent_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif($ent_id){
		$sql_query = "DELETE FROM news WHERE ent_id=".$ent_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//elseif
	
	load_page("index.php?section=news");
	
}//delete_news

function deactivate_news(){
	
	admin_validate();
	$ent_id = $GLOBALS["Get"]->val("ent_id");
	
	if(is_array($ent_id) && count($ent_id)){
		$sql_query = "UPDATE news SET `status`='0' WHERE ent_id IN (".implode(",",$ent_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif($ent_id){
		$sql_query = "UPDATE news SET `status`='0' WHERE ent_id=".$ent_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//elseif
	
	load_page("index.php?section=news");
	
}//deactivate_news

function activate_news(){
	
	admin_validate();
	$ent_id = $GLOBALS["Get"]->val("ent_id");
	
	if(is_array($ent_id) && count($ent_id)){
		$sql_query = "UPDATE news SET `status`='1' WHERE ent_id IN (".implode(",",$ent_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif($ent_id){
		$sql_query = "UPDATE news SET `status`='1' WHERE ent_id=".$ent_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//elseif
	
	load_page("index.php?section=news");
	
}//activate_news
						
?>