<?php

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

						///////////////////////////////////////
							
						admin_validate_section("tools",1);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									manage_newsletters();
									break;
									
								case 'send':
									send_newsletter();
									break;
									
								case 'view':
									view_newsletter();
									break;
									
								case 'delete':
									delete_newsletter();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_newsletters(){
	
	admin_validate();
	
	$sql_query = "SELECT * FROM newsletter ORDER BY date DESC";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$newsletters = array();
	
	foreach($result as $res){
		$letter["let_id"]  = $res["let_id"];
		$letter["date"]    = extract_date($res["date"]);
		$letter["subject"] = $res["subject"];
		$letter["view"]    = "index.php?section=newsletter&action=view&let_id=".$res["let_id"];
		$letter["delete"]  = "index.php?section=newsletter&action=delete&let_id=".$res["let_id"];
		$newsletters[] = $letter;
	}//foreach
	
	$vals = array("forced","subject","message");
	$data = $GLOBALS["Get"]->arr($vals,1);
	
	$data["section"] = "tools";
	$data["subsection"] = 1;
	$data["title_id"] = "40";	$data["newsletters"] = $newsletters;	
	
	$layout[0] = "newsletters";
	
	admin_output($layout,$data);
	
}//manage_newsletters

function send_newsletter(){
	
	admin_validate();
	
	$vals = array("forced","subject","message");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["subject"]) || empty($values["message"])){
		$GLOBALS["aerr_id"] = 10;
		manage_newsletters();
		exit;
	}//if
	
	$sql_query = "INSERT INTO newsletter(subject,message,date,forced)
				  VALUES (':subject:',':message:',:time:,':forced:')";
	
	$values["time"] = time();
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=newsletter");
	
}//send_newsletter

function view_newsletter(){
	
	admin_validate();
	
	$let_id = $GLOBALS["Get"]->type_val("let_id","integer");
	
	$sql_query  = "SELECT * FROM newsletter WHERE let_id=".$let_id;
	$newsletter = $GLOBALS["DB"]->row($sql_query);
	
	$data["section"] = "tools";
	$data["subsection"] = 1;
	$data["title_id"] = "40";	
	$data["date"]    = extract_date($newsletter["date"]);
	$data["subject"] = $newsletter["subject"];
	$data["message"] = nl2br($newsletter["message"]);
	
	$data["link"] = "index.php?section=newsletter";
	
	$layout[0] = "view_newsletter";
	
	admin_output($layout,$data);
	
}//view_newsletter

function delete_newsletter(){
	
	admin_validate();
	
	$let_id = $GLOBALS["Get"]->type_val("let_id","integer");
	
	if(is_array($let_id) && count($let_id)){
		$sql_query = "DELETE FROM newsletter WHERE let_id IN (".implode(",",$let_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif($let_id){	
		$sql_query = "DELETE FROM newsletter WHERE let_id=".$let_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//else
	
	
	load_page("index.php?section=newsletter");
	
}//delete_newsletter
						
?>