<?php

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

						///////////////////////////////////////

						admin_validate_section("manage",18);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':									
									manage_polls();
									break;
									
								case 'approve':
									approve_polls();
									break;
									
								case 'approve_all':
									approve_all_polls();
									break;
									
								case 'delete_all':
									delete_all_polls();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_polls(){
	
	$admin = admin_validate();
	$p = $GLOBALS["Get"]->val("p");
	
	$data["section"] = "manage";
	$data["subsection"] = 18;
	$data["title_id"] = "58";	
	$start = $p*30;
	
	$ret_link = "index.php?section=manage_polls&p=".$p;	
	$data["link"] = stripslashes(html_entity_decode($ret_link));
	
	$sql_query_total = "SELECT COUNT(pol_id) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query="FROM polls
				WHERE approved='0'	
				ORDER BY pol_id DESC ";
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
				
	$sql_query.= "LIMIT ".$start.",30";
	
	$results = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$data["pages"] = handle_multipages($p,$total,"index.php?section=manage_polls",30);	
	
	$data["polls"] = array();
	foreach($results as $result){
		
		$poll = array();
		
		if(hs_strlen($result["question"] > 30))
			$result["question"] = hs_substr($result["question"],0,27)."...";
			
		$poll["id"]              = $result["pol_id"];
		$poll["question"]        = $result["question"];
		$poll["author"]          = $GLOBALS["Network"]->card($result["mem_id"]);
		$poll["date"]            = extract_date($result["created"]);
		$poll["edit"]            = "index.php?section=polls&action=edit&pol_id=".$result["pol_id"]."&ret_link=".urlencode($data["link"]);
		$poll["delete"]          = "index.php?section=polls&action=delete&pol_id=".$result["pol_id"]."&ret_link=".urlencode($data["link"]);
		$poll["approve"]         = "index.php?section=manage_polls&action=approve&pol_id=".$result["pol_id"]."&ret_link=".urlencode($data["link"]);
		
		$data["polls"][] = $poll;
		
	}//foreach
	
	$layout[0] = "unapproved_polls_list";
	admin_output($layout,$data);
	
}//manage_polls

function approve_polls(){
	
	$admin = admin_validate();
	$pol_id = $GLOBALS["Get"]->val("pol_id");
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	
	if(is_array($pol_id) && count($pol_id)){
		$sql_query = "UPDATE polls
					  SET approved='1'
					  WHERE pol_id IN (".implode(",",$pol_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif(!empty($pol_id)){
		$sql_query="UPDATE polls
					SET approved='1'
					WHERE pol_id=".$pol_id;	
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	
	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);	
	load_page($ret_link);
	
}//approve_polls

function delete_polls(){
	
	$admin = admin_validate();
	$pol_id = $GLOBALS["Get"]->val("pol_id");
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	
	include_once(DOC_ROOT."/functions/delete_functions.php");
	
	if(is_array($pol_id) && count($pol_id)){
		foreach($pol_id as $id){
			delete_poll($id);
		}//foreach
	}//if
	elseif(!empty($pol_id))
		delete_poll($pol_id);
	
	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);	
	load_page($ret_link);
	
}//delete_polls

function approve_all_polls(){
	
	$admin = admin_validate();
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	
	$sql_query="UPDATE polls
				SET approved='1'";
		
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);	
	load_page($ret_link);
	
}//approve_all_polls

function delete_all_polls(){
	
	$admin = admin_validate();
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	
	$sql_query = "SELECT pol_id FROM polls WHERE approved='0' AND pol_id > 100000";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	include_once(DOC_ROOT."/functions/delete_functions.php");
	
	foreach($result as $res){
		delete_poll($res["pol_id"]);
	}//foreach
	
	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);	
	load_page($ret_link);
	
}//delete_all_polls

?>