<?php

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

						///////////////////////////////////////
						
						admin_validate_section("manage",17);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':
									manage_polls();
									break;
									
								case 'delete':
									delete_polls();
									break;
									
								case 'edit':
									edit_poll();
									break;
									
								case 'save':
									save_poll();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function manage_polls(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 17;
	$data["title_id"] = "57";	
	$p = $GLOBALS["Get"]->val("p");
	
	$sql_query_total  = "SELECT COUNT(*) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query = " FROM polls WHERE pol_id > 100000 ";
	
	$valnames = array("search_text","search_for","sort_by","sort_order","genre");
	
	$values = $GLOBALS["Get"]->arr($valnames,1);
	
	if(!empty($values["search_text"]) && !empty($values["search_for"])){
		
		switch($values["search_for"]){
			case 'pol_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND pol_id=".$values["search_text"];
				break;
			case 'question':
				$sql_query.= " AND question LIKE '%".$values["search_text"]."%' ";
				break;
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND mem_id=".$values["search_text"];
				break;
		}//switch
		
	}//if
	
	switch($values["sort_by"]){
		case '':
			$sql_query.= " ORDER BY created DESC ";
			break;
		case 'question':
			$sql_query.= " ORDER BY question ".$values["sort_order"];
			break;
		case 'created':
			$sql_query.= " ORDER BY created ".$values["sort_order"];
			break;
	}//switch
	
	$data["values"] = $values;
	$data["polls"] = array();
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
	
	$sql_query.= " LIMIT ".$start.",30";
	$results = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$search_link = "index.php?section=polls";	
	while(list($key,$val) = each($values)){
		$search_link.= "&".$key."=".urlencode(stripslashes(html_entity_decode($val)));
	}//while
	$data["pages"] = handle_multipages($p,$total,$search_link,30);
	
	$data["link"] = $search_link."&p=".$p;
	
	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"]);
		
		$data["polls"][] = $poll;
	}//foreach
	
	$layout[0] = "polls_list";
	
	admin_output($layout,$data);
	
}//manage_polls

function edit_poll(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 17;
	$data["title_id"] = "57";
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$pol_id = $GLOBALS["Get"]->type_val("pol_id","integer");
	$data["pol_id"] = $pol_id;
	
	build_form("polls_edit",$pol_id);
	
	$layout[0] = "edit_poll";
	
	admin_output($layout,$data);
	
}//edit_poll

function save_poll(){
	
	admin_validate();
	
	include_once(DOC_ROOT."/functions/form_functions.php");
	
	$values = form_handle_input("polls_edit",1,1);
	
	if(!extract_null_value($values["err_id"])){
		$GLOBALS["uerr_id"] = $values["err_id"];
		edit_poll();
		exit;
	}//if
	
	$values["pol_id"] = $GLOBALS["Get"]->type_val("pol_id","integer");
	
	$sql_query = "UPDATE polls SET question=':question:',answers=':answers:'
				  WHERE pol_id=:pol_id:";
	
	$vals = $values;
	
	$GLOBALS["DB"]->values = $vals;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	load_page("index.php?section=polls&action=edit&pol_id=".$values["pol_id"]."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));
	
}//save_poll

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